gorm.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package gorm
  2. import (
  3. "context"
  4. "time"
  5. "github.com/jinzhu/gorm/clause"
  6. "github.com/jinzhu/gorm/logger"
  7. "github.com/jinzhu/gorm/schema"
  8. )
  9. // Config GORM config
  10. type Config struct {
  11. // GORM perform single create, update, delete operations in transactions by default to ensure database data integrity
  12. // You can cancel it by setting `SkipDefaultTransaction` to true
  13. SkipDefaultTransaction bool
  14. // NamingStrategy tables, columns naming strategy
  15. NamingStrategy schema.Namer
  16. // Logger
  17. Logger logger.Interface
  18. // NowFunc the function to be used when creating a new timestamp
  19. NowFunc func() time.Time
  20. }
  21. // Dialector GORM database dialector
  22. type Dialector interface {
  23. Migrator() Migrator
  24. BindVar(stmt Statement, v interface{}) string
  25. }
  26. // DB GORM DB definition
  27. type DB struct {
  28. *Config
  29. Dialector
  30. Instance
  31. clone bool
  32. }
  33. // Session session config when create new session
  34. type Session struct {
  35. Context context.Context
  36. Logger logger.Interface
  37. NowFunc func() time.Time
  38. }
  39. // Open initialize db session based on dialector
  40. func Open(dialector Dialector, config *Config) (db *DB, err error) {
  41. if config.NamingStrategy == nil {
  42. config.NamingStrategy = schema.NamingStrategy{}
  43. }
  44. return &DB{
  45. Config: config,
  46. Dialector: dialector,
  47. clone: true,
  48. }, nil
  49. }
  50. // Session create new db session
  51. func (db *DB) Session(config *Session) *DB {
  52. var (
  53. tx = db.getInstance()
  54. txConfig = *tx.Config
  55. )
  56. if config.Context != nil {
  57. tx.Context = config.Context
  58. }
  59. if config.Logger != nil {
  60. txConfig.Logger = config.Logger
  61. }
  62. if config.NowFunc != nil {
  63. txConfig.NowFunc = config.NowFunc
  64. }
  65. tx.Config = &txConfig
  66. tx.clone = true
  67. return tx
  68. }
  69. // WithContext change current instance db's context to ctx
  70. func (db *DB) WithContext(ctx context.Context) *DB {
  71. return db.Session(&Session{Context: ctx})
  72. }
  73. // Debug start debug mode
  74. func (db *DB) Debug() (tx *DB) {
  75. return db.Session(&Session{Logger: db.Logger.LogMode(logger.Info)})
  76. }
  77. func (db *DB) Close() error {
  78. return nil
  79. }
  80. // Set store value with key into current db instance's context
  81. func (db *DB) Set(key string, value interface{}) *DB {
  82. tx := db.getInstance()
  83. tx.Statement.Settings.Store(key, value)
  84. return tx
  85. }
  86. // Get get value with key from current db instance's context
  87. func (db *DB) Get(key string) (interface{}, bool) {
  88. if db.Statement != nil {
  89. return db.Statement.Settings.Load(key)
  90. }
  91. return nil, false
  92. }
  93. func (db *DB) getInstance() *DB {
  94. if db.clone {
  95. ctx := db.Instance.Context
  96. if ctx == nil {
  97. ctx = context.Background()
  98. }
  99. return &DB{
  100. Config: db.Config,
  101. Dialector: db.Dialector,
  102. Instance: Instance{
  103. Context: ctx,
  104. Statement: &Statement{DB: db, Clauses: map[string]clause.Clause{}},
  105. },
  106. }
  107. }
  108. return db
  109. }