Skip to content

Commit

Permalink
support not like
Browse files Browse the repository at this point in the history
  • Loading branch information
caibirdme committed Jul 13, 2019
1 parent e45861c commit 55d5813
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
3 changes: 2 additions & 1 deletion builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ operators supported:
* !=
* <>
* in
* not in`
* not in
* like
* not like
* between
* not between

Expand Down
24 changes: 23 additions & 1 deletion builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ const (
opLt = "<"
opLte = "<="
opLike = "like"
opNotLike = "not like"
opBetween = "between"
opNotBetween = "not between"
// special
Expand Down Expand Up @@ -310,12 +311,15 @@ var op2Comparable = map[string]compareProducer{
opLike: func(m map[string]interface{}) (Comparable, error) {
return Like(m), nil
},
opNotLike: func(m map[string]interface{}) (Comparable, error) {
return NotLike(m), nil
},
opNull: func(m map[string]interface{}) (Comparable, error) {
return nullCompareble(m), nil
},
}

var opOrder = []string{opEq, opIn, opNe1, opNe2, opNotIn, opGt, opGte, opLt, opLte, opLike, opBetween, opNotBetween, opNull}
var opOrder = []string{opEq, opIn, opNe1, opNe2, opNotIn, opGt, opGte, opLt, opLte, opLike, opNotLike, opBetween, opNotBetween, opNull}

func buildWhereCondition(mapSet *whereMapSet) ([]Comparable, func(), error) {
cpArr, release := getCpPool()
Expand Down Expand Up @@ -379,10 +383,28 @@ func splitKey(key string) (field string, operator string, err error) {
} else {
field = key[:idx]
operator = strings.Trim(key[idx+1:], " ")
operator = removeInnerSpace(operator)
}
return
}

func removeInnerSpace(operator string) string {
n := len(operator)
firstSpace := strings.IndexByte(operator, ' ')
if firstSpace == -1 {
return operator
}
lastSpace := firstSpace
for i := firstSpace+1; i<n; i++ {
if operator[i] == ' ' {
lastSpace = i
} else {
break
}
}
return operator[:firstSpace] + operator[lastSpace:]
}

func splitOrderBy(orderby string) ([]eleOrderBy, error) {
var err error
var eleOrder []eleOrderBy
Expand Down
25 changes: 25 additions & 0 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,3 +843,28 @@ func TestBuildCombinedBetween(t *testing.T) {
ass.Equal(expectCond, cond)
ass.Equal([]interface{}{"caibirdme", "beijing", "chengdu", 3.5, 7.2, 10, 30}, vals)
}

func TestNotLike(t *testing.T) {
where := map[string]interface{}{
"name not like ": "%ny",
}
cond, vals, err := BuildSelect("tb", where, nil)
ass := assert.New(t)
ass.NoError(err)
expectCond := `SELECT * FROM tb WHERE (name NOT LIKE ?)`
ass.Equal(expectCond, cond)
ass.Equal([]interface{}{"%ny"}, vals)
}

func TestNotLike_1(t *testing.T) {
where := map[string]interface{}{
"name not like ": "%ny",
"age": 20,
}
cond, vals, err := BuildSelect("tb", where, nil)
ass := assert.New(t)
ass.NoError(err)
expectCond := `SELECT * FROM tb WHERE (age=? AND name NOT LIKE ?)`
ass.Equal(expectCond, cond)
ass.Equal([]interface{}{20, "%ny"}, vals)
}
21 changes: 21 additions & 0 deletions builder/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ func (l Like) Build() ([]string, []interface{}) {
return cond, vals
}

type NotLike map[string]interface{}

// Build implements the Comparable interface
func (l NotLike) Build() ([]string, []interface{}) {
if nil == l || 0 == len(l) {
return nil, nil
}
var cond []string
var vals []interface{}
for k := range l {
cond = append(cond, k)
}
defaultSortAlgorithm(cond)
for j := 0; j < len(cond); j++ {
val := l[cond[j]]
cond[j] = cond[j] + " NOT LIKE ?"
vals = append(vals, val)
}
return cond, vals
}

//Eq means equal(=)
type Eq map[string]interface{}

Expand Down

0 comments on commit 55d5813

Please sign in to comment.