Skip to content

Commit

Permalink
expose SelectBuilder for table Get operation (#251)
Browse files Browse the repository at this point in the history
* expose select builder for get

* add comment
  • Loading branch information
YeminLi authored Sep 30, 2024
1 parent 3ea85fd commit cc68867
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ 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...)
}

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 {
Expand Down
11 changes: 11 additions & 0 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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) {
Expand Down

0 comments on commit cc68867

Please sign in to comment.