Add qb.ContainsKey to query keys in a map (#57)

qb: add ContainsKey and ContainsKeyNamed
This commit is contained in:
Vincent Rischmann
2018-07-28 06:56:44 +02:00
committed by Michał Matczuk
parent 9b530002fa
commit 2f13a81c46
2 changed files with 31 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ const (
geq
in
cnt
cntKey
)
// Cmp if a filtering comparator that is used in WHERE and IF clauses.
@@ -47,6 +48,8 @@ func (c Cmp) writeCql(cql *bytes.Buffer) (names []string) {
cql.WriteString(" IN ")
case cnt:
cql.WriteString(" CONTAINS ")
case cntKey:
cql.WriteString(" CONTAINS KEY ")
}
return c.value.writeCql(cql)
}
@@ -267,6 +270,15 @@ func Contains(column string) Cmp {
}
}
// ContainsKey produces column CONTAINS KEY ?.
func ContainsKey(column string) Cmp {
return Cmp{
op: cntKey,
column: column,
value: param(column),
}
}
// ContainsNamed produces column CONTAINS ? with a custom parameter name.
func ContainsNamed(column, name string) Cmp {
return Cmp{
@@ -276,6 +288,15 @@ func ContainsNamed(column, name string) Cmp {
}
}
// ContainsKeyNamed produces column CONTAINS KEY ? with a custom parameter name.
func ContainsKeyNamed(column, name string) Cmp {
return Cmp{
op: cntKey,
column: column,
value: param(name),
}
}
// ContainsLit produces column CONTAINS literal and does not add a parameter to the query.
func ContainsLit(column, literal string) Cmp {
return Cmp{

View File

@@ -53,6 +53,11 @@ func TestCmp(t *testing.T) {
S: "cnt CONTAINS ?",
N: []string{"cnt"},
},
{
C: ContainsKey("cntKey"),
S: "cntKey CONTAINS KEY ?",
N: []string{"cntKey"},
},
// Custom bind names
{
@@ -90,6 +95,11 @@ func TestCmp(t *testing.T) {
S: "cnt CONTAINS ?",
N: []string{"name"},
},
{
C: ContainsKeyNamed("cntKey", "name"),
S: "cntKey CONTAINS KEY ?",
N: []string{"name"},
},
// Literals
{