-
Notifications
You must be signed in to change notification settings - Fork 542
/
string.go
117 lines (104 loc) · 1.93 KB
/
string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package column
import (
"fmt"
"reflect"
"github.com/ClickHouse/clickhouse-go/v2/lib/binary"
)
type String []string
func (String) Type() Type {
return "String"
}
func (String) ScanType() reflect.Type {
return scanTypeString
}
func (col *String) Rows() int {
return len(*col)
}
func (s *String) Row(i int, ptr bool) interface{} {
value := *s
if ptr {
return &value[i]
}
return value[i]
}
func (s *String) ScanRow(dest interface{}, row int) error {
v := *s
switch d := dest.(type) {
case *string:
*d = v[row]
case **string:
*d = new(string)
**d = v[row]
default:
return &ColumnConverterError{
Op: "ScanRow",
To: fmt.Sprintf("%T", dest),
From: "String",
}
}
return nil
}
func (s *String) Append(v interface{}) (nulls []uint8, err error) {
switch v := v.(type) {
case []string:
*s, nulls = append(*s, v...), make([]uint8, len(v))
case []*string:
nulls = make([]uint8, len(v))
for i, v := range v {
switch {
case v != nil:
*s = append(*s, *v)
default:
*s, nulls[i] = append(*s, ""), 1
}
}
default:
return nil, &ColumnConverterError{
Op: "Append",
To: "String",
From: fmt.Sprintf("%T", v),
}
}
return
}
func (s *String) AppendRow(v interface{}) error {
switch v := v.(type) {
case string:
*s = append(*s, v)
case *string:
switch {
case v != nil:
*s = append(*s, *v)
default:
*s = append(*s, "")
}
case nil:
*s = append(*s, "")
default:
return &ColumnConverterError{
Op: "AppendRow",
To: "String",
From: fmt.Sprintf("%T", v),
}
}
return nil
}
func (s *String) Decode(decoder *binary.Decoder, rows int) error {
for i := 0; i < int(rows); i++ {
v, err := decoder.String()
if err != nil {
return err
}
*s = append(*s, v)
}
return nil
}
func (s *String) Encode(encoder *binary.Encoder) error {
for _, v := range *s {
if err := encoder.String(v); err != nil {
return err
}
}
return nil
}
var _ Interface = (*String)(nil)