5
5
6
6
'use strict' ;
7
7
var g = require ( 'strong-globalize' ) ( ) ;
8
+ var SqlConnector = require ( 'loopback-connector' ) . SqlConnector ;
9
+ var ParameterizedSQL = SqlConnector . ParameterizedSQL ;
8
10
9
11
module . exports = mixinDiscovery ;
10
12
@@ -31,7 +33,7 @@ function mixinDiscovery(PostgreSQL) {
31
33
* @param options {all: for all owners, owner: for a given owner}
32
34
* @returns {string } The sql statement
33
35
*/
34
- function queryTables ( options ) {
36
+ PostgreSQL . prototype . queryTables = function ( options ) {
35
37
var sqlTables = null ;
36
38
var owner = options . owner || options . schema ;
37
39
@@ -54,7 +56,7 @@ function mixinDiscovery(PostgreSQL) {
54
56
* @param options {all: for all owners, owner: for a given owner}
55
57
* @returns {string } The sql statement
56
58
*/
57
- function queryViews ( options ) {
59
+ PostgreSQL . prototype . queryViews = function ( options ) {
58
60
var sqlViews = null ;
59
61
if ( options . views ) {
60
62
var owner = options . owner || options . schema ;
@@ -82,44 +84,14 @@ function mixinDiscovery(PostgreSQL) {
82
84
* @param {Object } options Options for discovery
83
85
* @param {Function } [cb] The callback function
84
86
*/
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
- } ;
115
87
116
88
/*!
117
89
* Normalize the arguments
118
90
* @param table string, required
119
91
* @param options object, optional
120
92
* @param cb function, optional
121
93
*/
122
- function getArgs ( table , options , cb ) {
94
+ PostgreSQL . prototype . getArgs = function ( table , options , cb ) {
123
95
if ( 'string' !== typeof table || ! table ) {
124
96
throw new Error ( g . f ( '{{table}} is a required string argument: %s' + table ) ) ;
125
97
}
@@ -145,7 +117,7 @@ function mixinDiscovery(PostgreSQL) {
145
117
* @param table
146
118
* @returns {String } The sql statement
147
119
*/
148
- function queryColumns ( owner , table ) {
120
+ PostgreSQL . prototype . queryColumns = function ( owner , table ) {
149
121
var sql = null ;
150
122
if ( owner ) {
151
123
sql = paginateSQL ( 'SELECT table_schema AS "owner", table_name AS "tableName", column_name AS "columnName",'
@@ -173,26 +145,6 @@ function mixinDiscovery(PostgreSQL) {
173
145
* @param {Function } [cb] The callback function
174
146
*
175
147
*/
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
- } ;
196
148
197
149
// http://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html#getPrimaryKeys(java.lang.String, java.lang.String, java.lang.String)
198
150
@@ -212,7 +164,7 @@ function mixinDiscovery(PostgreSQL) {
212
164
* @param table
213
165
* @returns {string }
214
166
*/
215
- function queryForPrimaryKeys ( owner , table ) {
167
+ PostgreSQL . prototype . queryForPrimaryKeys = function ( owner , table ) {
216
168
var sql = 'SELECT kc.table_schema AS "owner", '
217
169
+ 'kc.table_name AS "tableName", kc.column_name AS "columnName",'
218
170
+ ' kc.ordinal_position AS "keySeq",'
@@ -239,16 +191,6 @@ function mixinDiscovery(PostgreSQL) {
239
191
* @param {Object } options The options for discovery
240
192
* @param {Function } [cb] The callback function
241
193
*/
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
- } ;
252
194
253
195
/*
254
196
SELECT
@@ -270,7 +212,7 @@ function mixinDiscovery(PostgreSQL) {
270
212
* @param table
271
213
* @returns {string }
272
214
*/
273
- function queryForeignKeys ( owner , table ) {
215
+ PostgreSQL . prototype . queryForeignKeys = function ( owner , table ) {
274
216
var sql =
275
217
'SELECT tc.table_schema AS "fkOwner", tc.constraint_name AS "fkName", tc.table_name AS "fkTableName",'
276
218
+ ' kcu.column_name AS "fkColumnName", kcu.ordinal_position AS "keySeq",'
@@ -297,16 +239,6 @@ function mixinDiscovery(PostgreSQL) {
297
239
* @param {Object } options The options for discovery
298
240
* @param {Function } [cb] The callback function
299
241
*/
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
- } ;
310
242
311
243
/*!
312
244
* 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) {
315
247
* @param table
316
248
* @returns {string }
317
249
*/
318
- function queryExportedForeignKeys ( owner , table ) {
250
+ PostgreSQL . prototype . queryExportedForeignKeys = function ( owner , table ) {
319
251
var sql = 'SELECT kcu.constraint_name AS "fkName", kcu.table_schema AS "fkOwner", kcu.table_name AS "fkTableName",'
320
252
+ ' kcu.column_name AS "fkColumnName", kcu.ordinal_position AS "keySeq",'
321
253
+ ' \'PK\' AS "pkName", ccu.table_schema AS "pkOwner",'
@@ -342,18 +274,8 @@ function mixinDiscovery(PostgreSQL) {
342
274
* @param {Object } options The options for discovery
343
275
* @param {Function } [cb] The callback function
344
276
*/
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
- } ;
355
277
356
- function mysqlDataTypeToJSONType ( mysqlType , dataLength ) {
278
+ PostgreSQL . prototype . mysqlDataTypeToJSONType = function ( mysqlType , dataLength ) {
357
279
var type = mysqlType . toUpperCase ( ) ;
358
280
switch ( type ) {
359
281
case 'BOOLEAN' :
0 commit comments