-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
181 lines (148 loc) · 4.6 KB
/
types.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
package schema
import (
"database/sql"
"errors"
"sort"
)
const (
RenameTable = "RENAME_TABLE"
CreateTable = "CREATE_TABLE"
DropTable = "DROP_TABLE"
ChangeColumn = "CHANGE_COLUMN"
AddColumn = "ADD_COLUMN"
DropColumn = "DROP_COLUMN"
AddIndex = "ADD_INDEX"
DropIndex = "DROP_INDEX"
ChangeCharacterSet = "CHANGE_CHARACTER_SET"
)
type SchemaList struct {
Schemas []*Schema `json:"schemas"`
}
// Schema represents a database
type Schema struct {
RunID int64 `json:"-"`
Name string `json:"name"`
SortedSetKeys []string `json:"-"`
Tables map[string]*Table `json:"tables"`
Enums map[string][]map[string]interface{} `json:"-"`
DefaultCharacterSet string `json:"defaultCharacterSet"`
DefaultCollation string `json:"defaultCollation"`
}
// ToSortedTables returns SortedTables
func (s *Schema) ToSortedTables() SortedTables {
sortedTables := make(SortedTables, 0, len(s.Tables))
for _, table := range s.Tables {
sortedTables = append(sortedTables, table)
}
sort.Sort(sortedTables)
return sortedTables
}
// FindTableByName finds a table by its name in the database
func (s *Schema) FindTableByName(tableName string) (table *Table, e error) {
// Search for table
for _, dbTable := range s.Tables {
if dbTable.Name == tableName {
table = dbTable
break
}
}
if table == nil {
e = errors.New("table not found")
}
return
}
// Table represents a table in a database
type Table struct {
Name string `json:"name"`
Engine string `json:"engine"`
Version int `json:"version"`
RowFormat string `json:"rowFormat"`
Rows int64 `json:"-"`
DataLength int64 `json:"-"`
Collation string `json:"collation"`
CharacterSet string `json:"characterSet"`
AutoIncrement int64 `json:"-"`
Columns map[string]*Column `json:"columns"`
}
// SortedColumns is a slice of Column objects
type SortedColumns []*Column
// Len is part of sort.Interface.
func (c SortedColumns) Len() int {
return len(c)
}
// Swap is part of sort.Interface.
func (c SortedColumns) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
// Less is part of sort.Interface. We use count as the value to sort by
func (c SortedColumns) Less(i, j int) bool {
return c[i].Position < c[j].Position
}
// ToSortedColumns returns SortedColumns
func (table *Table) ToSortedColumns() SortedColumns {
sortedColumns := make(SortedColumns, 0, len(table.Columns))
// Find the primary key
for _, column := range table.Columns {
sortedColumns = append(sortedColumns, column)
}
sort.Sort(sortedColumns)
return sortedColumns
}
// SortedTables is a slice of Table objects
type SortedTables []*Table
// Len is part of sort.Interface.
func (c SortedTables) Len() int {
return len(c)
}
// Swap is part of sort.Interface.
func (c SortedTables) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
// Less is part of sort.Interface. We use count as the value to sort by
func (c SortedTables) Less(i, j int) bool {
return c[i].Name < c[j].Name
}
// Column represents a column in a table
type Column struct {
Name string `json:"column"`
Position int `json:"position"`
Default string `json:"default"`
IsNullable bool `json:"isNullable"`
IsUnsigned bool `json:"isUnsigned"`
DataType string `json:"dataType"`
MaxLength int `json:"maxLength"`
Precision int `json:"precision"`
CharSet string `json:"charSet"`
Collation string `json:"collation"`
Type string `json:"type"`
ColumnKey string `json:"columnKey"`
NumericScale int `json:"numericScale"`
Extra string `json:"extra"`
FmtType string `json:"fmtType"`
GoType string `json:"goType"`
IsString bool `json:"isString"`
}
// ColumnWithTable is a column with the table name included
type ColumnWithTable struct {
*Column
TableName string
}
// Server represents a server
type Server struct {
Name string `json:"name"`
Host string `json:"host"`
Databases map[string]*Schema
Connection *sql.DB
CurrentDatabase string
}
// SchemaExists checks if the database `databaseName` exists in its list of databases
func (s *Server) SchemaExists(schemaName string) bool {
exists := false
for _, db := range s.Databases {
if db.Name == schemaName {
exists = true
break
}
}
return exists
}