// 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 // Functions reference: // http://cassandra.apache.org/doc/latest/cql/functions.html import ( "bytes" ) // op specifies Cmd operation type. type op byte const ( eq op = iota lt leq gt geq in cnt ) // Cmp if a filtering comparator that is used in WHERE and IF clauses. type Cmp struct { op op column string name string fn *Func } func (c Cmp) writeCql(cql *bytes.Buffer) (names []string) { cql.WriteString(c.column) switch c.op { case eq: cql.WriteByte('=') case lt: cql.WriteByte('<') case leq: cql.WriteByte('<') cql.WriteByte('=') case gt: cql.WriteByte('>') case geq: cql.WriteByte('>') cql.WriteByte('=') case in: cql.WriteString(" IN ") case cnt: cql.WriteString(" CONTAINS ") } if c.fn == nil { cql.WriteByte('?') if c.name == "" { names = append(names, c.column) } else { names = append(names, c.name) } } else { names = append(names, c.fn.writeCql(cql)...) } return } // Eq produces column=?. func Eq(column string) Cmp { return Cmp{ op: eq, column: column, } } // EqNamed produces column=? with a custom parameter name. func EqNamed(column, name string) Cmp { return Cmp{ op: eq, column: column, name: name, } } // EqFunc produces column=someFunc(?...). func EqFunc(column string, fn *Func) Cmp { return Cmp{ op: eq, column: column, fn: fn, } } // Lt produces column?. func Gt(column string) Cmp { return Cmp{ op: gt, column: column, } } // GtNamed produces column>? with a custom parameter name. func GtNamed(column, name string) Cmp { return Cmp{ op: gt, column: column, name: name, } } // GtFunc produces column>someFunc(?...). func GtFunc(column string, fn *Func) Cmp { return Cmp{ op: gt, column: column, fn: fn, } } // GtOrEq produces column>=?. func GtOrEq(column string) Cmp { return Cmp{ op: geq, column: column, } } // GtOrEqNamed produces column>=? with a custom parameter name. func GtOrEqNamed(column, name string) Cmp { return Cmp{ op: geq, column: column, name: name, } } // GtFunc produces column>=someFunc(?...). func GtOrEqFunc(column string, fn *Func) Cmp { return Cmp{ op: geq, column: column, fn: fn, } } // In produces column IN ?. func In(column string) Cmp { return Cmp{ op: in, column: column, } } // InNamed produces column IN ? with a custom parameter name. func InNamed(column, name string) Cmp { return Cmp{ op: in, column: column, name: name, } } // Contains produces column CONTAINS ?. func Contains(column string) Cmp { return Cmp{ op: cnt, column: column, } } // ContainsNamed produces column CONTAINS ? with a custom parameter name. func ContainsNamed(column, name string) Cmp { return Cmp{ op: cnt, column: column, name: name, } } type cmps []Cmp func (cs cmps) writeCql(cql *bytes.Buffer) (names []string) { for i, c := range cs { names = append(names, c.writeCql(cql)...) if i < len(cs)-1 { cql.WriteString(" AND ") } } cql.WriteByte(' ') return }