loopback-connector-mysql/lib/discovery.js

463 lines
14 KiB
JavaScript
Raw Normal View History

2016-05-03 23:52:03 +00:00
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
// Node module: loopback-connector-mysql
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2013-07-21 17:36:26 +00:00
module.exports = mixinDiscovery;
/*!
* @param {MySQL} MySQL connector class
* @param {Object} mysql mysql driver
*/
function mixinDiscovery(MySQL, mysql) {
2014-02-13 00:57:06 +00:00
var async = require('async');
2013-07-21 17:36:26 +00:00
2014-02-13 00:57:06 +00:00
function paginateSQL(sql, orderBy, options) {
options = options || {};
var limitClause = '';
2014-02-13 00:57:06 +00:00
if (options.offset || options.skip || options.limit) {
// Offset starts from 0
var offset = Number(options.offset || options.skip || 0);
if (isNaN(offset)) {
offset = 0;
}
limitClause = ' LIMIT ' + offset;
2014-02-13 00:57:06 +00:00
if (options.limit) {
var limit = Number(options.limit);
if (isNaN(limit)) {
limit = 0;
}
limitClause = limitClause + ',' + limit;
2014-02-13 00:57:06 +00:00
}
}
if (!orderBy) {
sql += ' ORDER BY ' + orderBy;
2013-07-21 06:38:40 +00:00
}
return sql + limitClause;
}
/*!
* Build sql for listing schemas (databases in MySQL)
* @params {Object} [options] Options object
* @returns {String} The SQL statement
*/
function querySchemas(options) {
var sql = 'SELECT catalog_name as "catalog",' +
' schema_name as "schema"' +
' FROM information_schema.schemata';
return paginateSQL(sql, 'schema_name', options);
2014-02-13 00:57:06 +00:00
}
2013-07-21 06:38:40 +00:00
2014-02-13 00:57:06 +00:00
/*!
* Build sql for listing tables
* @param options {all: for all owners, owner: for a given owner}
* @returns {string} The sql statement
*/
function queryTables(options) {
var sqlTables = null;
var schema = options.owner || options.schema;
2013-07-21 06:38:40 +00:00
if (options.all && !schema) {
sqlTables = paginateSQL('SELECT \'table\' AS "type",' +
' table_name AS "name", table_schema AS "owner"' +
' FROM information_schema.tables',
'table_schema, table_name', options);
} else if (schema) {
sqlTables = paginateSQL('SELECT \'table\' AS "type",' +
' table_name AS "name", table_schema AS "schema"' +
' FROM information_schema.tables' +
2015-05-14 15:39:36 +00:00
' WHERE table_schema=' + mysql.escape(schema),
'table_schema, table_name', options);
2014-02-13 00:57:06 +00:00
} else {
sqlTables = paginateSQL('SELECT \'table\' AS "type",' +
' table_name AS "name", ' +
' table_schema AS "owner" FROM information_schema.tables' +
' WHERE table_schema=SUBSTRING_INDEX(USER(),\'@\',1)',
2014-02-13 00:57:06 +00:00
'table_name', options);
2013-07-21 06:38:40 +00:00
}
2014-02-13 00:57:06 +00:00
return sqlTables;
}
2013-07-21 06:38:40 +00:00
2014-02-13 00:57:06 +00:00
/*!
* Build sql for listing views
* @param options {all: for all owners, owner: for a given owner}
* @returns {string} The sql statement
*/
function queryViews(options) {
var sqlViews = null;
if (options.views) {
2013-07-21 17:36:26 +00:00
var schema = options.owner || options.schema;
2013-07-21 17:36:26 +00:00
if (options.all && !schema) {
sqlViews = paginateSQL('SELECT \'view\' AS "type",' +
' table_name AS "name",' +
' table_schema AS "owner"' +
' FROM information_schema.views',
2014-02-13 00:57:06 +00:00
'table_schema, table_name', options);
} else if (schema) {
sqlViews = paginateSQL('SELECT \'view\' AS "type",' +
' table_name AS "name",' +
' table_schema AS "owner"' +
' FROM information_schema.views' +
2015-05-14 15:39:36 +00:00
' WHERE table_schema=' + mysql.escape(schema),
2014-02-13 00:57:06 +00:00
'table_schema, table_name', options);
} else {
sqlViews = paginateSQL('SELECT \'view\' AS "type",' +
' table_name AS "name",' +
' table_schema AS "owner"' +
' FROM information_schema.views',
2014-02-13 00:57:06 +00:00
'table_name', options);
}
2013-07-21 06:38:40 +00:00
}
2014-02-13 00:57:06 +00:00
return sqlViews;
}
2013-07-21 06:38:40 +00:00
MySQL.prototype.discoverDatabaseSchemas = function(options, cb) {
if (!cb && typeof options === 'function') {
cb = options;
options = {};
}
options = options || {};
this.execute(querySchemas(options), cb);
};
2014-02-13 00:57:06 +00:00
/**
* Discover model definitions
*
* @param {Object} options Options for discovery
* @param {Function} [cb] The callback function
*/
MySQL.prototype.discoverModelDefinitions = function(options, cb) {
2014-02-13 00:57:06 +00:00
if (!cb && typeof options === 'function') {
cb = options;
options = {};
}
options = options || {};
2013-07-21 06:38:40 +00:00
2014-02-13 00:57:06 +00:00
var self = this;
var calls = [function(callback) {
self.execute(queryTables(options), callback);
2014-02-13 00:57:06 +00:00
}];
2013-07-21 17:36:26 +00:00
2014-02-13 00:57:06 +00:00
if (options.views) {
calls.push(function(callback) {
self.execute(queryViews(options), callback);
2014-02-13 00:57:06 +00:00
});
}
async.parallel(calls, function(err, data) {
2014-02-13 00:57:06 +00:00
if (err) {
cb(err, data);
} else {
var merged = [];
merged = merged.concat(data.shift());
if (data.length) {
merged = merged.concat(data.shift());
2013-07-21 17:36:26 +00:00
}
2014-02-13 00:57:06 +00:00
cb(err, merged);
}
});
};
2013-07-21 06:38:40 +00:00
2014-02-13 00:57:06 +00:00
/*!
* Normalize the arguments
* @param table string, required
* @param options object, optional
* @param cb function, optional
*/
function getArgs(table, options, cb) {
if ('string' !== typeof table || !table) {
throw new Error('table is a required string argument: ' + table);
2013-07-21 06:38:40 +00:00
}
2014-02-13 00:57:06 +00:00
options = options || {};
if (!cb && 'function' === typeof options) {
cb = options;
options = {};
}
if (typeof options !== 'object') {
throw new Error('options must be an object: ' + options);
}
return {
schema: options.owner || options.schema,
2014-02-13 00:57:06 +00:00
table: table,
options: options,
cb: cb
};
}
2013-07-21 06:38:40 +00:00
2014-02-13 00:57:06 +00:00
/*!
* Build the sql statement to query columns for a given table
* @param schema
2014-02-13 00:57:06 +00:00
* @param table
* @returns {String} The sql statement
*/
function queryColumns(schema, table) {
2014-02-13 00:57:06 +00:00
var sql = null;
if (schema) {
sql = paginateSQL('SELECT table_schema AS "owner",' +
' table_name AS "tableName",' +
' column_name AS "columnName",' +
' data_type AS "dataType",' +
' character_maximum_length AS "dataLength",' +
' numeric_precision AS "dataPrecision",' +
' numeric_scale AS "dataScale",' +
' is_nullable = \'YES\' AS "nullable"' +
' FROM information_schema.columns' +
' WHERE table_schema=' + mysql.escape(schema) +
(table ? ' AND table_name=' + mysql.escape(table) : ''),
'table_name, ordinal_position', {});
2014-02-13 00:57:06 +00:00
} else {
sql = paginateSQL('SELECT table_schema AS "owner",' +
' table_name AS "tableName",' +
' column_name AS "columnName",' +
' data_type AS "dataType",' +
' character_maximum_length AS "dataLength",' +
' numeric_precision AS "dataPrecision",' +
' numeric_scale AS "dataScale",' +
' is_nullable = \'YES\' AS "nullable"' +
' FROM information_schema.columns' +
(table ? ' WHERE table_name=' + mysql.escape(table) : ''),
'table_name, ordinal_position', {});
2013-07-21 17:36:26 +00:00
}
2014-02-13 00:57:06 +00:00
return sql;
}
2013-07-21 06:38:40 +00:00
2014-02-13 00:57:06 +00:00
/**
* Discover model properties from a table
* @param {String} table The table name
* @param {Object} options The options for discovery
* @param {Function} [cb] The callback function
*
*/
MySQL.prototype.discoverModelProperties = function(table, options, cb) {
var self = this;
2014-02-13 00:57:06 +00:00
var args = getArgs(table, options, cb);
var schema = args.schema;
if (!schema) {
schema = this.getDefaultSchema();
}
2014-02-13 00:57:06 +00:00
table = args.table;
options = args.options;
cb = args.cb;
2013-07-21 17:36:26 +00:00
var sql = queryColumns(schema, table);
var callback = function(err, results) {
2014-02-13 00:57:06 +00:00
if (err) {
cb(err, results);
} else {
results.map(function(r) {
r.type = self.buildPropertyType(r);
r.nullable = r.nullable ? 'Y' : 'N';
2014-02-13 00:57:06 +00:00
});
cb(err, results);
}
2013-10-03 21:50:38 +00:00
};
this.execute(sql, callback);
2014-02-13 00:57:06 +00:00
};
2013-07-21 06:38:40 +00:00
2014-02-13 00:57:06 +00:00
/*!
* Build the sql statement for querying primary keys of a given table
* @param schema
2014-02-13 00:57:06 +00:00
* @param table
* @returns {string}
*/
// http://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html
// #getPrimaryKeys(java.lang.String, java.lang.String, java.lang.String)
function queryPrimaryKeys(schema, table) {
var sql = 'SELECT table_schema AS "owner",' +
' table_name AS "tableName",' +
' column_name AS "columnName",' +
' ordinal_position AS "keySeq",' +
' constraint_name AS "pkName"' +
' FROM information_schema.key_column_usage' +
' WHERE constraint_name=\'PRIMARY\'';
2013-07-21 17:36:26 +00:00
if (schema) {
sql += ' AND table_schema=' + mysql.escape(schema);
2014-02-13 00:57:06 +00:00
}
if (table) {
sql += ' AND table_name=' + mysql.escape(table);
2013-07-21 06:38:40 +00:00
}
sql += ' ORDER BY' +
' table_schema, constraint_name, table_name, ordinal_position';
2014-02-13 00:57:06 +00:00
return sql;
}
2013-07-21 17:36:26 +00:00
2014-02-13 00:57:06 +00:00
/**
* Discover primary keys for a given table
* @param {String} table The table name
* @param {Object} options The options for discovery
* @param {Function} [cb] The callback function
*/
MySQL.prototype.discoverPrimaryKeys = function(table, options, cb) {
2014-02-13 00:57:06 +00:00
var args = getArgs(table, options, cb);
var schema = args.schema;
if (!schema) {
schema = this.getDefaultSchema();
}
2014-02-13 00:57:06 +00:00
table = args.table;
options = args.options;
cb = args.cb;
2013-07-21 17:36:26 +00:00
var sql = queryPrimaryKeys(schema, table);
this.execute(sql, cb);
2014-02-13 00:57:06 +00:00
};
2013-07-21 06:38:40 +00:00
2014-02-13 00:57:06 +00:00
/*!
* Build the sql statement for querying foreign keys of a given table
* @param schema
2014-02-13 00:57:06 +00:00
* @param table
* @returns {string}
*/
function queryForeignKeys(schema, table) {
2014-02-13 00:57:06 +00:00
var sql =
'SELECT table_schema AS "fkOwner",' +
' constraint_name AS "fkName",' +
' table_name AS "fkTableName",' +
' column_name AS "fkColumnName",' +
' ordinal_position AS "keySeq",' +
' referenced_table_schema AS "pkOwner", \'PRIMARY\' AS "pkName",' +
' referenced_table_name AS "pkTableName",' +
' referenced_column_name AS "pkColumnName"' +
' FROM information_schema.key_column_usage' +
' WHERE constraint_name!=\'PRIMARY\'' +
' AND POSITION_IN_UNIQUE_CONSTRAINT IS NOT NULL';
if (schema) {
sql += ' AND table_schema=' + mysql.escape(schema);
2013-07-21 17:36:26 +00:00
}
2014-02-13 00:57:06 +00:00
if (table) {
sql += ' AND table_name=' + mysql.escape(table);
2014-02-13 00:57:06 +00:00
}
return sql;
}
2013-07-21 06:38:40 +00:00
2014-02-13 00:57:06 +00:00
/**
* Discover foreign keys for a given table
* @param {String} table The table name
* @param {Object} options The options for discovery
* @param {Function} [cb] The callback function
*/
MySQL.prototype.discoverForeignKeys = function(table, options, cb) {
2014-02-13 00:57:06 +00:00
var args = getArgs(table, options, cb);
var schema = args.schema;
if (!schema) {
schema = this.getDefaultSchema();
}
2014-02-13 00:57:06 +00:00
table = args.table;
options = args.options;
cb = args.cb;
2013-07-21 17:36:26 +00:00
var sql = queryForeignKeys(schema, table);
this.execute(sql, cb);
2014-02-13 00:57:06 +00:00
};
2013-10-08 20:44:37 +00:00
2014-02-13 00:57:06 +00:00
/*!
* Retrieves a description of the foreign key columns that reference the
* given table's primary key columns (the foreign keys exported by a table).
2014-02-13 00:57:06 +00:00
* They are ordered by fkTableOwner, fkTableName, and keySeq.
* @param schema
2014-02-13 00:57:06 +00:00
* @param table
* @returns {string}
*/
function queryExportedForeignKeys(schema, table) {
var sql = 'SELECT a.constraint_name AS "fkName",' +
' a.table_schema AS "fkOwner",' +
' a.table_name AS "fkTableName",' +
' a.column_name AS "fkColumnName",' +
' a.ordinal_position AS "keySeq",' +
' NULL AS "pkName",' +
' a.referenced_table_schema AS "pkOwner",' +
' a.referenced_table_name AS "pkTableName",' +
' a.referenced_column_name AS "pkColumnName"' +
' FROM information_schema.key_column_usage a' +
' WHERE a.position_in_unique_constraint IS NOT NULL';
if (schema) {
sql += ' AND a.referenced_table_schema=' + mysql.escape(schema);
2013-07-21 17:36:26 +00:00
}
2014-02-13 00:57:06 +00:00
if (table) {
sql += ' AND a.referenced_table_name=' + mysql.escape(table);
2014-02-13 00:57:06 +00:00
}
sql += ' ORDER BY a.table_schema, a.table_name, a.ordinal_position';
2013-07-21 06:38:40 +00:00
2014-02-13 00:57:06 +00:00
return sql;
}
2013-07-21 06:38:40 +00:00
2014-02-13 00:57:06 +00:00
/**
* Discover foreign keys that reference to the primary key of this table
* @param {String} table The table name
* @param {Object} options The options for discovery
* @param {Function} [cb] The callback function
*/
MySQL.prototype.discoverExportedForeignKeys = function(table, options, cb) {
2014-02-13 00:57:06 +00:00
var args = getArgs(table, options, cb);
var schema = args.schema;
if (!schema) {
schema = this.getDefaultSchema();
}
2014-02-13 00:57:06 +00:00
table = args.table;
options = args.options;
cb = args.cb;
2013-07-21 06:38:40 +00:00
var sql = queryExportedForeignKeys(schema, table);
this.execute(sql, cb);
2014-02-13 00:57:06 +00:00
};
2013-07-21 17:36:26 +00:00
MySQL.prototype.buildPropertyType = function (columnDefinition) {
var mysqlType = columnDefinition.dataType;
var dataLength = columnDefinition.dataLength;
2014-02-13 00:57:06 +00:00
var type = mysqlType.toUpperCase();
switch (type) {
case 'CHAR':
if (dataLength === 1) {
// Treat char(1) as boolean
return 'Boolean';
} else {
return 'String';
2013-07-21 17:36:26 +00:00
}
2014-05-25 16:46:55 +00:00
break;
2014-02-13 00:57:06 +00:00
case 'VARCHAR':
case 'TINYTEXT':
case 'MEDIUMTEXT':
case 'LONGTEXT':
case 'TEXT':
case 'ENUM':
case 'SET':
return 'String';
case 'TINYBLOB':
case 'MEDIUMBLOB':
case 'LONGBLOB':
case 'BLOB':
case 'BINARY':
case 'VARBINARY':
case 'BIT':
return 'Binary';
case 'TINYINT':
case 'SMALLINT':
case 'INT':
case 'MEDIUMINT':
case 'YEAR':
case 'FLOAT':
case 'DOUBLE':
2014-05-25 16:46:55 +00:00
case 'BIGINT':
2014-02-13 00:57:06 +00:00
return 'Number';
case 'DATE':
case 'TIMESTAMP':
case 'DATETIME':
return 'Date';
2014-02-13 16:35:52 +00:00
case 'POINT':
2014-02-13 00:57:06 +00:00
return 'GeoPoint';
default:
return 'String';
2013-07-21 17:36:26 +00:00
}
2014-02-13 00:57:06 +00:00
}
MySQL.prototype.getDefaultSchema = function() {
if (this.dataSource && this.dataSource.settings &&
this.dataSource.settings.database) {
return this.dataSource.settings.database;
}
return undefined;
};
2013-07-21 06:38:40 +00:00
}