Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ language: node_js
node_js:
- '0.8'
- '0.10'
- '0.12'
- 'iojs'
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 12 additions & 2 deletions lib/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 10 additions & 3 deletions test/integration/test-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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`"
);