/
postgresql_type.go
102 lines (77 loc) · 2.28 KB
/
postgresql_type.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
package kotlin
import (
"log"
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
"github.com/kyleconroy/sqlc/internal/plugin"
)
func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) (string, bool) {
columnType := sdk.DataType(col.Type)
switch columnType {
case "serial", "pg_catalog.serial4":
return "Int", false
case "bigserial", "pg_catalog.serial8":
return "Long", false
case "smallserial", "pg_catalog.serial2":
return "Short", false
case "integer", "int", "int4", "pg_catalog.int4":
return "Int", false
case "bigint", "pg_catalog.int8":
return "Long", false
case "smallint", "pg_catalog.int2":
return "Short", false
case "float", "double precision", "pg_catalog.float8":
return "Double", false
case "real", "pg_catalog.float4":
return "Float", false
case "pg_catalog.numeric":
return "java.math.BigDecimal", false
case "bool", "pg_catalog.bool":
return "Boolean", false
case "jsonb":
// TODO: support json and byte types
return "String", false
case "bytea", "blob", "pg_catalog.bytea":
return "String", false
case "date":
// Date and time mappings from https://jdbc.postgresql.org/documentation/head/java8-date-time.html
return "LocalDate", false
case "pg_catalog.time", "pg_catalog.timetz":
return "LocalTime", false
case "pg_catalog.timestamp":
return "LocalDateTime", false
case "pg_catalog.timestamptz", "timestamptz":
// TODO
return "OffsetDateTime", false
case "text", "pg_catalog.varchar", "pg_catalog.bpchar", "string":
return "String", false
case "uuid":
return "UUID", false
case "inet":
// TODO
return "net.IP", false
case "void":
// TODO
// A void value always returns NULL. Since there is no built-in NULL
// value into the SQL package, we'll use sql.NullBool
return "sql.NullBool", false
case "any":
// TODO
return "Any", false
default:
for _, schema := range req.Catalog.Schemas {
if schema.Name == "pg_catalog" {
continue
}
for _, enum := range schema.Enums {
if columnType == enum.Name {
if schema.Name == req.Catalog.DefaultSchema {
return dataClassName(enum.Name, req.Settings), true
}
return dataClassName(schema.Name+"_"+enum.Name, req.Settings), true
}
}
}
log.Printf("unknown PostgreSQL type: %s\n", columnType)
return "Any", false
}
}