Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.