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:
parent
5cd79b48c3
commit
2d63e3b0ac
@ -1,6 +1,7 @@
|
|||||||
package chschema
|
package chschema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/uptrace/go-clickhouse/ch/internal"
|
"github.com/uptrace/go-clickhouse/ch/internal"
|
||||||
@ -163,3 +164,43 @@ func SafeQueryWithSep(query string, args []any, sep string) QueryWithSep {
|
|||||||
Sep: sep,
|
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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user