/
constraints.go
110 lines (98 loc) · 3.34 KB
/
constraints.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
package geneddl
import (
"fmt"
"strings"
"github.com/cihangir/schema"
"github.com/cihangir/stringext"
)
// DefineConstraints creates constraints definition for tables
func DefineConstraints(settings schema.Generator, s *schema.Schema) ([]byte, error) {
primaryKeyConstraint := ""
primaryKey := settings.Get("primaryKey")
if primaryKey != nil {
pmi := primaryKey.([]interface{})
if len(pmi) > 0 {
sl := make([]string, len(pmi))
for i, pm := range pmi {
sl[i] = stringext.ToFieldName(pm.(string))
}
primaryKeyConstraint = fmt.Sprintf(
"ALTER TABLE %q.%q ADD PRIMARY KEY (%q) NOT DEFERRABLE INITIALLY IMMEDIATE;\n",
settings.Get("schemaName"),
settings.Get("tableName"),
strings.Join(sl, ", "),
)
primaryKeyConstraint = fmt.Sprintf("-------------------------------\n-- Primary key structure for table %s\n-- ----------------------------\n%s",
settings.Get("tableName"),
primaryKeyConstraint,
)
}
}
uniqueKeyConstraints := ""
uniqueKeys := settings.Get("uniqueKeys")
if uniqueKeys != nil {
ukci := uniqueKeys.([]interface{})
if len(ukci) > 0 {
for _, ukc := range ukci {
ukcs := ukc.([]interface{})
ukcsps := make([]string, len(ukcs))
for i, ukc := range ukcs {
ukcsps[i] = stringext.ToFieldName(ukc.(string))
}
keyName := fmt.Sprintf(
"%s_%s_%s",
stringext.ToFieldName("key"),
stringext.ToFieldName(settings.Get("tableName").(string)),
strings.Join(ukcsps, "_"),
)
uniqueKeyConstraints += fmt.Sprintf(
"ALTER TABLE %q.%q ADD CONSTRAINT %q UNIQUE (\"%s\") NOT DEFERRABLE INITIALLY IMMEDIATE;\n",
settings.Get("schemaName"),
settings.Get("tableName"),
keyName,
strings.Join(ukcsps, "\", \""),
)
}
uniqueKeyConstraints = fmt.Sprintf("-------------------------------\n-- Unique key structure for table %s\n-- ----------------------------\n%s",
settings.Get("tableName"),
uniqueKeyConstraints,
)
}
}
foreignKeyConstraints := ""
foreignKeys := settings.Get("foreignKeys")
if foreignKeys != nil {
fkci := foreignKeys.([]interface{})
if len(fkci) > 0 {
for _, fkc := range fkci {
fkcs := fkc.([]interface{})
localField := stringext.ToFieldName(fkcs[0].(string))
refFields := strings.Split(fkcs[1].(string), ".")
if len(refFields) != 3 {
return nil, fmt.Errorf("need schemaName.tableName.fieldName")
}
keyName := fmt.Sprintf(
"%s_%s_%s",
stringext.ToFieldName("fkey"),
stringext.ToFieldName(settings.Get("tableName").(string)),
localField,
)
foreignKeyConstraints += fmt.Sprintf(
"ALTER TABLE %q.%q ADD CONSTRAINT %q FOREIGN KEY (\"%s\") REFERENCES %s.%s (%s) ON UPDATE NO ACTION ON DELETE NO ACTION NOT DEFERRABLE INITIALLY IMMEDIATE;\n",
settings.Get("schemaName"),
settings.Get("tableName"),
keyName,
localField,
stringext.ToFieldName(refFields[0]), // first item schema name
stringext.ToFieldName(refFields[1]), // second item table name
stringext.ToFieldName(refFields[2]), // third item field name
)
foreignKeyConstraints = fmt.Sprintf("-------------------------------\n-- Foreign keys structure for table %s\n-- ----------------------------\n%s",
settings.Get("tableName"),
foreignKeyConstraints,
)
}
}
}
return clean([]byte(primaryKeyConstraint + uniqueKeyConstraints + foreignKeyConstraints)), nil
}