Query Builder (qb)

The qb package provides a fluent API for generating CQL statements. It creates the CQL string and the list of parameter names required by gocqlx.

Select

import "github.com/scylladb/gocqlx/v3/qb"

// SELECT id, name FROM users WHERE id = ? ALLOW FILTERING
stmt, names := qb.Select("users").
    Columns("id", "name").
    Where(qb.Eq("id")).
    AllowFiltering().
    ToCql()

Insert

// INSERT INTO users (id, name, created_at) VALUES (?, ?, ?) USING TTL ?
stmt, names := qb.Insert("users").
    Columns("id", "name", "created_at").
    TTLNamed("ttl").
    ToCql()

Update

// UPDATE users SET name = ? WHERE id = ?
stmt, names := qb.Update("users").
    Set("name").
    Where(qb.Eq("id")).
    ToCql()

Delete

// DELETE FROM users WHERE id = ?
stmt, names := qb.Delete("users").
    Where(qb.Eq("id")).
    ToCql()

Comparators (Cmp)

The Where and If clauses accept Comparators:

  • qb.Eq("col") -> col = ?
  • qb.Lt("col") -> col < ?
  • qb.Gt("col") -> col > ?
  • qb.In("col") -> col IN ?
  • qb.Contains("col") -> col CONTAINS ?
  • qb.EqLit("col", "val") -> col = val (Literal, no parameter binding)

Functions

You can use CQL functions:

// ... WHERE id = maxTimeuuid(?)
qb.Select("events").Where(qb.EqFunc("id", qb.MaxTimeuuid("t")))