Skip to content

Commit c0108e1

Browse files
committed
Allow specifying join type dresende#22
1 parent 67fd40e commit c0108e1

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

Changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### v0.1.25 - 22 Mar 2015
2+
3+
- Added support for left/right joins (#22)
4+
5+
### v0.1.24 - 23 Dec 2014
6+
7+
- Allow insertion of empty row using default values (#37)

lib/Select.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function SelectQuery(Dialect, opts) {
9292
fun_stack = [];
9393
return this;
9494
},
95-
from: function (table, from_id, to_table, to_id) {
95+
from: function (table, from_id, to_table, to_id, fromOpts) {
9696
var from = {
9797
t: table, // table
9898
a: "t" + (sql.from.length + 1) // alias
@@ -104,7 +104,14 @@ function SelectQuery(Dialect, opts) {
104104
}
105105

106106
var a, f = from_id, t;
107-
if (arguments.length == 3) {
107+
var args = Array.prototype.slice.call(arguments);
108+
var last = args[args.length - 1];
109+
110+
if (typeof last == 'object' && !Array.isArray(last)) {
111+
from.opts = args.pop();
112+
}
113+
114+
if (args.length == 3) {
108115
a = sql.from[sql.from.length - 1].a;
109116
t = to_table;
110117
} else {
@@ -321,6 +328,9 @@ function SelectQuery(Dialect, opts) {
321328
from = sql.from[i];
322329

323330
if (i > 0) {
331+
if (from.opts && from.opts.joinType) {
332+
query.push(from.opts.joinType.toUpperCase());
333+
}
324334
query.push("JOIN");
325335
}
326336
if (sql.from.length == 1 && !sql.where_exists) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"sql",
77
"query"
88
],
9-
"version": "0.1.24",
9+
"version": "0.1.25",
1010
"license": "MIT",
1111
"repository": {
1212
"url": "http://github.com/dresende/node-sql-query"

test/integration/test-select.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ assert.equal(
5959
"SELECT `t1`.`id1`, `t1`.`name`, `t2`.`id2` FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`"
6060
);
6161

62+
assert.equal(
63+
common.Select().from('table1').select('id1')
64+
.from('table2', 'id2', 'id1', { joinType: 'left inner' }).select('id2').build(),
65+
"SELECT `t1`.`id1`, `t2`.`id2` FROM `table1` `t1` LEFT INNER JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`"
66+
)
67+
68+
6269
assert.equal(
6370
common.Select().from('table1').select('id1', 'name')
6471
.from('table2', 'id2', 'table1', 'id1').select('id2').build(),
@@ -102,7 +109,7 @@ assert.equal(
102109
);
103110

104111
assert.equal(
105-
common.Select().from('table1')
106-
.from('table2',['id2a', 'id2b'], 'table1', ['id1a', 'id1b']).count('id').build(),
107-
"SELECT COUNT(`t2`.`id`) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2a` = `t1`.`id1a` AND `t2`.`id2b` = `t1`.`id1b`"
112+
common.Select().from('table1')
113+
.from('table2',['id2a', 'id2b'], 'table1', ['id1a', 'id1b']).count('id').build(),
114+
"SELECT COUNT(`t2`.`id`) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2a` = `t1`.`id1a` AND `t2`.`id2b` = `t1`.`id1b`"
108115
);

0 commit comments

Comments
 (0)