gorm.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package gorm
  2. import (
  3. "context"
  4. "time"
  5. "github.com/jinzhu/gorm/clause"
  6. "github.com/jinzhu/gorm/logger"
  7. )
  8. // Config GORM config
  9. type Config struct {
  10. // Set true to use singular table name, by default, GORM will pluralize your struct's name as table name
  11. // Refer https://github.com/jinzhu/inflection for inflection rules
  12. SingularTable bool
  13. // GORM perform single create, update, delete operations in transactions by default to ensure database data integrity
  14. // You can cancel it by setting `SkipDefaultTransaction` to true
  15. SkipDefaultTransaction bool
  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. // Model a basic GoLang struct which includes the following fields: ID, CreatedAt, UpdatedAt, DeletedAt
  22. // It may be embeded into your model or you may build your own model without it
  23. // type User struct {
  24. // gorm.Model
  25. // }
  26. type Model struct {
  27. ID uint `gorm:"primary_key"`
  28. CreatedAt time.Time
  29. UpdatedAt time.Time
  30. DeletedAt *time.Time `gorm:"index"`
  31. }
  32. // Dialector GORM database dialector
  33. type Dialector interface {
  34. Migrator() Migrator
  35. BindVar(stmt Statement, v interface{}) string
  36. }
  37. // Result
  38. type Result struct {
  39. Error error
  40. RowsAffected int64
  41. Statement *Statement
  42. }
  43. // DB GORM DB definition
  44. type DB struct {
  45. *Config
  46. Dialector
  47. Result
  48. Context context.Context
  49. }
  50. // WithContext change current instance db's context to ctx
  51. func (db *DB) WithContext(ctx context.Context) *DB {
  52. tx := db.getInstance()
  53. tx.Context = ctx
  54. return tx
  55. }
  56. // Set store value with key into current db instance's context
  57. func (db *DB) Set(key string, value interface{}) *DB {
  58. tx := db.getInstance()
  59. tx.Statement.Settings.Store(key, value)
  60. return tx
  61. }
  62. // Get get value with key from current db instance's context
  63. func (db *DB) Get(key string) (interface{}, bool) {
  64. if db.Statement != nil {
  65. return db.Statement.Settings.Load(key)
  66. }
  67. return nil, false
  68. }
  69. func (db *DB) Close() *DB {
  70. // TODO
  71. return db
  72. }
  73. func (db *DB) getInstance() *DB {
  74. // db.Result.Statement == nil means root DB
  75. if db.Result.Statement == nil {
  76. return &DB{
  77. Config: db.Config,
  78. Dialector: db.Dialector,
  79. Context: context.Background(),
  80. Result: Result{
  81. Statement: &Statement{DB: db, Clauses: map[string][]clause.Interface{}},
  82. },
  83. }
  84. }
  85. return db
  86. }
  87. // Debug start debug mode
  88. func (db *DB) Debug() (tx *DB) {
  89. tx = db.getInstance()
  90. return
  91. }