Merge pull request #4 from strongloop/more-fix

More fix
This commit is contained in:
Raymond Feng 2013-10-08 14:03:45 -07:00
commit 802e61aba9
4 changed files with 215 additions and 104 deletions

View File

@ -1,6 +1,3 @@
var config = require('rc')('loopback');
config = (config.test && config.test.mysql) || {};
var DataSource = require('loopback-datasource-juggler').DataSource; var DataSource = require('loopback-datasource-juggler').DataSource;
var config = require('rc')('loopback', {dev: {mysql: {}}}).dev.mysql; var config = require('rc')('loopback', {dev: {mysql: {}}}).dev.mysql;
@ -23,17 +20,17 @@ function show(err, models) {
ds.discoverModelDefinitions({views: true, limit: 20}, show); ds.discoverModelDefinitions({views: true, limit: 20}, show);
ds.discoverModelProperties('User', show); ds.discoverModelProperties('customer', show);
ds.discoverModelProperties('Post', {owner: 'test'}, show); ds.discoverModelProperties('location', {owner: 'strongloop'}, show);
ds.discoverPrimaryKeys('User', show); ds.discoverPrimaryKeys('customer', show);
// ds.discoverForeignKeys('User', show); ds.discoverForeignKeys('inventory', show);
// ds.discoverExportedForeignKeys('User', show); ds.discoverExportedForeignKeys('location', show);
ds.discoverAndBuildModels('User', {owner: 'test', visited: {}, associations: true}, function (err, models) { ds.discoverAndBuildModels('weapon', {owner: 'strongloop', visited: {}, associations: true}, function (err, models) {
for (var m in models) { for (var m in models) {
models[m].all(show); models[m].all(show);

File diff suppressed because one or more lines are too long

View File

@ -344,30 +344,20 @@ function mixinDiscovery(MySQL) {
* @returns {string} * @returns {string}
*/ */
function queryExportedForeignKeys(owner, table) { function queryExportedForeignKeys(owner, table) {
var sql = 'SELECT a.constraint_name AS "fkName", a.owner AS "fkOwner", a.table_name AS "fkTableName",' var sql = 'SELECT a.constraint_name AS "fkName", a.table_schema AS "fkOwner", a.table_name AS "fkTableName",'
+ ' a.column_name AS "fkColumnName", a.position AS "keySeq",' + ' a.column_name AS "fkColumnName", a.ordinal_position AS "keySeq",'
+ ' jcol.constraint_name AS "pkName", jcol.owner AS "pkOwner",' + ' NULL AS "pkName", a.referenced_table_schema AS "pkOwner",'
+ ' jcol.table_name AS "pkTableName", jcol.column_name AS "pkColumnName"' + ' a.referenced_table_name AS "pkTableName", a.referenced_column_name AS "pkColumnName"'
+ ' FROM' + ' FROM'
+ ' (SELECT' + ' information_schema.key_column_usage a'
+ ' uc1.table_name, uc1.constraint_name, uc1.r_constraint_name, col.column_name, col.position, col.owner' + ' WHERE a.position_in_unique_constraint IS NOT NULL';
+ ' FROM'
+ ' information_schema.key_column_usage'
+ ' WHERE'
+ ' uc.constraint_type=\'P\' and uc1.r_constraint_name = uc.constraint_name and uc1.constraint_type = \'R\''
+ ' and uc1.constraint_name=col.constraint_name';
if (owner) { if (owner) {
sql += ' and col.owner=\'' + owner + '\''; sql += ' and a.referenced_table_schema=\'' + owner + '\'';
} }
if (table) { if (table) {
sql += ' and uc.table_Name=\'' + table + '\''; sql += ' and a.referenced_table_name=\'' + table + '\'';
} }
sql += ' ) a' sql += ' order by a.table_schema, a.table_name, a.ordinal_position';
+ ' INNER JOIN'
+ ' USER_CONS_COLUMNS jcol'
+ ' ON'
+ ' a.r_constraint_name=jcol.constraint_name'
+ ' order by a.owner, a.table_name, a.position';
return sql; return sql;
} }

View File

@ -535,6 +535,22 @@ MySQL.prototype.autoupdate = function (models, cb) {
} }
}; };
/*!
* Create table
* @param model
* @param cb
*/
MySQL.prototype.createTable = function (model, cb) {
var metadata = this._models[model].settings[this.name];
var engine = metadata && metadata.engine;
var sql = 'CREATE TABLE ' + this.tableEscaped(model) +
' (\n ' + this.propertiesSQL(model) + '\n)';
if(engine) {
sql += 'ENGINE=' + engine + '\n';
}
this.query(sql, cb);
};
/** /**
* Check if the models exist * Check if the models exist
* @param {String[]} [models] A model name or an array of model names. If not present, apply to all models * @param {String[]} [models] A model name or an array of model names. If not present, apply to all models
@ -786,11 +802,28 @@ MySQL.prototype.indexSettingsSQL = function (model, prop) {
MySQL.prototype.propertySettingsSQL = function (model, prop) { MySQL.prototype.propertySettingsSQL = function (model, prop) {
var p = this._models[model].properties[prop]; var p = this._models[model].properties[prop];
var line = datatype(p) + ' ' + var line = this.columnDataType(model, prop) + ' ' +
(p.allowNull === false || p['null'] === false ? 'NOT NULL' : 'NULL'); (p.nullable === false || p.allowNull === false || p['null'] === false ? 'NOT NULL' : 'NULL');
return line; return line;
}; };
MySQL.prototype.columnDataType = function (model, property) {
var columnMetadata = this.columnMetadata(model, property);
var colType = columnMetadata && columnMetadata.dataType;
if(colType) {
colType = colType.toUpperCase();
}
var prop = this._models[model].properties[property];
if(!prop) {
return null;
}
var colLength = columnMetadata && columnMetadata.dataLength || prop.length;
if (colType) {
return colType + (colLength ? '(' + colLength + ')' : '');
}
return datatype(prop);
};
function datatype(p) { function datatype(p) {
var dt = ''; var dt = '';
switch (p.type.name) { switch (p.type.name) {