From ac089db161172acc0f235d2b3b8cd7f87372f06c Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 15 Oct 2017 11:16:24 +0800 Subject: [PATCH] Refactor GORM Config --- config.go | 31 +++++++++++++++++++++++++++++++ gorm.go | 22 ++++++++++------------ main.go | 19 ++----------------- 3 files changed, 43 insertions(+), 29 deletions(-) create mode 100644 config.go diff --git a/config.go b/config.go new file mode 100644 index 00000000..dba6ad94 --- /dev/null +++ b/config.go @@ -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 +} diff --git a/gorm.go b/gorm.go index 8ddf6e92..b040a891 100644 --- a/gorm.go +++ b/gorm.go @@ -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 } diff --git a/main.go b/main.go index af5285c9..6e840711 100644 --- a/main.go +++ b/main.go @@ -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() @@ -437,7 +421,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