table: Added shortcuts to Queryx
These shortucuts allows to write shorter more straightforward code. It allows to write: ``` clusters.InsertQuery(session).BindStruct(r).ExecRelease() ``` instead of: ``` tmt, names := clusters.Insert() session.Query(stmt,names).BindStruct(r).ExecRelease() ```
This commit is contained in:
committed by
Michal Jan Matczuk
parent
009b5ec4b5
commit
564db08698
@@ -4,7 +4,10 @@
|
|||||||
|
|
||||||
package table
|
package table
|
||||||
|
|
||||||
import "github.com/scylladb/gocqlx/v2/qb"
|
import (
|
||||||
|
"github.com/scylladb/gocqlx/v2"
|
||||||
|
"github.com/scylladb/gocqlx/v2/qb"
|
||||||
|
)
|
||||||
|
|
||||||
// Metadata represents table schema.
|
// Metadata represents table schema.
|
||||||
type Metadata struct {
|
type Metadata struct {
|
||||||
@@ -99,6 +102,11 @@ func (t *Table) Select(columns ...string) (stmt string, names []string) {
|
|||||||
ToCql()
|
ToCql()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SelectQuery returns query which selects by partition key statement.
|
||||||
|
func (t *Table) SelectQuery(session gocqlx.Session, columns ...string) *gocqlx.Queryx {
|
||||||
|
return session.Query(t.Select(columns...))
|
||||||
|
}
|
||||||
|
|
||||||
// SelectBuilder returns a builder initialised to select by partition key
|
// SelectBuilder returns a builder initialised to select by partition key
|
||||||
// statement.
|
// statement.
|
||||||
func (t *Table) SelectBuilder(columns ...string) *qb.SelectBuilder {
|
func (t *Table) SelectBuilder(columns ...string) *qb.SelectBuilder {
|
||||||
@@ -110,11 +118,21 @@ func (t *Table) Insert() (stmt string, names []string) {
|
|||||||
return t.insert.stmt, t.insert.names
|
return t.insert.stmt, t.insert.names
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InsertQuery returns query which inserts all columns.
|
||||||
|
func (t *Table) InsertQuery(session gocqlx.Session) *gocqlx.Queryx {
|
||||||
|
return session.Query(t.Insert())
|
||||||
|
}
|
||||||
|
|
||||||
// Update returns update by primary key statement.
|
// Update returns update by primary key statement.
|
||||||
func (t *Table) Update(columns ...string) (stmt string, names []string) {
|
func (t *Table) Update(columns ...string) (stmt string, names []string) {
|
||||||
return t.UpdateBuilder(columns...).ToCql()
|
return t.UpdateBuilder(columns...).ToCql()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateQuery returns query which updates by primary key.
|
||||||
|
func (t *Table) UpdateQuery(session gocqlx.Session, columns ...string) *gocqlx.Queryx {
|
||||||
|
return session.Query(t.Update(columns...))
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateBuilder returns a builder initialised to update by primary key statement.
|
// UpdateBuilder returns a builder initialised to update by primary key statement.
|
||||||
func (t *Table) UpdateBuilder(columns ...string) *qb.UpdateBuilder {
|
func (t *Table) UpdateBuilder(columns ...string) *qb.UpdateBuilder {
|
||||||
return qb.Update(t.metadata.Name).Set(columns...).Where(t.primaryKeyCmp...)
|
return qb.Update(t.metadata.Name).Set(columns...).Where(t.primaryKeyCmp...)
|
||||||
@@ -125,6 +143,11 @@ func (t *Table) Delete(columns ...string) (stmt string, names []string) {
|
|||||||
return t.DeleteBuilder(columns...).ToCql()
|
return t.DeleteBuilder(columns...).ToCql()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteQuery returns query which delete by primary key.
|
||||||
|
func (t *Table) DeleteQuery(session gocqlx.Session, columns ...string) *gocqlx.Queryx {
|
||||||
|
return session.Query(t.Delete(columns...))
|
||||||
|
}
|
||||||
|
|
||||||
// DeleteBuilder returns a builder initialised to delete by primary key statement.
|
// DeleteBuilder returns a builder initialised to delete by primary key statement.
|
||||||
func (t *Table) DeleteBuilder(columns ...string) *qb.DeleteBuilder {
|
func (t *Table) DeleteBuilder(columns ...string) *qb.DeleteBuilder {
|
||||||
return qb.Delete(t.metadata.Name).Columns(columns...).Where(t.primaryKeyCmp...)
|
return qb.Delete(t.metadata.Name).Columns(columns...).Where(t.primaryKeyCmp...)
|
||||||
|
|||||||
Reference in New Issue
Block a user