Skip to content

Commit 46428da

Browse files
Suman DeSuman De
authored andcommitted
refactor parseDriver so that we can reuse the PGXV5
1 parent 4b4596e commit 46428da

File tree

2 files changed

+53
-40
lines changed

2 files changed

+53
-40
lines changed

internal/codegen/golang/driver.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,16 @@ func parseDriver(sqlPackage string) opts.SQLDriver {
1414
return opts.SQLDriverLibPQ
1515
}
1616
}
17+
18+
func parseDriverPGType(sqlPackage string) opts.SQLDriver {
19+
switch sqlPackage {
20+
case opts.SQLPackagePGXV4:
21+
return opts.SQLDriverPGXV4
22+
case opts.SQLPackagePGXV5:
23+
return opts.SQLDriverPGXV5
24+
case opts.SQLPackageYugaBytePGXV5:
25+
return opts.SQLDriverPGXV5
26+
default:
27+
return opts.SQLDriverLibPQ
28+
}
29+
}

internal/codegen/golang/postgresql_type.go

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func parseIdentifierString(name string) (*plugin.Identifier, error) {
3737
func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
3838
columnType := sdk.DataType(col.Type)
3939
notNull := col.NotNull || col.IsArray
40-
driver := parseDriver(options.SqlPackage)
40+
driver := parseDriverPGType(options.SqlPackage)
4141
emitPointersForNull := driver.IsPGX() && options.EmitPointersForNullTypes
4242

4343
switch columnType {
@@ -48,7 +48,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
4848
if emitPointersForNull {
4949
return "*int32"
5050
}
51-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
51+
if driver == opts.SQLDriverPGXV5 {
5252
return "pgtype.Int4"
5353
}
5454
return "sql.NullInt32"
@@ -60,7 +60,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
6060
if emitPointersForNull {
6161
return "*int64"
6262
}
63-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
63+
if driver == opts.SQLDriverPGXV5 {
6464
return "pgtype.Int8"
6565
}
6666
return "sql.NullInt64"
@@ -72,7 +72,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
7272
if emitPointersForNull {
7373
return "*int16"
7474
}
75-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
75+
if driver == opts.SQLDriverPGXV5 {
7676
return "pgtype.Int2"
7777
}
7878
return "sql.NullInt16"
@@ -84,7 +84,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
8484
if emitPointersForNull {
8585
return "*int32"
8686
}
87-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
87+
if driver == opts.SQLDriverPGXV5 {
8888
return "pgtype.Int4"
8989
}
9090
return "sql.NullInt32"
@@ -96,7 +96,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
9696
if emitPointersForNull {
9797
return "*int64"
9898
}
99-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
99+
if driver == opts.SQLDriverPGXV5 {
100100
return "pgtype.Int8"
101101
}
102102
return "sql.NullInt64"
@@ -108,7 +108,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
108108
if emitPointersForNull {
109109
return "*int16"
110110
}
111-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
111+
if driver == opts.SQLDriverPGXV5 {
112112
return "pgtype.Int2"
113113
}
114114
return "sql.NullInt16"
@@ -120,7 +120,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
120120
if emitPointersForNull {
121121
return "*float64"
122122
}
123-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
123+
if driver == opts.SQLDriverPGXV5 {
124124
return "pgtype.Float8"
125125
}
126126
return "sql.NullFloat64"
@@ -132,7 +132,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
132132
if emitPointersForNull {
133133
return "*float32"
134134
}
135-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
135+
if driver == opts.SQLDriverPGXV5 {
136136
return "pgtype.Float4"
137137
}
138138
return "sql.NullFloat64" // TODO: Change to sql.NullFloat32 after updating the go.mod file
@@ -160,14 +160,14 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
160160
if emitPointersForNull {
161161
return "*bool"
162162
}
163-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
163+
if driver == opts.SQLDriverPGXV5 {
164164
return "pgtype.Bool"
165165
}
166166
return "sql.NullBool"
167167

168168
case "json", "pg_catalog.json":
169169
switch driver {
170-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
170+
case opts.SQLDriverPGXV5:
171171
return "[]byte"
172172
case opts.SQLDriverPGXV4:
173173
return "pgtype.JSON"
@@ -183,7 +183,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
183183

184184
case "jsonb", "pg_catalog.jsonb":
185185
switch driver {
186-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
186+
case opts.SQLDriverPGXV5:
187187
return "[]byte"
188188
case opts.SQLDriverPGXV4:
189189
return "pgtype.JSONB"
@@ -201,7 +201,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
201201
return "[]byte"
202202

203203
case "date":
204-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
204+
if driver == opts.SQLDriverPGXV5 {
205205
return "pgtype.Date"
206206
}
207207
if notNull {
@@ -213,7 +213,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
213213
return "sql.NullTime"
214214

215215
case "pg_catalog.time":
216-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
216+
if driver == opts.SQLDriverPGXV5 {
217217
return "pgtype.Time"
218218
}
219219
if notNull {
@@ -234,7 +234,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
234234
return "sql.NullTime"
235235

236236
case "pg_catalog.timestamp", "timestamp":
237-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
237+
if driver == opts.SQLDriverPGXV5 {
238238
return "pgtype.Timestamp"
239239
}
240240
if notNull {
@@ -246,7 +246,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
246246
return "sql.NullTime"
247247

248248
case "pg_catalog.timestamptz", "timestamptz":
249-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
249+
if driver == opts.SQLDriverPGXV5 {
250250
return "pgtype.Timestamptz"
251251
}
252252
if notNull {
@@ -264,13 +264,13 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
264264
if emitPointersForNull {
265265
return "*string"
266266
}
267-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
267+
if driver == opts.SQLDriverPGXV5 {
268268
return "pgtype.Text"
269269
}
270270
return "sql.NullString"
271271

272272
case "uuid":
273-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
273+
if driver == opts.SQLDriverPGXV5 {
274274
return "pgtype.UUID"
275275
}
276276
if notNull {
@@ -283,7 +283,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
283283

284284
case "inet":
285285
switch driver {
286-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
286+
case opts.SQLDriverPGXV5:
287287
if notNull {
288288
return "netip.Addr"
289289
}
@@ -298,7 +298,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
298298

299299
case "cidr":
300300
switch driver {
301-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
301+
case opts.SQLDriverPGXV5:
302302
if notNull {
303303
return "netip.Prefix"
304304
}
@@ -313,7 +313,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
313313

314314
case "macaddr", "macaddr8":
315315
switch driver {
316-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
316+
case opts.SQLDriverPGXV5:
317317
return "net.HardwareAddr"
318318
case opts.SQLDriverPGXV4:
319319
return "pgtype.Macaddr"
@@ -335,13 +335,13 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
335335
if emitPointersForNull {
336336
return "*string"
337337
}
338-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
338+
if driver == opts.SQLDriverPGXV5 {
339339
return "pgtype.Text"
340340
}
341341
return "sql.NullString"
342342

343343
case "interval", "pg_catalog.interval":
344-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
344+
if driver == opts.SQLDriverPGXV5 {
345345
return "pgtype.Interval"
346346
}
347347
if notNull {
@@ -356,15 +356,15 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
356356
switch driver {
357357
case opts.SQLDriverPGXV4:
358358
return "pgtype.Daterange"
359-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
359+
case opts.SQLDriverPGXV5:
360360
return "pgtype.Range[pgtype.Date]"
361361
default:
362362
return "interface{}"
363363
}
364364

365365
case "datemultirange":
366366
switch driver {
367-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
367+
case opts.SQLDriverPGXV5:
368368
return "pgtype.Multirange[pgtype.Range[pgtype.Date]]"
369369
default:
370370
return "interface{}"
@@ -374,15 +374,15 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
374374
switch driver {
375375
case opts.SQLDriverPGXV4:
376376
return "pgtype.Tsrange"
377-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
377+
case opts.SQLDriverPGXV5:
378378
return "pgtype.Range[pgtype.Timestamp]"
379379
default:
380380
return "interface{}"
381381
}
382382

383383
case "tsmultirange":
384384
switch driver {
385-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
385+
case opts.SQLDriverPGXV5:
386386
return "pgtype.Multirange[pgtype.Range[pgtype.Timestamp]]"
387387
default:
388388
return "interface{}"
@@ -392,15 +392,15 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
392392
switch driver {
393393
case opts.SQLDriverPGXV4:
394394
return "pgtype.Tstzrange"
395-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
395+
case opts.SQLDriverPGXV5:
396396
return "pgtype.Range[pgtype.Timestamptz]"
397397
default:
398398
return "interface{}"
399399
}
400400

401401
case "tstzmultirange":
402402
switch driver {
403-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
403+
case opts.SQLDriverPGXV5:
404404
return "pgtype.Multirange[pgtype.Range[pgtype.Timestamptz]]"
405405
default:
406406
return "interface{}"
@@ -410,15 +410,15 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
410410
switch driver {
411411
case opts.SQLDriverPGXV4:
412412
return "pgtype.Numrange"
413-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
413+
case opts.SQLDriverPGXV5:
414414
return "pgtype.Range[pgtype.Numeric]"
415415
default:
416416
return "interface{}"
417417
}
418418

419419
case "nummultirange":
420420
switch driver {
421-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
421+
case opts.SQLDriverPGXV5:
422422
return "pgtype.Multirange[pgtype.Range[pgtype.Numeric]]"
423423
default:
424424
return "interface{}"
@@ -428,15 +428,15 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
428428
switch driver {
429429
case opts.SQLDriverPGXV4:
430430
return "pgtype.Int4range"
431-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
431+
case opts.SQLDriverPGXV5:
432432
return "pgtype.Range[pgtype.Int4]"
433433
default:
434434
return "interface{}"
435435
}
436436

437437
case "int4multirange":
438438
switch driver {
439-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
439+
case opts.SQLDriverPGXV5:
440440
return "pgtype.Multirange[pgtype.Range[pgtype.Int4]]"
441441
default:
442442
return "interface{}"
@@ -446,15 +446,15 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
446446
switch driver {
447447
case opts.SQLDriverPGXV4:
448448
return "pgtype.Int8range"
449-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
449+
case opts.SQLDriverPGXV5:
450450
return "pgtype.Range[pgtype.Int8]"
451451
default:
452452
return "interface{}"
453453
}
454454

455455
case "int8multirange":
456456
switch driver {
457-
case opts.SQLDriverPGXV5, opts.SQLDriverYugaBytePGXV5:
457+
case opts.SQLDriverPGXV5:
458458
return "pgtype.Multirange[pgtype.Range[pgtype.Int8]]"
459459
default:
460460
return "interface{}"
@@ -467,23 +467,23 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
467467
return "interface{}"
468468

469469
case "bit", "varbit", "pg_catalog.bit", "pg_catalog.varbit":
470-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
470+
if driver == opts.SQLDriverPGXV5 {
471471
return "pgtype.Bits"
472472
}
473473
if driver == opts.SQLDriverPGXV4 {
474474
return "pgtype.Varbit"
475475
}
476476

477477
case "cid":
478-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
478+
if driver == opts.SQLDriverPGXV5 {
479479
return "pgtype.Uint32"
480480
}
481481
if driver == opts.SQLDriverPGXV4 {
482482
return "pgtype.CID"
483483
}
484484

485485
case "oid":
486-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
486+
if driver == opts.SQLDriverPGXV5 {
487487
return "pgtype.Uint32"
488488
}
489489
if driver == opts.SQLDriverPGXV4 {
@@ -496,7 +496,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
496496
}
497497

498498
case "xid":
499-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
499+
if driver == opts.SQLDriverPGXV5 {
500500
return "pgtype.Uint32"
501501
}
502502
if driver == opts.SQLDriverPGXV4 {
@@ -539,7 +539,7 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
539539
}
540540

541541
case "vector":
542-
if driver == opts.SQLDriverPGXV5 || driver == opts.SQLDriverYugaBytePGXV5 {
542+
if driver == opts.SQLDriverPGXV5 {
543543
if emitPointersForNull {
544544
return "*pgvector.Vector"
545545
} else {

0 commit comments

Comments
 (0)