2017-07-26 13:57:10 +02:00
|
|
|
package qb
|
|
|
|
|
|
|
|
|
|
// INSERT reference:
|
2017-07-28 10:18:38 +02:00
|
|
|
// https://cassandra.apache.org/doc/latest/cql/dml.html#insert
|
2017-07-26 13:57:10 +02:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2017-07-28 10:18:38 +02:00
|
|
|
// InsertBuilder builds CQL INSERT statements.
|
2017-07-26 13:57:10 +02:00
|
|
|
type InsertBuilder struct {
|
|
|
|
|
table string
|
2017-07-27 09:48:33 +02:00
|
|
|
columns columns
|
2017-07-26 13:57:10 +02:00
|
|
|
unique bool
|
|
|
|
|
using using
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert returns a new InsertBuilder with the given table name.
|
|
|
|
|
func Insert(table string) *InsertBuilder {
|
|
|
|
|
return &InsertBuilder{
|
|
|
|
|
table: table,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 10:18:38 +02:00
|
|
|
// ToCql builds the query into a CQL string and named args.
|
2017-07-28 09:43:58 +02:00
|
|
|
func (b *InsertBuilder) ToCql() (stmt string, names []string) {
|
2017-07-26 13:57:10 +02:00
|
|
|
cql := bytes.Buffer{}
|
|
|
|
|
|
|
|
|
|
cql.WriteString("INSERT ")
|
|
|
|
|
|
|
|
|
|
cql.WriteString("INTO ")
|
|
|
|
|
cql.WriteString(b.table)
|
2017-07-27 09:48:33 +02:00
|
|
|
cql.WriteByte(' ')
|
2017-07-26 13:57:10 +02:00
|
|
|
|
2017-07-27 09:48:33 +02:00
|
|
|
cql.WriteByte('(')
|
|
|
|
|
b.columns.writeCql(&cql)
|
2017-07-26 13:57:10 +02:00
|
|
|
cql.WriteString(") ")
|
|
|
|
|
|
|
|
|
|
cql.WriteString("VALUES (")
|
2017-07-27 09:48:33 +02:00
|
|
|
placeholders(&cql, len(b.columns))
|
2017-07-26 13:57:10 +02:00
|
|
|
cql.WriteString(") ")
|
|
|
|
|
|
2017-07-27 09:48:33 +02:00
|
|
|
b.using.writeCql(&cql)
|
2017-07-26 13:57:10 +02:00
|
|
|
|
|
|
|
|
if b.unique {
|
|
|
|
|
cql.WriteString("IF NOT EXISTS ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt, names = cql.String(), b.columns
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 10:18:38 +02:00
|
|
|
// Into sets the INTO clause of the query.
|
2017-07-26 13:57:10 +02:00
|
|
|
func (b *InsertBuilder) Into(table string) *InsertBuilder {
|
|
|
|
|
b.table = table
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 10:18:38 +02:00
|
|
|
// Columns adds insert columns to the query.
|
2017-07-26 13:57:10 +02:00
|
|
|
func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder {
|
|
|
|
|
b.columns = append(b.columns, columns...)
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 10:18:38 +02:00
|
|
|
// Unique sets a IF NOT EXISTS clause on the query.
|
2017-07-26 13:57:10 +02:00
|
|
|
func (b *InsertBuilder) Unique() *InsertBuilder {
|
|
|
|
|
b.unique = true
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 10:18:38 +02:00
|
|
|
// Timestamp sets a USING TIMESTAMP clause on the query.
|
2017-07-26 13:57:10 +02:00
|
|
|
func (b *InsertBuilder) Timestamp(t time.Time) *InsertBuilder {
|
|
|
|
|
b.using.timestamp = t
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 10:18:38 +02:00
|
|
|
// TTL sets a USING TTL clause on the query.
|
2017-07-26 13:57:10 +02:00
|
|
|
func (b *InsertBuilder) TTL(d time.Duration) *InsertBuilder {
|
|
|
|
|
b.using.ttl = d
|
|
|
|
|
return b
|
|
|
|
|
}
|