Skip to content

Commit 559d38b

Browse files
author
Loay Gewily
committed
Refactor discovery models
1 parent 679fb05 commit 559d38b

File tree

1 file changed

+10
-88
lines changed

1 file changed

+10
-88
lines changed

lib/discovery.js

Lines changed: 10 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
'use strict';
77
var g = require('strong-globalize')();
8+
var SqlConnector = require('loopback-connector').SqlConnector;
9+
var ParameterizedSQL = SqlConnector.ParameterizedSQL;
810

911
module.exports = mixinDiscovery;
1012

@@ -31,7 +33,7 @@ function mixinDiscovery(PostgreSQL) {
3133
* @param options {all: for all owners, owner: for a given owner}
3234
* @returns {string} The sql statement
3335
*/
34-
function queryTables(options) {
36+
PostgreSQL.prototype.queryTables = function(options) {
3537
var sqlTables = null;
3638
var owner = options.owner || options.schema;
3739

@@ -54,7 +56,7 @@ function mixinDiscovery(PostgreSQL) {
5456
* @param options {all: for all owners, owner: for a given owner}
5557
* @returns {string} The sql statement
5658
*/
57-
function queryViews(options) {
59+
PostgreSQL.prototype.queryViews = function(options) {
5860
var sqlViews = null;
5961
if (options.views) {
6062
var owner = options.owner || options.schema;
@@ -82,44 +84,14 @@ function mixinDiscovery(PostgreSQL) {
8284
* @param {Object} options Options for discovery
8385
* @param {Function} [cb] The callback function
8486
*/
85-
PostgreSQL.prototype.discoverModelDefinitions = function(options, cb) {
86-
if (!cb && typeof options === 'function') {
87-
cb = options;
88-
options = {};
89-
}
90-
options = options || {};
91-
92-
var self = this;
93-
var calls = [function(callback) {
94-
self.execute(queryTables(options), callback);
95-
}];
96-
97-
if (options.views) {
98-
calls.push(function(callback) {
99-
self.execute(queryViews(options), callback);
100-
});
101-
}
102-
async.parallel(calls, function(err, data) {
103-
if (err) {
104-
cb(err, data);
105-
} else {
106-
var merged = [];
107-
merged = merged.concat(data.shift());
108-
if (data.length) {
109-
merged = merged.concat(data.shift());
110-
}
111-
cb(err, merged);
112-
}
113-
});
114-
};
11587

11688
/*!
11789
* Normalize the arguments
11890
* @param table string, required
11991
* @param options object, optional
12092
* @param cb function, optional
12193
*/
122-
function getArgs(table, options, cb) {
94+
PostgreSQL.prototype.getArgs = function(table, options, cb) {
12395
if ('string' !== typeof table || !table) {
12496
throw new Error(g.f('{{table}} is a required string argument: %s' + table));
12597
}
@@ -145,7 +117,7 @@ function mixinDiscovery(PostgreSQL) {
145117
* @param table
146118
* @returns {String} The sql statement
147119
*/
148-
function queryColumns(owner, table) {
120+
PostgreSQL.prototype.queryColumns = function(owner, table) {
149121
var sql = null;
150122
if (owner) {
151123
sql = paginateSQL('SELECT table_schema AS "owner", table_name AS "tableName", column_name AS "columnName",'
@@ -173,26 +145,6 @@ function mixinDiscovery(PostgreSQL) {
173145
* @param {Function} [cb] The callback function
174146
*
175147
*/
176-
PostgreSQL.prototype.discoverModelProperties = function(table, options, cb) {
177-
var args = getArgs(table, options, cb);
178-
var owner = args.owner;
179-
table = args.table;
180-
options = args.options;
181-
cb = args.cb;
182-
183-
var sql = queryColumns(owner, table);
184-
var callback = function(err, results) {
185-
if (err) {
186-
cb(err, results);
187-
} else {
188-
results.map(function(r) {
189-
r.type = mysqlDataTypeToJSONType(r.dataType, r.dataLength);
190-
});
191-
cb(err, results);
192-
}
193-
};
194-
this.execute(sql, callback);
195-
};
196148

197149
// http://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html#getPrimaryKeys(java.lang.String, java.lang.String, java.lang.String)
198150

@@ -212,7 +164,7 @@ function mixinDiscovery(PostgreSQL) {
212164
* @param table
213165
* @returns {string}
214166
*/
215-
function queryForPrimaryKeys(owner, table) {
167+
PostgreSQL.prototype.queryForPrimaryKeys = function(owner, table) {
216168
var sql = 'SELECT kc.table_schema AS "owner", '
217169
+ 'kc.table_name AS "tableName", kc.column_name AS "columnName",'
218170
+ ' kc.ordinal_position AS "keySeq",'
@@ -239,16 +191,6 @@ function mixinDiscovery(PostgreSQL) {
239191
* @param {Object} options The options for discovery
240192
* @param {Function} [cb] The callback function
241193
*/
242-
PostgreSQL.prototype.discoverPrimaryKeys = function(table, options, cb) {
243-
var args = getArgs(table, options, cb);
244-
var owner = args.owner;
245-
table = args.table;
246-
options = args.options;
247-
cb = args.cb;
248-
249-
var sql = queryForPrimaryKeys(owner, table);
250-
this.execute(sql, cb);
251-
};
252194

253195
/*
254196
SELECT
@@ -270,7 +212,7 @@ function mixinDiscovery(PostgreSQL) {
270212
* @param table
271213
* @returns {string}
272214
*/
273-
function queryForeignKeys(owner, table) {
215+
PostgreSQL.prototype.queryForeignKeys = function(owner, table) {
274216
var sql =
275217
'SELECT tc.table_schema AS "fkOwner", tc.constraint_name AS "fkName", tc.table_name AS "fkTableName",'
276218
+ ' kcu.column_name AS "fkColumnName", kcu.ordinal_position AS "keySeq",'
@@ -297,16 +239,6 @@ function mixinDiscovery(PostgreSQL) {
297239
* @param {Object} options The options for discovery
298240
* @param {Function} [cb] The callback function
299241
*/
300-
PostgreSQL.prototype.discoverForeignKeys = function(table, options, cb) {
301-
var args = getArgs(table, options, cb);
302-
var owner = args.owner;
303-
table = args.table;
304-
options = args.options;
305-
cb = args.cb;
306-
307-
var sql = queryForeignKeys(owner, table);
308-
this.execute(sql, cb);
309-
};
310242

311243
/*!
312244
* Retrieves a description of the foreign key columns that reference the given table's primary key columns (the foreign keys exported by a table).
@@ -315,7 +247,7 @@ function mixinDiscovery(PostgreSQL) {
315247
* @param table
316248
* @returns {string}
317249
*/
318-
function queryExportedForeignKeys(owner, table) {
250+
PostgreSQL.prototype.queryExportedForeignKeys = function(owner, table) {
319251
var sql = 'SELECT kcu.constraint_name AS "fkName", kcu.table_schema AS "fkOwner", kcu.table_name AS "fkTableName",'
320252
+ ' kcu.column_name AS "fkColumnName", kcu.ordinal_position AS "keySeq",'
321253
+ ' \'PK\' AS "pkName", ccu.table_schema AS "pkOwner",'
@@ -342,18 +274,8 @@ function mixinDiscovery(PostgreSQL) {
342274
* @param {Object} options The options for discovery
343275
* @param {Function} [cb] The callback function
344276
*/
345-
PostgreSQL.prototype.discoverExportedForeignKeys = function(table, options, cb) {
346-
var args = getArgs(table, options, cb);
347-
var owner = args.owner;
348-
table = args.table;
349-
options = args.options;
350-
cb = args.cb;
351-
352-
var sql = queryExportedForeignKeys(owner, table);
353-
this.execute(sql, cb);
354-
};
355277

356-
function mysqlDataTypeToJSONType(mysqlType, dataLength) {
278+
PostgreSQL.prototype.mysqlDataTypeToJSONType = function(mysqlType, dataLength) {
357279
var type = mysqlType.toUpperCase();
358280
switch (type) {
359281
case 'BOOLEAN':

0 commit comments

Comments
 (0)