mirror of https://github.com/go-gorm/gorm.git
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{}) {
|
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 {
|
if _, ok := s.d.logger.(Logger); !ok {
|
||||||
fmt.Println("logger haven't been set, using os.Stdout")
|
fmt.Println("logger haven't been set, using os.Stdout")
|
||||||
s.d.logger = default_logger
|
s.d.logger = default_logger
|
||||||
|
|
10
main.go
10
main.go
|
@ -11,10 +11,10 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
type DB struct {
|
type DB struct {
|
||||||
db sql_common
|
db sql_common
|
||||||
dialect dialect.Dialect
|
dialect dialect.Dialect
|
||||||
logger Logger
|
logger Logger
|
||||||
log_mode bool
|
logMode bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func Open(driver, source string) (db DB, err error) {
|
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) {
|
func (s *DB) LogMode(b bool) {
|
||||||
s.log_mode = b
|
s.logMode = b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) SingularTable(result bool) {
|
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
|
||||||
|
}
|
2
sql.go
2
sql.go
|
@ -2,7 +2,7 @@ package gorm
|
||||||
|
|
||||||
import "database/sql"
|
import "database/sql"
|
||||||
|
|
||||||
type sql_common interface {
|
type sqlcommon interface {
|
||||||
Exec(query string, args ...interface{}) (sql.Result, error)
|
Exec(query string, args ...interface{}) (sql.Result, error)
|
||||||
Prepare(query string) (*sql.Stmt, error)
|
Prepare(query string) (*sql.Stmt, error)
|
||||||
Query(query string, args ...interface{}) (*sql.Rows, error)
|
Query(query string, args ...interface{}) (*sql.Rows, error)
|
||||||
|
|
Loading…
Reference in New Issue