This repository has been archived by the owner on Aug 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
dialect.go
287 lines (244 loc) · 6.58 KB
/
dialect.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
package schema
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/modern-go/reflect2"
)
type DialectType string
type Dialect interface {
// PrimaryKeys returns the primary keys of table according to dialect
PrimaryKeys(t *Table) []string
// Columns returns the columns of table according to dialect
Columns(t *Table) ColumnList
// Constraints returns constraint definitions for table, according to dialect
Constraints(t, parent *Table) []string
// Extra returns additional definitions for table outside the CREATE TABLE statement, according to dialect
Extra(t, parent *Table) []string
// DBTypeFromType returns the database type from the given ValueType. Always lowercase.
DBTypeFromType(v ValueType) string
// GetResourceValues will return column values from the resource, ready to go in pgx.CopyFromSlice
GetResourceValues(r *Resource) ([]interface{}, error)
}
type PostgresDialect struct{}
type TSDBDialect struct {
pg PostgresDialect
}
const (
Postgres = DialectType("postgres")
TSDB = DialectType("timescale")
)
var (
_ Dialect = (*PostgresDialect)(nil)
_ Dialect = (*TSDBDialect)(nil)
)
func (t DialectType) MigrationDirectory() string {
return string(t)
}
// GetDialect creates and returns a dialect specified by the DialectType
func GetDialect(t DialectType) (Dialect, error) {
switch t {
case Postgres:
return PostgresDialect{}, nil
case TSDB:
return TSDBDialect{}, nil
default:
return nil, fmt.Errorf("unknown dialect %q", t)
}
}
func (PostgresDialect) PrimaryKeys(t *Table) []string {
if len(t.Options.PrimaryKeys) > 0 {
return t.Options.PrimaryKeys
}
return []string{cqIdColumn.Name}
}
func (PostgresDialect) Columns(t *Table) ColumnList {
return append([]Column{cqIdColumn, cqMeta}, t.Columns...)
}
func (d PostgresDialect) Constraints(t, parent *Table) []string {
ret := make([]string, 0, len(t.Columns))
ret = append(ret, fmt.Sprintf("CONSTRAINT %s_pk PRIMARY KEY(%s)", truncatePKConstraint(t.Name), strings.Join(d.PrimaryKeys(t), ",")))
for _, c := range d.Columns(t) {
if !c.CreationOptions.Unique {
continue
}
ret = append(ret, fmt.Sprintf("UNIQUE(%s)", c.Name))
}
if parent != nil {
pc := findParentIdColumn(t)
if pc != nil {
ret = append(ret, fmt.Sprintf("FOREIGN KEY (%s) REFERENCES %s(%s) ON DELETE CASCADE", pc.Name, parent.Name, cqIdColumn.Name))
}
}
return ret
}
func (PostgresDialect) Extra(_, _ *Table) []string {
return nil
}
func (PostgresDialect) DBTypeFromType(v ValueType) string {
switch v {
case TypeBool:
return "boolean"
case TypeInt:
return "integer"
case TypeBigInt:
return "bigint"
case TypeSmallInt:
return "smallint"
case TypeFloat:
return "float"
case TypeUUID:
return "uuid"
case TypeString:
return "text"
case TypeJSON:
return "jsonb"
case TypeIntArray:
return "integer[]"
case TypeStringArray:
return "text[]"
case TypeTimestamp:
return "timestamp without time zone"
case TypeByteArray:
return "bytea"
case TypeInvalid:
fallthrough
case TypeInet:
return "inet"
case TypeMacAddr:
return "mac"
case TypeInetArray:
return "inet[]"
case TypeMacAddrArray:
return "mac[]"
case TypeCIDR:
return "cidr"
case TypeCIDRArray:
return "cidr[]"
default:
panic("invalid type")
}
}
func (d PostgresDialect) GetResourceValues(r *Resource) ([]interface{}, error) {
return doResourceValues(d, r)
}
func (d TSDBDialect) PrimaryKeys(t *Table) []string {
return append([]string{cqFetchDateColumn.Name}, d.pg.PrimaryKeys(t)...)
}
func (TSDBDialect) Columns(t *Table) ColumnList {
return append([]Column{cqIdColumn, cqMeta, cqFetchDateColumn}, t.Columns...)
}
func (d TSDBDialect) Constraints(t, _ *Table) []string {
ret := make([]string, 0, len(t.Columns))
ret = append(ret, fmt.Sprintf("CONSTRAINT %s_pk PRIMARY KEY(%s)", truncatePKConstraint(t.Name), strings.Join(d.PrimaryKeys(t), ",")))
for _, c := range d.Columns(t) {
if !c.CreationOptions.Unique {
continue
}
ret = append(ret, fmt.Sprintf("UNIQUE(%s,%s)", cqFetchDateColumn.Name, c.Name))
}
return ret
}
func (TSDBDialect) Extra(t, parent *Table) []string {
pc := findParentIdColumn(t)
if parent == nil || pc == nil {
return []string{
fmt.Sprintf("SELECT setup_tsdb_parent('%s');", t.Name),
}
}
return []string{
fmt.Sprintf("CREATE INDEX ON %s (%s, %s);", t.Name, cqFetchDateColumn.Name, pc.Name),
fmt.Sprintf("SELECT setup_tsdb_child('%s', '%s', '%s', '%s');", t.Name, pc.Name, parent.Name, cqIdColumn.Name),
}
}
func (d TSDBDialect) DBTypeFromType(v ValueType) string {
return d.pg.DBTypeFromType(v)
}
func (d TSDBDialect) GetResourceValues(r *Resource) ([]interface{}, error) {
return doResourceValues(d, r)
}
func doResourceValues(dialect Dialect, r *Resource) ([]interface{}, error) {
values := make([]interface{}, 0)
for _, c := range dialect.Columns(r.table) {
v := r.Get(c.Name)
if err := c.ValidateType(v); err != nil {
return nil, err
}
switch c.Type {
case TypeJSON:
if v == nil {
values = append(values, v)
continue
}
if reflect2.TypeOf(v).Kind() == reflect.Map {
values = append(values, v)
continue
}
switch data := v.(type) {
case map[string]interface{}:
values = append(values, data)
case string:
newV := make(map[string]interface{})
err := json.Unmarshal([]byte(data), &newV)
if err != nil {
return nil, err
}
values = append(values, newV)
case *string:
var newV interface{}
err := json.Unmarshal([]byte(*data), &newV)
if err != nil {
return nil, err
}
values = append(values, newV)
case []byte:
var newV interface{}
err := json.Unmarshal(data, &newV)
if err != nil {
return nil, err
}
values = append(values, newV)
default:
d, err := json.Marshal(data)
if err != nil {
return nil, err
}
var newV interface{}
err = json.Unmarshal(d, &newV)
if err != nil {
return nil, err
}
values = append(values, newV)
}
case TypeInt:
switch data := v.(type) {
case int64:
values = append(values, int32(data))
default:
values = append(values, data)
}
default:
values = append(values, v)
}
}
return values, nil
}
func findParentIdColumn(t *Table) (ret *Column) {
for _, c := range t.Columns {
if c.Meta().Resolver != nil && c.Meta().Resolver.Name == "schema.ParentIdResolver" {
return &c
}
}
return nil
}
func truncatePKConstraint(name string) string {
const (
// MaxTableLength in postgres is 63 when building _fk or _pk we want to truncate the name to 60 chars max
maxTableNamePKConstraint = 60
)
if len(name) > maxTableNamePKConstraint {
return name[:maxTableNamePKConstraint]
}
return name
}