diff --git a/qb/cmp.go b/qb/cmp.go index fe13260..53bfec8 100644 --- a/qb/cmp.go +++ b/qb/cmp.go @@ -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{ diff --git a/qb/cmp_test.go b/qb/cmp_test.go index ff4b08d..f9f459d 100644 --- a/qb/cmp_test.go +++ b/qb/cmp_test.go @@ -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 {