Support for literals in INSERT and UPDATE and comparisons
The 'value' interface represents a CQL value for use in a comparison, update, or intitialization operation. A consistent interface for this allows us to easily support specifying default-named, custom-named, literal, and evaluated-function values in all these contexts. Parameters to Func should probably also be values to support full composition, but that would be a breaking change because Func's properties are exposed. The value interface could itself be exposed if we wanted to allow clients to pass their own values to SetValue, etc, but for now it is a package-internal abstraction. BLA
This commit is contained in:
committed by
Michał Matczuk
parent
9fa5432a65
commit
12d360a0c3
57
qb/insert.go
57
qb/insert.go
@@ -11,10 +11,16 @@ import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
// initializer specifies an value for a column in an insert operation.
|
||||
type initializer struct {
|
||||
column string
|
||||
value value
|
||||
}
|
||||
|
||||
// InsertBuilder builds CQL INSERT statements.
|
||||
type InsertBuilder struct {
|
||||
table string
|
||||
columns columns
|
||||
columns []initializer
|
||||
unique bool
|
||||
using using
|
||||
}
|
||||
@@ -37,12 +43,21 @@ func (b *InsertBuilder) ToCql() (stmt string, names []string) {
|
||||
cql.WriteByte(' ')
|
||||
|
||||
cql.WriteByte('(')
|
||||
b.columns.writeCql(&cql)
|
||||
names = append(names, b.columns...)
|
||||
for i, c := range b.columns {
|
||||
cql.WriteString(c.column)
|
||||
if i < len(b.columns)-1 {
|
||||
cql.WriteByte(',')
|
||||
}
|
||||
}
|
||||
cql.WriteString(") ")
|
||||
|
||||
cql.WriteString("VALUES (")
|
||||
placeholders(&cql, len(b.columns))
|
||||
for i, c := range b.columns {
|
||||
names = append(names, c.value.writeCql(&cql)...)
|
||||
if i < len(b.columns)-1 {
|
||||
cql.WriteByte(',')
|
||||
}
|
||||
}
|
||||
cql.WriteString(") ")
|
||||
|
||||
if b.unique {
|
||||
@@ -62,7 +77,39 @@ func (b *InsertBuilder) Into(table string) *InsertBuilder {
|
||||
|
||||
// Columns adds insert columns to the query.
|
||||
func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder {
|
||||
b.columns = append(b.columns, columns...)
|
||||
for _, c := range columns {
|
||||
b.columns = append(b.columns, initializer{
|
||||
column: c,
|
||||
value: param(c),
|
||||
})
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// NamedColumn adds an insert column with a custom parameter name.
|
||||
func (b *InsertBuilder) NamedColumn(column, name string) *InsertBuilder {
|
||||
b.columns = append(b.columns, initializer{
|
||||
column: column,
|
||||
value: param(name),
|
||||
})
|
||||
return b
|
||||
}
|
||||
|
||||
// LitColumn adds an insert column with a literal value to the query.
|
||||
func (b *InsertBuilder) LitColumn(column, literal string) *InsertBuilder {
|
||||
b.columns = append(b.columns, initializer{
|
||||
column: column,
|
||||
value: lit(literal),
|
||||
})
|
||||
return b
|
||||
}
|
||||
|
||||
// FuncColumn adds an insert column initialized by evaluating a CQL function.
|
||||
func (b *InsertBuilder) FuncColumn(column string, fn *Func) *InsertBuilder {
|
||||
b.columns = append(b.columns, initializer{
|
||||
column: column,
|
||||
value: fn,
|
||||
})
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user