qb: json support

This commit is contained in:
Henrik Johansson
2019-05-13 14:01:13 +02:00
committed by Michal Matczuk
parent eadf9b5427
commit 31ae81aba6
5 changed files with 43 additions and 0 deletions

View File

@@ -20,3 +20,4 @@ issues:
exclude: exclude:
- composite literal uses unkeyed fields - composite literal uses unkeyed fields
- Error return value of `.+\.Close` is not checked - Error return value of `.+\.Close` is not checked
- method Json should be JSON

View File

@@ -24,6 +24,7 @@ type InsertBuilder struct {
columns []initializer columns []initializer
unique bool unique bool
using using using using
json bool
} }
// Insert returns a new InsertBuilder with the given table name. // Insert returns a new InsertBuilder with the given table name.
@@ -43,6 +44,13 @@ func (b *InsertBuilder) ToCql() (stmt string, names []string) {
cql.WriteString(b.table) cql.WriteString(b.table)
cql.WriteByte(' ') cql.WriteByte(' ')
if b.json {
// Ignore everything else since it goes into the Json
cql.WriteString("JSON ?")
stmt = cql.String()
return
}
cql.WriteByte('(') cql.WriteByte('(')
for i, c := range b.columns { for i, c := range b.columns {
cql.WriteString(c.column) cql.WriteString(c.column)
@@ -76,6 +84,12 @@ func (b *InsertBuilder) Into(table string) *InsertBuilder {
return b 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. // Columns adds insert columns to the query.
func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder { func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder {
for _, c := range columns { for _, c := range columns {

View File

@@ -24,6 +24,12 @@ func TestInsertBuilder(t *testing.T) {
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) ", S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) ",
N: []string{"id", "user_uuid", "firstname"}, 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 // Change table name
{ {
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Into("Foobar"), B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Into("Foobar"),

View File

@@ -40,6 +40,7 @@ type SelectBuilder struct {
limit uint limit uint
limitPerPartition uint limitPerPartition uint
allowFiltering bool allowFiltering bool
json bool
} }
// Select returns a new SelectBuilder with the given table name. // Select returns a new SelectBuilder with the given table name.
@@ -54,6 +55,11 @@ func (b *SelectBuilder) ToCql() (stmt string, names []string) {
cql := bytes.Buffer{} cql := bytes.Buffer{}
cql.WriteString("SELECT ") cql.WriteString("SELECT ")
if b.json {
cql.WriteString("JSON ")
}
switch { switch {
case len(b.distinct) > 0: case len(b.distinct) > 0:
cql.WriteString("DISTINCT ") cql.WriteString("DISTINCT ")
@@ -113,6 +119,12 @@ func (b *SelectBuilder) From(table string) *SelectBuilder {
return b 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. // Columns adds result columns to the query.
func (b *SelectBuilder) Columns(columns ...string) *SelectBuilder { func (b *SelectBuilder) Columns(columns ...string) *SelectBuilder {
b.columns = append(b.columns, columns...) b.columns = append(b.columns, columns...)

View File

@@ -33,6 +33,16 @@ func TestSelectBuilder(t *testing.T) {
B: Select("cycling.cyclist_name").Columns("id", "user_uuid", As("firstname", "name")), B: Select("cycling.cyclist_name").Columns("id", "user_uuid", As("firstname", "name")),
S: "SELECT id,user_uuid,firstname AS name FROM cycling.cyclist_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 // Add a SELECT AS column 2
{ {
B: Select("cycling.cyclist_name"). B: Select("cycling.cyclist_name").