Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add <Cmp>TupleNamed for two-sided range-queries on tuples #219

Merged
merged 3 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions qb/cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ func EqNamed(column, name string) Cmp {
}
}

// EqTupleNamed produces column=(?,?,...) with count number of placeholders and custom name.
func EqTupleNamed(column string, count int, name string) Cmp {
return Cmp{
op: eq,
column: column,
value: tupleParam{
param: param(name),
count: count,
},
}
}

// EqLit produces column=literal and does not add a parameter to the query.
func EqLit(column, literal string) Cmp {
return Cmp{
Expand Down Expand Up @@ -138,6 +150,18 @@ func NeNamed(column, name string) Cmp {
}
}

// NeTupleNamed produces column!=(?,?,...) with count number of placeholders and custom name.
func NeTupleNamed(column string, count int, name string) Cmp {
return Cmp{
op: ne,
column: column,
value: tupleParam{
param: param(name),
count: count,
},
}
}

// NeLit produces column!=literal and does not add a parameter to the query.
func NeLit(column, literal string) Cmp {
return Cmp{
Expand Down Expand Up @@ -186,6 +210,18 @@ func LtNamed(column, name string) Cmp {
}
}

// LtTupleNamed produces column<(?,?,...) with count placeholders and custom name.
func LtTupleNamed(column string, count int, name string) Cmp {
return Cmp{
op: lt,
column: column,
value: tupleParam{
param: param(name),
count: count,
},
}
}

// LtLit produces column<literal and does not add a parameter to the query.
func LtLit(column, literal string) Cmp {
return Cmp{
Expand Down Expand Up @@ -234,6 +270,18 @@ func LtOrEqNamed(column, name string) Cmp {
}
}

// LtOrEqTupleNamed produces column<=(?,?,...) with count placeholders and custom name.
func LtOrEqTupleNamed(column string, count int, name string) Cmp {
return Cmp{
op: leq,
column: column,
value: tupleParam{
param: param(name),
count: count,
},
}
}

// LtOrEqLit produces column<=literal and does not add a parameter to the query.
func LtOrEqLit(column, literal string) Cmp {
return Cmp{
Expand Down Expand Up @@ -282,6 +330,18 @@ func GtNamed(column, name string) Cmp {
}
}

// GtTupleNamed produces column>(?,?,...) with count placeholders and custom name.
func GtTupleNamed(column string, count int, name string) Cmp {
return Cmp{
op: gt,
column: column,
value: tupleParam{
param: param(name),
count: count,
},
}
}

// GtLit produces column>literal and does not add a parameter to the query.
func GtLit(column, literal string) Cmp {
return Cmp{
Expand Down Expand Up @@ -330,6 +390,18 @@ func GtOrEqNamed(column, name string) Cmp {
}
}

// GtOrEqTupleNamed produces column>=(?,?,...) with count placeholders and custom name.
func GtOrEqTupleNamed(column string, count int, name string) Cmp {
return Cmp{
op: geq,
column: column,
value: tupleParam{
param: param(name),
count: count,
},
}
}

// GtOrEqLit produces column>=literal and does not add a parameter to the query.
func GtOrEqLit(column, literal string) Cmp {
return Cmp{
Expand Down Expand Up @@ -378,6 +450,18 @@ func InNamed(column, name string) Cmp {
}
}

// InTupleNamed produces column IN ? with a custom parameter name.
func InTupleNamed(column string, count int, name string) Cmp {
return Cmp{
op: in,
column: column,
value: tupleParam{
param: param(name),
count: count,
},
}
}

// InLit produces column IN literal and does not add a parameter to the query.
func InLit(column, literal string) Cmp {
return Cmp{
Expand Down Expand Up @@ -447,6 +531,30 @@ func ContainsKeyNamed(column, name string) Cmp {
}
}

// ContainsTupleNamed produces column CONTAINS (?,?,...) with count placeholders and custom name.
func ContainsTupleNamed(column string, count int, name string) Cmp {
return Cmp{
op: cnt,
column: column,
value: tupleParam{
param: param(name),
count: count,
},
}
}

// ContainsKeyTupleNamed produces column CONTAINS KEY (?,?,...) with count placehplders and custom name.
func ContainsKeyTupleNamed(column string, count int, name string) Cmp {
return Cmp{
op: cntKey,
column: column,
value: tupleParam{
param: param(name),
count: count,
},
}
}

// ContainsLit produces column CONTAINS literal and does not add a parameter to the query.
func ContainsLit(column, literal string) Cmp {
return Cmp{
Expand Down Expand Up @@ -477,6 +585,18 @@ func LikeTuple(column string, count int) Cmp {
}
}

// LikeTupleNamed produces column LIKE (?,?,...) with count placeholders and custom name.
func LikeTupleNamed(column string, count int, name string) Cmp {
return Cmp{
op: like,
column: column,
value: tupleParam{
param: param(name),
count: count,
},
}
}

type cmps []Cmp

func (cs cmps) writeCql(cql *bytes.Buffer) (names []string) {
Expand Down
71 changes: 62 additions & 9 deletions qb/cmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestCmp(t *testing.T) {
{
C: NeTuple("ne", 3),
S: "ne!=(?,?,?)",
N: []string{"ne_0", "ne_1", "ne_2"},
N: []string{"ne[0]", "ne[1]", "ne[2]"},
},
{
C: Lt("lt"),
Expand All @@ -41,7 +41,7 @@ func TestCmp(t *testing.T) {
{
C: LtTuple("lt", 2),
S: "lt<(?,?)",
N: []string{"lt_0", "lt_1"},
N: []string{"lt[0]", "lt[1]"},
},
{
C: LtOrEq("lt"),
Expand All @@ -51,7 +51,7 @@ func TestCmp(t *testing.T) {
{
C: LtOrEqTuple("lt", 2),
S: "lt<=(?,?)",
N: []string{"lt_0", "lt_1"},
N: []string{"lt[0]", "lt[1]"},
},
{
C: Gt("gt"),
Expand All @@ -61,7 +61,7 @@ func TestCmp(t *testing.T) {
{
C: GtTuple("gt", 2),
S: "gt>(?,?)",
N: []string{"gt_0", "gt_1"},
N: []string{"gt[0]", "gt[1]"},
},
{
C: GtOrEq("gt"),
Expand All @@ -71,7 +71,7 @@ func TestCmp(t *testing.T) {
{
C: GtOrEqTuple("gt", 2),
S: "gt>=(?,?)",
N: []string{"gt_0", "gt_1"},
N: []string{"gt[0]", "gt[1]"},
},
{
C: In("in"),
Expand All @@ -81,7 +81,7 @@ func TestCmp(t *testing.T) {
{
C: InTuple("in", 2),
S: "in IN (?,?)",
N: []string{"in_0", "in_1"},
N: []string{"in[0]", "in[1]"},
},
{
C: Contains("cnt"),
Expand All @@ -91,7 +91,7 @@ func TestCmp(t *testing.T) {
{
C: ContainsTuple("cnt", 2),
S: "cnt CONTAINS (?,?)",
N: []string{"cnt_0", "cnt_1"},
N: []string{"cnt[0]", "cnt[1]"},
},
{
C: ContainsKey("cntKey"),
Expand All @@ -101,7 +101,7 @@ func TestCmp(t *testing.T) {
{
C: ContainsKeyTuple("cntKey", 2),
S: "cntKey CONTAINS KEY (?,?)",
N: []string{"cntKey_0", "cntKey_1"},
N: []string{"cntKey[0]", "cntKey[1]"},
},
{
C: Like("like"),
Expand All @@ -111,7 +111,7 @@ func TestCmp(t *testing.T) {
{
C: LikeTuple("like", 2),
S: "like LIKE (?,?)",
N: []string{"like_0", "like_1"},
N: []string{"like[0]", "like[1]"},
},

// Custom bind names
Expand Down Expand Up @@ -160,6 +160,59 @@ func TestCmp(t *testing.T) {
S: "cntKey CONTAINS KEY ?",
N: []string{"name"},
},
{
C: LikeTupleNamed("like", 2, "name"),
S: "like LIKE (?,?)",
N: []string{"name[0]", "name[1]"},
},


// Custom bind names on tuples
{
C: EqTupleNamed("eq", 2, "name"),
S: "eq=(?,?)",
N: []string{"name[0]", "name[1]"},
},
{
C: NeTupleNamed("ne", 3, "name"),
S: "ne!=(?,?,?)",
N: []string{"name[0]", "name[1]", "name[2]"},
},
{
C: LtTupleNamed("lt", 2, "name"),
S: "lt<(?,?)",
N: []string{"name[0]", "name[1]"},
},
{
C: LtOrEqTupleNamed("lt", 2, "name"),
S: "lt<=(?,?)",
N: []string{"name[0]", "name[1]"},
},
{
C: GtTupleNamed("gt", 2, "name"),
S: "gt>(?,?)",
N: []string{"name[0]", "name[1]"},
},
{
C: GtOrEqTupleNamed("gt", 2, "name"),
S: "gt>=(?,?)",
N: []string{"name[0]", "name[1]"},
},
{
C: InTupleNamed("in", 2, "name"),
S: "in IN (?,?)",
N: []string{"name[0]", "name[1]"},
},
{
C: ContainsTupleNamed("cnt", 2, "name"),
S: "cnt CONTAINS (?,?)",
N: []string{"name[0]", "name[1]"},
},
{
C: ContainsKeyTupleNamed("cntKey", 2, "name"),
S: "cntKey CONTAINS KEY (?,?)",
N: []string{"name[0]", "name[1]"},
},

// Literals
{
Expand Down
6 changes: 3 additions & 3 deletions qb/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ func TestDeleteBuilder(t *testing.T) {
{
B: Delete("cycling.cyclist_name").Where(EqTuple("id", 2)).Columns("stars"),
S: "DELETE stars FROM cycling.cyclist_name WHERE id=(?,?) ",
N: []string{"id_0", "id_1"},
N: []string{"id[0]", "id[1]"},
},
// Add WHERE for tuple column
{
B: Delete("cycling.cyclist_name").Where(w, GtTuple("firstname", 2)),
S: "DELETE FROM cycling.cyclist_name WHERE id=? AND firstname>(?,?) ",
N: []string{"expr", "firstname_0", "firstname_1"},
N: []string{"expr", "firstname[0]", "firstname[1]"},
},
// Add WHERE for all tuple columns
{
B: Delete("cycling.cyclist_name").Where(EqTuple("id", 2), GtTuple("firstname", 2)),
S: "DELETE FROM cycling.cyclist_name WHERE id=(?,?) AND firstname>(?,?) ",
N: []string{"id_0", "id_1", "firstname_0", "firstname_1"},
N: []string{"id[0]", "id[1]", "firstname[0]", "firstname[1]"},
},
// Add IF
{
Expand Down
4 changes: 2 additions & 2 deletions qb/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ func TestInsertBuilder(t *testing.T) {
{
B: Insert("cycling.cyclist_name").TupleColumn("id", 2),
S: "INSERT INTO cycling.cyclist_name (id) VALUES ((?,?)) ",
N: []string{"id_0", "id_1"},
N: []string{"id[0]", "id[1]"},
},
{
B: Insert("cycling.cyclist_name").TupleColumn("id", 2).Columns("user_uuid"),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid) VALUES ((?,?),?) ",
N: []string{"id_0", "id_1", "user_uuid"},
N: []string{"id[0]", "id[1]", "user_uuid"},
},
// Add IF NOT EXISTS
{
Expand Down
4 changes: 2 additions & 2 deletions qb/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func TestSelectBuilder(t *testing.T) {
{
B: Select("cycling.cyclist_name").Where(EqTuple("id", 2), Gt("firstname")),
S: "SELECT * FROM cycling.cyclist_name WHERE id=(?,?) AND firstname>? ",
N: []string{"id_0", "id_1", "firstname"},
N: []string{"id[0]", "id[1]", "firstname"},
},
// Add WHERE with only tuples
{
B: Select("cycling.cyclist_name").Where(EqTuple("id", 2), GtTuple("firstname", 2)),
S: "SELECT * FROM cycling.cyclist_name WHERE id=(?,?) AND firstname>(?,?) ",
N: []string{"id_0", "id_1", "firstname_0", "firstname_1"},
N: []string{"id[0]", "id[1]", "firstname[0]", "firstname[1]"},
},
// Add TIMEOUT
{
Expand Down
2 changes: 1 addition & 1 deletion qb/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestUpdateBuilder(t *testing.T) {
{
B: Update("cycling.cyclist_name").SetTuple("id", 2).Set("user_uuid", "firstname").Where(EqTuple("id", 2)),
S: "UPDATE cycling.cyclist_name SET id=(?,?),user_uuid=?,firstname=? WHERE id=(?,?) ",
N: []string{"id_0", "id_1", "user_uuid", "firstname", "id_0", "id_1"},
N: []string{"id[0]", "id[1]", "user_uuid", "firstname", "id[0]", "id[1]"},
},
// Add SET SetFunc
{
Expand Down
Loading