You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.3 KiB

  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. // Clone clone an engine
  93. func (engine *Engine) Clone() (*Engine, error) {
  94. return NewEngine(engine.DriverName(), engine.DataSourceName())
  95. }