2018-10-26 17:01:00 +02:00
|
|
|
// Copyright (C) 2017 ScyllaDB
|
|
|
|
|
// Use of this source code is governed by a ALv2-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package table
|
|
|
|
|
|
2020-06-17 15:50:34 +02:00
|
|
|
import (
|
|
|
|
|
"github.com/scylladb/gocqlx/v2"
|
|
|
|
|
"github.com/scylladb/gocqlx/v2/qb"
|
|
|
|
|
)
|
2018-10-26 17:01:00 +02:00
|
|
|
|
|
|
|
|
// Metadata represents table schema.
|
|
|
|
|
type Metadata struct {
|
|
|
|
|
Name string
|
|
|
|
|
Columns []string
|
|
|
|
|
PartKey []string
|
|
|
|
|
SortKey []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type cql struct {
|
|
|
|
|
stmt string
|
|
|
|
|
names []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Table allows for simple CRUD operations, it's backed by query builders from
|
|
|
|
|
// gocqlx/qb package.
|
|
|
|
|
type Table struct {
|
|
|
|
|
metadata Metadata
|
|
|
|
|
primaryKeyCmp []qb.Cmp
|
|
|
|
|
partKeyCmp []qb.Cmp
|
|
|
|
|
|
|
|
|
|
get cql
|
|
|
|
|
sel cql
|
|
|
|
|
insert cql
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New creates new Table based on table schema read from Metadata.
|
2019-11-08 00:03:26 +01:00
|
|
|
func New(m Metadata) *Table { // nolint: gocritic
|
2018-10-26 17:01:00 +02:00
|
|
|
t := &Table{
|
|
|
|
|
metadata: m,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// prepare primary and partition key comparators
|
|
|
|
|
t.primaryKeyCmp = make([]qb.Cmp, 0, len(m.PartKey)+len(m.SortKey))
|
|
|
|
|
for _, k := range m.PartKey {
|
|
|
|
|
t.primaryKeyCmp = append(t.primaryKeyCmp, qb.Eq(k))
|
|
|
|
|
}
|
|
|
|
|
for _, k := range m.SortKey {
|
|
|
|
|
t.primaryKeyCmp = append(t.primaryKeyCmp, qb.Eq(k))
|
|
|
|
|
}
|
2020-02-25 12:35:44 +01:00
|
|
|
t.partKeyCmp = make([]qb.Cmp, len(m.PartKey))
|
|
|
|
|
copy(t.partKeyCmp, t.primaryKeyCmp[:len(t.metadata.PartKey)])
|
2018-10-26 17:01:00 +02:00
|
|
|
|
|
|
|
|
// prepare get stmt
|
|
|
|
|
t.get.stmt, t.get.names = qb.Select(m.Name).Where(t.primaryKeyCmp...).ToCql()
|
|
|
|
|
// prepare select stmt
|
|
|
|
|
t.sel.stmt, t.sel.names = qb.Select(m.Name).Where(t.partKeyCmp...).ToCql()
|
|
|
|
|
// prepare insert stmt
|
|
|
|
|
t.insert.stmt, t.insert.names = qb.Insert(m.Name).Columns(m.Columns...).ToCql()
|
|
|
|
|
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 15:35:33 +01:00
|
|
|
// Metadata returns copy of table metadata.
|
|
|
|
|
func (t *Table) Metadata() Metadata {
|
|
|
|
|
return t.metadata
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 10:12:56 +08:00
|
|
|
// PrimaryKeyCmp returns copy of table's primaryKeyCmp.
|
|
|
|
|
func (t *Table) PrimaryKeyCmp() []qb.Cmp {
|
|
|
|
|
primaryKeyCmp := make([]qb.Cmp, len(t.primaryKeyCmp))
|
|
|
|
|
copy(primaryKeyCmp, t.primaryKeyCmp)
|
|
|
|
|
return primaryKeyCmp
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 17:01:00 +02:00
|
|
|
// Name returns table name.
|
|
|
|
|
func (t *Table) Name() string {
|
|
|
|
|
return t.metadata.Name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get returns select by primary key statement.
|
|
|
|
|
func (t *Table) Get(columns ...string) (stmt string, names []string) {
|
|
|
|
|
if len(columns) == 0 {
|
|
|
|
|
return t.get.stmt, t.get.names
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return qb.Select(t.metadata.Name).
|
|
|
|
|
Columns(columns...).
|
|
|
|
|
Where(t.primaryKeyCmp...).
|
|
|
|
|
ToCql()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Select returns select by partition key statement.
|
|
|
|
|
func (t *Table) Select(columns ...string) (stmt string, names []string) {
|
|
|
|
|
if len(columns) == 0 {
|
|
|
|
|
return t.sel.stmt, t.sel.names
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return qb.Select(t.metadata.Name).
|
|
|
|
|
Columns(columns...).
|
|
|
|
|
Where(t.primaryKeyCmp[0:len(t.metadata.PartKey)]...).
|
|
|
|
|
ToCql()
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 15:50:34 +02:00
|
|
|
// 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...))
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 17:01:00 +02:00
|
|
|
// SelectBuilder returns a builder initialised to select by partition key
|
|
|
|
|
// statement.
|
|
|
|
|
func (t *Table) SelectBuilder(columns ...string) *qb.SelectBuilder {
|
|
|
|
|
return qb.Select(t.metadata.Name).Columns(columns...).Where(t.partKeyCmp...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert returns insert all columns statement.
|
|
|
|
|
func (t *Table) Insert() (stmt string, names []string) {
|
|
|
|
|
return t.insert.stmt, t.insert.names
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 15:50:34 +02:00
|
|
|
// InsertQuery returns query which inserts all columns.
|
|
|
|
|
func (t *Table) InsertQuery(session gocqlx.Session) *gocqlx.Queryx {
|
|
|
|
|
return session.Query(t.Insert())
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 17:01:00 +02:00
|
|
|
// Update returns update by primary key statement.
|
|
|
|
|
func (t *Table) Update(columns ...string) (stmt string, names []string) {
|
2020-03-13 10:56:00 +08:00
|
|
|
return t.UpdateBuilder(columns...).ToCql()
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 15:50:34 +02:00
|
|
|
// 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...))
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 10:56:00 +08:00
|
|
|
// UpdateBuilder returns a builder initialised to update by primary key statement.
|
|
|
|
|
func (t *Table) UpdateBuilder(columns ...string) *qb.UpdateBuilder {
|
|
|
|
|
return qb.Update(t.metadata.Name).Set(columns...).Where(t.primaryKeyCmp...)
|
2018-10-26 17:01:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete returns delete by primary key statement.
|
|
|
|
|
func (t *Table) Delete(columns ...string) (stmt string, names []string) {
|
2020-03-13 10:56:00 +08:00
|
|
|
return t.DeleteBuilder(columns...).ToCql()
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 15:50:34 +02:00
|
|
|
// 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...))
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 10:56:00 +08:00
|
|
|
// DeleteBuilder returns a builder initialised to delete by primary key statement.
|
|
|
|
|
func (t *Table) DeleteBuilder(columns ...string) *qb.DeleteBuilder {
|
|
|
|
|
return qb.Delete(t.metadata.Name).Columns(columns...).Where(t.primaryKeyCmp...)
|
2018-10-26 17:01:00 +02:00
|
|
|
}
|