Files
gocqlx/example_test.go

206 lines
4.8 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"
"github.com/gocql/gocql"
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)
)`
var placeSchema = `
2017-07-28 14:38:19 +02:00
CREATE TABLE IF NOT EXISTS gocqlx_test.place (
2017-07-24 16:22:30 +02:00
country text,
city text,
code int,
2017-07-24 16:22:30 +02:00
PRIMARY KEY(country, city)
)`
// 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
}
type Place struct {
Country string
City string
TelCode int `db:"code"`
2017-07-24 16:22:30 +02:00
}
func TestExample(t *testing.T) {
session := createSession(t)
defer session.Close()
mustExec := func(q *gocql.Query) {
if err := q.Exec(); err != nil {
2017-07-28 10:18:38 +02:00
t.Fatal("query:", q, err)
2017-07-24 16:22:30 +02:00
}
}
2017-07-25 11:08:53 +02:00
2017-07-28 10:18:38 +02:00
// Fill person table.
2017-07-25 11:08:53 +02:00
{
2017-07-28 10:18:38 +02:00
if err := createTable(session, personSchema); err != nil {
t.Fatal("create table:", err)
}
2017-07-25 11:08:53 +02:00
q := session.Query("INSERT INTO gocqlx_test.person (first_name, last_name, email) VALUES (?, ?, ?)")
mustExec(q.Bind("Jason", "Moiron", []string{"jmoiron@jmoiron.net"}))
mustExec(q.Bind("John", "Doe", []string{"johndoeDNE@gmail.net"}))
q.Release()
}
2017-07-28 10:18:38 +02:00
// Fill place table.
2017-07-25 11:08:53 +02:00
{
2017-07-28 10:18:38 +02:00
if err := createTable(session, placeSchema); err != nil {
t.Fatal("create table:", err)
}
2017-07-25 11:08:53 +02:00
q := session.Query("INSERT INTO gocqlx_test.place (country, city, code) VALUES (?, ?, ?)")
2017-07-25 11:08:53 +02:00
mustExec(q.Bind("United States", "New York", 1))
mustExec(q.Bind("Hong Kong", "", 852))
mustExec(q.Bind("Singapore", "", 65))
q.Release()
}
2017-07-24 16:22:30 +02:00
2017-07-28 10:18:38 +02:00
// Query the database, storing results in a []Person (wrapped in []interface{}).
2017-07-24 16:22:30 +02:00
{
2017-07-28 10:18:38 +02:00
var people []Person
2017-07-28 14:38:19 +02:00
if err := gocqlx.Select(&people, session.Query("SELECT * FROM gocqlx_test.person")); err != nil {
2017-07-24 16:22:30 +02:00
t.Fatal("select:", err)
}
2017-07-28 10:18:38 +02:00
t.Log(people)
2017-07-24 16:22:30 +02:00
2017-07-28 10:18:38 +02:00
// [{John Doe [johndoeDNE@gmail.net]} {Jason Moiron [jmoiron@jmoiron.net]}]
2017-07-24 16:22:30 +02:00
}
2017-07-28 10:18:38 +02:00
// Get a single result.
2017-07-24 16:22:30 +02:00
{
var jason Person
2017-07-28 14:38:19 +02:00
if err := gocqlx.Get(&jason, session.Query("SELECT * FROM gocqlx_test.person WHERE first_name=?", "Jason")); err != nil {
2017-07-24 16:22:30 +02:00
t.Fatal("get:", err)
}
2017-07-28 10:18:38 +02:00
t.Log(jason)
// Jason Moiron [jmoiron@jmoiron.net]}
2017-07-24 16:22:30 +02:00
}
2017-07-28 10:18:38 +02:00
// Loop through rows using only one struct.
2017-07-24 16:22:30 +02:00
{
var place Place
2017-07-28 14:38:19 +02:00
iter := gocqlx.Iter(session.Query("SELECT * FROM gocqlx_test.place"))
2017-07-24 16:22:30 +02:00
for iter.StructScan(&place) {
2017-07-28 10:18:38 +02:00
t.Log(place)
2017-07-24 16:22:30 +02:00
}
2017-07-25 09:13:47 +02:00
if err := iter.Close(); err != nil {
2017-07-24 16:22:30 +02:00
t.Fatal("iter:", err)
}
2017-07-25 09:13:47 +02:00
iter.ReleaseQuery()
2017-07-28 10:18:38 +02:00
// {Hong Kong 852}
// {United States New York 1}
// {Singapore 65}
}
// Query builder, using DSL to build queries, using `:name` as the bindvar.
{
// helper function for creating session queries
Query := gocqlx.SessionQuery(session)
p := &Person{
"Patricia",
"Citizen",
[]string{"patricia.citzen@gocqlx_test.com"},
}
// Insert
{
2017-07-28 14:38:19 +02:00
q := Query(qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email").ToCql())
2017-07-28 10:18:38 +02:00
if err := q.BindStruct(p); err != nil {
t.Fatal("bind:", err)
}
mustExec(q.Query)
}
// Update
{
p.Email = append(p.Email, "patricia1.citzen@gocqlx_test.com")
2017-07-28 14:38:19 +02:00
q := Query(qb.Update("gocqlx_test.person").Set("email").Where(qb.Eq("first_name"), qb.Eq("last_name")).ToCql())
2017-07-28 10:18:38 +02:00
if err := q.BindStruct(p); err != nil {
t.Fatal("bind:", err)
}
mustExec(q.Query)
}
// Select
{
2017-07-28 14:38:19 +02:00
q := Query(qb.Select("gocqlx_test.person").Where(qb.In("first_name")).ToCql())
2017-07-28 10:18:38 +02:00
m := map[string]interface{}{
"first_name": []string{"Patricia", "John"},
}
if err := q.BindMap(m); err != nil {
t.Fatal("bind:", err)
}
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]} {John Doe [johndoeDNE@gmail.net]}]
}
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"},
}
if err := q.BindStruct(p); err != nil {
t.Fatal("bind:", err)
}
mustExec(q.Query)
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"},
}
if err := q.BindMap(m); err != nil {
t.Fatal("bind:", err)
}
mustExec(q.Query)
2017-07-25 12:25:59 +02:00
}
}
2017-07-24 16:22:30 +02:00
}