main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. When using xorm, you can create multiple orm engines, an engine means a database.
  3. */
  4. package main
  5. import (
  6. // Core.
  7. "fmt"
  8. "os"
  9. // SQLite 3
  10. _ "github.com/mattn/go-sqlite3"
  11. // MySQL. Use either driver.
  12. // _ "github.com/go-sql-driver/mysql"
  13. // _ "github.com/ziutek/mymysql"
  14. // PostgreSQL
  15. // _ "github.com/lib/pq"
  16. // SQL Server
  17. // _ "github.com/lunny/godbc"
  18. // XORM.
  19. "github.com/go-xorm/xorm"
  20. // Others.
  21. // "github.com/davecgh/go-spew/spew"
  22. "github.com/go-xorm/core"
  23. )
  24. type Foo struct {
  25. Baz string `xorm:`
  26. }
  27. // Implement the TableName interface to generate a fully custom table name for
  28. // a struct type.
  29. //
  30. // @see xorm.TableName interface.
  31. func (f Foo) TableName() string {
  32. return "foobar";
  33. }
  34. func check(err error) {
  35. if err != nil {
  36. fmt.Println(err.Error())
  37. os.Exit(1)
  38. }
  39. }
  40. func NewOrm() *xorm.Engine {
  41. // NewEngine parameters are those of the SQL driver: driver and DSN.
  42. engine, err := xorm.NewEngine("sqlite3", "./test.db")
  43. check(err)
  44. fmt.Println("ORM loaded")
  45. return engine
  46. }
  47. func setupLogging(engine *xorm.Engine) {
  48. // Enable built-in Logging features.
  49. engine.ShowSQL = true
  50. engine.ShowDebug = true
  51. engine.ShowErr = true
  52. engine.ShowWarn = true
  53. // Optional: declare a custom logger.
  54. f, err := os.Create("sql.log")
  55. check(err)
  56. engine.SetLogger(xorm.NewSimpleLogger(f))
  57. }
  58. func setupConnections(engine *xorm.Engine) {
  59. // Optional: limit connections.
  60. const CONNECTION_LIMIT = 4
  61. engine.SetMaxOpenConns(CONNECTION_LIMIT)
  62. // The number of idle connections is bound by the number of open ones.
  63. engine.SetMaxIdleConns(CONNECTION_LIMIT)
  64. }
  65. /*
  66. ------------------------------+---------------------------------------+------------
  67. go type's kind value method xorm type
  68. ------------------------------+---------------------------------------+------------
  69. implemented Conversion Conversion.ToDB / Conversion.FromDB Text
  70. int, int8, int16, int32 Int
  71. uint, uint8, uint16, uint32 Int
  72. int64, uint64 BigInt
  73. float32 Float
  74. float64 Double
  75. complex64, complex128 json.Marshal / json.UnMarshal Varchar(64)
  76. []uint8 Blob
  77. array, slice, other maps json.Marshal / json.UnMarshal Text
  78. bool 1 or 0 Bool
  79. string Varchar(255)
  80. time.Time DateTime
  81. cascade struct primary key field value BigInt
  82. struct json.Marshal / json.UnMarshal Text
  83. Others Text
  84. ------------------------------+---------------------------------------+------------
  85. */
  86. func setupMapping(engine *xorm.Engine) {
  87. same := core.SameMapper{}
  88. gonic := core.GonicMapper{}
  89. snake := core.SnakeMapper{}
  90. // Global mapping. Default is snake.
  91. // engine.SetMapper(same)
  92. // Table-specific mapping.
  93. engine.SetTableMapper(gonic)
  94. // Column-specific mapping.
  95. engine.SetColumnMapper(snake)
  96. prefixedMapper := core.NewPrefixMapper(same, "prefix")
  97. wrappedMapper := core.NewSuffixMapper(prefixedMapper, "suffix")
  98. decoratedMapper := core.NewCacheMapper(wrappedMapper)
  99. engine.SetMapper(decoratedMapper)
  100. }
  101. func main() {
  102. var engine *xorm.Engine = NewOrm()
  103. setupLogging(engine)
  104. setupConnections(engine)
  105. setupMapping(engine)
  106. // Not needed: called automatically on exit.
  107. engine.Close()
  108. fmt.Println("ORM closed")
  109. }