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

chore: add chschema.Array

This commit is contained in:
Vladimir Mihailenco 2023-03-29 16:13:10 +03:00
parent 5cd79b48c3
commit 2d63e3b0ac

View File

@ -1,6 +1,7 @@
package chschema
import (
"reflect"
"strings"
"github.com/uptrace/go-clickhouse/ch/internal"
@ -163,3 +164,43 @@ func SafeQueryWithSep(query string, args []any, sep string) QueryWithSep {
Sep: sep,
}
}
//------------------------------------------------------------------------------
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
}