gorm/gorm.go

134 lines
2.7 KiB
Go
Raw Normal View History

2020-01-28 18:01:35 +03:00
package gorm
import (
2020-01-29 14:22:44 +03:00
"context"
2020-01-28 18:01:35 +03:00
"time"
2020-01-29 14:22:44 +03:00
"github.com/jinzhu/gorm/clause"
2020-01-28 18:01:35 +03:00
"github.com/jinzhu/gorm/logger"
2020-01-31 09:17:02 +03:00
"github.com/jinzhu/gorm/schema"
2020-01-28 18:01:35 +03:00
)
// Config GORM config
type Config struct {
// 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
2020-01-31 09:17:02 +03:00
// NamingStrategy tables, columns naming strategy
NamingStrategy schema.Namer
2020-01-28 18:01:35 +03:00
// Logger
Logger logger.Interface
// NowFunc the function to be used when creating a new timestamp
NowFunc func() time.Time
}
// Dialector GORM database dialector
type Dialector interface {
Migrator() Migrator
2020-01-29 14:22:44 +03:00
BindVar(stmt Statement, v interface{}) string
}
2020-01-28 18:01:35 +03:00
// DB GORM DB definition
type DB struct {
*Config
2020-01-29 14:22:44 +03:00
Dialector
2020-01-30 10:14:48 +03:00
Instance
clone bool
}
// Session session config when create new session
type Session struct {
2020-01-29 14:22:44 +03:00
Context context.Context
2020-01-30 10:14:48 +03:00
Logger logger.Interface
NowFunc func() time.Time
}
// Open initialize db session based on dialector
func Open(dialector Dialector, config *Config) (db *DB, err error) {
2020-01-31 09:17:02 +03:00
if config.NamingStrategy == nil {
config.NamingStrategy = schema.NamingStrategy{}
}
2020-01-30 10:14:48 +03:00
return &DB{
Config: config,
Dialector: dialector,
clone: true,
}, nil
}
// Session create new db session
func (db *DB) Session(config *Session) *DB {
var (
tx = db.getInstance()
txConfig = *tx.Config
)
if config.Context != nil {
tx.Context = config.Context
}
if config.Logger != nil {
txConfig.Logger = config.Logger
}
if config.NowFunc != nil {
txConfig.NowFunc = config.NowFunc
}
tx.Config = &txConfig
tx.clone = true
return tx
2020-01-29 14:22:44 +03:00
}
// WithContext change current instance db's context to ctx
func (db *DB) WithContext(ctx context.Context) *DB {
2020-01-30 10:14:48 +03:00
return db.Session(&Session{Context: ctx})
}
// Debug start debug mode
func (db *DB) Debug() (tx *DB) {
return db.Session(&Session{Logger: db.Logger.LogMode(logger.Info)})
}
func (db *DB) Close() error {
return nil
2020-01-29 14:22:44 +03:00
}
// 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) getInstance() *DB {
2020-01-30 10:14:48 +03:00
if db.clone {
ctx := db.Instance.Context
if ctx == nil {
ctx = context.Background()
}
2020-01-29 14:22:44 +03:00
return &DB{
Config: db.Config,
Dialector: db.Dialector,
2020-01-30 10:14:48 +03:00
Instance: Instance{
Context: ctx,
Statement: &Statement{DB: db, Clauses: map[string]clause.Clause{}},
2020-01-29 14:22:44 +03:00
},
}
}
return db
}