table: introduced table package

It adds support for super simple CRUD operations based on table schema model.
This commit is contained in:
Michał Matczuk
2018-10-26 17:01:00 +02:00
committed by Michal Matczuk
parent a503f75a9c
commit 8083fa27ee
6 changed files with 513 additions and 93 deletions

7
table/doc.go Normal file
View File

@@ -0,0 +1,7 @@
// 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 adds support for super simple CRUD operations based on table
// model.
package table

100
table/example_test.go Normal file
View File

@@ -0,0 +1,100 @@
// Copyright (C) 2017 ScyllaDB
// Use of this source code is governed by a ALv2-style
// license that can be found in the LICENSE file.
// +build all integration
package table_test
import (
"testing"
"github.com/scylladb/gocqlx"
. "github.com/scylladb/gocqlx/gocqlxtest"
"github.com/scylladb/gocqlx/qb"
"github.com/scylladb/gocqlx/table"
)
func TestExample(t *testing.T) {
session := CreateSession(t)
defer session.Close()
const personSchema = `
CREATE TABLE IF NOT EXISTS gocqlx_test.person (
first_name text,
last_name text,
email list<text>,
PRIMARY KEY(first_name, last_name)
)`
if err := ExecStmt(session, personSchema); err != nil {
t.Fatal("create table:", err)
}
// metadata specifies table name and columns it must be in sync with schema.
var personMetadata = table.Metadata{
Name: "person",
Columns: []string{"first_name", "last_name", "email"},
PartKey: []string{"first_name"},
SortKey: []string{"last_name"},
}
// personTable allows for simple CRUD operations based on personMetadata.
var personTable = table.New(personMetadata)
// Person represents a row in person table.
// Field names are converted to camel case by default, no need to add special tags.
// If you want to disable a field add `db:"-"` tag, it will not be persisted.
type Person struct {
FirstName string
LastName string
Email []string
}
// Insert, bind data from struct.
{
p := Person{
"Patricia",
"Citizen",
[]string{"patricia.citzen@gocqlx_test.com"},
}
stmt, names := personTable.Insert()
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
if err := q.ExecRelease(); err != nil {
t.Fatal(err)
}
}
// Get by primary key.
{
p := Person{
"Patricia",
"Citizen",
nil, // no email
}
stmt, names := personTable.Get() // you can filter columns too
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
if err := q.GetRelease(&p); err != nil {
t.Fatal(err)
}
t.Log(p)
// stdout: {Patricia Citizen [patricia.citzen@gocqlx_test.com]}
}
// Load all rows in a partition to a slice.
{
var people []Person
stmt, names := personTable.Select() // you can filter columns too
q := gocqlx.Query(session.Query(stmt), names).BindMap(qb.M{"first_name": "Patricia"})
if err := q.SelectRelease(&people); err != nil {
t.Fatal(err)
}
t.Log(people)
// stdout: [{Patricia Citizen [patricia.citzen@gocqlx_test.com]}]
}
}

108
table/table.go Normal file
View File

@@ -0,0 +1,108 @@
// 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
import "github.com/scylladb/gocqlx/qb"
// 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.
func New(m Metadata) *Table {
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))
}
t.partKeyCmp = t.primaryKeyCmp[:len(t.metadata.PartKey)]
// 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
}
// 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()
}
// 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
}
// Update returns update by primary key statement.
func (t *Table) Update(columns ...string) (stmt string, names []string) {
return qb.Update(t.metadata.Name).Set(columns...).Where(t.primaryKeyCmp...).ToCql()
}
// Delete returns delete by primary key statement.
func (t *Table) Delete(columns ...string) (stmt string, names []string) {
return qb.Delete(t.metadata.Name).Columns(columns...).Where(t.primaryKeyCmp...).ToCql()
}

221
table/table_test.go Normal file
View File

@@ -0,0 +1,221 @@
// 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
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestTableGet(t *testing.T) {
table := []struct {
M Metadata
C []string
N []string
S string
}{
{
M: Metadata{
Name: "table",
Columns: []string{"a", "b", "c", "d"},
PartKey: []string{"a"},
SortKey: []string{"b"},
},
N: []string{"a", "b"},
S: "SELECT * FROM table WHERE a=? AND b=? ",
},
{
M: Metadata{
Name: "table",
Columns: []string{"a", "b", "c", "d"},
PartKey: []string{"a"},
},
N: []string{"a"},
S: "SELECT * FROM table WHERE a=? ",
},
{
M: Metadata{
Name: "table",
Columns: []string{"a", "b", "c", "d"},
PartKey: []string{"a"},
},
C: []string{"d"},
N: []string{"a"},
S: "SELECT d FROM table WHERE a=? ",
},
}
for _, test := range table {
stmt, names := New(test.M).Get(test.C...)
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff, names)
}
}
}
func TestTableSelect(t *testing.T) {
table := []struct {
M Metadata
C []string
N []string
S string
}{
{
M: Metadata{
Name: "table",
Columns: []string{"a", "b", "c", "d"},
PartKey: []string{"a"},
SortKey: []string{"b"},
},
N: []string{"a"},
S: "SELECT * FROM table WHERE a=? ",
},
{
M: Metadata{
Name: "table",
Columns: []string{"a", "b", "c", "d"},
PartKey: []string{"a"},
SortKey: []string{"b"},
},
C: []string{"d"},
N: []string{"a"},
S: "SELECT d FROM table WHERE a=? ",
},
}
for _, test := range table {
stmt, names := New(test.M).Select(test.C...)
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff, names)
}
}
// run SelectBuilder on the same data set
for _, test := range table {
stmt, names := New(test.M).SelectBuilder(test.C...).ToCql()
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff, names)
}
}
}
func TestTableInsert(t *testing.T) {
table := []struct {
M Metadata
N []string
S string
}{
{
M: Metadata{
Name: "table",
Columns: []string{"a", "b", "c", "d"},
PartKey: []string{"a"},
SortKey: []string{"b"},
},
N: []string{"a", "b", "c", "d"},
S: "INSERT INTO table (a,b,c,d) VALUES (?,?,?,?) ",
},
}
for _, test := range table {
stmt, names := New(test.M).Insert()
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff, names)
}
}
}
func TestTableUpdate(t *testing.T) {
table := []struct {
M Metadata
C []string
N []string
S string
}{
{
M: Metadata{
Name: "table",
Columns: []string{"a", "b", "c", "d"},
PartKey: []string{"a"},
SortKey: []string{"b"},
},
C: []string{"d"},
N: []string{"d", "a", "b"},
S: "UPDATE table SET d=? WHERE a=? AND b=? ",
},
}
for _, test := range table {
stmt, names := New(test.M).Update(test.C...)
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff, names)
}
}
}
func TestTableDelete(t *testing.T) {
table := []struct {
M Metadata
C []string
N []string
S string
}{
{
M: Metadata{
Name: "table",
Columns: []string{"a", "b", "c", "d"},
PartKey: []string{"a"},
SortKey: []string{"b"},
},
N: []string{"a", "b"},
S: "DELETE FROM table WHERE a=? AND b=? ",
},
{
M: Metadata{
Name: "table",
Columns: []string{"a", "b", "c", "d"},
PartKey: []string{"a"},
},
N: []string{"a"},
S: "DELETE FROM table WHERE a=? ",
},
{
M: Metadata{
Name: "table",
Columns: []string{"a", "b", "c", "d"},
PartKey: []string{"a"},
},
C: []string{"d"},
N: []string{"a"},
S: "DELETE d FROM table WHERE a=? ",
},
}
for _, test := range table {
stmt, names := New(test.M).Delete(test.C...)
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff, names)
}
}
}