/
ddl.go
96 lines (77 loc) · 1.91 KB
/
ddl.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
package codegen
import (
"fmt"
"strings"
"github.com/iancoleman/strcase"
)
var PostgresSchema = "%%SCHEMA%%"
var fieldTypesNotNullable = map[string]string{
"String": "text not null",
"Boolean": "boolean not null",
"Bytes": "bytea not null",
"Int": "numeric not null",
"Float": "numeric not null",
"BigInt": "numeric not null",
"BigDecimal": "numeric not null",
}
var fieldTypesNullable = map[string]string{
"String": "text",
"Boolean": "boolean",
"Bytes": "bytea",
"Int": "numeric",
"Float": "numeric",
"BigInt": "numeric",
"BigDecimal": "numeric",
}
func (f *Field) getPostgresType() string {
switch {
case f.Nullable:
if dt, ok := fieldTypesNullable[f.Type]; ok {
return dt
}
default:
if dt, ok := fieldTypesNotNullable[f.Type]; ok {
return dt
}
}
if f.Array {
if f.Nullable {
return "text[]"
}
return "text[] not null"
}
if _, ok := enumTypes[f.Type]; ok {
return fmt.Sprintf(`%s."%s"`, PostgresSchema, strings.ToLower(strcase.ToSnake(f.Name)))
}
//this should be an ID
if f.Nullable {
return "text"
}
return "text not null"
}
func (f *Field) getPostgresIndex() string {
//CREATE INDEX attr_4_5_transaction_swaps ON sgd3.transaction USING gin (swaps);
_, ok := map[string]bool{
"ID": true,
"Boolean": true,
"Bytes": true,
"Int": true,
"Float": true,
"BigInt": true,
"BigDecimal": true,
}[f.Type]
snakeName := fmt.Sprintf(`"%s"`, strings.ToLower(strcase.ToSnake(f.Name)))
if ok {
return fmt.Sprintf("using btree (%s)", snakeName)
}
if f.Type == "String" {
return fmt.Sprintf(`("left"(%s, 256))`, snakeName)
}
if f.Array {
return fmt.Sprintf("using gin (%s)", strings.ToLower(strcase.ToSnake(f.Name)))
}
if _, ok = enumTypes[f.Type]; ok {
return fmt.Sprintf("using btree (%s)", snakeName)
}
return fmt.Sprintf("using gist (%s, block_range)", snakeName)
}