/
postgres_types.go
231 lines (222 loc) · 4.94 KB
/
postgres_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"encoding/json"
"reflect"
"strings"
"time"
"github.com/google/uuid"
"github.com/lib/pq"
"github.com/shopspring/decimal"
"github.com/volatiletech/null/v8"
)
var (
NotSupportedType = reflect.TypeOf(nil)
BoolType = reflect.TypeOf(false)
ByteType = reflect.TypeOf(byte(0))
StringType = reflect.TypeOf(string(""))
IntType = reflect.TypeOf(int(0))
Int16Type = reflect.TypeOf(int16(0))
Int32Type = reflect.TypeOf(int32(0))
Int64Type = reflect.TypeOf(int64(0))
Float32Type = reflect.TypeOf(float32(0))
Float64Type = reflect.TypeOf(float64(0))
RawJSONType = reflect.TypeOf(json.RawMessage{})
TimeType = reflect.TypeOf(time.Time{})
DecimalType = reflect.TypeOf(decimal.Decimal{})
UUIDType = reflect.TypeOf(uuid.UUID{})
)
var pgtypes = map[reflect.Type]struct {
Basic reflect.Type
Null reflect.Type
Array reflect.Type
NullArray reflect.Type
}{
BoolType: {
Basic: BoolType,
Null: reflect.TypeOf(null.Bool{}),
Array: reflect.TypeOf(pq.BoolArray{}),
NullArray: reflect.TypeOf(pq.BoolArray{}),
},
ByteType: {
Basic: ByteType,
Null: reflect.TypeOf(null.Byte{}),
Array: reflect.TypeOf(null.Bytes{}),
NullArray: reflect.TypeOf(null.Bytes{}),
},
StringType: {
Basic: StringType,
Null: reflect.TypeOf(null.String{}),
Array: reflect.TypeOf(pq.StringArray{}),
NullArray: reflect.TypeOf(pq.StringArray{}),
},
IntType: {
Basic: IntType,
Null: reflect.TypeOf(null.Int{}),
Array: reflect.TypeOf(pq.Int64Array{}),
NullArray: reflect.TypeOf(pq.Int64Array{}),
},
Int16Type: {
Basic: Int16Type,
Null: reflect.TypeOf(null.Int16{}),
Array: reflect.TypeOf(pq.Int32Array{}),
NullArray: reflect.TypeOf(pq.Int32Array{}),
},
Int32Type: {
Basic: Int32Type,
Null: reflect.TypeOf(null.Int32{}),
Array: reflect.TypeOf(pq.Int32Array{}),
NullArray: reflect.TypeOf(pq.Int32Array{}),
},
Int64Type: {
Basic: Int64Type,
Null: reflect.TypeOf(null.Int64{}),
Array: reflect.TypeOf(pq.Int64Array{}),
NullArray: reflect.TypeOf(pq.Int64Array{}),
},
Float32Type: {
Basic: Float32Type,
Null: reflect.TypeOf(null.Float32{}),
Array: reflect.TypeOf(pq.Float32Array{}),
NullArray: reflect.TypeOf(pq.Float32Array{}),
},
Float64Type: {
Basic: Float64Type,
Null: reflect.TypeOf(null.Float64{}),
Array: reflect.TypeOf(pq.Float64Array{}),
NullArray: reflect.TypeOf(pq.Float64Array{}),
},
RawJSONType: {
Basic: RawJSONType,
Null: reflect.TypeOf(null.JSON{}),
Array: NotSupportedType,
NullArray: NotSupportedType,
},
TimeType: {
Basic: TimeType,
Null: reflect.TypeOf(null.Time{}),
Array: NotSupportedType,
NullArray: NotSupportedType,
},
DecimalType: {
Basic: DecimalType,
Null: reflect.TypeOf(decimal.NullDecimal{}),
Array: NotSupportedType,
NullArray: NotSupportedType,
},
UUIDType: {
Basic: UUIDType,
Null: NotSupportedType,
Array: NotSupportedType,
NullArray: NotSupportedType,
},
}
func goType(ci postgresColumnInfo) reflect.Type {
pgtype := func(typ reflect.Type) reflect.Type {
if ci.IsArray && ci.Nullable {
return pgtypes[typ].NullArray
} else if ci.IsArray {
return pgtypes[typ].Array
} else if ci.Nullable {
return pgtypes[typ].Null
} else {
return pgtypes[typ].Basic
}
}
// remove array and keep only data type
dataTypeName := strings.TrimRight(ci.DataTypeName, "[]")
switch dataTypeName {
case "bool":
return pgtype(BoolType)
case "character":
return pgtype(ByteType)
case "bit", "interval", "bit varying", "money", "character varying", "cidr", "inet", "macaddr", "macaddr8", "text", "xml", "varchar":
return pgtype(StringType)
case "smallint":
return pgtype(Int16Type)
case "integer", "serial":
return pgtype(Int32Type)
case "bigint", "bigserial":
return pgtype(Int64Type)
case "real":
return pgtype(Float32Type)
case "double precision":
return pgtype(Float64Type)
case "json", "jsonb":
return pgtype(RawJSONType)
case "date", "timestamp without time zone", "timestamp with time zone", "time without time zone", "time with time zone":
return pgtype(TimeType)
case "decimal", "numeric":
return pgtype(DecimalType)
case "uuid":
return pgtype(UUIDType)
}
return NotSupportedType
}
/*
TODO:
bigint
bit
bit varying
boolean
box
bytea
character
character varying
cid
cidr
circle
citext
cube
date
daterange
double precision
ean13
gtsvector
hstore
inet
int4range
int8range
integer
interval
isbn
isbn13
ismn
ismn13
issn
issn13
json
jsonb
jsonpath
line
lseg
ltree
macaddr
macaddr8
money
name
numeric
numrange
oid
path
pg_lsn
point
polygon
real
smallint
text
tid
time with time zone
time without time zone
timestamp with time zone
timestamp without time zone
tsquery
tsrange
tstzrange
tsvector
txid_snapshot
upc
uuid
xid
xid8
xml
*/