@@ -13,14 +13,20 @@ type Table struct {
1313 Schema string
1414 Name string
1515 Fields []Field
16- Indexes [ ]Index
16+ Indexes map [ string ]Index
1717 Constraints []Constraint
1818 Triggers []Trigger
1919 //
2020 conn * sql.DB
2121}
2222
2323type Index struct {
24+ Name string
25+ Unique bool
26+ Fields []string
27+ }
28+
29+ type IndexField struct {
2430 NonUnique bool
2531 KeyName string
2632 SeqInIndex int
@@ -119,28 +125,7 @@ func (t *Table) parse() error {
119125 // | | +-------- extra info (unsigned, etc)
120126 // | | |
121127 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`" +
144129 fmt .Sprintf (" WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'" , t .Schema , t .Name )
145130
146131 constraints := constraintsAsMap (t .Constraints )
@@ -154,7 +139,8 @@ func (t *Table) parse() error {
154139 for rows .Next () {
155140 var f Field
156141 var allowNull string
157- err := rows .Scan (& f .TableCatalog ,
142+ fields := []interface {}{
143+ & f .TableCatalog ,
158144 & f .TableSchema ,
159145 & f .TableName ,
160146 & f .ColumnName ,
@@ -174,10 +160,13 @@ func (t *Table) parse() error {
174160 & f .Extra ,
175161 & f .Privileges ,
176162 & 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 ... )
179169 if err != nil {
180- fmt .Println (err )
181170 continue
182171 }
183172
@@ -207,26 +196,44 @@ func (t *Table) parse() error {
207196 return nil
208197}
209198
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 ) {
211208 query := fmt .Sprintf ("SHOW INDEXES FROM `%s`.`%s`" , schema , tableName )
212209 rows , err := db .Query (query )
213210 if err != nil {
214211 return nil , err
215212 }
216213 defer rows .Close ()
217214
218- indexes := [ ]Index {}
215+ indexes := make ( map [ string ]Index )
219216
220217 for rows .Next () {
221- var i Index
218+ var i IndexField
222219 var table string
223220 err := rows .Scan (& table , & i .NonUnique , & i .KeyName , & i .SeqInIndex ,
224221 & i .ColumnName , & i .Collation , & i .Cardinality , & i .SubPart ,
225222 & i .Packed , & i .Null , & i .IndexType , & i .Comment , & i .IndexComment )
226223 if err != nil {
227224 return nil , fmt .Errorf ("cannot read constraints: %s" , err )
228225 }
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+ }
230237 }
231238
232239 return indexes , nil
0 commit comments