feat: fetch index information for columns

Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
This commit is contained in:
Muhammad Aaqil 2024-10-13 23:17:13 +05:00
parent 9f21dee283
commit 6af3b7cf98
1 changed files with 80 additions and 27 deletions

View File

@ -157,34 +157,87 @@ function mixinDiscovery(MySQL, mysql) {
MySQL.prototype.buildQueryColumns = function(schema, table, options = {}) { MySQL.prototype.buildQueryColumns = function(schema, table, options = {}) {
let sql = null; let sql = null;
if (schema) { if (schema) {
sql = paginateSQL('SELECT table_schema AS "owner",' + sql = paginateSQL(
' table_name AS "tableName",' + `SELECT
' column_name AS "columnName",' + c.table_schema AS "owner",
' data_type AS "dataType",' + c.table_name AS "tableName",
' character_maximum_length AS "dataLength",' + c.column_name AS "columnName",
' numeric_precision AS "dataPrecision",' + c.data_type AS "dataType",
' numeric_scale AS "dataScale",' + c.character_maximum_length AS "dataLength",
' column_type AS "columnType",' + c.numeric_precision AS "dataPrecision",
' is_nullable = \'YES\' AS "nullable",' + c.numeric_scale AS "dataScale",
' CASE WHEN extra LIKE \'%auto_increment%\' THEN 1 ELSE 0 END AS "generated"' + c.column_type AS "columnType",
' FROM information_schema.columns' + c.is_nullable = 'YES' AS "nullable",
' WHERE table_schema=' + mysql.escape(schema) + CASE WHEN c.extra LIKE '%auto_increment%' THEN 1 ELSE 0 END AS "generated",
(table ? ' AND table_name=' + mysql.escape(table) : ''), s.index_name AS "indexName",
'table_name, ordinal_position', {}); s.non_unique AS "nonUnique",
s.seq_in_index AS "seqInIndex",
s.cardinality AS "cardinality",
s.index_type AS "indexType",
CASE WHEN fk.column_name IS NOT NULL THEN 1 ELSE 0 END AS "isForeignKey" -- Flag for foreign key
FROM
information_schema.columns c
LEFT JOIN
information_schema.statistics s
ON
c.table_schema = s.table_schema
AND c.table_name = s.table_name
AND c.column_name = s.column_name
LEFT JOIN
information_schema.KEY_COLUMN_USAGE fk
ON
c.table_schema = fk.table_schema
AND c.table_name = fk.table_name
AND c.column_name = fk.column_name
AND fk.referenced_table_name IS NOT NULL -- Ensure it's a foreign key
WHERE
c.table_schema = ${mysql.escape(schema)}
${table ? ' AND c.table_name = ' + mysql.escape(table) : ''}
`,
'c.table_name, c.ordinal_position',
{},
);
} else { } else {
sql = paginateSQL('SELECT table_schema AS "owner",' + sql = paginateSQL(
' table_name AS "tableName",' + `SELECT
' column_name AS "columnName",' + columns.table_schema AS "owner",
' data_type AS "dataType",' + columns.table_name AS "tableName",
' character_maximum_length AS "dataLength",' + columns.column_name AS "columnName",
' numeric_precision AS "dataPrecision",' + columns.data_type AS "dataType",
' numeric_scale AS "dataScale",' + columns.character_maximum_length AS "dataLength",
' column_type AS "columnType",' + columns.numeric_precision AS "dataPrecision",
' is_nullable = \'YES\' AS "nullable",' + columns.numeric_scale AS "dataScale",
' CASE WHEN extra LIKE \'%auto_increment%\' THEN 1 ELSE 0 END AS "generated"' + columns.column_type AS "columnType",
' FROM information_schema.columns' + columns.is_nullable = 'YES' AS "nullable",
(table ? ' WHERE table_name=' + mysql.escape(table) : ''), CASE WHEN columns.extra LIKE '%auto_increment%' THEN 1 ELSE 0 END AS "generated",
'table_name, ordinal_position', {}); indexes.index_name AS "indexName",
indexes.seq_in_index AS "indexColumnOrder",
indexes.non_unique AS "nonUnique",
indexes.cardinality AS "cardinality", -- Cardinality of the index
indexes.index_type AS "indexType", -- Type of the index
CASE WHEN fk.column_name IS NOT NULL THEN 1 ELSE 0 END AS "isForeignKey" -- Flag for foreign key
FROM
information_schema.columns AS columns
LEFT JOIN
information_schema.statistics AS indexes
ON
columns.table_schema = indexes.table_schema
AND columns.table_name = indexes.table_name
AND columns.column_name = indexes.column_name
LEFT JOIN
information_schema.KEY_COLUMN_USAGE AS fk
ON
columns.table_schema = fk.table_schema
AND columns.table_name = fk.table_name
AND columns.column_name = fk.column_name
AND fk.referenced_table_name IS NOT NULL -- Ensure it's a foreign key
WHERE
columns.table_schema = ${mysql.escape(schema)}
${table ? ' AND columns.table_name = ' + mysql.escape(table) : ''}
`,
'columns.table_name, columns.ordinal_position',
{},
);
} }
if (options.orderBy) { if (options.orderBy) {
sql += ' ORDER BY ' + options.orderBy; sql += ' ORDER BY ' + options.orderBy;