2019-04-18 21:32:38 +00:00
|
|
|
// Copyright IBM Corp. 2015,2019. All Rights Reserved.
|
2016-05-06 04:50:59 +00:00
|
|
|
// Node module: loopback-connector
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2017-03-06 23:40:47 +00:00
|
|
|
'use strict';
|
2019-08-02 13:50:51 +00:00
|
|
|
const assert = require('assert');
|
2020-07-13 23:03:05 +00:00
|
|
|
const util = require('util');
|
2019-08-02 13:50:51 +00:00
|
|
|
const PLACEHOLDER = '?';
|
2015-05-13 17:14:44 +00:00
|
|
|
|
|
|
|
module.exports = ParameterizedSQL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class for parameterized SQL clauses
|
|
|
|
* @param {String|Object} sql The SQL clause. If the value is a string, treat
|
|
|
|
* it as the template using `?` as the placeholder, for example, `(?,?)`. If
|
|
|
|
* the value is an object, treat it as {sql: '...', params: [...]}
|
2017-06-22 17:06:24 +00:00
|
|
|
* @param {Array} params An array of parameter values. The length should match the
|
2015-05-13 17:14:44 +00:00
|
|
|
* number of placeholders in the template
|
|
|
|
* @returns {ParameterizedSQL} A new instance of ParameterizedSQL
|
2017-06-22 17:06:24 +00:00
|
|
|
* @class
|
2015-05-13 17:14:44 +00:00
|
|
|
*/
|
|
|
|
function ParameterizedSQL(sql, params) {
|
|
|
|
if (!(this instanceof ParameterizedSQL)) {
|
|
|
|
return new ParameterizedSQL(sql, params);
|
|
|
|
}
|
|
|
|
sql = sql || '';
|
|
|
|
if (arguments.length === 1 && typeof sql === 'object') {
|
|
|
|
this.sql = sql.sql;
|
|
|
|
this.params = sql.params || [];
|
|
|
|
} else {
|
|
|
|
this.sql = sql;
|
|
|
|
this.params = params || [];
|
|
|
|
}
|
|
|
|
assert(typeof this.sql === 'string', 'sql must be a string');
|
|
|
|
assert(Array.isArray(this.params), 'params must be an array');
|
|
|
|
|
2019-08-02 13:50:51 +00:00
|
|
|
const parts = this.sql.split(PLACEHOLDER);
|
2020-07-13 23:03:05 +00:00
|
|
|
if (parts.length - 1 !== this.params.length) {
|
|
|
|
throw new assert.AssertionError({
|
|
|
|
message: util.format(
|
|
|
|
'The number of ? (%s) in the sql (%s) must match the number of params (%s) %o',
|
|
|
|
parts.length - 1,
|
|
|
|
this.sql,
|
|
|
|
this.params.length,
|
|
|
|
this.params,
|
|
|
|
),
|
|
|
|
actual: this.params.length,
|
|
|
|
expected: parts.length - 1,
|
|
|
|
});
|
|
|
|
}
|
2015-05-13 17:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge the parameterized sqls into the current instance
|
|
|
|
* @param {Object|Object[]} ps A parametered SQL or an array of parameterized
|
|
|
|
* SQLs
|
|
|
|
* @param {String} [separator] Separator, default to ` `
|
|
|
|
* @returns {ParameterizedSQL} The current instance
|
|
|
|
*/
|
|
|
|
ParameterizedSQL.prototype.merge = function(ps, separator) {
|
|
|
|
if (Array.isArray(ps)) {
|
|
|
|
return this.constructor.append(this,
|
|
|
|
this.constructor.join(ps, separator), separator);
|
|
|
|
} else {
|
|
|
|
return this.constructor.append(this, ps, separator);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ParameterizedSQL.prototype.toJSON = function() {
|
|
|
|
return {
|
|
|
|
sql: this.sql,
|
2016-04-09 18:35:52 +00:00
|
|
|
params: this.params,
|
2015-05-13 17:14:44 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append the statement into the current statement
|
|
|
|
* @param {Object} currentStmt The current SQL statement
|
|
|
|
* @param {Object} stmt The statement to be appended
|
|
|
|
* @param {String} [separator] Separator, default to ` `
|
|
|
|
* @returns {*} The merged statement
|
|
|
|
*/
|
|
|
|
ParameterizedSQL.append = function(currentStmt, stmt, separator) {
|
|
|
|
currentStmt = (currentStmt instanceof ParameterizedSQL) ?
|
|
|
|
currentStmt : new ParameterizedSQL(currentStmt);
|
|
|
|
stmt = (stmt instanceof ParameterizedSQL) ? stmt :
|
|
|
|
new ParameterizedSQL(stmt);
|
|
|
|
separator = typeof separator === 'string' ? separator : ' ';
|
|
|
|
if (currentStmt.sql) {
|
|
|
|
currentStmt.sql += separator;
|
|
|
|
}
|
|
|
|
if (stmt.sql) {
|
|
|
|
currentStmt.sql += stmt.sql;
|
|
|
|
}
|
|
|
|
currentStmt.params = currentStmt.params.concat(stmt.params);
|
|
|
|
return currentStmt;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Join multiple parameterized SQLs into one
|
|
|
|
* @param {Object[]} sqls An array of parameterized SQLs
|
|
|
|
* @param {String} [separator] Separator, default to ` `
|
|
|
|
* @returns {ParameterizedSQL}
|
|
|
|
*/
|
|
|
|
ParameterizedSQL.join = function(sqls, separator) {
|
|
|
|
assert(Array.isArray(sqls), 'sqls must be an array');
|
2019-08-02 13:50:51 +00:00
|
|
|
const ps = new ParameterizedSQL('', []);
|
|
|
|
for (let i = 0, n = sqls.length; i < n; i++) {
|
2015-05-13 17:14:44 +00:00
|
|
|
this.append(ps, sqls[i], separator);
|
|
|
|
}
|
|
|
|
return ps;
|
|
|
|
};
|
|
|
|
|
|
|
|
ParameterizedSQL.PLACEHOLDER = PLACEHOLDER;
|