Fix discovery of exported keys

This commit is contained in:
Raymond Feng 2013-10-08 13:44:37 -07:00
parent 6686a9d8f9
commit 869348df09
3 changed files with 182 additions and 102 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 config = require('rc')('loopback', {dev: {mysql: {}}}).dev.mysql;
@ -23,17 +20,17 @@ function show(err, models) {
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.discoverForeignKeys('User', show);
ds.discoverPrimaryKeys('customer', 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) {
models[m].all(show);

File diff suppressed because one or more lines are too long

View File

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