Add qb.ContainsKey to query keys in a map (#57)
qb: add ContainsKey and ContainsKeyNamed
This commit is contained in:
committed by
Michał Matczuk
parent
9b530002fa
commit
2f13a81c46
21
qb/cmp.go
21
qb/cmp.go
@@ -19,6 +19,7 @@ const (
|
|||||||
geq
|
geq
|
||||||
in
|
in
|
||||||
cnt
|
cnt
|
||||||
|
cntKey
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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.
|
||||||
@@ -47,6 +48,8 @@ func (c Cmp) writeCql(cql *bytes.Buffer) (names []string) {
|
|||||||
cql.WriteString(" IN ")
|
cql.WriteString(" IN ")
|
||||||
case cnt:
|
case cnt:
|
||||||
cql.WriteString(" CONTAINS ")
|
cql.WriteString(" CONTAINS ")
|
||||||
|
case cntKey:
|
||||||
|
cql.WriteString(" CONTAINS KEY ")
|
||||||
}
|
}
|
||||||
return c.value.writeCql(cql)
|
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.
|
// ContainsNamed produces column CONTAINS ? with a custom parameter name.
|
||||||
func ContainsNamed(column, name string) Cmp {
|
func ContainsNamed(column, name string) Cmp {
|
||||||
return 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.
|
// ContainsLit produces column CONTAINS literal and does not add a parameter to the query.
|
||||||
func ContainsLit(column, literal string) Cmp {
|
func ContainsLit(column, literal string) Cmp {
|
||||||
return Cmp{
|
return Cmp{
|
||||||
|
|||||||
@@ -53,6 +53,11 @@ func TestCmp(t *testing.T) {
|
|||||||
S: "cnt CONTAINS ?",
|
S: "cnt CONTAINS ?",
|
||||||
N: []string{"cnt"},
|
N: []string{"cnt"},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
C: ContainsKey("cntKey"),
|
||||||
|
S: "cntKey CONTAINS KEY ?",
|
||||||
|
N: []string{"cntKey"},
|
||||||
|
},
|
||||||
|
|
||||||
// Custom bind names
|
// Custom bind names
|
||||||
{
|
{
|
||||||
@@ -90,6 +95,11 @@ func TestCmp(t *testing.T) {
|
|||||||
S: "cnt CONTAINS ?",
|
S: "cnt CONTAINS ?",
|
||||||
N: []string{"name"},
|
N: []string{"name"},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
C: ContainsKeyNamed("cntKey", "name"),
|
||||||
|
S: "cntKey CONTAINS KEY ?",
|
||||||
|
N: []string{"name"},
|
||||||
|
},
|
||||||
|
|
||||||
// Literals
|
// Literals
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user