Skip to content

Instantly share code, notes, and snippets.

@dahankzter
Created May 1, 2018 13:28
Show Gist options
  • Save dahankzter/a7197e4e661f77a681732d520bb878d0 to your computer and use it in GitHub Desktop.
Save dahankzter/a7197e4e661f77a681732d520bb878d0 to your computer and use it in GitHub Desktop.
import (
"testing"
"github.com/gocql/gocql"
"github.com/scylladb/gocqlx"
"github.com/scylladb/gocqlx/qb"
)
const (
createTypeStmt = `CREATE TYPE IF NOT EXISTS gocqlx_test.segment (
name text
);`
createTableStmt = `CREATE TABLE IF NOT EXISTS gocqlx_test.segments (
id uuid PRIMARY KEY,
segments frozen<list<frozen <segment>>>
);`
)
func TestComplex(t *testing.T) {
session := createSession(t)
if err := createTable(session, createTypeStmt); err != nil {
t.Fatal(err)
}
if err := createTable(session, createTableStmt); err != nil {
t.Fatal(err)
}
id := gocql.TimeUUID()
h := Holder{
ID: id,
Segments: []Segment{
{
Name: "name 1",
},
{
Name: "name 2",
},
},
}
stmt, names := qb.Insert("gocqlx_test.segments").Columns("id", "segments").ToCql()
q := gocqlx.Query(session.Query(stmt), names).BindStruct(h)
if err := q.ExecRelease(); err != nil {
t.Fatal(err)
}
stmt, names = qb.Select("gocqlx_test.segments").Where(qb.Eq("id")).ToCql()
q = gocqlx.Query(session.Query(stmt), names).BindMap(qb.M{
"id": id,
})
var hRes Holder
if err := gocqlx.Get(&hRes, q.Query); err != nil {
t.Fatal("get:", err)
}
t.Log(hRes)
}
type Holder struct {
ID gocql.UUID
Segments []Segment
}
type Segment struct {
Name string `cql:"name"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment