forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema.go
144 lines (125 loc) · 3.44 KB
/
schema.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
package translators
import (
"fmt"
"strings"
"github.com/gobuffalo/pop/fizz"
"github.com/jmoiron/sqlx"
)
type SchemaQuery interface {
ReplaceSchema(map[string]*fizz.Table)
Build() error
TableInfo(string) (*fizz.Table, error)
ReplaceColumn(table string, oldColumn string, newColumn fizz.Column) error
ColumnInfo(table string, column string) (*fizz.Column, error)
IndexInfo(table string, idx string) (*fizz.Index, error)
Delete(string)
SetTable(*fizz.Table)
DeleteColumn(string, string)
}
type Schema struct {
schema map[string]*fizz.Table
Builder SchemaQuery
Name string
URL string
db *sqlx.DB
}
func CreateSchema(name string, url string, schema map[string]*fizz.Table) Schema {
return Schema{
Name: name,
URL: url,
schema: schema,
}
}
func (s *Schema) ReplaceSchema(newSchema map[string]*fizz.Table) {
s.schema = newSchema
}
func (s *Schema) Build() error {
return fmt.Errorf("Build not implemented for this translator!")
}
func (s *Schema) TableInfo(table string) (*fizz.Table, error) {
if ti, ok := s.schema[table]; ok {
return ti, nil
}
if s.Builder != nil {
err := s.Builder.Build()
if err != nil {
return nil, err
}
} else {
err := s.Build()
if err != nil {
return nil, err
}
}
if ti, ok := s.schema[table]; ok {
return ti, nil
}
return nil, fmt.Errorf("Could not find table data for %s!", table)
}
func (s *Schema) ReplaceColumn(table string, oldColumn string, newColumn fizz.Column) error {
tableInfo, err := s.TableInfo(table)
if err != nil {
return err
}
for i, col := range tableInfo.Columns {
if strings.ToLower(strings.TrimSpace(col.Name)) == strings.ToLower(strings.TrimSpace(oldColumn)) {
tableInfo.Columns[i] = newColumn
return nil
}
}
return fmt.Errorf("Could not find column(%s) in table(%s)!", oldColumn, table)
}
func (s *Schema) ColumnInfo(table string, column string) (*fizz.Column, error) {
ti, err := s.TableInfo(table)
if err != nil {
return nil, err
}
if ci, ok := s.findColumnInfo(ti, column); ok {
return ci, nil
}
return nil, fmt.Errorf("Could not find column data for %s in table %s!", column, table)
}
func (s *Schema) IndexInfo(table string, idx string) (*fizz.Index, error) {
ti, err := s.TableInfo(table)
if err != nil {
return nil, err
}
if i, ok := s.findIndexInfo(ti, idx); ok {
return i, nil
}
return nil, fmt.Errorf("Could not find index data for %s in table %s!", idx, table)
}
func (s *Schema) Delete(table string) {
delete(s.schema, table)
}
func (s *Schema) SetTable(table *fizz.Table) {
s.schema[table.Name] = table
}
func (s *Schema) DeleteColumn(table string, column string) {
tableInfo, err := s.TableInfo(table)
if err != nil {
return
}
for i, col := range tableInfo.Columns {
if strings.ToLower(strings.TrimSpace(col.Name)) == strings.ToLower(strings.TrimSpace(column)) {
tableInfo.Columns = append(tableInfo.Columns[:i], tableInfo.Columns[i+1:]...)
return
}
}
}
func (s *Schema) findColumnInfo(tableInfo *fizz.Table, column string) (*fizz.Column, bool) {
for _, col := range tableInfo.Columns {
if strings.ToLower(strings.TrimSpace(col.Name)) == strings.ToLower(strings.TrimSpace(column)) {
return &col, true
}
}
return nil, false
}
func (s *Schema) findIndexInfo(tableInfo *fizz.Table, index string) (*fizz.Index, bool) {
for _, ind := range tableInfo.Indexes {
if strings.ToLower(strings.TrimSpace(ind.Name)) == strings.ToLower(strings.TrimSpace(index)) {
return &ind, true
}
}
return nil, false
}