diff --git a/logger.go b/logger.go index 42d5239c..989beb25 100644 --- a/logger.go +++ b/logger.go @@ -43,7 +43,7 @@ func init() { } func (s *Chain) print(level string, v ...interface{}) { - if s.d.log_mode || s.debug_mode || level == "debug" { + if s.d.logMode || s.debug_mode || level == "debug" { if _, ok := s.d.logger.(Logger); !ok { fmt.Println("logger haven't been set, using os.Stdout") s.d.logger = default_logger diff --git a/main.go b/main.go index dd234758..218ccbc7 100644 --- a/main.go +++ b/main.go @@ -11,10 +11,10 @@ func init() { } type DB struct { - db sql_common - dialect dialect.Dialect - logger Logger - log_mode bool + db sql_common + dialect dialect.Dialect + logger Logger + logMode bool } func Open(driver, source string) (db DB, err error) { @@ -38,7 +38,7 @@ func (s *DB) SetLogger(l Logger) { } func (s *DB) LogMode(b bool) { - s.log_mode = b + s.logMode = b } func (s *DB) SingularTable(result bool) { diff --git a/search.go b/search.go new file mode 100644 index 00000000..db32bd11 --- /dev/null +++ b/search.go @@ -0,0 +1,65 @@ +package gorm + +type search struct { + conditions map[string][]interface{} + orders []string + selectStr string + offsetStr string + limitStr string + specifiedTableName string + unscope bool +} + +func (s search) addToCondition(typ string, value interface{}) { + s.conditions[typ] = append(s.conditions[typ], value) +} + +func (s search) where(query string, values ...interface{}) { + s.addToCondition("where", map[string]interface{}{"query": query, "args": values}) +} + +func (s search) not(query string, values ...interface{}) { + s.addToCondition("not", map[string]interface{}{"query": query, "args": values}) +} + +func (s search) or(query string, values ...interface{}) { + s.addToCondition("or", map[string]interface{}{"query": query, "args": values}) +} + +func (s search) attrs(attrs ...interface{}) { + s.addToCondition("attrs", toSearchableMap(attrs...)) +} + +func (s search) assign(attrs ...interface{}) { + s.addToCondition("assign", toSearchableMap(attrs...)) +} + +func (s search) order(value string, reorder ...bool) { + if len(reorder) > 0 && reorder[0] { + s.orders = []string{value} + } else { + s.orders = append(s.orders, value) + } +} + +func (s search) selects(value interface{}) { + if str, err := getInterfaceAsString(value); err == nil { + s.selectStr = str + } +} + +func (s search) limit(value interface{}) { + if str, err := getInterfaceAsString(value); err == nil { + s.limitStr = str + } +} + +func (s search) offset(value interface{}) { + if str, err := getInterfaceAsString(value); err == nil { + s.offsetStr = str + } +} + +func (s search) unscoped() { + s.unscope = true +} diff --git a/sql.go b/sql.go index 6d62d5a4..3933f4aa 100644 --- a/sql.go +++ b/sql.go @@ -2,7 +2,7 @@ package gorm import "database/sql" -type sql_common interface { +type sqlcommon interface { Exec(query string, args ...interface{}) (sql.Result, error) Prepare(query string) (*sql.Stmt, error) Query(query string, args ...interface{}) (*sql.Rows, error)