2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2013-09-13 16:58:38 -04:00
|
|
|
// JoinClause
|
2014-04-15 11:43:47 -04:00
|
|
|
// -------
|
2013-09-03 22:25:54 -04:00
|
|
|
|
2013-11-27 16:51:01 -05:00
|
|
|
// The "JoinClause" is an object holding any necessary info about a join,
|
|
|
|
// including the type, and any associated tables & columns being joined.
|
2014-04-27 19:36:40 -04:00
|
|
|
function JoinClause(table, type) {
|
2014-04-08 16:25:57 -04:00
|
|
|
this.table = table;
|
2013-11-27 16:51:01 -05:00
|
|
|
this.joinType = type;
|
|
|
|
this.clauses = [];
|
2014-08-14 18:02:50 -04:00
|
|
|
this.and = this;
|
2014-04-27 19:36:40 -04:00
|
|
|
}
|
2013-11-27 16:51:01 -05:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
JoinClause.prototype.grouping = 'join';
|
2013-11-27 16:51:01 -05:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Adds an "on" clause to the current join object.
|
|
|
|
JoinClause.prototype.on = function(first, operator, second) {
|
2014-08-12 17:22:24 +02:00
|
|
|
var data;
|
2014-10-01 19:18:25 -04:00
|
|
|
switch (arguments.length) {
|
|
|
|
case 1: data = ['on', this._bool(), first]; break;
|
|
|
|
case 2: data = ['on', this._bool(), first, '=', operator]; break;
|
|
|
|
default: data = ['on', this._bool(), first, operator, second];
|
2014-04-08 16:25:57 -04:00
|
|
|
}
|
|
|
|
this.clauses.push(data);
|
|
|
|
return this;
|
|
|
|
};
|
2014-03-14 16:56:55 -04:00
|
|
|
|
2014-10-01 19:18:25 -04:00
|
|
|
// Adds a "using" clause to the current join.
|
|
|
|
JoinClause.prototype.using = function(table) {
|
|
|
|
return this.clauses.push(['using', this._bool(), table]);
|
|
|
|
};
|
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Adds an "and on" clause to the current join object.
|
|
|
|
JoinClause.prototype.andOn = function() {
|
|
|
|
return this.on.apply(this, arguments);
|
|
|
|
};
|
2013-11-27 16:51:01 -05:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Adds an "or on" clause to the current join object.
|
|
|
|
JoinClause.prototype.orOn = function(first, operator, second) {
|
2014-09-02 22:56:51 +02:00
|
|
|
/*jshint unused: false*/
|
2014-04-08 16:25:57 -04:00
|
|
|
return this._bool('or').on.apply(this, arguments);
|
|
|
|
};
|
2013-11-27 16:51:01 -05:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Explicitly set the type of join, useful within a function when creating a grouped join.
|
|
|
|
JoinClause.prototype.type = function(type) {
|
|
|
|
this.joinType = type;
|
|
|
|
return this;
|
|
|
|
};
|
2013-11-27 16:51:01 -05:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
JoinClause.prototype._bool = function(bool) {
|
|
|
|
if (arguments.length === 1) {
|
|
|
|
this._boolFlag = bool;
|
2013-11-27 16:51:01 -05:00
|
|
|
return this;
|
2013-12-27 14:44:21 -05:00
|
|
|
}
|
2014-04-08 16:25:57 -04:00
|
|
|
var ret = this._boolFlag || 'and';
|
|
|
|
this._boolFlag = 'and';
|
|
|
|
return ret;
|
2014-03-14 16:56:55 -04:00
|
|
|
};
|
|
|
|
|
2014-07-01 08:53:35 -04:00
|
|
|
Object.defineProperty(JoinClause.prototype, 'or', {
|
|
|
|
get: function () {
|
|
|
|
return this._bool('or');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-03-14 16:56:55 -04:00
|
|
|
module.exports = JoinClause;
|