Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support fields linked with ops #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,7 @@ func splitKey(key string, val interface{}) (field string, operator string, err e
}
idx := strings.IndexByte(key, ' ')
if idx == -1 {
field = key
operator = "="
field, operator = splitKeyNoSpace(key)
if reflect.ValueOf(val).Kind() == reflect.Slice {
operator = "in"
}
Expand All @@ -436,6 +435,23 @@ func splitKey(key string, val interface{}) (field string, operator string, err e
return
}

var opCanLinkedWithField = []string{opNe1, opNe2, opGte, opLte, opEq, opGt, opLt} // 2 chars op first

func splitKeyNoSpace(key string) (field string, operator string) {
field = key
operator = "="
for _, op := range opCanLinkedWithField {
idx := strings.Index(key, op)
if idx != -1 {
field = key[:idx]
operator = key[idx:]
break
}
continue
}
return
}

func removeInnerSpace(operator string) string {
n := len(operator)
firstSpace := strings.IndexByte(operator, ' ')
Expand Down
29 changes: 29 additions & 0 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,35 @@ func Test_BuildSelect(t *testing.T) {
err: nil,
},
},
{
in: inStruct{
table: "tb",
where: map[string]interface{}{
"foo": "bar",
"foo1>=": "bar",
"foo2<=": "bar",
"foo3<>": "bar",
"foo4!=": "bar",
"foo5>": "bar",
"foo6<": "bar",
"foo7=": "bar",
"foo11 >=": "bar",
"foo12 <=": "bar",
"foo13 <>": "bar",
"foo14 !=": "bar",
"foo15 >": "bar",
"foo16 <": "bar",
"foo17 =": "bar",
"_orderby": " ",
},
fields: nil,
},
out: outStruct{
cond: "SELECT * FROM tb WHERE (foo=? AND foo17=? AND foo7=? AND foo14!=? AND foo4!=? AND foo13!=? AND foo3!=? AND foo15>? AND foo5>? AND foo1>=? AND foo11>=? AND foo16<? AND foo6<? AND foo12<=? AND foo2<=?)",
vals: []interface{}{"bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar"},
err: nil,
},
},
}
ass := assert.New(t)
for _, tc := range data {
Expand Down