Refactor GORM Config

This commit is contained in:
Jinzhu 2017-10-15 11:16:24 +08:00
parent 48cf28258f
commit a699c1e560
3 changed files with 43 additions and 29 deletions

31
config.go Normal file
View 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
View File

@ -1,16 +1,14 @@
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
type Config struct {
// 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.Interface
LogMode logger.LogMode
// Result result fields
Error error
RowsAffected int64
}

19
main.go
View File

@ -63,7 +63,6 @@ func Open(dialect string, args ...interface{}) (db *DB, err error) {
db = &DB{
db: dbSQL,
logger: defaultLogger,
values: map[string]interface{}{},
callbacks: DefaultCallback,
dialect: newDialect(dialect, dbSQL),
@ -126,21 +125,6 @@ func (s *DB) Callback() *Callback {
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
func (s *DB) NewScope(value interface{}) *Scope {
dbClone := s.clone()
@ -444,7 +428,8 @@ func (s *DB) Table(name string) *DB {
// Debug start debug mode
func (s *DB) Debug() *DB {
return s.clone().LogMode(true)
// FIXME Debug Mode
return s
}
// Begin begin a transaction