Files
gocqlx/example_test.go

212 lines
4.9 KiB
Go
Raw Normal View History

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"
. "github.com/scylladb/gocqlx/gocqlxtest"
2017-07-28 10:18:38 +02:00
"github.com/scylladb/gocqlx/qb"
2017-07-24 16:22:30 +02:00
)
func TestExample(t *testing.T) {
session := CreateSession(t)
defer session.Close()
const 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)
)`
if err := session.ExecStmt(personSchema); err != nil {
2017-08-03 18:39:16 +02:00
t.Fatal("create table:", err)
2017-07-24 16:22:30 +02:00
}
2017-07-25 11:08:53 +02:00
// 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
}
p := Person{
2017-08-03 18:39:16 +02:00
"Patricia",
"Citizen",
[]string{"patricia.citzen@gocqlx_test.com"},
2017-07-25 11:08:53 +02:00
}
// Insert, bind data from struct.
2017-07-25 11:08:53 +02:00
{
stmt, names := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email").ToCql()
q := session.Query(stmt, names).BindStruct(p)
2017-07-25 11:08:53 +02:00
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
// Insert with TTL and timestamp, bind data from struct and 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").
2018-05-25 13:06:43 +02:00
TTL(86400 * time.Second).
Timestamp(time.Now()).
2017-08-24 11:54:41 +02:00
ToCql()
q := session.Query(stmt, names).BindStruct(p)
2017-07-24 16:22:30 +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
}
// Update email, bind data 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()
q := session.Query(stmt, names).BindStruct(p)
2017-07-24 16:22:30 +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-07-28 10:18:38 +02:00
}
// Add email to a list.
{
stmt, names := qb.Update("gocqlx_test.person").
AddNamed("email", "new_email").
Where(qb.Eq("first_name"), qb.Eq("last_name")).
ToCql()
q := 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)
}
}
// Batch insert two rows in a single query.
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-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
}
q := session.Query(stmt, names).BindStruct(&batch)
2017-07-28 10:18:38 +02:00
if err := q.ExecRelease(); err != nil {
2017-08-03 18:39:16 +02:00
t.Fatal(err)
}
}
// Get first result into a struct.
2017-08-03 18:39:16 +02:00
{
var p Person
stmt, names := qb.Select("gocqlx_test.person").Where(qb.Eq("first_name")).ToCql()
q := session.Query(stmt, names).BindMap(qb.M{
2017-08-30 11:02:55 +02:00
"first_name": "Patricia",
})
if err := q.GetRelease(&p); err != nil {
t.Fatal(err)
2017-08-01 13:29:52 +02:00
}
2017-08-24 11:54:41 +02:00
t.Log(p)
// stdout: {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
// Load all the results into a slice.
2017-08-03 18:39:16 +02:00
{
var people []Person
stmt, names := qb.Select("gocqlx_test.person").Where(qb.In("first_name")).ToCql()
q := session.Query(stmt, names).BindMap(qb.M{
2017-08-24 11:54:41 +02:00
"first_name": []string{"Patricia", "Igy", "Ian"},
})
if err := q.SelectRelease(&people); err != nil {
t.Fatal(err)
2017-08-03 17:06:03 +02:00
}
2017-08-24 11:54:41 +02:00
t.Log(people)
// stdout: [{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
}
// Support for token based pagination.
2017-08-30 11:02:55 +02:00
{
p := &Person{
"Ian",
"Citizen",
[]string{"ian.citzen@gocqlx_test.com"},
}
stmt, names := qb.Select("gocqlx_test.person").
Columns("first_name").
2017-08-30 11:02:55 +02:00
Where(qb.Token("first_name").Gt()).
Limit(10).
ToCql()
q := session.Query(stmt, names).BindStruct(p)
2017-08-30 11:02:55 +02:00
var people []Person
if err := q.SelectRelease(&people); err != nil {
t.Fatal(err)
2017-08-30 11:02:55 +02:00
}
t.Log(people)
// [{Patricia []} {Igy []}]
2017-07-24 16:22:30 +02:00
}
2017-07-25 12:25:59 +02:00
// Support for named parameters in query string.
2017-07-25 12:25:59 +02:00
{
const query = "INSERT INTO gocqlx_test.person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)"
stmt, names, err := gocqlx.CompileNamedQuery([]byte(query))
if err != nil {
t.Fatal(err)
}
2017-08-30 11:02:55 +02:00
p := &Person{
"Jane",
"Citizen",
[]string{"jane.citzen@gocqlx_test.com"},
}
q := session.Query(stmt, names).BindStruct(p)
if err := q.ExecRelease(); err != nil {
2017-08-30 11:02:55 +02:00
t.Fatal(err)
2017-07-25 12:25:59 +02:00
}
}
2017-07-24 16:22:30 +02:00
}