Skip to content

Commit

Permalink
qb: json support
Browse files Browse the repository at this point in the history
  • Loading branch information
dahankzter authored and mmatczuk committed May 14, 2019
1 parent eadf9b5 commit 31ae81a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ issues:
exclude:
- composite literal uses unkeyed fields
- Error return value of `.+\.Close` is not checked
- method Json should be JSON
14 changes: 14 additions & 0 deletions qb/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type InsertBuilder struct {
columns []initializer
unique bool
using using
json bool
}

// Insert returns a new InsertBuilder with the given table name.
Expand All @@ -43,6 +44,13 @@ func (b *InsertBuilder) ToCql() (stmt string, names []string) {
cql.WriteString(b.table)
cql.WriteByte(' ')

if b.json {
// Ignore everything else since it goes into the Json
cql.WriteString("JSON ?")
stmt = cql.String()
return
}

cql.WriteByte('(')
for i, c := range b.columns {
cql.WriteString(c.column)
Expand Down Expand Up @@ -76,6 +84,12 @@ func (b *InsertBuilder) Into(table string) *InsertBuilder {
return b
}

// Json sets the Json clause of the query.
func (b *InsertBuilder) Json() *InsertBuilder {
b.json = true
return b
}

// Columns adds insert columns to the query.
func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder {
for _, c := range columns {
Expand Down
6 changes: 6 additions & 0 deletions qb/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ func TestInsertBuilder(t *testing.T) {
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) ",
N: []string{"id", "user_uuid", "firstname"},
},
// Basic test for insert JSON
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Json(),
S: "INSERT INTO cycling.cyclist_name JSON ?",
N: nil,
},
// Change table name
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Into("Foobar"),
Expand Down
12 changes: 12 additions & 0 deletions qb/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type SelectBuilder struct {
limit uint
limitPerPartition uint
allowFiltering bool
json bool
}

// Select returns a new SelectBuilder with the given table name.
Expand All @@ -54,6 +55,11 @@ func (b *SelectBuilder) ToCql() (stmt string, names []string) {
cql := bytes.Buffer{}

cql.WriteString("SELECT ")

if b.json {
cql.WriteString("JSON ")
}

switch {
case len(b.distinct) > 0:
cql.WriteString("DISTINCT ")
Expand Down Expand Up @@ -113,6 +119,12 @@ func (b *SelectBuilder) From(table string) *SelectBuilder {
return b
}

// Json sets the clause of the query.
func (b *SelectBuilder) Json() *SelectBuilder {
b.json = true
return b
}

// Columns adds result columns to the query.
func (b *SelectBuilder) Columns(columns ...string) *SelectBuilder {
b.columns = append(b.columns, columns...)
Expand Down
10 changes: 10 additions & 0 deletions qb/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ func TestSelectBuilder(t *testing.T) {
B: Select("cycling.cyclist_name").Columns("id", "user_uuid", As("firstname", "name")),
S: "SELECT id,user_uuid,firstname AS name FROM cycling.cyclist_name ",
},
// Basic test for select columns as JSON
{
B: Select("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Json(),
S: "SELECT JSON id,user_uuid,firstname FROM cycling.cyclist_name ",
},
// Add a SELECT AS column as JSON
{
B: Select("cycling.cyclist_name").Columns("id", "user_uuid", As("firstname", "name")).Json(),
S: "SELECT JSON id,user_uuid,firstname AS name FROM cycling.cyclist_name ",
},
// Add a SELECT AS column 2
{
B: Select("cycling.cyclist_name").
Expand Down

0 comments on commit 31ae81a

Please sign in to comment.