/* 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" ) type Foo struct { Baz string `xorm:` } // Implement the TableName interface to generate a fully custom table name for // a struct type. // // @see xorm.TableName interface. func (f Foo) TableName() string { return "foobar"; } 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) prefixedMapper := core.NewPrefixMapper(same, "prefix") wrappedMapper := core.NewSuffixMapper(prefixedMapper, "suffix") decoratedMapper := core.NewCacheMapper(wrappedMapper) engine.SetMapper(decoratedMapper) } 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") }