discovery.js
12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
// Node module: loopback-connector-mysql
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
var g = require('strong-globalize')();
module.exports = mixinDiscovery;
/*!
* @param {MySQL} MySQL connector class
* @param {Object} mysql mysql driver
*/
function mixinDiscovery(MySQL, mysql) {
var async = require('async');
function paginateSQL(sql, orderBy, options) {
options = options || {};
var limitClause = '';
if (options.offset || options.skip || options.limit) {
// Offset starts from 0
var offset = Number(options.offset || options.skip || 0);
if (isNaN(offset)) {
offset = 0;
}
limitClause = ' LIMIT ' + offset;
if (options.limit) {
var limit = Number(options.limit);
if (isNaN(limit)) {
limit = 0;
}
limitClause = limitClause + ',' + limit;
}
}
if (!orderBy) {
sql += ' ORDER BY ' + orderBy;
}
return sql + limitClause;
}
/*!
* Build sql for listing schemas (databases in MySQL)
* @params {Object} [options] Options object
* @returns {String} The SQL statement
*/
MySQL.prototype.buildQuerySchemas = function(options) {
var sql = 'SELECT catalog_name as "catalog",' +
' schema_name as "schema"' +
' FROM information_schema.schemata';
return paginateSQL(sql, 'schema_name', options);
};
/*!
* Build sql for listing tables
* @param options {all: for all owners, owner: for a given owner}
* @returns {string} The sql statement
*/
MySQL.prototype.buildQueryTables = function(options) {
var sqlTables = null;
var schema = options.owner || options.schema;
if (options.all && !schema) {
sqlTables = paginateSQL('SELECT \'table\' AS "type",' +
' table_name AS "name", table_schema AS "owner"' +
' FROM information_schema.tables',
'table_schema, table_name', options);
} else if (schema) {
sqlTables = paginateSQL('SELECT \'table\' AS "type",' +
' table_name AS "name", table_schema AS "schema"' +
' FROM information_schema.tables' +
' WHERE table_schema=' + mysql.escape(schema),
'table_schema, table_name', options);
} else {
sqlTables = paginateSQL('SELECT \'table\' AS "type",' +
' table_name AS "name", ' +
' table_schema AS "owner" FROM information_schema.tables' +
' WHERE table_schema=SUBSTRING_INDEX(USER(),\'@\',1)',
'table_name', options);
}
return sqlTables;
};
/*!
* Build sql for listing views
* @param options {all: for all owners, owner: for a given owner}
* @returns {string} The sql statement
*/
MySQL.prototype.buildQueryViews = function(options) {
var sqlViews = null;
if (options.views) {
var schema = options.owner || options.schema;
if (options.all && !schema) {
sqlViews = paginateSQL('SELECT \'view\' AS "type",' +
' table_name AS "name",' +
' table_schema AS "owner"' +
' FROM information_schema.views',
'table_schema, table_name', options);
} else if (schema) {
sqlViews = paginateSQL('SELECT \'view\' AS "type",' +
' table_name AS "name",' +
' table_schema AS "owner"' +
' FROM information_schema.views' +
' WHERE table_schema=' + mysql.escape(schema),
'table_schema, table_name', options);
} else {
sqlViews = paginateSQL('SELECT \'view\' AS "type",' +
' table_name AS "name",' +
' table_schema AS "owner"' +
' FROM information_schema.views',
'table_name', options);
}
}
return sqlViews;
};
/**
* Discover model definitions
*
* @param {Object} options Options for discovery
* @param {Function} [cb] The callback function
*/
/*!
* Normalize the arguments
* @param table string, required
* @param options object, optional
* @param cb function, optional
*/
MySQL.prototype.getArgs = function(table, options, cb) {
if ('string' !== typeof table || !table) {
throw new Error(g.f('{{table}} is a required string argument: %s', table));
}
options = options || {};
if (!cb && 'function' === typeof options) {
cb = options;
options = {};
}
if (typeof options !== 'object') {
throw new Error(g.f('{{options}} must be an {{object}}: %s', options));
}
return {
schema: options.owner || options.schema,
table: table,
options: options,
cb: cb,
};
};
/*!
* Build the sql statement to query columns for a given table
* @param schema
* @param table
* @returns {String} The sql statement
*/
MySQL.prototype.buildQueryColumns = function(schema, table) {
var sql = null;
if (schema) {
sql = paginateSQL('SELECT table_schema AS "owner",' +
' table_name AS "tableName",' +
' column_name AS "columnName",' +
' data_type AS "dataType",' +
' character_maximum_length AS "dataLength",' +
' numeric_precision AS "dataPrecision",' +
' numeric_scale AS "dataScale",' +
' column_type AS "columnType",' +
' is_nullable = \'YES\' AS "nullable",' +
' CASE WHEN extra LIKE \'%auto_increment%\' THEN 1 ELSE 0 END AS "generated"' +
' FROM information_schema.columns' +
' WHERE table_schema=' + mysql.escape(schema) +
(table ? ' AND table_name=' + mysql.escape(table) : ''),
'table_name, ordinal_position', {});
} else {
sql = paginateSQL('SELECT table_schema AS "owner",' +
' table_name AS "tableName",' +
' column_name AS "columnName",' +
' data_type AS "dataType",' +
' character_maximum_length AS "dataLength",' +
' numeric_precision AS "dataPrecision",' +
' numeric_scale AS "dataScale",' +
' column_type AS "columnType",' +
' is_nullable = \'YES\' AS "nullable",' +
' CASE WHEN extra LIKE \'%auto_increment%\' THEN 1 ELSE 0 END AS "generated"' +
' FROM information_schema.columns' +
(table ? ' WHERE table_name=' + mysql.escape(table) : ''),
'table_name, ordinal_position', {});
}
return sql;
};
/**
* Discover model properties from a table
* @param {String} table The table name
* @param {Object} options The options for discovery
* @param {Function} [cb] The callback function
*
*/
/*!
* Build the sql statement for querying primary keys of a given table
* @param schema
* @param table
* @returns {string}
*/
// http://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html
// #getPrimaryKeys(java.lang.String, java.lang.String, java.lang.String)
MySQL.prototype.buildQueryPrimaryKeys = function(schema, table) {
var sql = 'SELECT table_schema AS "owner",' +
' table_name AS "tableName",' +
' column_name AS "columnName",' +
' ordinal_position AS "keySeq",' +
' constraint_name AS "pkName"' +
' FROM information_schema.key_column_usage' +
' WHERE constraint_name=\'PRIMARY\'';
if (schema) {
sql += ' AND table_schema=' + mysql.escape(schema);
}
if (table) {
sql += ' AND table_name=' + mysql.escape(table);
}
sql += ' ORDER BY' +
' table_schema, constraint_name, table_name, ordinal_position';
return sql;
};
/**
* Discover primary keys for a given table
* @param {String} table The table name
* @param {Object} options The options for discovery
* @param {Function} [cb] The callback function
*/
/*!
* Build the sql statement for querying foreign keys of a given table
* @param schema
* @param table
* @returns {string}
*/
MySQL.prototype.buildQueryForeignKeys = function(schema, table) {
var sql =
'SELECT table_schema AS "fkOwner",' +
' constraint_name AS "fkName",' +
' table_name AS "fkTableName",' +
' column_name AS "fkColumnName",' +
' ordinal_position AS "keySeq",' +
' referenced_table_schema AS "pkOwner", \'PRIMARY\' AS "pkName",' +
' referenced_table_name AS "pkTableName",' +
' referenced_column_name AS "pkColumnName"' +
' FROM information_schema.key_column_usage' +
' WHERE constraint_name!=\'PRIMARY\'' +
' AND POSITION_IN_UNIQUE_CONSTRAINT IS NOT NULL';
if (schema) {
sql += ' AND table_schema=' + mysql.escape(schema);
}
if (table) {
sql += ' AND table_name=' + mysql.escape(table);
}
return sql;
};
/**
* Discover foreign keys for a given table
* @param {String} table The table name
* @param {Object} options The options for discovery
* @param {Function} [cb] The callback function
*/
/*!
* Retrieves a description of the foreign key columns that reference the
* given table's primary key columns (the foreign keys exported by a table).
* They are ordered by fkTableOwner, fkTableName, and keySeq.
* @param schema
* @param table
* @returns {string}
*/
MySQL.prototype.buildQueryExportedForeignKeys = function(schema, table) {
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 information_schema.key_column_usage a' +
' WHERE a.position_in_unique_constraint IS NOT NULL';
if (schema) {
sql += ' AND a.referenced_table_schema=' + mysql.escape(schema);
}
if (table) {
sql += ' AND a.referenced_table_name=' + mysql.escape(table);
}
sql += ' ORDER BY a.table_schema, a.table_name, a.ordinal_position';
return sql;
};
/**
* Discover foreign keys that reference to the primary key of this table
* @param {String} table The table name
* @param {Object} options The options for discovery
* @param {Function} [cb] The callback function
*/
MySQL.prototype.buildPropertyType = function(columnDefinition, options) {
var mysqlType = columnDefinition.dataType;
var columnType = columnDefinition.columnType;
var dataLength = columnDefinition.dataLength;
var type = mysqlType.toUpperCase();
switch (type) {
case 'CHAR':
if (!options.treatCHAR1AsString && columnType === 'char(1)') {
// Treat char(1) as boolean ('Y', 'N', 'T', 'F', '0', '1')
return 'Boolean';
}
case 'VARCHAR':
case 'TINYTEXT':
case 'MEDIUMTEXT':
case 'LONGTEXT':
case 'TEXT':
case 'ENUM':
case 'SET':
return 'String';
case 'TINYBLOB':
case 'MEDIUMBLOB':
case 'LONGBLOB':
case 'BLOB':
case 'BINARY':
case 'VARBINARY':
case 'BIT':
// treat BIT(1) as boolean as it's 1 or 0
if (!options.treatBIT1AsBit && columnType === 'bit(1)') {
return 'Boolean';
}
return 'Binary';
case 'TINYINT':
// treat TINYINT(1) as boolean as it is aliased as BOOL and BOOLEAN in mysql
if (!options.treatTINYINT1AsTinyInt && columnType === 'tinyint(1)') {
return 'Boolean';
}
case 'SMALLINT':
case 'INT':
case 'MEDIUMINT':
case 'YEAR':
case 'FLOAT':
case 'DOUBLE':
case 'BIGINT':
return 'Number';
case 'DATE':
case 'TIMESTAMP':
case 'DATETIME':
return 'Date';
case 'POINT':
return 'GeoPoint';
case 'BOOL':
case 'BOOLEAN':
return 'Boolean';
default:
return 'String';
}
};
MySQL.prototype.getDefaultSchema = function() {
if (this.dataSource && this.dataSource.settings &&
this.dataSource.settings.database) {
return this.dataSource.settings.database;
}
return undefined;
};
// Recommended MySQL 5.7 Boolean scheme. See
// http://dev.mysql.com/doc/refman/5.7/en/numeric-type-overview.html
// Currently default is the inverse of the recommendation for backward compatibility.
MySQL.prototype.setDefaultOptions = function(options) {
var defaultOptions = {
treatCHAR1AsString: false,
treatBIT1AsBit: true,
treatTINYINT1AsTinyInt: true,
};
for (var opt in defaultOptions) {
if (defaultOptions.hasOwnProperty(opt) && !options.hasOwnProperty(opt)) {
options[opt] = defaultOptions[opt];
}
}
};
MySQL.prototype.setNullableProperty = function(r) {
r.nullable = r.nullable ? 'Y' : 'N';
};
}