From f04abb835da95dbf68889e19b321a851d5e8c902 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Thu, 4 Oct 2018 22:23:58 +0200 Subject: [PATCH 1/5] fix: output `.noWait()` as 'NOWAIT' was previously output as 'NO WAIT' --- index.html | 4 ++-- sql-bricks.js | 16 ++++++++-------- tests/doctests.js | 2 +- tests/tests.js | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/index.html b/index.html index 2912377..7341437 100644 --- a/index.html +++ b/index.html @@ -495,10 +495,10 @@

select

forUpdate, noWaitsel.forUpdate([tbl, ...]) / sel.noWait()
-

Add the FOR UPDATE clause to lock all selected records from all tables in the select (or just the tables specified), along with an optional NO WAIT at the end:

+

Add the FOR UPDATE clause to lock all selected records from all tables in the select (or just the tables specified), along with an optional NOWAIT at the end:

 select('addr_id').from('person').forUpdate().of('addr_id').noWait();
-// SELECT addr_id FROM person FOR UPDATE OF addr_id NO WAIT
+// SELECT addr_id FROM person FOR UPDATE OF addr_id NOWAIT
 

diff --git a/sql-bricks.js b/sql-bricks.js index dbb1c4b..037186a 100644 --- a/sql-bricks.js +++ b/sql-bricks.js @@ -3,7 +3,7 @@ var is_common_js = typeof exports != 'undefined'; var default_opts = { placeholder: '$%d' }; - + var _; if (is_common_js) _ = require('underscore'); @@ -108,13 +108,13 @@ if (typeof templ_fn != 'function') templ_fn = function(opts) { return templ(template, this, opts); }; this.prototype[clause_id + 'ToString'] = templ_fn; - + var index; if (opts.after || opts.before) { index = clauses.indexOf(opts.after || opts.before); if (index == -1) throw new Error('Error adding clause ' + clause_id + ': dependent clause "' + opts.after + '" not found'); - + if (opts.after) index++; } @@ -294,7 +294,7 @@ }); Select.defineClause('orderBy', '{{#if _orderBy}}ORDER BY {{columns _orderBy}}{{/if}}'); - Select.defineClause('forUpdate', '{{#if _forUpdate}}FOR UPDATE{{#if _of}} OF {{columns _of}}{{/if}}{{#if _noWait}} NO WAIT{{/if}}{{/if}}'); + Select.defineClause('forUpdate', '{{#if _forUpdate}}FOR UPDATE{{#if _of}} OF {{columns _of}}{{/if}}{{#if _noWait}} NOWAIT{{/if}}{{/if}}'); // INSERT statement @@ -572,7 +572,7 @@ // Debt: Determining whether join is natural/cross by reading the string is slightly hacky... but works. if (/^(natural|cross)/i.test(this.type)) return this.type + ' JOIN ' + tbl; - + // Not a natural or cross, check for criteria. if (!on || _.isEmpty(on)) { if (sql._joinCriteria) { @@ -802,7 +802,7 @@ sql = handleValues(this.list, opts).join(', '); else if (this.list instanceof Statement) sql = this.list._toString(opts); - + return col_sql + ' IN (' + sql + ')'; }; @@ -958,7 +958,7 @@ // auto-quote tbl & col names if they have caps or are reserved words sql._autoQuoteChar = '"'; - + function autoQuote(str) { if (/^\w+$/.test(str) && (/[A-Z]/.test(str) || str in reserved)) return sql._autoQuoteChar + str + sql._autoQuoteChar; @@ -1104,7 +1104,7 @@ function cls() { if (!(this instanceof cls)) return applyNew(cls, arguments); - + base.apply(this, arguments); } return inherits(cls, base); diff --git a/tests/doctests.js b/tests/doctests.js index 3e1eb84..146b08b 100644 --- a/tests/doctests.js +++ b/tests/doctests.js @@ -161,7 +161,7 @@ check(select().from('person').where({'last_name': 'Flintstone'}).union() .sele }); it("select('addr_id').from('person').forUpdate().of('addr_id').noWait();", function() { -check(select('addr_id').from('person').forUpdate().of('addr_id').noWait(), "SELECT addr_id FROM person FOR UPDATE OF addr_id NO WAIT"); +check(select('addr_id').from('person').forUpdate().of('addr_id').noWait(), "SELECT addr_id FROM person FOR UPDATE OF addr_id NOWAIT"); }); it("insert('person', {'first_name': 'Fred', 'last_name': 'Flintstone'});", function() { diff --git a/tests/tests.js b/tests/tests.js index 650d586..793ddef 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -402,9 +402,9 @@ describe('SQL Bricks', function() { check(select().from('user').forUpdate().of('user'), 'SELECT * FROM "user" FOR UPDATE OF "user"'); }); - it('should support FOR UPDATE OF ... NO WAIT', function() { + it('should support FOR UPDATE OF ... NOWAIT', function() { check(select().from('user').forUpdate().of('user').noWait(), - 'SELECT * FROM "user" FOR UPDATE OF "user" NO WAIT'); + 'SELECT * FROM "user" FOR UPDATE OF "user" NOWAIT'); }); }); From 63dfa2154a5a741b1a9b5cd4355098e9b6e637f9 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Mon, 5 Nov 2018 23:15:11 +0100 Subject: [PATCH 2/5] chore(ci): run tests on current LTS verions of node --- .travis.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 57b78fb..6d17c6d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,4 @@ language: node_js node_js: - - "4.1" - - "4.0" - - "0.12" - - "0.10" + - "8" + - "6" From 221015c4b1c549bbd0fb40f0c00440cbb071244c Mon Sep 17 00:00:00 2001 From: Peter Rust Date: Tue, 6 Nov 2018 00:04:35 -0600 Subject: [PATCH 3/5] 2.0.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3fee40b..eb8303d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sql-bricks", - "version": "2.0.3", + "version": "2.0.4", "author": "Peter Rust ", "description": "Transparent, Schemaless SQL Generation", "homepage": "http://csnw.github.io/sql-bricks", From 2a44836473896f233a293da4b63b1c18a804a6c2 Mon Sep 17 00:00:00 2001 From: Peter Rust Date: Fri, 15 May 2020 08:42:11 -0700 Subject: [PATCH 4/5] Added support for direct cloning of literal sql expressions --- sql-bricks.js | 4 ++++ tests/tests.js | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/sql-bricks.js b/sql-bricks.js index 037186a..98891d8 100644 --- a/sql-bricks.js +++ b/sql-bricks.js @@ -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); diff --git a/tests/tests.js b/tests/tests.js index 793ddef..1d2c706 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -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() { From 667537fbb2e60f258a351f7ca5d0865873505087 Mon Sep 17 00:00:00 2001 From: Peter Rust Date: Fri, 15 May 2020 12:40:36 -0700 Subject: [PATCH 5/5] 2.0.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index eb8303d..33abeb0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sql-bricks", - "version": "2.0.4", + "version": "2.0.5", "author": "Peter Rust ", "description": "Transparent, Schemaless SQL Generation", "homepage": "http://csnw.github.io/sql-bricks",