-
Notifications
You must be signed in to change notification settings - Fork 0
/
column.go
190 lines (145 loc) · 3.22 KB
/
column.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
package ddltable
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
)
type column struct {
Name string
PGType string
DefaultValue string
IndexName string
IndexType string
OrderNumber uint
IsPK bool
IsNullable bool
IsUnique bool
IsIndexed bool
IsToBeSkipped bool
}
func newColumns(rootTagName string, object any) (columns, string, error) {
result := make([]*column, 0)
var alreadyHavePK bool
var tableName string
for i := 0; i < reflect.TypeOf(object).Elem().NumField(); i++ {
fieldRoot := reflect.TypeOf(object).
Elem().
FieldByIndex([]int{i})
column := column{
Name: fieldRoot.Name,
OrderNumber: uint(i),
}
if valueTag, hasTag := fieldRoot.Tag.Lookup(rootTagName); hasTag {
errUpdate := column.UpdateWith(valueTag, alreadyHavePK)
if errUpdate != nil {
if errUpdate.Error() == errIsOverrideTableName.Error() {
tableName = strings.ToLower(fieldRoot.Name)
continue
}
return nil, "",
errUpdate
}
}
if !fieldRoot.IsExported() {
continue
}
var isNullable bool
column.PGType, isNullable = reflectToPG(fieldRoot.Type.String(), column.IsPK)
if isNullable {
column.IsNullable = true
}
if column.IsPK {
alreadyHavePK = true
}
result = append(result, &column)
}
return result,
tableName,
nil
}
func (col *column) UpdateWith(tagValues string, alreadyHavePK bool) error {
for _, tagValue := range strings.Split(
tagValues, ",",
) {
tagClean := strings.ToLower(
strings.TrimSpace(tagValue),
)
var compoundTagValue string
if strings.Contains(tagClean, _TagSeparator) {
tagCompound := strings.Split(tagClean, _TagSeparator)
if len(tagCompound) != 2 {
return fmt.Errorf(
"malformed tag value: %s",
tagClean,
)
}
tagClean = tagCompound[0]
compoundTagValue = tagCompound[1]
}
switch tagClean {
case "":
return nil
case "-":
col.IsToBeSkipped = true
return nil
case _TagOverrideTableName:
return errIsOverrideTableName
case _TagPK:
if alreadyHavePK {
return errors.New("more than one primary key field detected. max is 1")
}
col.IsPK = true
case _TagOverrideOrder:
order, errConv := strconv.Atoi(compoundTagValue)
if errConv != nil {
return fmt.Errorf(
"override order tag: %w",
errConv,
)
}
col.OrderNumber = uint(order)
case _TagIndexName:
col.IsIndexed = true
col.IndexName = compoundTagValue
case _TagIndexUnique:
col.IsIndexed = true
col.IndexType = _indexUnique
case _TagRequired:
col.IsNullable = false
case _TagDefault:
col.DefaultValue = compoundTagValue
case _TagOverrideColumnName:
col.Name = compoundTagValue
}
}
return nil
}
func (col *column) AsDDLPostgres() string {
if col.IsToBeSkipped {
return ""
}
result := []string{
strings.ToLower(col.Name),
}
result = append(result,
col.PGType,
)
if col.IsPK {
result = append(result,
_SQLPrimaryKey,
)
}
if col.IsUnique {
result = append(result, "UNIQUE")
}
if !col.IsNullable {
result = append(result, _SQLNotNull)
}
if len(col.DefaultValue) > 0 {
result = append(result, "DEFAULT")
result = append(result, col.DefaultValue)
}
return strings.Join(result, " ")
}