From 4f4f94e2e68390b12a4d1afc75830ae673668b4c Mon Sep 17 00:00:00 2001 From: Maciej Zimnoch Date: Wed, 17 Jun 2020 16:36:24 +0200 Subject: [PATCH] qb: Added shortcuts to Queryx It allows to write shorter and more straightforward code. Instead writing: ``` session.Query(qb.Select("cluster").Columns("id").ToCql()) ``` you can write: ``` qb.Select("cluster").Columns("id").Query(session) ``` --- qb/batch.go | 7 +++++++ qb/delete.go | 7 +++++++ qb/insert.go | 7 +++++++ qb/select.go | 7 +++++++ qb/update.go | 7 +++++++ 5 files changed, 35 insertions(+) diff --git a/qb/batch.go b/qb/batch.go index 4fa8bd5..973b31a 100644 --- a/qb/batch.go +++ b/qb/batch.go @@ -8,6 +8,8 @@ import ( "bytes" "fmt" "time" + + "github.com/scylladb/gocqlx/v2" ) // BATCH reference: @@ -64,6 +66,11 @@ func (b *BatchBuilder) ToCql() (stmt string, names []string) { return } +// Query returns query built on top of current BatchBuilder state. +func (b *BatchBuilder) Query(session gocqlx.Session) *gocqlx.Queryx { + return session.Query(b.ToCql()) +} + // Add builds the builder and adds the statement to the batch. func (b *BatchBuilder) Add(builder Builder) *BatchBuilder { return b.AddStmt(builder.ToCql()) diff --git a/qb/delete.go b/qb/delete.go index db8f6e8..089b8dc 100644 --- a/qb/delete.go +++ b/qb/delete.go @@ -10,6 +10,8 @@ package qb import ( "bytes" "time" + + "github.com/scylladb/gocqlx/v2" ) // DeleteBuilder builds CQL DELETE statements. @@ -54,6 +56,11 @@ func (b *DeleteBuilder) ToCql() (stmt string, names []string) { return } +// Query returns query built on top of current DeleteBuilder state. +func (b *DeleteBuilder) Query(session gocqlx.Session) *gocqlx.Queryx { + return session.Query(b.ToCql()) +} + // From sets the table to be deleted from. func (b *DeleteBuilder) From(table string) *DeleteBuilder { b.table = table diff --git a/qb/insert.go b/qb/insert.go index bf45771..0f8b7df 100644 --- a/qb/insert.go +++ b/qb/insert.go @@ -10,6 +10,8 @@ package qb import ( "bytes" "time" + + "github.com/scylladb/gocqlx/v2" ) // initializer specifies an value for a column in an insert operation. @@ -78,6 +80,11 @@ func (b *InsertBuilder) ToCql() (stmt string, names []string) { return } +// Query returns query built on top of current InsertBuilder state. +func (b *InsertBuilder) Query(session gocqlx.Session) *gocqlx.Queryx { + return session.Query(b.ToCql()) +} + // Into sets the INTO clause of the query. func (b *InsertBuilder) Into(table string) *InsertBuilder { b.table = table diff --git a/qb/select.go b/qb/select.go index 9e4a36c..12d4249 100644 --- a/qb/select.go +++ b/qb/select.go @@ -10,6 +10,8 @@ package qb import ( "bytes" "fmt" + + "github.com/scylladb/gocqlx/v2" ) // Order specifies sorting order. @@ -118,6 +120,11 @@ func (b *SelectBuilder) ToCql() (stmt string, names []string) { return } +// Query returns query built on top of current SelectBuilder state. +func (b *SelectBuilder) Query(session gocqlx.Session) *gocqlx.Queryx { + return session.Query(b.ToCql()) +} + // From sets the table to be selected from. func (b *SelectBuilder) From(table string) *SelectBuilder { b.table = table diff --git a/qb/update.go b/qb/update.go index 089e3ce..fa3f8f8 100644 --- a/qb/update.go +++ b/qb/update.go @@ -10,6 +10,8 @@ package qb import ( "bytes" "time" + + "github.com/scylladb/gocqlx/v2" ) // assignment specifies an assignment in a set operation. @@ -73,6 +75,11 @@ func (b *UpdateBuilder) ToCql() (stmt string, names []string) { return } +// Query returns query built on top of current UpdateBuilder state. +func (b *UpdateBuilder) Query(session gocqlx.Session) *gocqlx.Queryx { + return session.Query(b.ToCql()) +} + // Table sets the table to be updated. func (b *UpdateBuilder) Table(table string) *UpdateBuilder { b.table = table