knex/lib/query/joinclause.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

2013-09-13 16:58:38 -04:00
// JoinClause
// -------
// 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) {
this.table = table;
this.joinType = type;
this.clauses = [];
2014-07-01 08:53:35 -04:00
this.and = this;
2014-04-27 19:36:40 -04:00
}
JoinClause.prototype.grouping = 'join';
// 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;
};
// Adds an "and on" clause to the current join object.
JoinClause.prototype.andOn = function() {
return this.on.apply(this, arguments);
};
// 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);
};
// 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;
};
JoinClause.prototype._bool = function(bool) {
if (arguments.length === 1) {
this._boolFlag = bool;
return this;
2013-12-27 14:44:21 -05:00
}
var ret = this._boolFlag || 'and';
this._boolFlag = 'and';
return ret;
};
2014-07-01 08:53:35 -04:00
Object.defineProperty(JoinClause.prototype, 'or', {
get: function () {
return this._bool('or');
}
});
module.exports = JoinClause;