forked from databendlabs/databend-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
129 lines (102 loc) · 2.76 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package godatabend
import (
"database/sql/driver"
"fmt"
"net"
"strconv"
"time"
)
func Map(v interface{}) driver.Valuer {
return tmap{v: v}
}
type tmap struct {
v interface{}
}
func (a tmap) Value() (driver.Value, error) {
return textEncode.Encode(a)
}
// 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)
}
type NullableValue struct {
val any
}
// Scan implements the [Scanner] interface.
func (nv *NullableValue) Scan(value any) error {
nv.val = value
return nil
}
// Value implements the [driver.Valuer] interface.
func (nv NullableValue) Value() (driver.Value, error) {
return nv.val, nil
}