add In() function
This commit is contained in:
parent
eaa2bcaa57
commit
22259898f3
|
@ -64,6 +64,11 @@ func (engine *Engine) Id(id int) *Engine {
|
||||||
return engine
|
return engine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) In(column string, args ...interface{}) *Engine {
|
||||||
|
engine.Statement.In(column, args...)
|
||||||
|
return engine
|
||||||
|
}
|
||||||
|
|
||||||
func (engine *Engine) Limit(limit int, start ...int) *Engine {
|
func (engine *Engine) Limit(limit int, start ...int) *Engine {
|
||||||
engine.Statement.Limit(limit, start...)
|
engine.Statement.Limit(limit, start...)
|
||||||
return engine
|
return engine
|
||||||
|
|
|
@ -37,6 +37,11 @@ func (session *Session) Id(id int) *Session {
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (session *Session) In(column string, args ...interface{}) *Session {
|
||||||
|
session.Statement.In(column, args...)
|
||||||
|
return session
|
||||||
|
}
|
||||||
|
|
||||||
func (session *Session) Limit(limit int, start ...int) *Session {
|
func (session *Session) Limit(limit int, start ...int) *Session {
|
||||||
session.Statement.Limit(limit, start...)
|
session.Statement.Limit(limit, start...)
|
||||||
return session
|
return session
|
||||||
|
|
20
statement.go
20
statement.go
|
@ -2,6 +2,7 @@ package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Statement struct {
|
type Statement struct {
|
||||||
|
@ -19,6 +20,14 @@ type Statement struct {
|
||||||
BeanArgs []interface{}
|
BeanArgs []interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MakeArray(elem string, count int) []string {
|
||||||
|
res := make([]string, count)
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
res[i] = elem
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
func (statement *Statement) Init() {
|
func (statement *Statement) Init() {
|
||||||
statement.Table = nil
|
statement.Table = nil
|
||||||
statement.Start = 0
|
statement.Start = 0
|
||||||
|
@ -48,6 +57,17 @@ func (statement *Statement) Id(id int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (statement *Statement) In(column string, args ...interface{}) {
|
||||||
|
inStr := fmt.Sprintf("%v in (%v)", column, strings.Join(MakeArray("?", len(args)), ","))
|
||||||
|
if statement.WhereStr == "" {
|
||||||
|
statement.WhereStr = inStr
|
||||||
|
statement.Params = args
|
||||||
|
} else {
|
||||||
|
statement.WhereStr = statement.WhereStr + " and " + inStr
|
||||||
|
statement.Params = append(statement.Params, args...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (statement *Statement) Limit(limit int, start ...int) {
|
func (statement *Statement) Limit(limit int, start ...int) {
|
||||||
statement.LimitN = limit
|
statement.LimitN = limit
|
||||||
if len(start) > 0 {
|
if len(start) > 0 {
|
||||||
|
|
19
xorm_test.go
19
xorm_test.go
|
@ -190,6 +190,23 @@ func where(t *testing.T) {
|
||||||
fmt.Println(users)
|
fmt.Println(users)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func in(t *testing.T) {
|
||||||
|
users := make([]Userinfo, 0)
|
||||||
|
err := engine.In("id", 1, 2, 3).Find(&users)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(users)
|
||||||
|
|
||||||
|
err = engine.Where("id > ?", 2).In("id", 1, 2, 3).Find(&users)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(users)
|
||||||
|
}
|
||||||
|
|
||||||
func limit(t *testing.T) {
|
func limit(t *testing.T) {
|
||||||
users := make([]Userinfo, 0)
|
users := make([]Userinfo, 0)
|
||||||
err := engine.Limit(2, 1).Find(&users)
|
err := engine.Limit(2, 1).Find(&users)
|
||||||
|
@ -341,6 +358,7 @@ func TestMysql(t *testing.T) {
|
||||||
find(t)
|
find(t)
|
||||||
count(t)
|
count(t)
|
||||||
where(t)
|
where(t)
|
||||||
|
in(t)
|
||||||
limit(t)
|
limit(t)
|
||||||
order(t)
|
order(t)
|
||||||
join(t)
|
join(t)
|
||||||
|
@ -366,6 +384,7 @@ func TestSqlite(t *testing.T) {
|
||||||
find(t)
|
find(t)
|
||||||
count(t)
|
count(t)
|
||||||
where(t)
|
where(t)
|
||||||
|
in(t)
|
||||||
limit(t)
|
limit(t)
|
||||||
order(t)
|
order(t)
|
||||||
join(t)
|
join(t)
|
||||||
|
|
Loading…
Reference in New Issue