2017-09-21 21:43:27 +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.
|
|
|
|
|
|
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)
|
|
|
|
|
)`
|
|
|
|
|
|
2017-07-26 10:46:06 +02:00
|
|
|
// 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 {
|
2017-07-26 10:46:06 +02:00
|
|
|
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-09-22 09:33:46 +02:00
|
|
|
// Advanced update, adding and removing elements to collections and counters.
|
|
|
|
|
{
|
|
|
|
|
stmt, names := qb.Update("gocqlx_test.person").
|
|
|
|
|
AddNamed("email", "new_email").
|
|
|
|
|
Where(qb.Eq("first_name"), qb.Eq("last_name")).
|
|
|
|
|
ToCql()
|
|
|
|
|
|
|
|
|
|
q := gocqlx.Query(session.Query(stmt), names).BindStructMap(p, qb.M{
|
|
|
|
|
"new_email": []string{"patricia2.citzen@gocqlx_test.com", "patricia3.citzen@gocqlx_test.com"},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err := q.ExecRelease(); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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().
|
2017-08-30 16:26:30 +02:00
|
|
|
AddWithPrefix("a", i).
|
|
|
|
|
AddWithPrefix("b", i).
|
2017-08-03 18:39:16 +02:00
|
|
|
ToCql()
|
2017-08-02 11:12:31 +02:00
|
|
|
|
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-02 11:12:31 +02:00
|
|
|
|
2017-08-31 16:41:31 +02:00
|
|
|
// Get the 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-11-11 09:27:15 -05:00
|
|
|
defer q.Release()
|
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-09-22 09:33:46 +02:00
|
|
|
// {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com patricia2.citzen@gocqlx_test.com patricia3.citzen@gocqlx_test.com]}
|
2017-08-03 18:39:16 +02:00
|
|
|
}
|
2017-07-28 10:18:38 +02:00
|
|
|
|
2017-08-31 16:41:31 +02:00
|
|
|
// Select, load all the 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-02 11:12:31 +02:00
|
|
|
|
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-11-11 09:27:15 -05:00
|
|
|
defer q.Release()
|
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-09-22 09:33:46 +02:00
|
|
|
// [{Ian Citizen [ian.citzen@gocqlx_test.com]} {Igy Citizen [igy.citzen@gocqlx_test.com]} {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com patricia2.citzen@gocqlx_test.com patricia3.citzen@gocqlx_test.com]}]
|
2017-08-30 11:02:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Easy token based pagination.
|
|
|
|
|
{
|
|
|
|
|
p := &Person{
|
|
|
|
|
"Ian",
|
|
|
|
|
"Citizen",
|
|
|
|
|
[]string{"ian.citzen@gocqlx_test.com"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt, names := qb.Select("gocqlx_test.person").
|
2017-09-22 09:33:46 +02:00
|
|
|
Columns("first_name").
|
2017-08-30 11:02:55 +02:00
|
|
|
Where(qb.Token("first_name").Gt()).
|
|
|
|
|
Limit(10).
|
|
|
|
|
ToCql()
|
|
|
|
|
|
|
|
|
|
q := gocqlx.Query(session.Query(stmt), names).BindStruct(p)
|
2017-11-11 09:27:15 -05:00
|
|
|
defer q.Release()
|
2017-08-30 11:02:55 +02:00
|
|
|
|
|
|
|
|
var people []Person
|
|
|
|
|
if err := gocqlx.Select(&people, q.Query); err != nil {
|
|
|
|
|
t.Fatal("select:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Log(people)
|
2017-09-22 09:33:46 +02:00
|
|
|
// [{Patricia []} {Igy []}]
|
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
|
|
|
}
|