From c0108e1a6921ac34d12daeeeb661e2ea7f525c5f Mon Sep 17 00:00:00 2001 From: Arek W Date: Sun, 22 Mar 2015 00:54:36 +0000 Subject: [PATCH 1/2] Allow specifying join type #22 --- Changelog.md | 7 +++++++ lib/Select.js | 14 ++++++++++++-- package.json | 2 +- test/integration/test-select.js | 13 ++++++++++--- 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 Changelog.md diff --git a/Changelog.md b/Changelog.md new file mode 100644 index 0000000..6150c0b --- /dev/null +++ b/Changelog.md @@ -0,0 +1,7 @@ +### v0.1.25 - 22 Mar 2015 + +- Added support for left/right joins (#22) + +### v0.1.24 - 23 Dec 2014 + +- Allow insertion of empty row using default values (#37) diff --git a/lib/Select.js b/lib/Select.js index 09f7b1c..205b9b7 100644 --- a/lib/Select.js +++ b/lib/Select.js @@ -92,7 +92,7 @@ function SelectQuery(Dialect, opts) { fun_stack = []; return this; }, - from: function (table, from_id, to_table, to_id) { + from: function (table, from_id, to_table, to_id, fromOpts) { var from = { t: table, // table a: "t" + (sql.from.length + 1) // alias @@ -104,7 +104,14 @@ function SelectQuery(Dialect, opts) { } var a, f = from_id, t; - if (arguments.length == 3) { + var args = Array.prototype.slice.call(arguments); + var last = args[args.length - 1]; + + if (typeof last == 'object' && !Array.isArray(last)) { + from.opts = args.pop(); + } + + if (args.length == 3) { a = sql.from[sql.from.length - 1].a; t = to_table; } else { @@ -321,6 +328,9 @@ function SelectQuery(Dialect, opts) { from = sql.from[i]; if (i > 0) { + if (from.opts && from.opts.joinType) { + query.push(from.opts.joinType.toUpperCase()); + } query.push("JOIN"); } if (sql.from.length == 1 && !sql.where_exists) { diff --git a/package.json b/package.json index a447f1b..4b338a7 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "sql", "query" ], - "version": "0.1.24", + "version": "0.1.25", "license": "MIT", "repository": { "url": "http://github.com/dresende/node-sql-query" diff --git a/test/integration/test-select.js b/test/integration/test-select.js index 7b8b81e..5b9b3dd 100644 --- a/test/integration/test-select.js +++ b/test/integration/test-select.js @@ -59,6 +59,13 @@ assert.equal( "SELECT `t1`.`id1`, `t1`.`name`, `t2`.`id2` FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" ); +assert.equal( + common.Select().from('table1').select('id1') + .from('table2', 'id2', 'id1', { joinType: 'left inner' }).select('id2').build(), + "SELECT `t1`.`id1`, `t2`.`id2` FROM `table1` `t1` LEFT INNER JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" +) + + assert.equal( common.Select().from('table1').select('id1', 'name') .from('table2', 'id2', 'table1', 'id1').select('id2').build(), @@ -102,7 +109,7 @@ assert.equal( ); assert.equal( - common.Select().from('table1') - .from('table2',['id2a', 'id2b'], 'table1', ['id1a', 'id1b']).count('id').build(), - "SELECT COUNT(`t2`.`id`) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2a` = `t1`.`id1a` AND `t2`.`id2b` = `t1`.`id1b`" + common.Select().from('table1') + .from('table2',['id2a', 'id2b'], 'table1', ['id1a', 'id1b']).count('id').build(), + "SELECT COUNT(`t2`.`id`) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2a` = `t1`.`id1a` AND `t2`.`id2b` = `t1`.`id1b`" ); From f4450a4f0c65a275245635fd4613f3f0abd8c31e Mon Sep 17 00:00:00 2001 From: Arek W Date: Sun, 22 Mar 2015 00:58:28 +0000 Subject: [PATCH 2/2] Add node 0.12 and iojs --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index b9207e5..a152b92 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,3 +2,5 @@ language: node_js node_js: - '0.8' - '0.10' + - '0.12' + - 'iojs'