Files
gocqlx/qb/batch.go

126 lines
3.0 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-08-03 17:06:03 +02:00
package qb
import (
"bytes"
"fmt"
2018-05-25 13:06:43 +02:00
"time"
2017-08-03 17:06:03 +02:00
)
// BATCH reference:
// https://cassandra.apache.org/doc/latest/cql/dml.html#batch
// BatchBuilder builds CQL BATCH statements.
type BatchBuilder struct {
unlogged bool
counter bool
using using
stmts []string
names []string
}
// Batch returns a new BatchBuilder.
func Batch() *BatchBuilder {
return &BatchBuilder{}
}
// ToCql builds the query into a CQL string and named args.
func (b *BatchBuilder) ToCql() (stmt string, names []string) {
cql := bytes.Buffer{}
cql.WriteString("BEGIN ")
if b.unlogged {
cql.WriteString("UNLOGGED ")
}
if b.counter {
cql.WriteString("COUNTER ")
}
cql.WriteString("BATCH ")
names = append(names, b.using.writeCql(&cql)...)
for _, stmt := range b.stmts {
cql.WriteString(stmt)
cql.WriteByte(';')
cql.WriteByte(' ')
}
names = append(names, b.names...)
cql.WriteString("APPLY BATCH ")
stmt = cql.String()
return
}
// Add builds the builder and adds the statement to the batch.
2017-08-30 16:26:30 +02:00
func (b *BatchBuilder) Add(builder Builder) *BatchBuilder {
return b.AddStmt(builder.ToCql())
}
2017-08-30 16:26:30 +02:00
// AddStmt adds statement to the batch.
func (b *BatchBuilder) AddStmt(stmt string, names []string) *BatchBuilder {
2017-08-30 16:26:30 +02:00
b.stmts = append(b.stmts, stmt)
b.names = append(b.names, names...)
return b
}
// AddWithPrefix builds the builder and adds the statement to the batch. Names
// are prefixed with the prefix + ".".
2017-08-30 16:26:30 +02:00
func (b *BatchBuilder) AddWithPrefix(prefix string, builder Builder) *BatchBuilder {
stmt, names := builder.ToCql()
return b.AddStmtWithPrefix(prefix, stmt, names)
}
2017-08-30 16:26:30 +02:00
// AddStmtWithPrefix adds statement to the batch. Names are prefixed with
// the prefix + ".".
func (b *BatchBuilder) AddStmtWithPrefix(prefix, stmt string, names []string) *BatchBuilder {
2017-08-30 16:26:30 +02:00
b.stmts = append(b.stmts, stmt)
for _, name := range names {
if prefix != "" {
name = fmt.Sprint(prefix, ".", name)
}
b.names = append(b.names, name)
}
return b
}
2017-08-03 17:06:03 +02:00
// UnLogged sets a UNLOGGED BATCH clause on the query.
func (b *BatchBuilder) UnLogged() *BatchBuilder {
b.unlogged = true
return b
}
// Counter sets a COUNTER BATCH clause on the query.
func (b *BatchBuilder) Counter() *BatchBuilder {
b.counter = true
return b
}
2018-05-25 13:06:43 +02:00
// TTL adds USING TTL clause to the query.
func (b *BatchBuilder) TTL(d time.Duration) *BatchBuilder {
b.using.TTL(d)
2017-08-03 17:06:03 +02:00
return b
}
2018-05-25 13:06:43 +02:00
// TTLNamed adds USING TTL clause to the query with a custom parameter name.
func (b *BatchBuilder) TTLNamed(name string) *BatchBuilder {
b.using.TTLNamed(name)
return b
}
// Timestamp adds USING TIMESTAMP clause to the query.
func (b *BatchBuilder) Timestamp(t time.Time) *BatchBuilder {
b.using.Timestamp(t)
return b
}
// TimestampNamed adds a USING TIMESTAMP clause to the query with a custom
// parameter name.
func (b *BatchBuilder) TimestampNamed(name string) *BatchBuilder {
b.using.TimestampNamed(name)
2017-08-03 17:06:03 +02:00
return b
}