Skip to content

Commit

Permalink
table: Added cql.builders for insert, update, delete to enable table …
Browse files Browse the repository at this point in the history
…batch timestamp at statement granularity.
  • Loading branch information
xiang.wang authored and mmatczuk committed Mar 19, 2020
1 parent ab2a96d commit fc0b7be
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
14 changes: 12 additions & 2 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,20 @@ func (t *Table) Insert() (stmt string, names []string) {

// Update returns update by primary key statement.
func (t *Table) Update(columns ...string) (stmt string, names []string) {
return qb.Update(t.metadata.Name).Set(columns...).Where(t.primaryKeyCmp...).ToCql()
return t.UpdateBuilder(columns...).ToCql()
}

// UpdateBuilder returns a builder initialised to update by primary key statement.
func (t *Table) UpdateBuilder(columns ...string) *qb.UpdateBuilder {
return qb.Update(t.metadata.Name).Set(columns...).Where(t.primaryKeyCmp...)
}

// Delete returns delete by primary key statement.
func (t *Table) Delete(columns ...string) (stmt string, names []string) {
return qb.Delete(t.metadata.Name).Columns(columns...).Where(t.primaryKeyCmp...).ToCql()
return t.DeleteBuilder(columns...).ToCql()
}

// DeleteBuilder returns a builder initialised to delete by primary key statement.
func (t *Table) DeleteBuilder(columns ...string) *qb.DeleteBuilder {
return qb.Delete(t.metadata.Name).Columns(columns...).Where(t.primaryKeyCmp...)
}
22 changes: 22 additions & 0 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ func TestTableUpdate(t *testing.T) {
t.Error(diff, names)
}
}

// run UpdateBuilder on the same data set
for _, test := range table {
stmt, names := New(test.M).UpdateBuilder(test.C...).ToCql()
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff, names)
}
}
}

func TestTableDelete(t *testing.T) {
Expand Down Expand Up @@ -220,6 +231,17 @@ func TestTableDelete(t *testing.T) {
t.Error(diff, names)
}
}

// run DeleteBuilder on the same data set
for _, test := range table {
stmt, names := New(test.M).DeleteBuilder(test.C...).ToCql()
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff, names)
}
}
}

func TestTableConcurrentUsage(t *testing.T) {
Expand Down

0 comments on commit fc0b7be

Please sign in to comment.