-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres.go
349 lines (303 loc) · 7.84 KB
/
postgres.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package loaders
import (
"fmt"
"regexp"
"strconv"
"strings"
_ "github.com/lib/pq"
"github.com/knq/snaker"
"github.com/xoxo-go/xoxo/internal"
"github.com/xoxo-go/xoxo/models"
)
func init() {
internal.SchemaLoaders["postgres"] = internal.TypeLoader{
ProcessRelkind: PgRelkind,
Schema: func(*internal.ArgType) (string, error) { return "public", nil },
ParseType: PgParseType,
EnumList: models.PgEnums,
EnumValueList: models.PgEnumValues,
ProcList: models.PgProcs,
ProcParamList: models.PgProcParams,
TableList: PgTables,
ColumnList: func(db models.XODB, schema string, table string) ([]*models.Column, error) {
return models.PgTableColumns(db, schema, table, internal.Args.EnablePostgresOIDs)
},
ForeignKeyList: models.PgTableForeignKeys,
IndexList: models.PgTableIndexes,
IndexColumnList: PgIndexColumns,
QueryStrip: PgQueryStrip,
QueryColumnList: PgQueryColumns,
}
}
// PgRelkind returns the postgres string representation for RelType.
func PgRelkind(relType internal.RelType) string {
var s string
switch relType {
case internal.Table:
s = "r"
case internal.View:
s = "v"
default:
panic("unsupported RelType")
}
return s
}
// PgParseType parse a postgres type into a Go type based on the column
// definition.
func PgParseType(args *internal.ArgType, dt string, nullable bool) (int, string, string) {
precision := 0
nilVal := "nil"
asSlice := false
// handle SETOF
if strings.HasPrefix(dt, "SETOF ") {
_, _, t := PgParseType(args, dt[len("SETOF "):], false)
return 0, "nil", "[]" + t
}
// determine if it's a slice
if strings.HasSuffix(dt, "[]") {
dt = dt[:len(dt)-2]
asSlice = true
}
// extract precision
dt, precision, _ = args.ParsePrecision(dt)
var typ string
switch dt {
case "boolean":
nilVal = "false"
typ = "bool"
if nullable {
nilVal = "sql.NullBool{}"
typ = "sql.NullBool"
}
case "character", "character varying", "text", "money", "inet":
nilVal = `""`
typ = "string"
if nullable {
nilVal = "sql.NullString{}"
typ = "sql.NullString"
}
case "smallint":
nilVal = "0"
typ = "int16"
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "integer":
nilVal = "0"
typ = args.Int32Type
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "bigint":
nilVal = "0"
typ = "int64"
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "smallserial":
nilVal = "0"
typ = "uint16"
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "serial":
nilVal = "0"
typ = args.Uint32Type
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "bigserial":
nilVal = "0"
typ = "uint64"
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "real":
nilVal = "0.0"
typ = "float32"
if nullable {
nilVal = "sql.NullFloat64{}"
typ = "sql.NullFloat64"
}
case "numeric", "double precision":
nilVal = "0.0"
typ = "float64"
if nullable {
nilVal = "sql.NullFloat64{}"
typ = "sql.NullFloat64"
}
case "bytea":
asSlice = true
typ = "byte"
case "date", "timestamp with time zone", "time with time zone", "time without time zone", "timestamp without time zone":
nilVal = "time.Time{}"
typ = "time.Time"
if nullable {
nilVal = "pq.NullTime{}"
typ = "pq.NullTime"
}
case "interval":
typ = "*time.Duration"
case `"char"`, "bit":
// FIXME: this needs to actually be tested ...
// i think this should be 'rune' but I don't think database/sql
// supports 'rune' as a type?
//
// this is mainly here because postgres's pg_catalog.* meta tables have
// this as a type.
//typ = "rune"
nilVal = `uint8(0)`
typ = "uint8"
case `"any"`, "bit varying":
asSlice = true
typ = "byte"
case "hstore":
typ = "hstore.Hstore"
case "uuid":
nilVal = "uuid.New()"
typ = "uuid.UUID"
default:
if strings.HasPrefix(dt, args.Schema+".") {
// in the same schema, so chop off
typ = snaker.SnakeToCamelIdentifier(dt[len(args.Schema)+1:])
nilVal = typ + "(0)"
} else {
typ = snaker.SnakeToCamelIdentifier(dt)
nilVal = typ + "{}"
}
}
// special case for []slice
if typ == "string" && asSlice {
return precision, "StringSlice{}", "StringSlice"
}
// correct type if slice
if asSlice {
typ = "[]" + typ
nilVal = "nil"
}
return precision, nilVal, typ
}
// pgQueryStripRE is the regexp to match the '::type AS name' portion in a query,
// which is a quirk/requirement of generating queries as is done in this
// package.
var pgQueryStripRE = regexp.MustCompile(`(?i)::[a-z][a-z0-9_\.]+\s+AS\s+[a-z][a-z0-9_\.]+`)
// PgQueryStrip strips stuff.
func PgQueryStrip(query []string, queryComments []string) {
for i, l := range query {
pos := pgQueryStripRE.FindStringIndex(l)
if pos != nil {
query[i] = l[:pos[0]] + l[pos[1]:]
queryComments[i+1] = l[pos[0]:pos[1]]
} else {
queryComments[i+1] = ""
}
}
}
// PgTables returns the Postgres tables with the manual PK information added.
// ManualPk is true when the table does not have a sequence defined.
func PgTables(db models.XODB, schema string, relkind string) ([]*models.Table, error) {
var err error
// get the tables
rows, err := models.PgTables(db, schema, relkind)
if err != nil {
return nil, err
}
// Get the tables that have a sequence defined.
sequences, err := models.PgSequences(db, schema)
if err != nil {
// Set it to an empty set on error.
sequences = []*models.Sequence{}
}
// Add information about manual FK.
var tables []*models.Table
for _, row := range rows {
manualPk := true
// Look for a match in the table name where it contains the sequence
for _, sequence := range sequences {
if sequence.TableName == row.TableName {
manualPk = false
}
}
tables = append(tables, &models.Table{
TableName: row.TableName,
Type: row.Type,
ManualPk: manualPk,
})
}
return tables, nil
}
// PgQueryColumns parses the query and generates a type for it.
func PgQueryColumns(args *internal.ArgType, inspect []string) ([]*models.Column, error) {
var err error
// create temporary view xoid
xoid := "_xo_" + internal.GenRandomID()
viewq := `CREATE TEMPORARY VIEW ` + xoid + ` AS (` + strings.Join(inspect, "\n") + `)`
models.XOLog(viewq)
_, err = args.DB.Exec(viewq)
if err != nil {
return nil, err
}
// query to determine schema name where temporary view was created
var nspq = `SELECT n.nspname ` +
`FROM pg_class c ` +
`JOIN pg_namespace n ON n.oid = c.relnamespace ` +
`WHERE n.nspname LIKE 'pg_temp%' AND c.relname = $1`
// run query
var schema string
models.XOLog(nspq, xoid)
err = args.DB.QueryRow(nspq, xoid).Scan(&schema)
if err != nil {
return nil, err
}
// load column information
return models.PgTableColumns(args.DB, schema, xoid, false)
}
// PgIndexColumns returns the column list for an index.
func PgIndexColumns(db models.XODB, schema string, table string, index string) ([]*models.IndexColumn, error) {
var err error
// load columns
cols, err := models.PgIndexColumns(db, schema, index)
if err != nil {
return nil, err
}
// load col order
colOrd, err := models.PgGetColOrder(db, schema, index)
if err != nil {
return nil, err
}
// build schema name used in errors
s := schema
if s != "" {
s = s + "."
}
// put cols in order using colOrder
ret := []*models.IndexColumn{}
for _, v := range strings.Split(colOrd.Ord, " ") {
cid, err := strconv.Atoi(v)
if err != nil {
return nil, fmt.Errorf("could not convert %s%s index %s column %s to int", s, table, index, v)
}
// find column
found := false
var c *models.IndexColumn
for _, ic := range cols {
if cid == ic.Cid {
found = true
c = ic
break
}
}
// sanity check
if !found {
return nil, fmt.Errorf("could not find %s%s index %s column id %d", s, table, index, cid)
}
ret = append(ret, c)
}
return ret, nil
}