2013-09-13 16:58:38 -04:00
|
|
|
// JoinClause
|
|
|
|
// ---------
|
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.
|
2013-12-27 14:44:21 -05:00
|
|
|
var Helpers = require('../helpers');
|
|
|
|
|
|
|
|
var JoinClause = module.exports = function(type) {
|
2013-11-27 16:51:01 -05:00
|
|
|
this.joinType = type;
|
|
|
|
this.clauses = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
JoinClause.prototype = {
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
} else {
|
|
|
|
data = [this.__bool(), first, operator, second];
|
|
|
|
}
|
|
|
|
this.clauses.push(data);
|
2013-11-27 16:51:01 -05:00
|
|
|
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) {
|
2013-12-27 14:44:21 -05:00
|
|
|
return this.__bool('or').on.apply(this, arguments);
|
2013-11-27 16:51:01 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// Explicitly set the type of join, useful within a function when creating a grouped join.
|
|
|
|
type: function(type) {
|
2013-09-13 10:51:12 -04:00
|
|
|
this.joinType = type;
|
2013-11-27 16:51:01 -05:00
|
|
|
return this;
|
2013-12-27 14:44:21 -05:00
|
|
|
},
|
2013-09-03 22:25:54 -04:00
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
__bool: function(bool) {
|
|
|
|
if (arguments.length === 1) {
|
|
|
|
this.__boolFlag = bool;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
var ret = this.__boolFlag || 'and';
|
|
|
|
this.__boolFlag = 'and';
|
|
|
|
return ret;
|
|
|
|
}
|
2013-09-03 22:25:54 -04:00
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
};
|