Skip to content

Commit

Permalink
Merge pull request #119 from CSNW/clone-w-sql
Browse files Browse the repository at this point in the history
Fix clone of embedded literal sql
  • Loading branch information
prust authored May 15, 2020
2 parents 221015c + 2a44836 commit aebaf79
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sql-bricks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
if (_.isArray(this.vals[0]))
this.vals = this.vals[0];
}
sql.prototype.clone = function clone() {
var args = [this.str].concat(this.vals);
return sql.apply(null, args);
};
sql.setDefaultOpts = setDefaultOpts;
function setDefaultOpts(opts) {
default_opts = _.extend(default_opts, opts);
Expand Down
9 changes: 9 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,15 @@ describe('SQL Bricks', function() {
sel.clone().where('last_name', 'Flintstone');
check(sel, "SELECT * FROM \"user\" WHERE first_name IN (SELECT first_name FROM \"user\")");
});
it('should clone parameterized sub-expressions', function() {
checkParams(select().from('tbl').where(or(sql('a = $1', 444), sql('b = $1', 555), sql('c = $1', 666))).clone(),
'SELECT * FROM tbl WHERE a = $1 OR b = $2 OR c = $3',
[444, 555, 666]);
});
it('should clone non-parameterized sub-expressions', function() {
check(select().from('tbl').where(or(sql('a = 444'), sql('b = 555'), sql('c = 666'))).clone(),
'SELECT * FROM tbl WHERE a = 444 OR b = 555 OR c = 666');
});
});

describe('the AS keyword', function() {
Expand Down

0 comments on commit aebaf79

Please sign in to comment.