Files
gocqlx/example_test.go

202 lines
4.4 KiB
Go
Raw Normal View History

2017-07-24 16:22:30 +02:00
// +build all integration
2017-07-25 08:25:31 +02:00
package gocqlx_test
2017-07-24 16:22:30 +02:00
import (
"testing"
2017-08-01 13:29:52 +02:00
"time"
2017-07-24 16:22:30 +02:00
2017-07-24 16:59:39 +02:00
"github.com/scylladb/gocqlx"
2017-07-28 10:18:38 +02:00
"github.com/scylladb/gocqlx/qb"
2017-07-24 16:22:30 +02:00
)
var personSchema = `
2017-07-28 14:38:19 +02:00
CREATE TABLE IF NOT EXISTS gocqlx_test.person (
2017-07-24 16:22:30 +02:00
first_name text,
last_name text,
email list<text>,
PRIMARY KEY(first_name, last_name)
)`
// Field names are converted to camel case by default, no need to add
2017-07-28 10:18:38 +02:00
// `db:"first_name"`, if you want to disable a filed add `db:"-"` tag.
2017-07-24 16:22:30 +02:00
type Person struct {
FirstName string
LastName string
2017-07-24 16:22:30 +02:00
Email []string
}
func TestExample(t *testing.T) {
session := createSession(t)
defer session.Close()
2017-08-03 18:39:16 +02:00
if err := createTable(session, personSchema); err != nil {
t.Fatal("create table:", err)
2017-07-24 16:22:30 +02:00
}
2017-07-25 11:08:53 +02:00
2017-08-03 18:39:16 +02:00
p := &Person{
"Patricia",
"Citizen",
[]string{"patricia.citzen@gocqlx_test.com"},
2017-07-25 11:08:53 +02:00
}
2017-08-30 11:02:55 +02:00
// Insert with query parameters bound from struct.
2017-07-25 11:08:53 +02:00
{
2017-08-24 11:54:41 +02:00
stmt, names := qb.Insert("gocqlx_test.person").
Columns("first_name", "last_name", "email").
ToCql()
2017-07-25 11:08:53 +02:00
2017-08-30 11:02:55 +02:00
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
if err := q.ExecRelease(); err != nil {
2017-08-03 18:39:16 +02:00
t.Fatal(err)
}
2017-07-25 11:08:53 +02:00
}
2017-07-24 16:22:30 +02:00
2017-08-30 11:02:55 +02:00
// Insert with query parameters bound from struct extended with a map.
2017-07-24 16:22:30 +02:00
{
2017-08-24 11:54:41 +02:00
stmt, names := qb.Insert("gocqlx_test.person").
Columns("first_name", "last_name", "email").
TTL().
ToCql()
2017-07-24 16:22:30 +02:00
2017-08-30 11:02:55 +02:00
q := gocqlx.Query(session.Query(stmt), names).BindStructMap(p, qb.M{
2017-08-24 11:54:41 +02:00
"_ttl": qb.TTL(86400 * time.Second),
2017-08-30 11:02:55 +02:00
})
2017-08-24 11:54:41 +02:00
2017-08-30 11:02:55 +02:00
if err := q.ExecRelease(); err != nil {
2017-08-03 18:39:16 +02:00
t.Fatal(err)
}
2017-07-24 16:22:30 +02:00
}
2017-08-30 11:02:55 +02:00
// Easy update with all parameters bound from struct.
2017-07-24 16:22:30 +02:00
{
2017-08-03 18:39:16 +02:00
p.Email = append(p.Email, "patricia1.citzen@gocqlx_test.com")
2017-07-28 10:18:38 +02:00
2017-08-24 11:54:41 +02:00
stmt, names := qb.Update("gocqlx_test.person").
Set("email").
Where(qb.Eq("first_name"), qb.Eq("last_name")).
ToCql()
2017-07-24 16:22:30 +02:00
2017-08-30 11:02:55 +02:00
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
if err := q.ExecRelease(); err != nil {
2017-08-03 18:39:16 +02:00
t.Fatal(err)
2017-07-24 16:22:30 +02:00
}
2017-07-28 10:18:38 +02:00
}
2017-08-30 11:02:55 +02:00
// Batch insert two rows in a single query, advanced struct binding.
2017-07-28 10:18:38 +02:00
{
2017-08-03 18:39:16 +02:00
i := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email")
2017-07-28 10:18:38 +02:00
2017-08-03 18:39:16 +02:00
stmt, names := qb.Batch().
Add("a.", i).
Add("b.", i).
ToCql()
2017-08-30 11:02:55 +02:00
batch := struct {
2017-08-03 18:39:16 +02:00
A Person
B Person
}{
A: Person{
"Igy",
"Citizen",
2017-08-30 11:02:55 +02:00
[]string{"igy.citzen@gocqlx_test.com"},
2017-08-03 18:39:16 +02:00
},
B: Person{
"Ian",
"Citizen",
2017-08-30 11:02:55 +02:00
[]string{"ian.citzen@gocqlx_test.com"},
2017-08-03 18:39:16 +02:00
},
2017-07-28 10:18:38 +02:00
}
2017-08-30 11:02:55 +02:00
q := gocqlx.Query(session.Query(stmt), names).BindStruct(&batch)
if err := q.ExecRelease(); err != nil {
2017-08-03 18:39:16 +02:00
t.Fatal(err)
}
}
2017-08-30 11:02:55 +02:00
// Get first result into a struct.
2017-08-03 18:39:16 +02:00
{
2017-08-30 11:02:55 +02:00
stmt, names := qb.Select("gocqlx_test.person").
Where(qb.Eq("first_name")).
ToCql()
q := gocqlx.Query(session.Query(stmt), names).BindMap(qb.M{
"first_name": "Patricia",
})
2017-08-24 11:54:41 +02:00
2017-08-03 18:39:16 +02:00
var p Person
2017-08-30 11:02:55 +02:00
if err := gocqlx.Get(&p, q.Query); err != nil {
2017-08-03 18:39:16 +02:00
t.Fatal("get:", err)
2017-08-01 13:29:52 +02:00
}
2017-08-24 11:54:41 +02:00
t.Log(p)
2017-08-03 18:39:16 +02:00
// {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]}
}
2017-07-28 10:18:38 +02:00
2017-08-30 11:02:55 +02:00
// Select, load all results into a slice.
2017-08-03 18:39:16 +02:00
{
2017-08-24 11:54:41 +02:00
stmt, names := qb.Select("gocqlx_test.person").
Where(qb.In("first_name")).
ToCql()
2017-08-24 11:54:41 +02:00
q := gocqlx.Query(session.Query(stmt), names).BindMap(qb.M{
"first_name": []string{"Patricia", "Igy", "Ian"},
})
2017-07-28 10:18:38 +02:00
2017-08-03 18:39:16 +02:00
var people []Person
if err := gocqlx.Select(&people, q.Query); err != nil {
t.Fatal("select:", err)
2017-08-03 17:06:03 +02:00
}
2017-08-24 11:54:41 +02:00
t.Log(people)
2017-08-30 11:02:55 +02:00
// [{Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]} {Igy Citizen [igy.citzen@gocqlx_test.com]} {Ian Citizen [ian.citzen@gocqlx_test.com]}]
}
// Easy token based pagination.
{
p := &Person{
"Ian",
"Citizen",
[]string{"ian.citzen@gocqlx_test.com"},
}
stmt, names := qb.Select("gocqlx_test.person").
Where(qb.Token("first_name").Gt()).
Limit(10).
ToCql()
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
var people []Person
if err := gocqlx.Select(&people, q.Query); err != nil {
t.Fatal("select:", err)
}
t.Log(people)
// [{Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]} {Igy Citizen [igy.citzen@gocqlx_test.com]}]
2017-07-24 16:22:30 +02:00
}
2017-07-25 12:25:59 +02:00
2017-08-30 11:02:55 +02:00
// Named query compilation.
2017-07-25 12:25:59 +02:00
{
2017-08-30 11:02:55 +02:00
p := &Person{
"Jane",
"Citizen",
[]string{"jane.citzen@gocqlx_test.com"},
}
2017-07-28 14:38:19 +02:00
stmt, names, err := gocqlx.CompileNamedQuery([]byte("INSERT INTO gocqlx_test.person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)"))
2017-07-25 12:25:59 +02:00
if err != nil {
t.Fatal("compile:", err)
}
2017-07-28 10:18:38 +02:00
2017-08-30 11:02:55 +02:00
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
2017-07-25 12:25:59 +02:00
2017-08-30 11:02:55 +02:00
if err := q.ExecRelease(); err != nil {
t.Fatal(err)
2017-07-25 12:25:59 +02:00
}
}
2017-07-24 16:22:30 +02:00
}