Files
gocqlx/qb/expr.go

79 lines
1.3 KiB
Go
Raw Normal View History

2017-07-26 13:57:10 +02:00
package qb
import (
"bytes"
"fmt"
"strings"
"time"
)
type expr interface {
// WriteCql writes a CQL representation of the expr to a buffer and returns
// slice of parameter names.
WriteCql(cql *bytes.Buffer) (names []string)
}
type columns []string
2017-07-27 12:28:01 +02:00
func (cols columns) writeCql(cql *bytes.Buffer) {
for i, c := range cols {
cql.WriteString(c)
if i < len(cols)-1 {
cql.WriteByte(',')
}
}
}
2017-07-26 13:57:10 +02:00
type using struct {
timestamp time.Time
ttl time.Duration
}
2017-07-27 12:28:01 +02:00
func (u using) writeCql(cql *bytes.Buffer) {
2017-07-26 13:57:10 +02:00
var v []string
if !u.timestamp.IsZero() {
v = append(v, fmt.Sprint("TIMESTAMP ", u.timestamp.UnixNano()/1000))
}
if u.ttl != 0 {
v = append(v, fmt.Sprint("TTL ", int(u.ttl.Seconds())))
}
if len(v) > 0 {
cql.WriteString("USING ")
cql.WriteString(strings.Join(v, ","))
cql.WriteByte(' ')
2017-07-26 13:57:10 +02:00
}
}
type where []expr
func (w where) writeCql(cql *bytes.Buffer) (names []string) {
if len(w) == 0 {
return
}
cql.WriteString("WHERE ")
return writeCql(w, cql)
}
type _if []expr
func (w _if) writeCql(cql *bytes.Buffer) (names []string) {
if len(w) == 0 {
return
}
cql.WriteString("IF ")
return writeCql(w, cql)
}
func writeCql(es []expr, cql *bytes.Buffer) (names []string) {
for i, c := range es {
names = append(names, c.WriteCql(cql)...)
if i < len(es)-1 {
cql.WriteString(" AND ")
}
}
cql.WriteByte(' ')
return
}