From eb54c521efac971aafc7f4b18d6c3e00e8f8ce02 Mon Sep 17 00:00:00 2001 From: "yemin.li" Date: Fri, 3 Feb 2023 15:19:14 -0500 Subject: [PATCH 1/2] expose select builder for get --- table/table.go | 8 ++++++++ table/table_test.go | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/table/table.go b/table/table.go index 0ead82d..a2276a0 100644 --- a/table/table.go +++ b/table/table.go @@ -102,6 +102,14 @@ func (t *Table) GetQueryContext(ctx context.Context, session gocqlx.Session, col return t.GetQuery(session, columns...).WithContext(ctx) } +func (t *Table) GetBuilder(columns ...string) *qb.SelectBuilder { + if len(columns) == 0 { + return qb.Select(t.metadata.Name).Where(t.primaryKeyCmp...) + } + + return qb.Select(t.metadata.Name).Columns(columns...).Where(t.primaryKeyCmp...) +} + // Select returns select by partition key statement. func (t *Table) Select(columns ...string) (stmt string, names []string) { if len(columns) == 0 { diff --git a/table/table_test.go b/table/table_test.go index 32871de..c71ff9b 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -59,6 +59,17 @@ func TestTableGet(t *testing.T) { t.Error(diff, names) } } + + // run GetBuilder on the same data set + for _, test := range table { + stmt, names := New(test.M).GetBuilder(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 TestTableSelect(t *testing.T) { From a182598de0249362a061655a80ce373bcb7947b7 Mon Sep 17 00:00:00 2001 From: "yemin.li" Date: Fri, 3 Feb 2023 15:24:10 -0500 Subject: [PATCH 2/2] add comment --- table/table.go | 1 + 1 file changed, 1 insertion(+) diff --git a/table/table.go b/table/table.go index a2276a0..5c72026 100644 --- a/table/table.go +++ b/table/table.go @@ -102,6 +102,7 @@ func (t *Table) GetQueryContext(ctx context.Context, session gocqlx.Session, col return t.GetQuery(session, columns...).WithContext(ctx) } +// GetBuilder returns a builder initialised to select by primary key func (t *Table) GetBuilder(columns ...string) *qb.SelectBuilder { if len(columns) == 0 { return qb.Select(t.metadata.Name).Where(t.primaryKeyCmp...)