forked from mirror/gorm
Add search condition
This commit is contained in:
parent
5ded927a9a
commit
2d85c30fde
|
@ -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
|
||||
|
|
10
main.go
10
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) {
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue