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-03 18:39:16 +02:00
|
|
|
// Insert
|
2017-07-25 11:08:53 +02:00
|
|
|
{
|
2017-08-03 18:39:16 +02:00
|
|
|
stmt, names := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email").ToCql()
|
|
|
|
|
q := gocqlx.Query(session.Query(stmt), names)
|
2017-07-25 11:08:53 +02:00
|
|
|
|
2017-08-03 18:39:16 +02:00
|
|
|
if err := q.BindStruct(p).Exec(); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2017-07-25 11:08:53 +02:00
|
|
|
}
|
2017-07-24 16:22:30 +02:00
|
|
|
|
2017-08-03 18:39:16 +02:00
|
|
|
// Insert with TTL
|
2017-07-24 16:22:30 +02:00
|
|
|
{
|
2017-08-03 18:39:16 +02:00
|
|
|
stmt, names := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email").TTL().ToCql()
|
|
|
|
|
q := gocqlx.Query(session.Query(stmt), names)
|
2017-07-24 16:22:30 +02:00
|
|
|
|
2017-08-03 18:39:16 +02:00
|
|
|
if err := q.BindStructMap(p, qb.M{"_ttl": qb.TTL(86400 * time.Second)}).Exec(); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2017-07-24 16:22:30 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-03 18:39:16 +02:00
|
|
|
// Update
|
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-03 18:39:16 +02:00
|
|
|
stmt, names := qb.Update("gocqlx_test.person").Set("email").Where(qb.Eq("first_name"), qb.Eq("last_name")).ToCql()
|
|
|
|
|
q := gocqlx.Query(session.Query(stmt), names)
|
2017-07-24 16:22:30 +02:00
|
|
|
|
2017-08-03 18:39:16 +02:00
|
|
|
if err := q.BindStruct(p).Exec(); err != nil {
|
|
|
|
|
t.Fatal(err)
|
2017-07-24 16:22:30 +02:00
|
|
|
}
|
2017-07-28 10:18:38 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-03 18:39:16 +02:00
|
|
|
// Batch
|
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()
|
|
|
|
|
q := gocqlx.Query(session.Query(stmt), names)
|
2017-08-02 11:12:31 +02:00
|
|
|
|
2017-08-03 18:39:16 +02:00
|
|
|
b := struct {
|
|
|
|
|
A Person
|
|
|
|
|
B Person
|
|
|
|
|
}{
|
|
|
|
|
A: Person{
|
|
|
|
|
"Igy",
|
|
|
|
|
"Citizen",
|
|
|
|
|
[]string{"ian.citzen@gocqlx_test.com"},
|
|
|
|
|
},
|
|
|
|
|
B: Person{
|
|
|
|
|
"Ian",
|
|
|
|
|
"Citizen",
|
|
|
|
|
[]string{"igy.citzen@gocqlx_test.com"},
|
|
|
|
|
},
|
2017-07-28 10:18:38 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-03 18:39:16 +02:00
|
|
|
if err := q.BindStruct(&b).Exec(); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-02 11:12:31 +02:00
|
|
|
|
2017-08-03 18:39:16 +02:00
|
|
|
// Get
|
|
|
|
|
{
|
|
|
|
|
var p Person
|
|
|
|
|
if err := gocqlx.Get(&p, session.Query("SELECT * FROM gocqlx_test.person WHERE first_name=?", "Patricia")); err != nil {
|
|
|
|
|
t.Fatal("get:", err)
|
2017-08-01 13:29:52 +02:00
|
|
|
}
|
2017-08-03 18:39:16 +02:00
|
|
|
t.Log(p)
|
2017-08-01 13:29:52 +02:00
|
|
|
|
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-03 18:39:16 +02:00
|
|
|
// Select
|
|
|
|
|
{
|
|
|
|
|
stmt, names := qb.Select("gocqlx_test.person").Where(qb.In("first_name")).ToCql()
|
|
|
|
|
q := gocqlx.Query(session.Query(stmt), names)
|
2017-08-02 11:12:31 +02:00
|
|
|
|
2017-08-03 18:39:16 +02:00
|
|
|
q.BindMap(qb.M{"first_name": []string{"Patricia", "Igy", "Ian"}})
|
|
|
|
|
if err := q.Err(); err != nil {
|
|
|
|
|
t.Fatal(err)
|
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-03 18:39:16 +02:00
|
|
|
t.Log(people)
|
2017-08-03 17:06:03 +02:00
|
|
|
|
2017-08-03 18:39:16 +02:00
|
|
|
// [{Ian Citizen [igy.citzen@gocqlx_test.com]} {Igy Citizen [ian.citzen@gocqlx_test.com]} {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]}]
|
2017-07-24 16:22:30 +02:00
|
|
|
}
|
2017-07-25 12:25:59 +02:00
|
|
|
|
2017-07-28 10:18:38 +02:00
|
|
|
// Named queries, using `:name` as the bindvar.
|
2017-07-25 12:25:59 +02:00
|
|
|
{
|
2017-07-28 10:18:38 +02:00
|
|
|
// compile query to valid gocqlx query and list of named parameters
|
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
|
|
|
q := gocqlx.Query(session.Query(stmt), names)
|
|
|
|
|
|
|
|
|
|
// bind named parameters from a struct
|
|
|
|
|
{
|
|
|
|
|
p := &Person{
|
|
|
|
|
"Jane",
|
|
|
|
|
"Citizen",
|
|
|
|
|
[]string{"jane.citzen@gocqlx_test.com"},
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-02 11:12:31 +02:00
|
|
|
if err := q.BindStruct(p).Exec(); err != nil {
|
|
|
|
|
t.Fatal(err)
|
2017-07-28 10:18:38 +02:00
|
|
|
}
|
2017-07-25 12:25:59 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-28 10:18:38 +02:00
|
|
|
// bind named parameters from a map
|
|
|
|
|
{
|
|
|
|
|
m := map[string]interface{}{
|
|
|
|
|
"first_name": "Bin",
|
|
|
|
|
"last_name": "Smuth",
|
|
|
|
|
"email": []string{"bensmith@allblacks.nz"},
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-02 11:12:31 +02:00
|
|
|
if err := q.BindMap(m).Exec(); err != nil {
|
|
|
|
|
t.Fatal(err)
|
2017-07-28 10:18:38 +02:00
|
|
|
}
|
2017-07-25 12:25:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
2017-07-24 16:22:30 +02:00
|
|
|
}
|