knex/lib/query/joinclause.js

58 lines
1.4 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.
2013-12-27 14:44:21 -05:00
var Helpers = require('../helpers');
var JoinClause = function(table, type) {
this.table = table;
this.joinType = type;
this.clauses = [];
this.grouping = 'join';
};
JoinClause.prototype = {
constructor: JoinClause,
// Adds an "on" clause to the current join object.
on: function(first, operator, second) {
2013-12-27 14:44:21 -05:00
if (arguments.length === 2) {
data = [this._bool(), first, '=', operator];
2013-12-27 14:44:21 -05:00
} else {
data = [this._bool(), first, operator, second];
2013-12-27 14:44:21 -05:00
}
this.clauses.push(data);
return this;
},
// Adds an "and on" clause to the current join object.
andOn: function() {
return this.on.apply(this, arguments);
},
// Adds an "or on" clause to the current join object.
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.
type: function(type) {
this.joinType = type;
return this;
2013-12-27 14:44:21 -05:00
},
_bool: function(bool) {
2013-12-27 14:44:21 -05:00
if (arguments.length === 1) {
this._boolFlag = bool;
2013-12-27 14:44:21 -05:00
return this;
}
var ret = this._boolFlag || 'and';
this._boolFlag = 'and';
2013-12-27 14:44:21 -05:00
return ret;
}
};
module.exports = JoinClause;