@@ -157,34 +157,87 @@ function mixinDiscovery(MySQL, mysql) {
157
157
MySQL . prototype . buildQueryColumns = function ( schema , table , options = { } ) {
158
158
let sql = null ;
159
159
if ( schema ) {
160
- sql = paginateSQL ( 'SELECT table_schema AS "owner",' +
161
- ' table_name AS "tableName",' +
162
- ' column_name AS "columnName",' +
163
- ' data_type AS "dataType",' +
164
- ' character_maximum_length AS "dataLength",' +
165
- ' numeric_precision AS "dataPrecision",' +
166
- ' numeric_scale AS "dataScale",' +
167
- ' column_type AS "columnType",' +
168
- ' is_nullable = \'YES\' AS "nullable",' +
169
- ' CASE WHEN extra LIKE \'%auto_increment%\' THEN 1 ELSE 0 END AS "generated"' +
170
- ' FROM information_schema.columns' +
171
- ' WHERE table_schema=' + mysql . escape ( schema ) +
172
- ( table ? ' AND table_name=' + mysql . escape ( table ) : '' ) ,
173
- 'table_name, ordinal_position' , { } ) ;
160
+ sql = paginateSQL (
161
+ `SELECT
162
+ c.table_schema AS "owner",
163
+ c.table_name AS "tableName",
164
+ c.column_name AS "columnName",
165
+ c.data_type AS "dataType",
166
+ c.character_maximum_length AS "dataLength",
167
+ c.numeric_precision AS "dataPrecision",
168
+ c.numeric_scale AS "dataScale",
169
+ c.column_type AS "columnType",
170
+ c.is_nullable = 'YES' AS "nullable",
171
+ CASE WHEN c.extra LIKE '%auto_increment%' THEN 1 ELSE 0 END AS "generated",
172
+ s.index_name AS "indexName",
173
+ s.non_unique AS "nonUnique",
174
+ s.seq_in_index AS "seqInIndex",
175
+ s.cardinality AS "cardinality",
176
+ s.index_type AS "indexType",
177
+ CASE WHEN fk.column_name IS NOT NULL THEN 1 ELSE 0 END AS "isForeignKey" -- Flag for foreign key
178
+ FROM
179
+ information_schema.columns c
180
+ LEFT JOIN
181
+ information_schema.statistics s
182
+ ON
183
+ c.table_schema = s.table_schema
184
+ AND c.table_name = s.table_name
185
+ AND c.column_name = s.column_name
186
+ LEFT JOIN
187
+ information_schema.KEY_COLUMN_USAGE fk
188
+ ON
189
+ c.table_schema = fk.table_schema
190
+ AND c.table_name = fk.table_name
191
+ AND c.column_name = fk.column_name
192
+ AND fk.referenced_table_name IS NOT NULL -- Ensure it's a foreign key
193
+ WHERE
194
+ c.table_schema = ${ mysql . escape ( schema ) }
195
+ ${ table ? ' AND c.table_name = ' + mysql . escape ( table ) : '' }
196
+ ` ,
197
+ 'c.table_name, c.ordinal_position' ,
198
+ { }
199
+ ) ;
174
200
} else {
175
- sql = paginateSQL ( 'SELECT table_schema AS "owner",' +
176
- ' table_name AS "tableName",' +
177
- ' column_name AS "columnName",' +
178
- ' data_type AS "dataType",' +
179
- ' character_maximum_length AS "dataLength",' +
180
- ' numeric_precision AS "dataPrecision",' +
181
- ' numeric_scale AS "dataScale",' +
182
- ' column_type AS "columnType",' +
183
- ' is_nullable = \'YES\' AS "nullable",' +
184
- ' CASE WHEN extra LIKE \'%auto_increment%\' THEN 1 ELSE 0 END AS "generated"' +
185
- ' FROM information_schema.columns' +
186
- ( table ? ' WHERE table_name=' + mysql . escape ( table ) : '' ) ,
187
- 'table_name, ordinal_position' , { } ) ;
201
+ sql = paginateSQL (
202
+ `SELECT
203
+ columns.table_schema AS "owner",
204
+ columns.table_name AS "tableName",
205
+ columns.column_name AS "columnName",
206
+ columns.data_type AS "dataType",
207
+ columns.character_maximum_length AS "dataLength",
208
+ columns.numeric_precision AS "dataPrecision",
209
+ columns.numeric_scale AS "dataScale",
210
+ columns.column_type AS "columnType",
211
+ columns.is_nullable = 'YES' AS "nullable",
212
+ CASE WHEN columns.extra LIKE '%auto_increment%' THEN 1 ELSE 0 END AS "generated",
213
+ indexes.index_name AS "indexName",
214
+ indexes.seq_in_index AS "indexColumnOrder",
215
+ indexes.non_unique AS "nonUnique",
216
+ indexes.cardinality AS "cardinality", -- Cardinality of the index
217
+ indexes.index_type AS "indexType", -- Type of the index
218
+ CASE WHEN fk.column_name IS NOT NULL THEN 1 ELSE 0 END AS "isForeignKey" -- Flag for foreign key
219
+ FROM
220
+ information_schema.columns AS columns
221
+ LEFT JOIN
222
+ information_schema.statistics AS indexes
223
+ ON
224
+ columns.table_schema = indexes.table_schema
225
+ AND columns.table_name = indexes.table_name
226
+ AND columns.column_name = indexes.column_name
227
+ LEFT JOIN
228
+ information_schema.KEY_COLUMN_USAGE AS fk
229
+ ON
230
+ columns.table_schema = fk.table_schema
231
+ AND columns.table_name = fk.table_name
232
+ AND columns.column_name = fk.column_name
233
+ AND fk.referenced_table_name IS NOT NULL -- Ensure it's a foreign key
234
+ WHERE
235
+ columns.table_schema = ${ mysql . escape ( schema ) }
236
+ ${ table ? ' AND columns.table_name = ' + mysql . escape ( table ) : '' }
237
+ ` ,
238
+ 'columns.table_name, columns.ordinal_position' ,
239
+ { }
240
+ ) ;
188
241
}
189
242
if ( options . orderBy ) {
190
243
sql += ' ORDER BY ' + options . orderBy ;
0 commit comments