Merge pull request #241 from strongloop/refactor-discovery
Refactor Discovery Methods
This commit is contained in:
commit
030f8f9dc1
170
lib/discovery.js
170
lib/discovery.js
|
@ -44,19 +44,19 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
* @params {Object} [options] Options object
|
||||
* @returns {String} The SQL statement
|
||||
*/
|
||||
function querySchemas(options) {
|
||||
MySQL.prototype.buildQuerySchemas = function(options) {
|
||||
var sql = 'SELECT catalog_name as "catalog",' +
|
||||
' schema_name as "schema"' +
|
||||
' FROM information_schema.schemata';
|
||||
return paginateSQL(sql, 'schema_name', options);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Build sql for listing tables
|
||||
* @param options {all: for all owners, owner: for a given owner}
|
||||
* @returns {string} The sql statement
|
||||
*/
|
||||
function queryTables(options) {
|
||||
MySQL.prototype.buildQueryTables = function(options) {
|
||||
var sqlTables = null;
|
||||
var schema = options.owner || options.schema;
|
||||
|
||||
|
@ -79,14 +79,14 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
'table_name', options);
|
||||
}
|
||||
return sqlTables;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Build sql for listing views
|
||||
* @param options {all: for all owners, owner: for a given owner}
|
||||
* @returns {string} The sql statement
|
||||
*/
|
||||
function queryViews(options) {
|
||||
MySQL.prototype.buildQueryViews = function(options) {
|
||||
var sqlViews = null;
|
||||
if (options.views) {
|
||||
var schema = options.owner || options.schema;
|
||||
|
@ -113,15 +113,6 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
}
|
||||
}
|
||||
return sqlViews;
|
||||
}
|
||||
|
||||
MySQL.prototype.discoverDatabaseSchemas = function(options, cb) {
|
||||
if (!cb && typeof options === 'function') {
|
||||
cb = options;
|
||||
options = {};
|
||||
}
|
||||
options = options || {};
|
||||
this.execute(querySchemas(options), cb);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -130,36 +121,6 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
* @param {Object} options Options for discovery
|
||||
* @param {Function} [cb] The callback function
|
||||
*/
|
||||
MySQL.prototype.discoverModelDefinitions = function(options, cb) {
|
||||
if (!cb && typeof options === 'function') {
|
||||
cb = options;
|
||||
options = {};
|
||||
}
|
||||
options = options || {};
|
||||
|
||||
var self = this;
|
||||
var calls = [function(callback) {
|
||||
self.execute(queryTables(options), callback);
|
||||
}];
|
||||
|
||||
if (options.views) {
|
||||
calls.push(function(callback) {
|
||||
self.execute(queryViews(options), callback);
|
||||
});
|
||||
}
|
||||
async.parallel(calls, function(err, data) {
|
||||
if (err) {
|
||||
cb(err, data);
|
||||
} else {
|
||||
var merged = [];
|
||||
merged = merged.concat(data.shift());
|
||||
if (data.length) {
|
||||
merged = merged.concat(data.shift());
|
||||
}
|
||||
cb(err, merged);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/*!
|
||||
* Normalize the arguments
|
||||
|
@ -167,7 +128,7 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
* @param options object, optional
|
||||
* @param cb function, optional
|
||||
*/
|
||||
function getArgs(table, options, cb) {
|
||||
MySQL.prototype.getArgs = function(table, options, cb) {
|
||||
if ('string' !== typeof table || !table) {
|
||||
throw new Error(g.f('{{table}} is a required string argument: %s', table));
|
||||
}
|
||||
|
@ -185,7 +146,7 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
options: options,
|
||||
cb: cb,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Build the sql statement to query columns for a given table
|
||||
|
@ -193,7 +154,7 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
* @param table
|
||||
* @returns {String} The sql statement
|
||||
*/
|
||||
function queryColumns(schema, table) {
|
||||
MySQL.prototype.buildQueryColumns = function(schema, table) {
|
||||
var sql = null;
|
||||
if (schema) {
|
||||
sql = paginateSQL('SELECT table_schema AS "owner",' +
|
||||
|
@ -224,7 +185,7 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
'table_name, ordinal_position', {});
|
||||
}
|
||||
return sql;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Discover model properties from a table
|
||||
|
@ -233,47 +194,6 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
* @param {Function} [cb] The callback function
|
||||
*
|
||||
*/
|
||||
MySQL.prototype.discoverModelProperties = function(table, options, cb) {
|
||||
var self = this;
|
||||
var args = getArgs(table, options, cb);
|
||||
var schema = args.schema;
|
||||
if (!schema) {
|
||||
schema = this.getDefaultSchema();
|
||||
}
|
||||
table = args.table;
|
||||
options = args.options;
|
||||
|
||||
// Recommended MySQL 5.7 Boolean scheme. See
|
||||
// http://dev.mysql.com/doc/refman/5.7/en/numeric-type-overview.html
|
||||
// Currently default is the inverse of the recommendation for backward compatibility.
|
||||
var defaultOptions = {
|
||||
treatCHAR1AsString: false,
|
||||
treatBIT1AsBit: true,
|
||||
treatTINYINT1AsTinyInt: true,
|
||||
};
|
||||
|
||||
for (var opt in defaultOptions) {
|
||||
if (defaultOptions.hasOwnProperty(opt) && !options.hasOwnProperty(opt)) {
|
||||
options[opt] = defaultOptions[opt];
|
||||
}
|
||||
}
|
||||
|
||||
cb = args.cb;
|
||||
|
||||
var sql = queryColumns(schema, table);
|
||||
var callback = function(err, results) {
|
||||
if (err) {
|
||||
cb(err, results);
|
||||
} else {
|
||||
results.map(function(r) {
|
||||
r.type = self.buildPropertyType(r, options);
|
||||
r.nullable = r.nullable ? 'Y' : 'N';
|
||||
});
|
||||
cb(err, results);
|
||||
}
|
||||
};
|
||||
this.execute(sql, callback);
|
||||
};
|
||||
|
||||
/*!
|
||||
* Build the sql statement for querying primary keys of a given table
|
||||
|
@ -283,7 +203,7 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
*/
|
||||
// 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) {
|
||||
MySQL.prototype.buildQueryPrimaryKeys = function(schema, table) {
|
||||
var sql = 'SELECT table_schema AS "owner",' +
|
||||
' table_name AS "tableName",' +
|
||||
' column_name AS "columnName",' +
|
||||
|
@ -301,7 +221,7 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
sql += ' ORDER BY' +
|
||||
' table_schema, constraint_name, table_name, ordinal_position';
|
||||
return sql;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Discover primary keys for a given table
|
||||
|
@ -309,19 +229,6 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
* @param {Object} options The options for discovery
|
||||
* @param {Function} [cb] The callback function
|
||||
*/
|
||||
MySQL.prototype.discoverPrimaryKeys = function(table, options, cb) {
|
||||
var args = getArgs(table, options, cb);
|
||||
var schema = args.schema;
|
||||
if (!schema) {
|
||||
schema = this.getDefaultSchema();
|
||||
}
|
||||
table = args.table;
|
||||
options = args.options;
|
||||
cb = args.cb;
|
||||
|
||||
var sql = queryPrimaryKeys(schema, table);
|
||||
this.execute(sql, cb);
|
||||
};
|
||||
|
||||
/*!
|
||||
* Build the sql statement for querying foreign keys of a given table
|
||||
|
@ -329,7 +236,7 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
* @param table
|
||||
* @returns {string}
|
||||
*/
|
||||
function queryForeignKeys(schema, table) {
|
||||
MySQL.prototype.buildQueryForeignKeys = function(schema, table) {
|
||||
var sql =
|
||||
'SELECT table_schema AS "fkOwner",' +
|
||||
' constraint_name AS "fkName",' +
|
||||
|
@ -349,7 +256,7 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
sql += ' AND table_name=' + mysql.escape(table);
|
||||
}
|
||||
return sql;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Discover foreign keys for a given table
|
||||
|
@ -357,19 +264,6 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
* @param {Object} options The options for discovery
|
||||
* @param {Function} [cb] The callback function
|
||||
*/
|
||||
MySQL.prototype.discoverForeignKeys = function(table, options, cb) {
|
||||
var args = getArgs(table, options, cb);
|
||||
var schema = args.schema;
|
||||
if (!schema) {
|
||||
schema = this.getDefaultSchema();
|
||||
}
|
||||
table = args.table;
|
||||
options = args.options;
|
||||
cb = args.cb;
|
||||
|
||||
var sql = queryForeignKeys(schema, table);
|
||||
this.execute(sql, cb);
|
||||
};
|
||||
|
||||
/*!
|
||||
* Retrieves a description of the foreign key columns that reference the
|
||||
|
@ -379,7 +273,7 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
* @param table
|
||||
* @returns {string}
|
||||
*/
|
||||
function queryExportedForeignKeys(schema, table) {
|
||||
MySQL.prototype.buildQueryExportedForeignKeys = function(schema, table) {
|
||||
var sql = 'SELECT a.constraint_name AS "fkName",' +
|
||||
' a.table_schema AS "fkOwner",' +
|
||||
' a.table_name AS "fkTableName",' +
|
||||
|
@ -400,7 +294,7 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
sql += ' ORDER BY a.table_schema, a.table_name, a.ordinal_position';
|
||||
|
||||
return sql;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Discover foreign keys that reference to the primary key of this table
|
||||
|
@ -408,19 +302,6 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
* @param {Object} options The options for discovery
|
||||
* @param {Function} [cb] The callback function
|
||||
*/
|
||||
MySQL.prototype.discoverExportedForeignKeys = function(table, options, cb) {
|
||||
var args = getArgs(table, options, cb);
|
||||
var schema = args.schema;
|
||||
if (!schema) {
|
||||
schema = this.getDefaultSchema();
|
||||
}
|
||||
table = args.table;
|
||||
options = args.options;
|
||||
cb = args.cb;
|
||||
|
||||
var sql = queryExportedForeignKeys(schema, table);
|
||||
this.execute(sql, cb);
|
||||
};
|
||||
|
||||
MySQL.prototype.buildPropertyType = function(columnDefinition, options) {
|
||||
var mysqlType = columnDefinition.dataType;
|
||||
|
@ -488,4 +369,25 @@ function mixinDiscovery(MySQL, mysql) {
|
|||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
// Recommended MySQL 5.7 Boolean scheme. See
|
||||
// http://dev.mysql.com/doc/refman/5.7/en/numeric-type-overview.html
|
||||
// Currently default is the inverse of the recommendation for backward compatibility.
|
||||
MySQL.prototype.setDefaultOptions = function(options) {
|
||||
var defaultOptions = {
|
||||
treatCHAR1AsString: false,
|
||||
treatBIT1AsBit: true,
|
||||
treatTINYINT1AsTinyInt: true,
|
||||
};
|
||||
|
||||
for (var opt in defaultOptions) {
|
||||
if (defaultOptions.hasOwnProperty(opt) && !options.hasOwnProperty(opt)) {
|
||||
options[opt] = defaultOptions[opt];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
MySQL.prototype.setNullableProperty = function(r) {
|
||||
r.nullable = r.nullable ? 'Y' : 'N';
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue