xorm.go 3.6 KB

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