3
3
// This file is licensed under the Artistic License 2.0.
4
4
// License text available at https://opensource.org/licenses/Artistic-2.0
5
5
6
- 'use strict' ;
7
6
var g = require ( 'strong-globalize' ) ( ) ;
8
7
9
8
module . exports = mixinDiscovery ;
@@ -57,6 +56,7 @@ function mixinDiscovery(PostgreSQL) {
57
56
function queryViews ( options ) {
58
57
var sqlViews = null ;
59
58
if ( options . views ) {
59
+
60
60
var owner = options . owner || options . schema ;
61
61
62
62
if ( options . all && ! owner ) {
@@ -82,24 +82,24 @@ function mixinDiscovery(PostgreSQL) {
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 ) {
85
+ PostgreSQL . prototype . discoverModelDefinitions = function ( options , cb ) {
86
86
if ( ! cb && typeof options === 'function' ) {
87
87
cb = options ;
88
88
options = { } ;
89
89
}
90
90
options = options || { } ;
91
91
92
92
var self = this ;
93
- var calls = [ function ( callback ) {
93
+ var calls = [ function ( callback ) {
94
94
self . execute ( queryTables ( options ) , callback ) ;
95
95
} ] ;
96
96
97
97
if ( options . views ) {
98
- calls . push ( function ( callback ) {
98
+ calls . push ( function ( callback ) {
99
99
self . execute ( queryViews ( options ) , callback ) ;
100
100
} ) ;
101
101
}
102
- async . parallel ( calls , function ( err , data ) {
102
+ async . parallel ( calls , function ( err , data ) {
103
103
if ( err ) {
104
104
cb ( err , data ) ;
105
105
} else {
@@ -135,7 +135,7 @@ function mixinDiscovery(PostgreSQL) {
135
135
owner : options . owner || options . schema ,
136
136
table : table ,
137
137
options : options ,
138
- cb : cb ,
138
+ cb : cb
139
139
} ;
140
140
}
141
141
@@ -148,17 +148,15 @@ function mixinDiscovery(PostgreSQL) {
148
148
function queryColumns ( owner , table ) {
149
149
var sql = null ;
150
150
if ( owner ) {
151
- sql = paginateSQL ( 'SELECT table_schema AS "owner", table_name AS "tableName", column_name AS "columnName",'
152
- + 'data_type AS "dataType", character_maximum_length AS "dataLength", numeric_precision AS "dataPrecision",'
153
- + ' numeric_scale AS "dataScale", is_nullable AS "nullable"'
151
+ sql = paginateSQL ( 'SELECT table_schema AS "owner", table_name AS "tableName", column_name AS "columnName", data_type AS "dataType",'
152
+ + ' character_maximum_length AS "dataLength", numeric_precision AS "dataPrecision", numeric_scale AS "dataScale", is_nullable AS "nullable"'
154
153
+ ' FROM information_schema.columns'
155
154
+ ' WHERE table_schema=\'' + owner + '\''
156
155
+ ( table ? ' AND table_name=\'' + table + '\'' : '' ) ,
157
156
'table_name, ordinal_position' , { } ) ;
158
157
} else {
159
- sql = paginateSQL ( 'SELECT current_schema() AS "owner", table_name AS "tableName", column_name AS "columnName",'
160
- + ' data_type AS "dataType", character_maximum_length AS "dataLength", numeric_precision AS "dataPrecision",'
161
- + ' numeric_scale AS "dataScale", is_nullable AS "nullable"'
158
+ sql = paginateSQL ( 'SELECT current_schema() AS "owner", table_name AS "tableName", column_name AS "columnName", data_type AS "dataType",'
159
+ + ' character_maximum_length AS "dataLength", numeric_precision AS "dataPrecision", numeric_scale AS "dataScale", is_nullable AS "nullable"'
162
160
+ ' FROM information_schema.columns'
163
161
+ ( table ? ' WHERE table_name=\'' + table + '\'' : '' ) ,
164
162
'table_name, ordinal_position' , { } ) ;
@@ -173,19 +171,19 @@ function mixinDiscovery(PostgreSQL) {
173
171
* @param {Function } [cb] The callback function
174
172
*
175
173
*/
176
- PostgreSQL . prototype . discoverModelProperties = function ( table , options , cb ) {
174
+ PostgreSQL . prototype . discoverModelProperties = function ( table , options , cb ) {
177
175
var args = getArgs ( table , options , cb ) ;
178
176
var owner = args . owner ;
179
177
table = args . table ;
180
178
options = args . options ;
181
179
cb = args . cb ;
182
180
183
181
var sql = queryColumns ( owner , table ) ;
184
- var callback = function ( err , results ) {
182
+ var callback = function ( err , results ) {
185
183
if ( err ) {
186
184
cb ( err , results ) ;
187
185
} else {
188
- results . map ( function ( r ) {
186
+ results . map ( function ( r ) {
189
187
r . type = mysqlDataTypeToJSONType ( r . dataType , r . dataLength ) ;
190
188
} ) ;
191
189
cb ( err , results ) ;
@@ -194,6 +192,7 @@ function mixinDiscovery(PostgreSQL) {
194
192
this . execute ( sql , callback ) ;
195
193
} ;
196
194
195
+
197
196
// http://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html#getPrimaryKeys(java.lang.String, java.lang.String, java.lang.String)
198
197
199
198
/*
@@ -239,7 +238,7 @@ function mixinDiscovery(PostgreSQL) {
239
238
* @param {Object } options The options for discovery
240
239
* @param {Function } [cb] The callback function
241
240
*/
242
- PostgreSQL . prototype . discoverPrimaryKeys = function ( table , options , cb ) {
241
+ PostgreSQL . prototype . discoverPrimaryKeys = function ( table , options , cb ) {
243
242
var args = getArgs ( table , options , cb ) ;
244
243
var owner = args . owner ;
245
244
table = args . table ;
@@ -297,7 +296,7 @@ function mixinDiscovery(PostgreSQL) {
297
296
* @param {Object } options The options for discovery
298
297
* @param {Function } [cb] The callback function
299
298
*/
300
- PostgreSQL . prototype . discoverForeignKeys = function ( table , options , cb ) {
299
+ PostgreSQL . prototype . discoverForeignKeys = function ( table , options , cb ) {
301
300
var args = getArgs ( table , options , cb ) ;
302
301
var owner = args . owner ;
303
302
table = args . table ;
@@ -342,7 +341,7 @@ function mixinDiscovery(PostgreSQL) {
342
341
* @param {Object } options The options for discovery
343
342
* @param {Function } [cb] The callback function
344
343
*/
345
- PostgreSQL . prototype . discoverExportedForeignKeys = function ( table , options , cb ) {
344
+ PostgreSQL . prototype . discoverExportedForeignKeys = function ( table , options , cb ) {
346
345
var args = getArgs ( table , options , cb ) ;
347
346
var owner = args . owner ;
348
347
table = args . table ;
@@ -386,19 +385,21 @@ function mixinDiscovery(PostgreSQL) {
386
385
}
387
386
}
388
387
388
+
389
389
/**
390
390
* Discover database indexes for the specified table
391
391
* @param {String } table The table name
392
392
* @param {Function } [cb] The callback function
393
393
*/
394
- PostgreSQL . prototype . discoverModelIndexes = function ( model , cb ) {
394
+ PostgreSQL . prototype . discoverModelIndexes = function ( model , cb ) {
395
395
this . getTableStatus ( model , function ( err , fields , indexes ) {
396
396
var indexData = { } ;
397
- indexes . forEach ( function ( index ) {
397
+ indexes . forEach ( function ( index ) {
398
398
indexData [ index . name ] = index ;
399
399
delete index . name ;
400
400
} ) ;
401
401
cb ( err , indexData ) ;
402
402
} ) ;
403
- } ;
403
+ }
404
+
404
405
}
0 commit comments