knex/lib/query/joinclause.js

91 lines
2.2 KiB
JavaScript
Raw Normal View History

'use strict';
2015-04-29 15:13:15 -04:00
var assign = require('lodash/object/assign');
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.
2015-08-09 20:22:39 -03:00
function JoinClause(table, type, schema) {
this.schema = schema;
2015-05-09 13:58:18 -04:00
this.table = table;
this.joinType = type;
2015-05-09 13:58:18 -04:00
this.and = this;
this.clauses = [];
2014-04-27 19:36:40 -04:00
}
2015-04-29 15:13:15 -04:00
assign(JoinClause.prototype, {
2015-04-29 15:13:15 -04:00
grouping: 'join',
2015-04-29 15:13:15 -04:00
// Adds an "on" clause to the current join object.
2015-05-09 13:58:18 -04:00
on: function on(first, operator, second) {
var data,
bool = this._bool();
2015-04-29 15:13:15 -04:00
switch (arguments.length) {
2015-05-09 13:58:18 -04:00
case 1:
{
if (typeof first === 'object' && typeof first.toSQL !== 'function') {
var i = -1,
keys = Object.keys(first);
var method = bool === 'or' ? 'orOn' : 'on';
while (++i < keys.length) {
this[method](keys[i], first[keys[i]]);
}
return this;
} else {
data = [bool, 'on', first];
2015-04-29 15:13:15 -04:00
}
2015-05-09 13:58:18 -04:00
break;
2015-04-29 15:13:15 -04:00
}
2015-05-09 13:58:18 -04:00
case 2:
data = [bool, 'on', first, '=', operator];break;
default:
data = [bool, 'on', first, operator, second];
2015-04-29 15:13:15 -04:00
}
this.clauses.push(data);
return this;
},
2015-04-29 15:13:15 -04:00
// Adds a "using" clause to the current join.
2015-08-09 20:22:39 -03:00
using: function using(column) {
return this.clauses.push([this._bool(), 'using', column]);
2015-04-29 15:13:15 -04:00
},
2015-04-29 15:13:15 -04:00
// Adds an "and on" clause to the current join object.
2015-05-09 13:58:18 -04:00
andOn: function andOn() {
2015-04-29 15:13:15 -04:00
return this.on.apply(this, arguments);
},
2015-04-29 15:13:15 -04:00
// Adds an "or on" clause to the current join object.
2015-05-09 13:58:18 -04:00
orOn: function orOn(first, operator, second) {
2015-04-29 15:13:15 -04:00
/*jshint unused: false*/
return this._bool('or').on.apply(this, arguments);
},
2015-04-29 15:13:15 -04:00
// Explicitly set the type of join, useful within a function when creating a grouped join.
2015-05-09 13:58:18 -04:00
type: function type(_type) {
this.joinType = _type;
return this;
2015-04-29 15:13:15 -04:00
},
2015-05-09 13:58:18 -04:00
_bool: function _bool(bool) {
2015-04-29 15:13:15 -04:00
if (arguments.length === 1) {
this._boolFlag = bool;
return this;
}
var ret = this._boolFlag || 'and';
this._boolFlag = 'and';
return ret;
2013-12-27 14:44:21 -05:00
}
2015-04-29 15:13:15 -04:00
2015-05-09 13:58:18 -04:00
});
2014-07-01 08:53:35 -04:00
Object.defineProperty(JoinClause.prototype, 'or', {
2015-05-09 13:58:18 -04:00
get: function get() {
2014-07-01 08:53:35 -04:00
return this._bool('or');
}
});
module.exports = JoinClause;