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-07-01 08:53:35 -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) {
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
data = ['on', this._bool(), first, '=', operator];
|
|
|
|
} else {
|
|
|
|
data = ['on', this._bool(), first, operator, second];
|
|
|
|
}
|
|
|
|
this.clauses.push(data);
|
|
|
|
return this;
|
|
|
|
};
|
2014-03-14 16:56:55 -04:00
|
|
|
|
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) {
|
|
|
|
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;
|