xorm.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2015 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build go1.8
  5. package xorm
  6. import (
  7. "context"
  8. "fmt"
  9. "os"
  10. "reflect"
  11. "runtime"
  12. "sync"
  13. "time"
  14. "xorm.io/core"
  15. )
  16. const (
  17. // Version show the xorm's version
  18. Version string = "0.7.0.0504"
  19. )
  20. func regDrvsNDialects() bool {
  21. providedDrvsNDialects := map[string]struct {
  22. dbType core.DbType
  23. getDriver func() core.Driver
  24. getDialect func() core.Dialect
  25. }{
  26. "mssql": {"mssql", func() core.Driver { return &odbcDriver{} }, func() core.Dialect { return &mssql{} }},
  27. "odbc": {"mssql", func() core.Driver { return &odbcDriver{} }, func() core.Dialect { return &mssql{} }}, // !nashtsai! TODO change this when supporting MS Access
  28. "mysql": {"mysql", func() core.Driver { return &mysqlDriver{} }, func() core.Dialect { return &mysql{} }},
  29. "mymysql": {"mysql", func() core.Driver { return &mymysqlDriver{} }, func() core.Dialect { return &mysql{} }},
  30. "postgres": {"postgres", func() core.Driver { return &pqDriver{} }, func() core.Dialect { return &postgres{} }},
  31. "pgx": {"postgres", func() core.Driver { return &pqDriverPgx{} }, func() core.Dialect { return &postgres{} }},
  32. "sqlite3": {"sqlite3", func() core.Driver { return &sqlite3Driver{} }, func() core.Dialect { return &sqlite3{} }},
  33. "oci8": {"oracle", func() core.Driver { return &oci8Driver{} }, func() core.Dialect { return &oracle{} }},
  34. "goracle": {"oracle", func() core.Driver { return &goracleDriver{} }, func() core.Dialect { return &oracle{} }},
  35. }
  36. for driverName, v := range providedDrvsNDialects {
  37. if driver := core.QueryDriver(driverName); driver == nil {
  38. core.RegisterDriver(driverName, v.getDriver())
  39. core.RegisterDialect(v.dbType, v.getDialect)
  40. }
  41. }
  42. return true
  43. }
  44. func close(engine *Engine) {
  45. engine.Close()
  46. }
  47. func init() {
  48. regDrvsNDialects()
  49. }
  50. // NewEngine new a db manager according to the parameter. Currently support four
  51. // drivers
  52. func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
  53. driver := core.QueryDriver(driverName)
  54. if driver == nil {
  55. return nil, fmt.Errorf("Unsupported driver name: %v", driverName)
  56. }
  57. uri, err := driver.Parse(driverName, dataSourceName)
  58. if err != nil {
  59. return nil, err
  60. }
  61. dialect := core.QueryDialect(uri.DbType)
  62. if dialect == nil {
  63. return nil, fmt.Errorf("Unsupported dialect type: %v", uri.DbType)
  64. }
  65. db, err := core.Open(driverName, dataSourceName)
  66. if err != nil {
  67. return nil, err
  68. }
  69. err = dialect.Init(db, uri, driverName, dataSourceName)
  70. if err != nil {
  71. return nil, err
  72. }
  73. engine := &Engine{
  74. db: db,
  75. dialect: dialect,
  76. Tables: make(map[reflect.Type]*core.Table),
  77. mutex: &sync.RWMutex{},
  78. TagIdentifier: "xorm",
  79. TZLocation: time.Local,
  80. tagHandlers: defaultTagHandlers,
  81. cachers: make(map[string]core.Cacher),
  82. defaultContext: context.Background(),
  83. }
  84. if uri.DbType == core.SQLITE {
  85. engine.DatabaseTZ = time.UTC
  86. } else {
  87. engine.DatabaseTZ = time.Local
  88. }
  89. logger := NewSimpleLogger(os.Stdout)
  90. logger.SetLevel(core.LOG_INFO)
  91. engine.SetLogger(logger)
  92. engine.SetMapper(core.NewCacheMapper(new(core.SnakeMapper)))
  93. runtime.SetFinalizer(engine, close)
  94. return engine, nil
  95. }
  96. // NewEngineWithParams new a db manager with params. The params will be passed to dialect.
  97. func NewEngineWithParams(driverName string, dataSourceName string, params map[string]string) (*Engine, error) {
  98. engine, err := NewEngine(driverName, dataSourceName)
  99. engine.dialect.SetParams(params)
  100. return engine, err
  101. }
  102. // Clone clone an engine
  103. func (engine *Engine) Clone() (*Engine, error) {
  104. return NewEngine(engine.DriverName(), engine.DataSourceName())
  105. }