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

Support passing in custom columns for insert stmt #250

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 20 additions & 12 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,32 @@ func (t *Table) SelectAll() (stmt string, names []string) {
return qb.Select(t.metadata.Name).ToCql()
}

// Insert returns insert all columns statement.
func (t *Table) Insert() (stmt string, names []string) {
return t.insert.stmt, t.insert.names
// Insert returns insert statement.
func (t *Table) Insert(columns ...string) (stmt string, names []string) {
if len(columns) == 0 {
return t.insert.stmt, t.insert.names
}

return t.InsertBuilder(columns...).ToCql()
}

// InsertQuery returns query which inserts all columns.
func (t *Table) InsertQuery(session gocqlx.Session) *gocqlx.Queryx {
return session.Query(t.Insert())
// InsertQuery returns query which inserts columns.
func (t *Table) InsertQuery(session gocqlx.Session, columns ...string) *gocqlx.Queryx {
return session.Query(t.Insert(columns...))
}

// InsertQueryContext returns query wrapped with context which inserts all columns.
func (t *Table) InsertQueryContext(ctx context.Context, session gocqlx.Session) *gocqlx.Queryx {
return t.InsertQuery(session).WithContext(ctx)
// InsertQueryContext returns query wrapped with context which inserts columns.
func (t *Table) InsertQueryContext(ctx context.Context, session gocqlx.Session, columns ...string) *gocqlx.Queryx {
return t.InsertQuery(session, columns...).WithContext(ctx)
}

// InsertBuilder returns a builder initialised with all columns.
func (t *Table) InsertBuilder() *qb.InsertBuilder {
return qb.Insert(t.metadata.Name).Columns(t.metadata.Columns...)
// InsertBuilder returns a builder initialised with columns.
func (t *Table) InsertBuilder(columns ...string) *qb.InsertBuilder {
if len(columns) == 0 {
return qb.Insert(t.metadata.Name).Columns(t.metadata.Columns...)
}

return qb.Insert(t.metadata.Name).Columns(columns...)
}

// Update returns update by primary key statement.
Expand Down
25 changes: 24 additions & 1 deletion table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func TestTableSelect(t *testing.T) {
func TestTableInsert(t *testing.T) {
table := []struct {
M Metadata
C []string
N []string
S string
}{
Expand All @@ -129,10 +130,32 @@ func TestTableInsert(t *testing.T) {
N: []string{"a", "b", "c", "d"},
S: "INSERT INTO table (a,b,c,d) VALUES (?,?,?,?) ",
},
{
M: Metadata{
Name: "table",
Columns: []string{"a", "b", "c", "d"},
PartKey: []string{"a"},
SortKey: []string{"b"},
},
C: []string{"a", "b", "c"},
N: []string{"a", "b", "c"},
S: "INSERT INTO table (a,b,c) VALUES (?,?,?) ",
},
}

for _, test := range table {
stmt, names := New(test.M).Insert(test.C...)
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff, names)
}
}

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