1
0
mirror of https://github.com/uptrace/go-clickhouse.git synced 2025-06-08 23:26:11 +02:00

209 lines
4.1 KiB
Go
Raw Permalink Normal View History

2022-01-23 09:36:24 +02:00
package chschema
import (
2023-03-29 16:13:10 +03:00
"reflect"
2022-01-23 09:36:24 +02:00
"strings"
"github.com/uptrace/go-clickhouse/ch/internal"
)
type QueryAppender interface {
AppendQuery(fmter Formatter, b []byte) ([]byte, error)
}
type ColumnsAppender interface {
AppendColumns(fmter Formatter, b []byte) ([]byte, error)
}
//------------------------------------------------------------------------------
// Safe represents a safe SQL query.
type Safe string
var _ QueryAppender = (*Safe)(nil)
func (s Safe) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
return append(b, s...), nil
}
//------------------------------------------------------------------------------
2023-10-01 14:43:38 +03:00
// Name represents a SQL identifier, for example, table or column name.
type Name string
2022-01-23 09:36:24 +02:00
2023-10-01 14:43:38 +03:00
var _ QueryAppender = (*Name)(nil)
2022-01-23 09:36:24 +02:00
2023-10-01 14:43:38 +03:00
func (s Name) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
return fmter.AppendName(b, string(s)), nil
2022-01-23 09:36:24 +02:00
}
2023-10-01 14:43:38 +03:00
func AppendName(b []byte, field string) []byte {
return appendName(b, internal.Bytes(field))
2022-01-23 09:36:24 +02:00
}
2023-10-01 14:43:38 +03:00
func appendName(b, src []byte) []byte {
2022-01-23 09:36:24 +02:00
const quote = '"'
2023-10-01 14:43:38 +03:00
b = append(b, quote)
2022-01-23 09:36:24 +02:00
for _, c := range src {
if c == quote {
b = append(b, quote, quote)
} else {
b = append(b, c)
}
}
2023-10-01 14:43:38 +03:00
b = append(b, quote)
2022-01-23 09:36:24 +02:00
return b
}
2023-10-01 14:43:38 +03:00
//------------------------------------------------------------------------------
// Ident represents a fully qualified SQL name, for example, table or column name.
2022-01-23 09:36:24 +02:00
type Ident string
2023-10-01 14:43:38 +03:00
var _ QueryAppender = (*Name)(nil)
2022-01-23 09:36:24 +02:00
func (s Ident) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
return fmter.AppendIdent(b, string(s)), nil
}
func AppendIdent(b []byte, field string) []byte {
return appendIdent(b, internal.Bytes(field))
}
func appendIdent(b, src []byte) []byte {
const quote = '"'
2023-10-01 14:43:38 +03:00
var quoted bool
loop:
2022-01-23 09:36:24 +02:00
for _, c := range src {
2023-10-01 14:43:38 +03:00
switch c {
case '*':
if !quoted {
b = append(b, '*')
continue loop
}
case '.':
if quoted {
b = append(b, quote)
quoted = false
}
b = append(b, '.')
continue loop
}
if !quoted {
b = append(b, quote)
quoted = true
}
2022-01-23 09:36:24 +02:00
if c == quote {
b = append(b, quote, quote)
} else {
b = append(b, c)
}
}
2023-10-01 14:43:38 +03:00
if quoted {
b = append(b, quote)
}
2022-01-23 09:36:24 +02:00
return b
}
//------------------------------------------------------------------------------
type QueryWithArgs struct {
Query string
Args []any
}
var _ QueryAppender = (*QueryWithArgs)(nil)
func SafeQuery(query string, args []any) QueryWithArgs {
if args == nil {
args = make([]any, 0)
} else if len(query) > 0 && strings.IndexByte(query, '?') == -1 {
internal.Warn.Printf("query %q has %v args, but no placeholders", query, args)
}
return QueryWithArgs{
Query: query,
Args: args,
}
}
2023-10-01 14:43:38 +03:00
func UnsafeName(ident string) QueryWithArgs {
2022-01-23 09:36:24 +02:00
return QueryWithArgs{Query: ident}
}
func (q QueryWithArgs) IsZero() bool {
return q.Query == "" && q.Args == nil
}
2022-11-03 11:58:19 +02:00
func (q QueryWithArgs) IsEmpty() bool {
return q.Query == ""
}
2022-01-23 09:36:24 +02:00
func (q QueryWithArgs) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
if q.Args == nil {
2023-10-01 14:43:38 +03:00
return fmter.AppendName(b, q.Query), nil
2022-01-23 09:36:24 +02:00
}
return fmter.AppendQuery(b, q.Query, q.Args...), nil
}
func (q QueryWithArgs) Value() Safe {
b, _ := q.AppendQuery(emptyFmter, nil)
return Safe(b)
}
//------------------------------------------------------------------------------
type QueryWithSep struct {
QueryWithArgs
Sep string
}
func SafeQueryWithSep(query string, args []any, sep string) QueryWithSep {
return QueryWithSep{
QueryWithArgs: SafeQuery(query, args),
Sep: sep,
}
}
2023-03-29 16:13:10 +03:00
//------------------------------------------------------------------------------
type ArrayValue struct {
v reflect.Value
}
func Array(vi interface{}) *ArrayValue {
return &ArrayValue{
v: reflect.ValueOf(vi),
}
}
var _ QueryAppender = (*ArrayValue)(nil)
func (a *ArrayValue) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
if !a.v.IsValid() || a.v.Len() == 0 {
b = append(b, "[]"...)
return b, nil
}
typ := a.v.Type()
elemType := typ.Elem()
appendElem := Appender(elemType)
b = append(b, '[')
ln := a.v.Len()
for i := 0; i < ln; i++ {
if i > 0 {
b = append(b, ',')
}
elem := a.v.Index(i)
b = appendElem(fmter, b, elem)
}
b = append(b, ']')
return b, nil
}