qb: json support
This commit is contained in:
committed by
Michal Matczuk
parent
eadf9b5427
commit
31ae81aba6
@@ -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
qb/insert.go
14
qb/insert.go
@@ -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.
|
||||
@@ -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)
|
||||
@@ -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 {
|
||||
|
||||
@@ -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"),
|
||||
|
||||
12
qb/select.go
12
qb/select.go
@@ -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.
|
||||
@@ -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 ")
|
||||
@@ -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...)
|
||||
|
||||
@@ -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").
|
||||
|
||||
Reference in New Issue
Block a user