mirror of
https://github.com/hanzo-ds/go.git
synced 2026-10-30 19:45:01 +00:00
Source: the unexported clickhouse struct becomes client, the ch receiver and chproto import alias follow, error message prefixes read datastore, and the JSON serializer interface drops the vendor infix. resources' metadata key and its accessor are renamed together, with a test proving the embedded YAML still unmarshals — a silent drift there would have made every connection take the unsupported-server-version branch. Codegen: all three templates still emitted the upstream module paths, so regenerating would have quietly undone the rename. Note the templates have drifted from the files they produce (regenerating today drops QBit and scanTypeDuration), so the generated files are left as tracked and only the templates are corrected. Dead links: an earlier mechanical pass invented URLs that do not resolve — github.com/ClickHouse/datastore-go, hanzo-ds/driver issue numbers that belong to upstream, and mangled names of independent projects (mailru, uptrace, nikepan, EpicStep). Third-party names are not ours to rewrite, so those directories are removed rather than repaired, and provenance comments now point at real files. Dev infra: compose runs our own server image at the tag production runs, since the vendor's alpine tag does not exist in our registry. Cluster hostnames, nginx upstreams and server configs are renamed as one set. CHANGELOG.md is upstream's release history for versions this module never released; rewriting it would fabricate a lineage, so it is dropped and the upstream base recorded in README instead. The two long-standing vet findings are fixed by keying the struct literals rather than suppressed. LICENSE and contributors/list keep the vendor name on purpose: one is the copyright notice Apache-2.0 requires, the other is real people's names. CI now enforces build, vet, test, gofmt and a grep gate.
176 lines
3.5 KiB
Go
176 lines
3.5 KiB
Go
package column
|
|
|
|
import (
|
|
"database/sql"
|
|
"database/sql/driver"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hanzo-ds/native/proto"
|
|
)
|
|
|
|
type Time struct {
|
|
chType Type
|
|
name string
|
|
col proto.ColTime
|
|
}
|
|
|
|
func (col *Time) Reset() {
|
|
col.col.Reset()
|
|
}
|
|
|
|
func (col *Time) Name() string {
|
|
return col.name
|
|
}
|
|
|
|
func (col *Time) Type() Type {
|
|
return col.chType
|
|
}
|
|
|
|
func (col *Time) ScanType() reflect.Type {
|
|
return scanTypeDuration
|
|
}
|
|
|
|
func (col *Time) Rows() int {
|
|
return col.col.Rows()
|
|
}
|
|
|
|
func (col *Time) Row(i int, ptr bool) any {
|
|
value := col.row(i)
|
|
if ptr {
|
|
return &value
|
|
}
|
|
return value
|
|
}
|
|
|
|
// ScanRow implements column.Interface.
|
|
// It is used to read a single column value of the row and store in
|
|
// `dest` Go variable.
|
|
func (col *Time) ScanRow(dest any, row int) error {
|
|
switch d := dest.(type) {
|
|
case *time.Duration:
|
|
*d = col.row(row)
|
|
case **time.Duration:
|
|
*d = new(time.Duration)
|
|
**d = col.row(row)
|
|
default:
|
|
if scan, ok := dest.(sql.Scanner); ok {
|
|
return scan.Scan(col.row(row))
|
|
}
|
|
return &ColumnConverterError{
|
|
Op: "ScanRow",
|
|
To: fmt.Sprintf("%T", dest),
|
|
From: "Time",
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Append implements column.Interface.
|
|
// It is used for columnar inserts. Insert multiple Go value for
|
|
// single Datastore Time type.
|
|
func (col *Time) Append(v any) (nulls []uint8, err error) {
|
|
switch v := v.(type) {
|
|
case []time.Duration:
|
|
nulls = make([]uint8, len(v)) // default all zeros, meaning no null values
|
|
for i := range v {
|
|
col.col.Append(proto.IntoTime32(v[i]))
|
|
}
|
|
case []*time.Duration:
|
|
nulls = make([]uint8, len(v))
|
|
for i := range v {
|
|
switch {
|
|
case v[i] != nil:
|
|
col.col.Append(proto.IntoTime32(*v[i]))
|
|
default:
|
|
col.col.Append(proto.IntoTime32(time.Duration(0)))
|
|
nulls[i] = 1
|
|
}
|
|
}
|
|
default:
|
|
if valuer, ok := v.(driver.Valuer); ok {
|
|
val, err := valuer.Value()
|
|
if err != nil {
|
|
return nil, &ColumnConverterError{
|
|
Op: "Append",
|
|
To: "Time",
|
|
From: fmt.Sprintf("%T", v),
|
|
Hint: "could not get driver.Valuer value",
|
|
}
|
|
}
|
|
return col.Append(val)
|
|
}
|
|
return nil, &ColumnConverterError{
|
|
Op: "Append",
|
|
To: "Time",
|
|
From: fmt.Sprintf("%T", v),
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// AppendRow implements column.Interface.
|
|
// It is used to insert column value in a row.
|
|
// Converts Go type into Datastore type to be inserted.
|
|
func (col *Time) AppendRow(v any) error {
|
|
switch v := v.(type) {
|
|
case time.Duration:
|
|
col.col.Append(proto.IntoTime32(v))
|
|
case *time.Duration:
|
|
switch {
|
|
case v != nil:
|
|
col.col.Append(proto.IntoTime32(*v))
|
|
default:
|
|
col.col.Append(proto.IntoTime32(time.Duration(0)))
|
|
}
|
|
default:
|
|
if valuer, ok := v.(driver.Valuer); ok {
|
|
val, err := valuer.Value()
|
|
if err != nil {
|
|
return &ColumnConverterError{
|
|
Op: "AppendRow",
|
|
To: "Time",
|
|
From: fmt.Sprintf("%T", v),
|
|
Hint: "could not get driver.Valuer value",
|
|
}
|
|
}
|
|
return col.AppendRow(val)
|
|
}
|
|
return &ColumnConverterError{
|
|
Op: "AppendRow",
|
|
To: "Time",
|
|
From: fmt.Sprintf("%T", v),
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (col *Time) Decode(reader *proto.Reader, rows int) error {
|
|
return col.col.DecodeColumn(reader, rows)
|
|
}
|
|
|
|
func (col *Time) Encode(buffer *proto.Buffer) {
|
|
col.col.EncodeColumn(buffer)
|
|
}
|
|
|
|
func (col *Time) row(i int) time.Duration {
|
|
return col.col.Row(i).Duration()
|
|
}
|
|
|
|
func (col *Time) parseTime(value string) (time.Duration, error) {
|
|
return parseDuration(value)
|
|
}
|
|
|
|
// helpers
|
|
|
|
func parseDuration(value string) (time.Duration, error) {
|
|
value = strings.TrimSpace(value)
|
|
if value == "" {
|
|
return time.Duration(0), nil
|
|
}
|
|
|
|
return time.ParseDuration(value)
|
|
}
|