@@ -11,7 +11,7 @@ module.exports = mixinDiscovery;
11
11
function mixinDiscovery ( PostgreSQL ) {
12
12
var async = require ( 'async' ) ;
13
13
14
- function paginateSQL ( sql , orderBy , options ) {
14
+ PostgreSQL . prototype . paginateSQL = function ( sql , orderBy , options ) {
15
15
options = options || { } ;
16
16
var limit = '' ;
17
17
if ( options . offset || options . skip || options . limit ) {
@@ -24,102 +24,72 @@ function mixinDiscovery(PostgreSQL) {
24
24
sql += ' ORDER BY ' + orderBy ;
25
25
}
26
26
return sql + limit ;
27
- }
27
+ } ;
28
28
29
29
/*!
30
30
* Build sql for listing tables
31
31
* @param options {all: for all owners, owner: for a given owner}
32
32
* @returns {string } The sql statement
33
33
*/
34
- function queryTables ( options ) {
34
+ PostgreSQL . prototype . buildQueryTables = function ( options ) {
35
35
var sqlTables = null ;
36
36
var owner = options . owner || options . schema ;
37
37
38
38
if ( options . all && ! owner ) {
39
- sqlTables = paginateSQL ( 'SELECT \'table\' AS "type", table_name AS "name", table_schema AS "owner"'
39
+ sqlTables = this . paginateSQL ( 'SELECT \'table\' AS "type", table_name AS "name", table_schema AS "owner"'
40
40
+ ' FROM information_schema.tables' , 'table_schema, table_name' , options ) ;
41
41
} else if ( owner ) {
42
- sqlTables = paginateSQL ( 'SELECT \'table\' AS "type", table_name AS "name", table_schema AS "owner"'
42
+ sqlTables = this . paginateSQL ( 'SELECT \'table\' AS "type", table_name AS "name", table_schema AS "owner"'
43
43
+ ' FROM information_schema.tables WHERE table_schema=\'' + owner + '\'' , 'table_schema, table_name' , options ) ;
44
44
} else {
45
- sqlTables = paginateSQL ( 'SELECT \'table\' AS "type", table_name AS "name",'
45
+ sqlTables = this . paginateSQL ( 'SELECT \'table\' AS "type", table_name AS "name",'
46
46
+ ' table_schema AS "owner" FROM information_schema.tables WHERE table_schema=current_schema()' ,
47
47
'table_name' , options ) ;
48
48
}
49
49
return sqlTables ;
50
- }
50
+ } ;
51
51
52
52
/*!
53
53
* Build sql for listing views
54
54
* @param options {all: for all owners, owner: for a given owner}
55
55
* @returns {string } The sql statement
56
56
*/
57
- function queryViews ( options ) {
57
+ PostgreSQL . prototype . buildQueryViews = function ( options ) {
58
58
var sqlViews = null ;
59
59
if ( options . views ) {
60
60
var owner = options . owner || options . schema ;
61
61
62
62
if ( options . all && ! owner ) {
63
- sqlViews = paginateSQL ( 'SELECT \'view\' AS "type", table_name AS "name",'
63
+ sqlViews = this . paginateSQL ( 'SELECT \'view\' AS "type", table_name AS "name",'
64
64
+ ' table_schema AS "owner" FROM information_schema.views' ,
65
65
'table_schema, table_name' , options ) ;
66
66
} else if ( owner ) {
67
- sqlViews = paginateSQL ( 'SELECT \'view\' AS "type", table_name AS "name",'
67
+ sqlViews = this . paginateSQL ( 'SELECT \'view\' AS "type", table_name AS "name",'
68
68
+ ' table_schema AS "owner" FROM information_schema.views WHERE table_schema=\'' + owner + '\'' ,
69
69
'table_schema, table_name' , options ) ;
70
70
} else {
71
- sqlViews = paginateSQL ( 'SELECT \'view\' AS "type", table_name AS "name",'
71
+ sqlViews = this . paginateSQL ( 'SELECT \'view\' AS "type", table_name AS "name",'
72
72
+ ' current_schema() AS "owner" FROM information_schema.views' ,
73
73
'table_name' , options ) ;
74
74
}
75
75
}
76
76
return sqlViews ;
77
- }
77
+ } ;
78
78
79
79
/**
80
80
* Discover model definitions
81
81
*
82
82
* @param {Object } options Options for discovery
83
83
* @param {Function } [cb] The callback function
84
84
*/
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
85
116
86
/*!
117
87
* Normalize the arguments
118
88
* @param table string, required
119
89
* @param options object, optional
120
90
* @param cb function, optional
121
91
*/
122
- function getArgs ( table , options , cb ) {
92
+ PostgreSQL . prototype . getArgs = function ( table , options , cb ) {
123
93
if ( 'string' !== typeof table || ! table ) {
124
94
throw new Error ( g . f ( '{{table}} is a required string argument: %s' + table ) ) ;
125
95
}
@@ -133,38 +103,40 @@ function mixinDiscovery(PostgreSQL) {
133
103
}
134
104
return {
135
105
owner : options . owner || options . schema ,
106
+ schema : options . owner || options . schema ,
136
107
table : table ,
137
108
options : options ,
138
109
cb : cb ,
139
110
} ;
140
- }
111
+ } ;
141
112
142
113
/*!
143
114
* Build the sql statement to query columns for a given table
144
115
* @param owner
145
116
* @param table
146
117
* @returns {String } The sql statement
147
118
*/
148
- function queryColumns ( owner , table ) {
119
+ PostgreSQL . prototype . buildQueryColumns = function ( owner , table ) {
149
120
var sql = null ;
150
121
if ( owner ) {
151
- sql = paginateSQL ( 'SELECT table_schema AS "owner", table_name AS "tableName", column_name AS "columnName",'
122
+ sql = this . paginateSQL ( 'SELECT table_schema AS "owner", table_name AS "tableName", column_name AS "columnName",'
152
123
+ 'data_type AS "dataType", character_maximum_length AS "dataLength", numeric_precision AS "dataPrecision",'
153
124
+ ' numeric_scale AS "dataScale", is_nullable AS "nullable"'
154
125
+ ' FROM information_schema.columns'
155
126
+ ' WHERE table_schema=\'' + owner + '\''
156
127
+ ( table ? ' AND table_name=\'' + table + '\'' : '' ) ,
157
128
'table_name, ordinal_position' , { } ) ;
158
129
} else {
159
- sql = paginateSQL ( 'SELECT current_schema() AS "owner", table_name AS "tableName", column_name AS "columnName",'
130
+ sql = this . paginateSQL ( 'SELECT current_schema() AS "owner", table_name AS "tableName",'
131
+ + ' column_name AS "columnName",'
160
132
+ ' data_type AS "dataType", character_maximum_length AS "dataLength", numeric_precision AS "dataPrecision",'
161
133
+ ' numeric_scale AS "dataScale", is_nullable AS "nullable"'
162
134
+ ' FROM information_schema.columns'
163
135
+ ( table ? ' WHERE table_name=\'' + table + '\'' : '' ) ,
164
136
'table_name, ordinal_position' , { } ) ;
165
137
}
166
138
return sql ;
167
- }
139
+ } ;
168
140
169
141
/**
170
142
* Discover model properties from a table
@@ -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 . buildQueryPrimaryKeys = 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",'
@@ -231,24 +183,14 @@ function mixinDiscovery(PostgreSQL) {
231
183
}
232
184
sql += ' ORDER BY kc.table_schema, kc.table_name, kc.ordinal_position' ;
233
185
return sql ;
234
- }
186
+ } ;
235
187
236
188
/**
237
189
* Discover primary keys for a given table
238
190
* @param {String } table The table name
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 . buildQueryForeignKeys = 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",'
@@ -289,24 +231,14 @@ function mixinDiscovery(PostgreSQL) {
289
231
sql += ' AND tc.table_name=\'' + table + '\'' ;
290
232
}
291
233
return sql ;
292
- }
234
+ } ;
293
235
294
236
/**
295
237
* Discover foreign keys for a given table
296
238
* @param {String } table The table name
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 . buildQueryExportedForeignKeys = 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",'
@@ -334,26 +266,17 @@ function mixinDiscovery(PostgreSQL) {
334
266
sql += ' order by kcu.table_schema, kcu.table_name, kcu.ordinal_position' ;
335
267
336
268
return sql ;
337
- }
269
+ } ;
338
270
339
271
/**
340
272
* Discover foreign keys that reference to the primary key of this table
341
273
* @param {String } table The table name
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
277
352
- var sql = queryExportedForeignKeys ( owner , table ) ;
353
- this . execute ( sql , cb ) ;
354
- } ;
355
-
356
- function mysqlDataTypeToJSONType ( mysqlType , dataLength ) {
278
+ PostgreSQL . prototype . buildPropertyType = function ( columnDefinition , dataLength ) {
279
+ var mysqlType = columnDefinition . dataType ;
357
280
var type = mysqlType . toUpperCase ( ) ;
358
281
switch ( type ) {
359
282
case 'BOOLEAN' :
@@ -384,7 +307,7 @@ function mixinDiscovery(PostgreSQL) {
384
307
default :
385
308
return 'String' ;
386
309
}
387
- }
310
+ } ;
388
311
389
312
/**
390
313
* Discover database indexes for the specified table
@@ -401,4 +324,14 @@ function mixinDiscovery(PostgreSQL) {
401
324
cb ( err , indexData ) ;
402
325
} ) ;
403
326
} ;
327
+
328
+ PostgreSQL . prototype . setDefaultOptions = function ( options ) {
329
+ } ;
330
+
331
+ PostgreSQL . prototype . setNullableProperty = function ( property ) {
332
+ } ;
333
+
334
+ PostgreSQL . prototype . getDefaultSchema = function ( ) {
335
+ return '' ;
336
+ } ;
404
337
}
0 commit comments