Merge pull request #116 from pierDipi/not-equal

Add not equal comparators (!=)
This commit is contained in:
Henrik Johansson
2019-11-04 16:13:41 +01:00
committed by GitHub
2 changed files with 75 additions and 0 deletions

View File

@@ -21,6 +21,7 @@ const (
cnt cnt
cntKey cntKey
like like
ne
) )
// Cmp if a filtering comparator that is used in WHERE and IF clauses. // Cmp if a filtering comparator that is used in WHERE and IF clauses.
@@ -53,6 +54,8 @@ func (c Cmp) writeCql(cql *bytes.Buffer) (names []string) {
cql.WriteString(" CONTAINS KEY ") cql.WriteString(" CONTAINS KEY ")
case like: case like:
cql.WriteString(" LIKE ") cql.WriteString(" LIKE ")
case ne:
cql.WriteString("!=")
} }
return c.value.writeCql(cql) return c.value.writeCql(cql)
} }
@@ -105,6 +108,54 @@ func EqFunc(column string, fn *Func) Cmp {
} }
} }
// Ne produces column!=?.
func Ne(column string) Cmp {
return Cmp{
op: ne,
column: column,
value: param(column),
}
}
// NeTuple produces column!=(?,?,...) with count number of placeholders.
func NeTuple(column string, count int) Cmp {
return Cmp{
op: ne,
column: column,
value: tupleParam{
param: param(column),
count: count,
},
}
}
// NeNamed produces column!=? with a custom parameter name.
func NeNamed(column, name string) Cmp {
return Cmp{
op: ne,
column: column,
value: param(name),
}
}
// NeLit produces column!=literal and does not add a parameter to the query.
func NeLit(column, literal string) Cmp {
return Cmp{
op: ne,
column: column,
value: lit(literal),
}
}
// NeFunc produces column!=someFunc(?...).
func NeFunc(column string, fn *Func) Cmp {
return Cmp{
op: ne,
column: column,
value: fn,
}
}
// Lt produces column<?. // Lt produces column<?.
func Lt(column string) Cmp { func Lt(column string) Cmp {
return Cmp{ return Cmp{

View File

@@ -103,6 +103,16 @@ func TestCmp(t *testing.T) {
S: "like LIKE (?,?)", S: "like LIKE (?,?)",
N: []string{"like_0", "like_1"}, N: []string{"like_0", "like_1"},
}, },
{
C: Ne("ne"),
S: "ne!=?",
N: []string{"ne"},
},
{
C: NeTuple("ne", 3),
S: "ne!=(?,?,?)",
N: []string{"ne_0", "ne_1", "ne_2"},
},
// Custom bind names // Custom bind names
{ {
@@ -145,6 +155,11 @@ func TestCmp(t *testing.T) {
S: "cntKey CONTAINS KEY ?", S: "cntKey CONTAINS KEY ?",
N: []string{"name"}, N: []string{"name"},
}, },
{
C: NeNamed("ne", "name"),
S: "ne!=?",
N: []string{"name"},
},
// Literals // Literals
{ {
@@ -175,6 +190,10 @@ func TestCmp(t *testing.T) {
C: ContainsLit("cnt", "litval"), C: ContainsLit("cnt", "litval"),
S: "cnt CONTAINS litval", S: "cnt CONTAINS litval",
}, },
{
C: NeLit("ne", "litval"),
S: "ne!=litval",
},
// Functions // Functions
{ {
@@ -214,6 +233,11 @@ func TestCmp(t *testing.T) {
S: "eq>=maxTimeuuid(?)", S: "eq>=maxTimeuuid(?)",
N: []string{"arg0"}, N: []string{"arg0"},
}, },
{
C: NeFunc("ne", Fn("fn", "arg0", "arg1", "arg2")),
S: "ne!=fn(?,?,?)",
N: []string{"arg0", "arg1", "arg2"},
},
} }
buf := bytes.Buffer{} buf := bytes.Buffer{}