forked from mailru/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
102 lines (81 loc) · 2.3 KB
/
types.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
package clickhouse
import (
"database/sql/driver"
"fmt"
"net"
"strconv"
"time"
)
// Array wraps slice or array into driver.Valuer interface to allow pass through it from database/sql
func Array(v interface{}) driver.Valuer {
return array{v: v}
}
type array struct {
v interface{}
}
// Value implements driver.Valuer
func (a array) Value() (driver.Value, error) {
return textEncode.Encode(a)
}
// Date returns date for t
func Date(t time.Time) driver.Valuer {
return date(t)
}
type date time.Time
// Value implements driver.Valuer
func (d date) Value() (driver.Value, error) {
return []byte(formatDate(time.Time(d))), nil
}
// UInt64 returns uint64
func UInt64(u uint64) driver.Valuer {
return bigUint64(u)
}
type bigUint64 uint64
// Value implements driver.Valuer
func (u bigUint64) Value() (driver.Value, error) {
return []byte(strconv.FormatUint(uint64(u), 10)), nil
}
// Decimal32 converts value to Decimal32 of precision S.
// The value can be a number or a string. The S (scale) parameter specifies the number of decimal places.
func Decimal32(v interface{}, s int32) driver.Valuer {
return decimal{32, s, v}
}
// Decimal64 converts value to Decimal64 of precision S.
// The value can be a number or a string. The S (scale) parameter specifies the number of decimal places.
func Decimal64(v interface{}, s int32) driver.Valuer {
return decimal{64, s, v}
}
// Decimal128 converts value to Decimal128 of precision S.
// The value can be a number or a string. The S (scale) parameter specifies the number of decimal places.
func Decimal128(v interface{}, s int32) driver.Valuer {
return decimal{128, s, v}
}
type decimal struct {
p int32
s int32
v interface{}
}
// Value implements driver.Valuer
func (d decimal) Value() (driver.Value, error) {
return []byte(fmt.Sprintf("toDecimal%d(%v, %d)", d.p, d.v, d.s)), nil
}
// IP returns compatible database format for net.IP
func IP(i net.IP) driver.Valuer {
return ip(i)
}
type ip net.IP
// Value implements driver.Valuer
func (i ip) Value() (driver.Value, error) {
return net.IP(i).String(), nil
}
// Tuple converts a struct into a tuple
// struct{A string, B int}{"a", 1} -> ("a", 1)
func Tuple(v interface{}) driver.Valuer {
return tuple{v: v}
}
type tuple struct {
v interface{}
}
func (t tuple) Value() (driver.Value, error) {
return textEncode.Encode(t)
}