88 lines
1.6 KiB
Go
88 lines
1.6 KiB
Go
|
|
package qb
|
||
|
|
|
||
|
|
// INSERT reference:
|
||
|
|
// http://docs.datastax.com/en/dse/5.1/cql/cql/cql_reference/cql_commands/cqlInsert.html
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"errors"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
type InsertBuilder struct {
|
||
|
|
table string
|
||
|
|
columns []string
|
||
|
|
unique bool
|
||
|
|
using using
|
||
|
|
}
|
||
|
|
|
||
|
|
// Insert returns a new InsertBuilder with the given table name.
|
||
|
|
func Insert(table string) *InsertBuilder {
|
||
|
|
return &InsertBuilder{
|
||
|
|
table: table,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (b *InsertBuilder) ToCql() (stmt string, names []string, err error) {
|
||
|
|
if b.table == "" {
|
||
|
|
err = errors.New("insert statements must specify a table")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
if len(b.columns) == 0 {
|
||
|
|
err = errors.New("insert statements must specify columns")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
cql := bytes.Buffer{}
|
||
|
|
|
||
|
|
cql.WriteString("INSERT ")
|
||
|
|
|
||
|
|
cql.WriteString("INTO ")
|
||
|
|
cql.WriteString(b.table)
|
||
|
|
cql.WriteString(" ")
|
||
|
|
|
||
|
|
cql.WriteString("(")
|
||
|
|
cql.WriteString(strings.Join(b.columns, ","))
|
||
|
|
cql.WriteString(") ")
|
||
|
|
|
||
|
|
cql.WriteString("VALUES (")
|
||
|
|
cql.WriteString(placeholders(len(b.columns)))
|
||
|
|
cql.WriteString(") ")
|
||
|
|
|
||
|
|
b.using.WriteCql(&cql)
|
||
|
|
|
||
|
|
if b.unique {
|
||
|
|
cql.WriteString("IF NOT EXISTS ")
|
||
|
|
}
|
||
|
|
|
||
|
|
stmt, names = cql.String(), b.columns
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
func (b *InsertBuilder) Into(table string) *InsertBuilder {
|
||
|
|
b.table = table
|
||
|
|
return b
|
||
|
|
}
|
||
|
|
|
||
|
|
func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder {
|
||
|
|
b.columns = append(b.columns, columns...)
|
||
|
|
return b
|
||
|
|
}
|
||
|
|
|
||
|
|
func (b *InsertBuilder) Unique() *InsertBuilder {
|
||
|
|
b.unique = true
|
||
|
|
return b
|
||
|
|
}
|
||
|
|
|
||
|
|
func (b *InsertBuilder) Timestamp(t time.Time) *InsertBuilder {
|
||
|
|
b.using.timestamp = t
|
||
|
|
return b
|
||
|
|
}
|
||
|
|
|
||
|
|
func (b *InsertBuilder) TTL(d time.Duration) *InsertBuilder {
|
||
|
|
b.using.ttl = d
|
||
|
|
return b
|
||
|
|
}
|