-
Notifications
You must be signed in to change notification settings - Fork 553
/
Copy pathkeys.go
76 lines (59 loc) · 1.67 KB
/
keys.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
package drivers
import "fmt"
// PrimaryKey represents a primary key constraint in a database
type PrimaryKey struct {
Name string `json:"name"`
Columns []string `json:"columns"`
}
// ForeignKey represents a foreign key constraint in a database
type ForeignKey struct {
Table string `json:"table"`
Name string `json:"name"`
Column string `json:"column"`
Nullable bool `json:"nullable"`
Unique bool `json:"unique"`
ForeignTable string `json:"foreign_table"`
ForeignColumn string `json:"foreign_column"`
ForeignColumnNullable bool `json:"foreign_column_nullable"`
ForeignColumnUnique bool `json:"foreign_column_unique"`
}
// SQLColumnDef formats a column name and type like an SQL column definition.
type SQLColumnDef struct {
Name string
Type string
}
// String for fmt.Stringer
func (s SQLColumnDef) String() string {
return fmt.Sprintf("%s %s", s.Name, s.Type)
}
// SQLColumnDefs has small helper functions
type SQLColumnDefs []SQLColumnDef
// Names returns all the names
func (s SQLColumnDefs) Names() []string {
names := make([]string, len(s))
for i, sqlDef := range s {
names[i] = sqlDef.Name
}
return names
}
// Types returns all the types
func (s SQLColumnDefs) Types() []string {
types := make([]string, len(s))
for i, sqlDef := range s {
types[i] = sqlDef.Type
}
return types
}
// SQLColDefinitions creates a definition in sql format for a column
func SQLColDefinitions(cols []Column, names []string) SQLColumnDefs {
ret := make([]SQLColumnDef, len(names))
for i, n := range names {
for _, c := range cols {
if n != c.Name {
continue
}
ret[i] = SQLColumnDef{Name: n, Type: c.Type}
}
}
return ret
}