From fc0b7be8abe3e2a4ee26c88e12eb27e7e0cc475b Mon Sep 17 00:00:00 2001 From: "xiang.wang" Date: Fri, 13 Mar 2020 10:56:00 +0800 Subject: [PATCH] table: Added cql.builders for insert, update, delete to enable table batch timestamp at statement granularity. --- table/table.go | 14 ++++++++++++-- table/table_test.go | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/table/table.go b/table/table.go index 7d1ecd4..71c5817 100644 --- a/table/table.go +++ b/table/table.go @@ -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...) } diff --git a/table/table_test.go b/table/table_test.go index 151a5c9..dd806e2 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -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) { @@ -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) {