1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 13:50:54 +00:00

orm add operator between #518

This commit is contained in:
slene 2014-03-10 20:18:57 +08:00 committed by asta.xie
parent d0949b64c6
commit cacdb3228d
2 changed files with 17 additions and 4 deletions

View File

@ -35,7 +35,7 @@ var (
"istartswith": true, "istartswith": true,
"iendswith": true, "iendswith": true,
"in": true, "in": true,
// "range": true, "between": true,
// "year": true, // "year": true,
// "month": true, // "month": true,
// "day": true, // "day": true,
@ -916,13 +916,19 @@ func (d *dbBase) GenerateOperatorSql(mi *modelInfo, fi *fieldInfo, operator stri
} }
arg := params[0] arg := params[0]
if operator == "in" { switch operator {
case "in":
marks := make([]string, len(params)) marks := make([]string, len(params))
for i, _ := range marks { for i, _ := range marks {
marks[i] = "?" marks[i] = "?"
} }
sql = fmt.Sprintf("IN (%s)", strings.Join(marks, ", ")) sql = fmt.Sprintf("IN (%s)", strings.Join(marks, ", "))
} else { case "between":
if len(params) != 2 {
panic(fmt.Errorf("operator `%s` need 2 args not %d", operator, len(params)))
}
sql = "BETWEEN ? AND ?"
default:
if len(params) > 1 { if len(params) > 1 {
panic(fmt.Errorf("operator `%s` need 1 args not %d", operator, len(params))) panic(fmt.Errorf("operator `%s` need 1 args not %d", operator, len(params)))
} }

View File

@ -653,6 +653,14 @@ func TestOperators(t *testing.T) {
num, err = qs.Filter("status__in", []*int{&n1}, &n2).Count() num, err = qs.Filter("status__in", []*int{&n1}, &n2).Count()
throwFail(t, err) throwFail(t, err)
throwFail(t, AssertIs(num, 2)) throwFail(t, AssertIs(num, 2))
num, err = qs.Filter("id__between", 2, 3).Count()
throwFail(t, err)
throwFail(t, AssertIs(num, 2))
num, err = qs.Filter("id__between", []int{2, 3}).Count()
throwFail(t, err)
throwFail(t, AssertIs(num, 2))
} }
func TestSetCond(t *testing.T) { func TestSetCond(t *testing.T) {
@ -1611,7 +1619,6 @@ func TestDelete(t *testing.T) {
throwFail(t, err) throwFail(t, err)
throwFail(t, AssertIs(num, 4)) throwFail(t, AssertIs(num, 4))
fmt.Println("...")
qs = dORM.QueryTable("comment") qs = dORM.QueryTable("comment")
num, err = qs.Filter("Post__User", 3).Delete() num, err = qs.Filter("Post__User", 3).Delete()
throwFail(t, err) throwFail(t, err)