main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. func check(err error) {
  25. if err != nil {
  26. fmt.Println(err.Error())
  27. os.Exit(1)
  28. }
  29. }
  30. func NewOrm() *xorm.Engine {
  31. // NewEngine parameters are those of the SQL driver: driver and DSN.
  32. engine, err := xorm.NewEngine("sqlite3", "./test.db")
  33. check(err)
  34. fmt.Println("ORM loaded")
  35. return engine
  36. }
  37. func setupLogging(engine *xorm.Engine) {
  38. // Enable built-in Logging features.
  39. engine.ShowSQL = true
  40. engine.ShowDebug = true
  41. engine.ShowErr = true
  42. engine.ShowWarn = true
  43. // Optional: declare a custom logger.
  44. f, err := os.Create("sql.log")
  45. check(err)
  46. engine.SetLogger(xorm.NewSimpleLogger(f))
  47. }
  48. func setupConnections(engine *xorm.Engine) {
  49. // Optional: limit connections.
  50. const CONNECTION_LIMIT = 4
  51. engine.SetMaxOpenConns(CONNECTION_LIMIT)
  52. // The number of idle connections is bound by the number of open ones.
  53. engine.SetMaxIdleConns(CONNECTION_LIMIT)
  54. }
  55. /*
  56. ------------------------------+---------------------------------------+------------
  57. go type's kind value method xorm type
  58. ------------------------------+---------------------------------------+------------
  59. implemented Conversion Conversion.ToDB / Conversion.FromDB Text
  60. int, int8, int16, int32 Int
  61. uint, uint8, uint16, uint32 Int
  62. int64, uint64 BigInt
  63. float32 Float
  64. float64 Double
  65. complex64, complex128 json.Marshal / json.UnMarshal Varchar(64)
  66. []uint8 Blob
  67. array, slice, other maps json.Marshal / json.UnMarshal Text
  68. bool 1 or 0 Bool
  69. string Varchar(255)
  70. time.Time DateTime
  71. cascade struct primary key field value BigInt
  72. struct json.Marshal / json.UnMarshal Text
  73. Others Text
  74. ------------------------------+---------------------------------------+------------
  75. */
  76. func setupMapping(engine *xorm.Engine) {
  77. same := core.SameMapper{}
  78. gonic := core.GonicMapper{}
  79. snake := core.SnakeMapper{}
  80. // Global mapping. Default is snake.
  81. // engine.SetMapper(same)
  82. // Table-specific mapping.
  83. engine.SetTableMapper(gonic)
  84. // Column-specific mapping.
  85. engine.SetColumnMapper(snake)
  86. prefixedMapper := core.NewPrefixMapper(same, "prefix")
  87. wrappedMapper := core.NewSuffixMapper(prefixedMapper, "suffix")
  88. decoratedMapper := core.NewCacheMapper(wrappedMapper)
  89. engine.SetMapper(decoratedMapper)
  90. }
  91. func main() {
  92. var engine *xorm.Engine = NewOrm()
  93. setupLogging(engine)
  94. setupConnections(engine)
  95. setupMapping(engine)
  96. // Not needed: called automatically on exit.
  97. engine.Close()
  98. fmt.Println("ORM closed")
  99. }