-
Notifications
You must be signed in to change notification settings - Fork 0
/
qchar.go
146 lines (133 loc) · 3.94 KB
/
qchar.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package pgtype
import (
"math"
"strconv"
"github.com/pkg/errors"
)
// QChar is for PostgreSQL's special 8-bit-only "char" type more akin to the C
// language's char type, or Go's byte type. (Note that the name in PostgreSQL
// itself is "char", in double-quotes, and not char.) It gets used a lot in
// PostgreSQL's system tables to hold a single ASCII character value (eg
// pg_class.relkind). It is named Qchar for quoted char to disambiguate from SQL
// standard type char.
//
// Not all possible values of QChar are representable in the text format.
// Therefore, QChar does not implement TextEncoder and TextDecoder. In
// addition, database/sql Scanner and database/sql/driver Value are not
// implemented.
type QChar struct {
Int int8
Status Status
}
func (dst *QChar) Set(src interface{}) error {
if src == nil {
*dst = QChar{Status: Null}
return nil
}
switch value := src.(type) {
case int8:
*dst = QChar{Int: value, Status: Present}
case uint8:
if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case int16:
if value < math.MinInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value)
}
if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case uint16:
if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case int32:
if value < math.MinInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value)
}
if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case uint32:
if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case int64:
if value < math.MinInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value)
}
if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case uint64:
if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case int:
if value < math.MinInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value)
}
if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case uint:
if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case string:
num, err := strconv.ParseInt(value, 10, 8)
if err != nil {
return err
}
*dst = QChar{Int: int8(num), Status: Present}
default:
if originalSrc, ok := underlyingNumberType(src); ok {
return dst.Set(originalSrc)
}
return errors.Errorf("cannot convert %v to QChar", value)
}
return nil
}
func (dst *QChar) Get() interface{} {
switch dst.Status {
case Present:
return dst.Int
case Null:
return nil
default:
return dst.Status
}
}
func (src *QChar) AssignTo(dst interface{}) error {
return int64AssignTo(int64(src.Int), src.Status, dst)
}
func (dst *QChar) DecodeBinary(ci *ConnInfo, src []byte) error {
if src == nil {
*dst = QChar{Status: Null}
return nil
}
if len(src) != 1 {
return errors.Errorf(`invalid length for "char": %v`, len(src))
}
*dst = QChar{Int: int8(src[0]), Status: Present}
return nil
}
func (src *QChar) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
case Undefined:
return nil, errUndefined
}
return append(buf, byte(src.Int)), nil
}