123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package gorm
- import (
- "context"
- "time"
- "github.com/jinzhu/gorm/clause"
- "github.com/jinzhu/gorm/logger"
- )
- // Config GORM config
- type Config struct {
- // Set true to use singular table name, by default, GORM will pluralize your struct's name as table name
- // Refer https://github.com/jinzhu/inflection for inflection rules
- SingularTable bool
- // GORM perform single create, update, delete operations in transactions by default to ensure database data integrity
- // You can cancel it by setting `SkipDefaultTransaction` to true
- SkipDefaultTransaction bool
- // Logger
- Logger logger.Interface
- // NowFunc the function to be used when creating a new timestamp
- NowFunc func() time.Time
- }
- // Model a basic GoLang struct which includes the following fields: ID, CreatedAt, UpdatedAt, DeletedAt
- // It may be embeded into your model or you may build your own model without it
- // type User struct {
- // gorm.Model
- // }
- type Model struct {
- ID uint `gorm:"primary_key"`
- CreatedAt time.Time
- UpdatedAt time.Time
- DeletedAt *time.Time `gorm:"index"`
- }
- // Dialector GORM database dialector
- type Dialector interface {
- Migrator() Migrator
- BindVar(stmt Statement, v interface{}) string
- }
- // Result
- type Result struct {
- Error error
- RowsAffected int64
- Statement *Statement
- }
- // DB GORM DB definition
- type DB struct {
- *Config
- Dialector
- Result
- Context context.Context
- }
- // WithContext change current instance db's context to ctx
- func (db *DB) WithContext(ctx context.Context) *DB {
- tx := db.getInstance()
- tx.Context = ctx
- return tx
- }
- // Set store value with key into current db instance's context
- func (db *DB) Set(key string, value interface{}) *DB {
- tx := db.getInstance()
- tx.Statement.Settings.Store(key, value)
- return tx
- }
- // Get get value with key from current db instance's context
- func (db *DB) Get(key string) (interface{}, bool) {
- if db.Statement != nil {
- return db.Statement.Settings.Load(key)
- }
- return nil, false
- }
- func (db *DB) Close() *DB {
- // TODO
- return db
- }
- func (db *DB) getInstance() *DB {
- // db.Result.Statement == nil means root DB
- if db.Result.Statement == nil {
- return &DB{
- Config: db.Config,
- Dialector: db.Dialector,
- Context: context.Background(),
- Result: Result{
- Statement: &Statement{DB: db, Clauses: map[string][]clause.Interface{}},
- },
- }
- }
- return db
- }
- // Debug start debug mode
- func (db *DB) Debug() (tx *DB) {
- tx = db.getInstance()
- return
- }
|