Refactor GORM Config
This commit is contained in:
parent
293d60cdd2
commit
ac089db161
31
config.go
Normal file
31
config.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package gorm
|
||||||
|
|
||||||
|
import "github.com/jinzhu/gorm/logger"
|
||||||
|
|
||||||
|
// Config GORM config
|
||||||
|
type Config struct {
|
||||||
|
// MaxIdleConnections sets the maximum number of connections in the idle connection pool
|
||||||
|
MaxIdleConnections int
|
||||||
|
// MaxOpenConnections sets the maximum number of open connections to the database
|
||||||
|
MaxOpenConnections int
|
||||||
|
|
||||||
|
// SingularTable 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
|
||||||
|
|
||||||
|
// BlockGlobalUpdate generates an error on update/delete without where clause, this is to prevent eventual error with empty objects updates/deletions
|
||||||
|
BlockGlobalUpdate bool
|
||||||
|
|
||||||
|
// Logger
|
||||||
|
Logger logger.Interface
|
||||||
|
LogMode logger.LogLevel
|
||||||
|
|
||||||
|
// Dialect DB Dialect
|
||||||
|
Dialect Dialect
|
||||||
|
|
||||||
|
// Callbacks defined GORM callbacks
|
||||||
|
Callbacks *Callback
|
||||||
|
|
||||||
|
// db fresh db connection
|
||||||
|
globalDbConnection SQLCommon
|
||||||
|
}
|
22
gorm.go
22
gorm.go
@ -1,16 +1,14 @@
|
|||||||
package gorm
|
package gorm
|
||||||
|
|
||||||
import "github.com/jinzhu/gorm/logger"
|
// DB contains information for current db connection
|
||||||
|
type DB struct {
|
||||||
|
// Current operation
|
||||||
|
Value interface{} // Value current operation data
|
||||||
|
db SQLCommon
|
||||||
|
search *search
|
||||||
|
values map[string]interface{}
|
||||||
|
|
||||||
// Config GORM config
|
// Result result fields
|
||||||
type Config struct {
|
Error error
|
||||||
// SingularTable use singular table name, by default, GORM will pluralize your struct's name as table name
|
RowsAffected int64
|
||||||
// Refer https://github.com/jinzhu/inflection for inflection rules
|
|
||||||
SingularTable bool
|
|
||||||
|
|
||||||
// BlockGlobalUpdate generates an error on update/delete without where clause, this is to prevent eventual error with empty objects updates/deletions
|
|
||||||
BlockGlobalUpdate bool
|
|
||||||
|
|
||||||
Logger logger.Interface
|
|
||||||
LogMode logger.LogMode
|
|
||||||
}
|
}
|
||||||
|
19
main.go
19
main.go
@ -63,7 +63,6 @@ func Open(dialect string, args ...interface{}) (db *DB, err error) {
|
|||||||
|
|
||||||
db = &DB{
|
db = &DB{
|
||||||
db: dbSQL,
|
db: dbSQL,
|
||||||
logger: defaultLogger,
|
|
||||||
values: map[string]interface{}{},
|
values: map[string]interface{}{},
|
||||||
callbacks: DefaultCallback,
|
callbacks: DefaultCallback,
|
||||||
dialect: newDialect(dialect, dbSQL),
|
dialect: newDialect(dialect, dbSQL),
|
||||||
@ -126,21 +125,6 @@ func (s *DB) Callback() *Callback {
|
|||||||
return s.parent.callbacks
|
return s.parent.callbacks
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLogger replace default logger
|
|
||||||
func (s *DB) SetLogger(log logger) {
|
|
||||||
s.logger = log
|
|
||||||
}
|
|
||||||
|
|
||||||
// LogMode set log mode, `true` for detailed logs, `false` for no log, default, will only print error logs
|
|
||||||
func (s *DB) LogMode(enable bool) *DB {
|
|
||||||
if enable {
|
|
||||||
s.logMode = 2
|
|
||||||
} else {
|
|
||||||
s.logMode = 1
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewScope create a scope for current operation
|
// NewScope create a scope for current operation
|
||||||
func (s *DB) NewScope(value interface{}) *Scope {
|
func (s *DB) NewScope(value interface{}) *Scope {
|
||||||
dbClone := s.clone()
|
dbClone := s.clone()
|
||||||
@ -437,7 +421,8 @@ func (s *DB) Table(name string) *DB {
|
|||||||
|
|
||||||
// Debug start debug mode
|
// Debug start debug mode
|
||||||
func (s *DB) Debug() *DB {
|
func (s *DB) Debug() *DB {
|
||||||
return s.clone().LogMode(true)
|
// FIXME Debug Mode
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// Begin begin a transaction
|
// Begin begin a transaction
|
||||||
|
Loading…
x
Reference in New Issue
Block a user