Add search condition

This commit is contained in:
Jinzhu 2013-11-15 22:29:42 +08:00
parent 5ded927a9a
commit 2d85c30fde
4 changed files with 72 additions and 7 deletions

View File

@ -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

View File

@ -14,7 +14,7 @@ type DB struct {
db sql_common
dialect dialect.Dialect
logger Logger
log_mode bool
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) {

65
search.go Normal file
View File

@ -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
}

2
sql.go
View File

@ -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)