Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/parameterized-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,25 @@ ParameterizedSQL.append = function(currentStmt, stmt, separator) {
*/
ParameterizedSQL.join = function(sqls, separator) {
assert(Array.isArray(sqls), 'sqls must be an array');
const ps = new ParameterizedSQL('', []);
separator = typeof separator === 'string' ? separator : ' ';
let sql = '';
const params = [];
for (let i = 0, n = sqls.length; i < n; i++) {
this.append(ps, sqls[i], separator);
const stmt = (sqls[i] instanceof ParameterizedSQL) ? sqls[i] :
new ParameterizedSQL(sqls[i]);
if (sql) {
sql += separator;
}
if (stmt.sql) {
sql += stmt.sql;
}
for (let j = 0, m = stmt.params.length; j < m; j++) {
params.push(stmt.params[j]);
}
}
const ps = new ParameterizedSQL('', []);
ps.sql = sql;
ps.params = params;
return ps;
};

Expand Down
43 changes: 43 additions & 0 deletions test/sql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,49 @@ describe('sql connector', function() {
);
});

it('joins SQL statements', function() {
const stmt = ParameterizedSQL.join([
'',
new ParameterizedSQL('`NAME`=?', ['John']),
{sql: '`VIP`=?', params: [true]},
'',
'`TOKEN` IS NULL',
{sql: '`ID` IN (?,?)', params: [1, [2, 3]]},
], ' AND ');
expect(stmt).to.be.instanceOf(ParameterizedSQL);
expect(stmt.toJSON()).to.eql({
sql: '`NAME`=? AND `VIP`=? AND AND `TOKEN` IS NULL AND `ID` IN (?,?)',
params: ['John', true, 1, [2, 3]],
});
expect(ParameterizedSQL.join(['SELECT *', 'FROM `CUSTOMER`']).toJSON())
.to.eql({sql: 'SELECT * FROM `CUSTOMER`', params: []});
});

it('joins SQL statements without modifying their params', function() {
const params1 = ['John'];
const params2 = ['Mary'];
const stmt1 = new ParameterizedSQL('`NAME`=?', params1);
const stmt2 = new ParameterizedSQL('`NAME`=?', params2);
const joined = ParameterizedSQL.join([stmt1, stmt2], ' OR ');
joined.params.push('Jane');
expect(joined.params).to.eql(['John', 'Mary', 'Jane']);
expect(params1).to.eql(['John']);
expect(params2).to.eql(['Mary']);
expect(stmt1.params).to.equal(params1);
expect(stmt2.params).to.equal(params2);
});

it('joins a large number of SQL statements in linear time', function() {
this.timeout(1000);
const values = [];
for (let i = 0; i < 50000; i++) {
values.push(new ParameterizedSQL('?', [i]));
}
const stmt = ParameterizedSQL.join(values, ',');
expect(stmt.params).to.have.length(50000);
expect(stmt.params[49999]).to.equal(49999);
});

it('should throw if params does not match placeholders', function() {
expect(function() {
let stmt1 = 'SELECT * from `CUSTOMER`';
Expand Down
Loading