123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- /*
- When using xorm, you can create multiple orm engines, an engine means a database.
- */
- package main
- import (
- // Core.
- "fmt"
- "os"
- // SQLite 3
- _ "github.com/mattn/go-sqlite3"
- // MySQL. Use either driver.
- // _ "github.com/go-sql-driver/mysql"
- // _ "github.com/ziutek/mymysql"
- // PostgreSQL
- // _ "github.com/lib/pq"
- // SQL Server
- // _ "github.com/lunny/godbc"
- // XORM.
- "github.com/go-xorm/xorm"
- // Others.
- // "github.com/davecgh/go-spew/spew"
- "github.com/go-xorm/core"
- )
- func check(err error) {
- if err != nil {
- fmt.Println(err.Error())
- os.Exit(1)
- }
- }
- func NewOrm() *xorm.Engine {
- // NewEngine parameters are those of the SQL driver: driver and DSN.
- engine, err := xorm.NewEngine("sqlite3", "./test.db")
- check(err)
- fmt.Println("ORM loaded")
- return engine
- }
- func setupLogging(engine *xorm.Engine) {
- // Enable built-in Logging features.
- engine.ShowSQL = true
- engine.ShowDebug = true
- engine.ShowErr = true
- engine.ShowWarn = true
- // Optional: declare a custom logger.
- f, err := os.Create("sql.log")
- check(err)
- engine.SetLogger(xorm.NewSimpleLogger(f))
- }
- func setupConnections(engine *xorm.Engine) {
- // Optional: limit connections.
- const CONNECTION_LIMIT = 4
- engine.SetMaxOpenConns(CONNECTION_LIMIT)
- // The number of idle connections is bound by the number of open ones.
- engine.SetMaxIdleConns(CONNECTION_LIMIT)
- }
- /*
- ------------------------------+---------------------------------------+------------
- go type's kind value method xorm type
- ------------------------------+---------------------------------------+------------
- implemented Conversion Conversion.ToDB / Conversion.FromDB Text
- int, int8, int16, int32 Int
- uint, uint8, uint16, uint32 Int
- int64, uint64 BigInt
- float32 Float
- float64 Double
- complex64, complex128 json.Marshal / json.UnMarshal Varchar(64)
- []uint8 Blob
- array, slice, other maps json.Marshal / json.UnMarshal Text
- bool 1 or 0 Bool
- string Varchar(255)
- time.Time DateTime
- cascade struct primary key field value BigInt
- struct json.Marshal / json.UnMarshal Text
- Others Text
- ------------------------------+---------------------------------------+------------
- */
- func setupMapping(engine *xorm.Engine) {
- same := core.SameMapper{}
- gonic := core.GonicMapper{}
- snake := core.SnakeMapper{}
- // Global mapping. Default is snake.
- engine.SetMapper(same)
- // Table-specific mapping.
- engine.SetTableMapper(gonic)
- // Column-specific mapping.
- engine.SetColumnMapper(snake)
- }
- func main() {
- var engine *xorm.Engine = NewOrm()
- setupLogging(engine)
- setupConnections(engine)
- setupMapping(engine)
- // Not needed: called automatically on exit.
- engine.Close()
- fmt.Println("ORM closed")
- }
|