From 31ae81aba6678a4f78ce5006427266a28e2498c7 Mon Sep 17 00:00:00 2001 From: Henrik Johansson Date: Mon, 13 May 2019 14:01:13 +0200 Subject: [PATCH] qb: json support --- .golangci.yml | 1 + qb/insert.go | 14 ++++++++++++++ qb/insert_test.go | 6 ++++++ qb/select.go | 12 ++++++++++++ qb/select_test.go | 10 ++++++++++ 5 files changed, 43 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index b77191b..39fb158 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -20,3 +20,4 @@ issues: exclude: - composite literal uses unkeyed fields - Error return value of `.+\.Close` is not checked + - method Json should be JSON diff --git a/qb/insert.go b/qb/insert.go index dc628c4..3caacf1 100644 --- a/qb/insert.go +++ b/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 { diff --git a/qb/insert_test.go b/qb/insert_test.go index 53b4f87..7d8e3ca 100644 --- a/qb/insert_test.go +++ b/qb/insert_test.go @@ -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"), diff --git a/qb/select.go b/qb/select.go index 8c7e0b2..4407ed0 100644 --- a/qb/select.go +++ b/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...) diff --git a/qb/select_test.go b/qb/select_test.go index a2f20fe..00d20b3 100644 --- a/qb/select_test.go +++ b/qb/select_test.go @@ -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").