@@ -13,14 +13,20 @@ type Table struct {
13
13
Schema string
14
14
Name string
15
15
Fields []Field
16
- Indexes [ ]Index
16
+ Indexes map [ string ]Index
17
17
Constraints []Constraint
18
18
Triggers []Trigger
19
19
//
20
20
conn * sql.DB
21
21
}
22
22
23
23
type Index struct {
24
+ Name string
25
+ Unique bool
26
+ Fields []string
27
+ }
28
+
29
+ type IndexField struct {
24
30
NonUnique bool
25
31
KeyName string
26
32
SeqInIndex int
@@ -119,28 +125,7 @@ func (t *Table) parse() error {
119
125
// | | +-------- extra info (unsigned, etc)
120
126
// | | |
121
127
re := regexp .MustCompile ("^(.*?)(?:\\ ((.*?)\\ )(.*))?$" )
122
- query := "SELECT `TABLE_CATALOG`," +
123
- "`TABLE_SCHEMA`," +
124
- "`TABLE_NAME`," +
125
- "`COLUMN_NAME`," +
126
- "`ORDINAL_POSITION`," +
127
- "`COLUMN_DEFAULT`," +
128
- "`IS_NULLABLE`," +
129
- "`DATA_TYPE`," +
130
- "`CHARACTER_MAXIMUM_LENGTH`," +
131
- "`CHARACTER_OCTET_LENGTH`," +
132
- "`NUMERIC_PRECISION`," +
133
- "`NUMERIC_SCALE`," +
134
- "`DATETIME_PRECISION`," +
135
- "`CHARACTER_SET_NAME`," +
136
- "`COLLATION_NAME`," +
137
- "`COLUMN_TYPE`," +
138
- "`COLUMN_KEY`," +
139
- "`EXTRA`," +
140
- "`PRIVILEGES`," +
141
- "`COLUMN_COMMENT`," +
142
- "`GENERATION_EXPRESSION`" +
143
- " FROM `information_schema`.`COLUMNS`" +
128
+ query := "SELECT * FROM `information_schema`.`COLUMNS`" +
144
129
fmt .Sprintf (" WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'" , t .Schema , t .Name )
145
130
146
131
constraints := constraintsAsMap (t .Constraints )
@@ -154,7 +139,8 @@ func (t *Table) parse() error {
154
139
for rows .Next () {
155
140
var f Field
156
141
var allowNull string
157
- err := rows .Scan (& f .TableCatalog ,
142
+ fields := []interface {}{
143
+ & f .TableCatalog ,
158
144
& f .TableSchema ,
159
145
& f .TableName ,
160
146
& f .ColumnName ,
@@ -174,10 +160,13 @@ func (t *Table) parse() error {
174
160
& f .Extra ,
175
161
& f .Privileges ,
176
162
& f .ColumnComment ,
177
- & f .GenerationExpression ,
178
- )
163
+ }
164
+
165
+ if cols , err := rows .Columns (); err == nil && len (cols ) > 20 { //&& cols[20] == "GENERATION_EXPRESSION" {
166
+ fields = append (fields , & f .GenerationExpression )
167
+ }
168
+ err := rows .Scan (fields ... )
179
169
if err != nil {
180
- fmt .Println (err )
181
170
continue
182
171
}
183
172
@@ -207,26 +196,44 @@ func (t *Table) parse() error {
207
196
return nil
208
197
}
209
198
210
- func getIndexes (db * sql.DB , schema , tableName string ) ([]Index , error ) {
199
+ func (t * Table ) FieldNames () []string {
200
+ fields := []string {}
201
+ for _ , field := range t .Fields {
202
+ fields = append (fields , field .ColumnName )
203
+ }
204
+ return fields
205
+ }
206
+
207
+ func getIndexes (db * sql.DB , schema , tableName string ) (map [string ]Index , error ) {
211
208
query := fmt .Sprintf ("SHOW INDEXES FROM `%s`.`%s`" , schema , tableName )
212
209
rows , err := db .Query (query )
213
210
if err != nil {
214
211
return nil , err
215
212
}
216
213
defer rows .Close ()
217
214
218
- indexes := [ ]Index {}
215
+ indexes := make ( map [ string ]Index )
219
216
220
217
for rows .Next () {
221
- var i Index
218
+ var i IndexField
222
219
var table string
223
220
err := rows .Scan (& table , & i .NonUnique , & i .KeyName , & i .SeqInIndex ,
224
221
& i .ColumnName , & i .Collation , & i .Cardinality , & i .SubPart ,
225
222
& i .Packed , & i .Null , & i .IndexType , & i .Comment , & i .IndexComment )
226
223
if err != nil {
227
224
return nil , fmt .Errorf ("cannot read constraints: %s" , err )
228
225
}
229
- indexes = append (indexes , i )
226
+ if index , ok := indexes [i .KeyName ]; ! ok {
227
+ indexes [i .KeyName ] = Index {
228
+ Name : i .KeyName ,
229
+ Unique : ! i .NonUnique ,
230
+ Fields : []string {i .ColumnName },
231
+ }
232
+
233
+ } else {
234
+ index .Fields = append (index .Fields , i .ColumnName )
235
+ index .Unique = index .Unique || ! i .NonUnique
236
+ }
230
237
}
231
238
232
239
return indexes , nil
0 commit comments