Merge f705836e0f
into 6198eab5ad
This commit is contained in:
commit
8af8f0da57
109
lib/discovery.js
109
lib/discovery.js
|
@ -157,37 +157,90 @@ 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",' +
|
cols.table_schema AS "owner",
|
||||||
' data_type AS "dataType",' +
|
cols.table_name AS "tableName",
|
||||||
' character_maximum_length AS "dataLength",' +
|
cols.column_name AS "columnName",
|
||||||
' numeric_precision AS "dataPrecision",' +
|
cols.data_type AS "dataType",
|
||||||
' numeric_scale AS "dataScale",' +
|
cols.character_maximum_length AS "dataLength",
|
||||||
' column_type AS "columnType",' +
|
cols.numeric_precision AS "dataPrecision",
|
||||||
' is_nullable = \'YES\' AS "nullable",' +
|
cols.numeric_scale AS "dataScale",
|
||||||
' CASE WHEN extra LIKE \'%auto_increment%\' THEN 1 ELSE 0 END AS "generated"' +
|
cols.column_type AS "columnType",
|
||||||
' FROM information_schema.columns' +
|
cols.is_nullable = 'YES' AS "nullable",
|
||||||
' WHERE table_schema=' + mysql.escape(schema) +
|
CASE WHEN cols.extra LIKE '%auto_increment%' THEN 1 ELSE 0 END AS "generated",
|
||||||
(table ? ' AND table_name=' + mysql.escape(table) : ''),
|
indexes.index_name AS "indexName",
|
||||||
'table_name, ordinal_position', {});
|
indexes.non_unique AS "nonUnique",
|
||||||
|
indexes.seq_in_index AS "seqInIndex",
|
||||||
|
indexes.cardinality AS "cardinality",
|
||||||
|
indexes.index_type AS "indexType",
|
||||||
|
CASE WHEN fk.column_name IS NOT NULL THEN 1 ELSE 0 END AS "isForeignKey"
|
||||||
|
FROM
|
||||||
|
information_schema.columns cols
|
||||||
|
LEFT JOIN
|
||||||
|
information_schema.statistics indexes
|
||||||
|
ON
|
||||||
|
cols.table_schema = indexes.table_schema
|
||||||
|
AND cols.table_name = indexes.table_name
|
||||||
|
AND cols.column_name = indexes.column_name
|
||||||
|
LEFT JOIN
|
||||||
|
information_schema.KEY_COLUMN_USAGE fk
|
||||||
|
ON
|
||||||
|
cols.table_schema = fk.table_schema
|
||||||
|
AND cols.table_name = fk.table_name
|
||||||
|
AND cols.column_name = fk.column_name
|
||||||
|
AND fk.referenced_table_name IS NOT NULL
|
||||||
|
WHERE
|
||||||
|
cols.table_schema = ${mysql.escape(schema)}
|
||||||
|
${table ? ' AND cols.table_name = ' + mysql.escape(table) : ''}
|
||||||
|
`,
|
||||||
|
'cols.table_name, cols.ordinal_position',
|
||||||
|
{},
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
sql = paginateSQL('SELECT table_schema AS "owner",' +
|
sql = paginateSQL(
|
||||||
' table_name AS "tableName",' +
|
`SELECT
|
||||||
' column_name AS "columnName",' +
|
cols.table_schema AS "owner",
|
||||||
' data_type AS "dataType",' +
|
cols.table_name AS "tableName",
|
||||||
' character_maximum_length AS "dataLength",' +
|
cols.column_name AS "columnName",
|
||||||
' numeric_precision AS "dataPrecision",' +
|
cols.data_type AS "dataType",
|
||||||
' numeric_scale AS "dataScale",' +
|
cols.character_maximum_length AS "dataLength",
|
||||||
' column_type AS "columnType",' +
|
cols.numeric_precision AS "dataPrecision",
|
||||||
' is_nullable = \'YES\' AS "nullable",' +
|
cols.numeric_scale AS "dataScale",
|
||||||
' CASE WHEN extra LIKE \'%auto_increment%\' THEN 1 ELSE 0 END AS "generated"' +
|
cols.column_type AS "columnType",
|
||||||
' FROM information_schema.columns' +
|
cols.is_nullable = 'YES' AS "nullable",
|
||||||
(table ? ' WHERE table_name=' + mysql.escape(table) : ''),
|
CASE WHEN cols.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",
|
||||||
|
indexes.index_type AS "indexType",
|
||||||
|
CASE WHEN fk.column_name IS NOT NULL THEN 1 ELSE 0 END AS "isForeignKey"
|
||||||
|
FROM
|
||||||
|
information_schema.columns AS cols
|
||||||
|
LEFT JOIN
|
||||||
|
information_schema.statistics AS indexes
|
||||||
|
ON
|
||||||
|
cols.table_schema = indexes.table_schema
|
||||||
|
AND cols.table_name = indexes.table_name
|
||||||
|
AND cols.column_name = indexes.column_name
|
||||||
|
LEFT JOIN
|
||||||
|
information_schema.KEY_COLUMN_USAGE AS fk
|
||||||
|
ON
|
||||||
|
cols.table_schema = fk.table_schema
|
||||||
|
AND cols.table_name = fk.table_name
|
||||||
|
AND cols.column_name = fk.column_name
|
||||||
|
AND fk.referenced_table_name IS NOT NULL
|
||||||
|
WHERE
|
||||||
|
cols.table_schema = ${mysql.escape(schema)}
|
||||||
|
${table ? ' AND cols.table_name = ' + mysql.escape(table) : ''}
|
||||||
|
`,
|
||||||
|
'cols.table_name, cols.ordinal_position',
|
||||||
|
{},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (options.orderBy) {
|
if (options.orderBy) {
|
||||||
sql += ' ORDER BY ' + options.orderBy;
|
sql += ' ORDER BY ' + 'cols.' + options.orderBy;
|
||||||
}
|
}
|
||||||
return sql;
|
return sql;
|
||||||
};
|
};
|
||||||
|
|
|
@ -199,6 +199,23 @@ describe('Discover model primary keys', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Discover user model with index', function() {
|
||||||
|
it('should return user with index', function(done) {
|
||||||
|
db.discoverModelProperties('user', function(err, models) {
|
||||||
|
if (err) {
|
||||||
|
console.error(err);
|
||||||
|
done(err);
|
||||||
|
} else {
|
||||||
|
models.forEach(function(m) {
|
||||||
|
assert(m.tableName.toLowerCase() === 'user');
|
||||||
|
assert(m.properties.email.index);
|
||||||
|
});
|
||||||
|
done(null, models);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('Discover model foreign keys', function() {
|
describe('Discover model foreign keys', function() {
|
||||||
it('should return an array of foreign keys for INVENTORY', function(done) {
|
it('should return an array of foreign keys for INVENTORY', function(done) {
|
||||||
db.discoverForeignKeys('INVENTORY', function(err, models) {
|
db.discoverForeignKeys('INVENTORY', function(err, models) {
|
||||||
|
|
|
@ -209,6 +209,21 @@ LOCK TABLES `RESERVATION` WRITE;
|
||||||
/*!40000 ALTER TABLE `RESERVATION` ENABLE KEYS */;
|
/*!40000 ALTER TABLE `RESERVATION` ENABLE KEYS */;
|
||||||
UNLOCK TABLES;
|
UNLOCK TABLES;
|
||||||
|
|
||||||
|
DROP TABLE IF EXISTS `USER`;
|
||||||
|
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||||
|
/*!40101 SET character_set_client = utf8 */;
|
||||||
|
|
||||||
|
CREATE TABLE `USER` (
|
||||||
|
`ID` VARCHAR(20) NOT NULL,
|
||||||
|
`NAME` VARCHAR(100) NOT NULL,
|
||||||
|
`EMAIL` VARCHAR(255) NOT NULL,
|
||||||
|
`PASSWORD` VARCHAR(255) NOT NULL,
|
||||||
|
`CREATED_AT` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
`UPDATED_AT` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
|
PRIMARY KEY (`ID`),
|
||||||
|
UNIQUE KEY `USER_EMAIL_UNIQUE` (`EMAIL`)
|
||||||
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Table structure for table `TESTGEN`
|
-- Table structure for table `TESTGEN`
|
||||||
--
|
--
|
||||||
|
|
Loading…
Reference in New Issue