/
mapping.go
149 lines (138 loc) · 4.46 KB
/
mapping.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
package main
type mapping struct {
goFieldType string // non-nullable type
newFuncCall string // to create accessor for non-nullable type
// null
nullableGoFieldType string // full name of the nullable type
nullableValueFieldName string // to access the go field value of a nullable type
convertFuncName string // to convert from a go field value to a nullable type
newAccessFuncCall string // to create the accessor
}
// https://www.postgresql.org/docs/9.1/datatype-numeric.html
var pgMappings = map[string]mapping{
"timestamp with time zone": {
goFieldType: "time.Time",
newFuncCall: "NewTimeAccess",
nullableValueFieldName: "Time",
convertFuncName: "TimeToTimestamptz",
nullableGoFieldType: "pgtype.Timestamptz",
newAccessFuncCall: "NewFieldAccess[pgtype.Timestamptz]",
},
"timestamp without time zone": {
goFieldType: "time.Time",
newFuncCall: "NewTimeAccess",
nullableValueFieldName: "Time",
convertFuncName: "TimeToTimestamp",
nullableGoFieldType: "pgtype.Timestamp",
newAccessFuncCall: "NewFieldAccess[pgtype.Timestamp]",
},
"date": {
goFieldType: "time.Time",
newFuncCall: "NewTimeAccess",
nullableValueFieldName: "Time",
convertFuncName: "TimeToDate",
nullableGoFieldType: "pgtype.Date",
newAccessFuncCall: "NewFieldAccess[pgtype.Date]",
},
"citext": {
goFieldType: "string",
newFuncCall: "NewTextAccess",
nullableValueFieldName: "String",
convertFuncName: "StringToText",
nullableGoFieldType: "pgtype.Text",
newAccessFuncCall: "NewFieldAccess[pgtype.Text]",
},
"text": {
goFieldType: "string",
newFuncCall: "NewTextAccess",
nullableValueFieldName: "String",
convertFuncName: "StringToText",
nullableGoFieldType: "pgtype.Text",
newAccessFuncCall: "NewFieldAccess[pgtype.Text]",
},
"character varying": {
goFieldType: "string",
newFuncCall: "NewTextAccess",
nullableValueFieldName: "String",
convertFuncName: "StringToText",
nullableGoFieldType: "pgtype.Text",
newAccessFuncCall: "NewFieldAccess[pgtype.Text]",
},
"bigint": {
goFieldType: "int64",
newFuncCall: "NewInt64Access",
nullableValueFieldName: "Int",
convertFuncName: "Int64ToInt8",
nullableGoFieldType: "pgtype.Int8",
newAccessFuncCall: "NewFieldAccess[pgtype.Int8]",
},
"integer": {
goFieldType: "int32",
newFuncCall: "NewInt32Access",
nullableValueFieldName: "Int",
convertFuncName: "Int32ToInt4",
nullableGoFieldType: "pgtype.Int4",
newAccessFuncCall: "NewFieldAccess[pgtype.Int4]",
},
"jsonb": {
nullableGoFieldType: "p.NullJSON",
goFieldType: "p.NullJSON",
newAccessFuncCall: "NewJSONAccess",
},
"json": {
nullableGoFieldType: "p.NullJSON",
goFieldType: "p.NullJSON",
newAccessFuncCall: "NewJSONAccess",
},
"uuid": {
goFieldType: "pgtype.UUID",
nullableGoFieldType: "pgtype.UUID",
newAccessFuncCall: "NewFieldAccess[pgtype.UUID]",
},
// https://github.com/jackc/pgx/wiki/Numeric-and-decimal-support
"numeric": {
goFieldType: "decimal.NullDecimal",
nullableGoFieldType: "decimal.NullDecimal",
newAccessFuncCall: "NewFieldAccess[decimal.NullDecimal]",
},
"double precision": {
goFieldType: "float64",
nullableGoFieldType: "pgtype.Float8",
newAccessFuncCall: "NewFieldAccess[pgtype.Float8]",
},
"decimal": {
goFieldType: "decimal.NullDecimal",
nullableGoFieldType: "decimal.NullDecimal",
newAccessFuncCall: "NewFieldAccess[decimal.NullDecimal]",
},
"point": {
nullableGoFieldType: "pgtype.Point",
newAccessFuncCall: "NewFieldAccess[pgtype.Point]",
},
"boolean": {
goFieldType: "bool",
newFuncCall: "NewBooleanAccess",
convertFuncName: "Bool",
nullableValueFieldName: "Bool",
nullableGoFieldType: "pgtype.Bool",
newAccessFuncCall: "NewFieldAccess[pgtype.Bool]",
},
"daterange": {
nullableGoFieldType: "pgtype.Range[pgtype.Date]",
goFieldType: "pgtype.Range[pgtype.Date]",
newAccessFuncCall: "NewFieldAccess[pgtype.Range[pgtype.Date]]",
},
"bytea": {
nullableGoFieldType: "pgtype.Bytea",
newAccessFuncCall: "NewFieldAccess[pgtype.Bytea]",
},
"text[]": {
goFieldType: "pgtype.FlatArray[pgtype.Text]",
nullableGoFieldType: "pgtype.FlatArray[pgtype.Text]",
newAccessFuncCall: "NewFieldAccess[pgtype.FlatArray[pgtype.Text]]",
},
"interval": {
nullableGoFieldType: "pgtype.Interval",
newAccessFuncCall: "NewFieldAccess[pgtype.Interval]",
},
}