gorm/gorm.go

109 lines
2.4 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"
)
// 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
2020-01-29 14:22:44 +03:00
BindVar(stmt Statement, v interface{}) string
}
// Result
type Result struct {
Error error
RowsAffected int64
Statement *Statement
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
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
2020-01-28 18:01:35 +03:00
}