xorm.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.2.0412"
  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. // NewEngine new a db manager according to the parameter. Currently support four
  46. // drivers
  47. func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
  48. regDrvsNDialects()
  49. driver := core.QueryDriver(driverName)
  50. if driver == nil {
  51. return nil, fmt.Errorf("Unsupported driver name: %v", driverName)
  52. }
  53. uri, err := driver.Parse(driverName, dataSourceName)
  54. if err != nil {
  55. return nil, err
  56. }
  57. dialect := core.QueryDialect(uri.DbType)
  58. if dialect == nil {
  59. return nil, fmt.Errorf("Unsupported dialect type: %v", uri.DbType)
  60. }
  61. db, err := core.Open(driverName, dataSourceName)
  62. if err != nil {
  63. return nil, err
  64. }
  65. err = dialect.Init(db, uri, driverName, dataSourceName)
  66. if err != nil {
  67. return nil, err
  68. }
  69. engine := &Engine{
  70. db: db,
  71. dialect: dialect,
  72. Tables: make(map[reflect.Type]*core.Table),
  73. mutex: &sync.RWMutex{},
  74. TagIdentifier: "xorm",
  75. TZLocation: time.Local,
  76. tagHandlers: defaultTagHandlers,
  77. }
  78. logger := NewSimpleLogger(os.Stdout)
  79. logger.SetLevel(core.LOG_INFO)
  80. engine.SetLogger(logger)
  81. engine.SetMapper(core.NewCacheMapper(new(core.SnakeMapper)))
  82. runtime.SetFinalizer(engine, close)
  83. return engine, nil
  84. }
  85. // Clone clone an engine
  86. func (engine *Engine) Clone() (*Engine, error) {
  87. return NewEngine(engine.DriverName(), engine.DataSourceName())
  88. }