Skip to content
Browse files

Add outerJoin method

  • Loading branch information...
1 parent 5565878 commit 3301afece500a2956b038eb9e409430cad453e4c Alexandre Laurent committed Jan 25, 2014
Showing with 30 additions and 0 deletions.
  1. +8 −0 README.md
  2. +22 −0 lib/query.js
View
8 README.md
@@ -171,6 +171,14 @@ This appends a 'INNER JOIN' clause to this query on `table`
This is an alias to innerJoin
+#### outerJoin(table, condition[, right])
+
+This appends a 'OUTER JOIN' clause to this query on `table`
+
+- `table`: the SQL table to join
+- `condition`: the JOIN condition (string or object)
+- `right`: a boolean to say if the outer join if from the right
+
#### orderby(field[, desc])
This appends an 'ORDER BY field' clause to this query.
View
22 lib/query.js
@@ -174,6 +174,28 @@ Query.prototype.innerJoin = function(table, condition) {
Query.prototype.join = Query.prototype.innerJoin;
/**
+ * Append a OUTER JOIN clause to this query (left by default)
+ *
+ * @param {string} table
+ * @param {Object<string, (string|number|boolean)>|string} condition
+ * @param {boolean=} right
+ * @return {Query}
+ */
+Query.prototype.outerJoin = function(table, condition, right) {
+ if (arguments.length < 2)
+ throw new Error('Missing arguments');
+ if (this.statement == null)
+ throw new Error('Missing statement');
+ this.values.push(table);
+ this.statement += (right ? ' RIGHT ' : ' LEFT ') + 'JOIN ?? ON ';
+ if (typeof condition == 'string')
+ this.statement += condition;
+ else
+ this.statement += this.hashToClause(condition, ' AND ');
+ return this;
+};
+
+/**
* Append an ORDER BY clause to this query
*
* @param {string} by

0 comments on commit 3301afe

Please sign in to comment.
Something went wrong with that request. Please try again.