2017-10-29 13:18:49 -04: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.
|
|
|
|
|
|
|
|
|
|
package qb
|
|
|
|
|
|
2019-05-14 10:58:37 +02:00
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
2017-10-29 13:18:49 -04:00
|
|
|
|
|
|
|
|
// value is a CQL value expression for use in an initializer, assignment,
|
|
|
|
|
// or comparison.
|
|
|
|
|
type value interface {
|
|
|
|
|
// writeCql writes the bytes for this value to the buffer and returns the
|
|
|
|
|
// list of names of parameters which need substitution.
|
|
|
|
|
writeCql(cql *bytes.Buffer) (names []string)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// param is a named CQL '?' parameter.
|
|
|
|
|
type param string
|
|
|
|
|
|
|
|
|
|
func (p param) writeCql(cql *bytes.Buffer) (names []string) {
|
|
|
|
|
cql.WriteByte('?')
|
|
|
|
|
return []string{string(p)}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-13 11:30:19 +02:00
|
|
|
// param is a named CQL tuple '?' parameter.
|
|
|
|
|
type tupleParam struct {
|
|
|
|
|
param param
|
|
|
|
|
count int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t tupleParam) writeCql(cql *bytes.Buffer) (names []string) {
|
2019-05-14 10:58:37 +02:00
|
|
|
baseName := string(t.param) + "_"
|
2019-05-13 11:30:19 +02:00
|
|
|
cql.WriteByte('(')
|
|
|
|
|
for i := 0; i < t.count-1; i++ {
|
|
|
|
|
cql.WriteByte('?')
|
|
|
|
|
cql.WriteByte(',')
|
2019-05-14 10:58:37 +02:00
|
|
|
names = append(names, baseName+strconv.Itoa(i))
|
2019-05-13 11:30:19 +02:00
|
|
|
}
|
|
|
|
|
cql.WriteByte('?')
|
|
|
|
|
cql.WriteByte(')')
|
2019-05-14 10:58:37 +02:00
|
|
|
names = append(names, baseName+strconv.Itoa(t.count-1))
|
|
|
|
|
|
|
|
|
|
return
|
2019-05-13 11:30:19 +02:00
|
|
|
}
|
|
|
|
|
|
2017-10-29 13:18:49 -04:00
|
|
|
// lit is a literal CQL value.
|
|
|
|
|
type lit string
|
|
|
|
|
|
|
|
|
|
func (l lit) writeCql(cql *bytes.Buffer) (names []string) {
|
|
|
|
|
cql.WriteString(string(l))
|
|
|
|
|
return nil
|
|
|
|
|
}
|