joinclause.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _keys = require('babel-runtime/core-js/object/keys');
  4. var _keys2 = _interopRequireDefault(_keys);
  5. var _typeof2 = require('babel-runtime/helpers/typeof');
  6. var _typeof3 = _interopRequireDefault(_typeof2);
  7. var _assign2 = require('lodash/assign');
  8. var _assign3 = _interopRequireDefault(_assign2);
  9. var _assert = require('assert');
  10. var _assert2 = _interopRequireDefault(_assert);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. // JoinClause
  13. // -------
  14. // The "JoinClause" is an object holding any necessary info about a join,
  15. // including the type, and any associated tables & columns being joined.
  16. function JoinClause(table, type, schema) {
  17. this.schema = schema;
  18. this.table = table;
  19. this.joinType = type;
  20. this.and = this;
  21. this.clauses = [];
  22. }
  23. (0, _assign3.default)(JoinClause.prototype, {
  24. grouping: 'join',
  25. // Adds an "on" clause to the current join object.
  26. on: function on(first, operator, second) {
  27. if (typeof first === 'function') {
  28. this.clauses.push({
  29. type: 'onWrapped',
  30. value: first,
  31. bool: this._bool()
  32. });
  33. return this;
  34. }
  35. var data = void 0;
  36. var bool = this._bool();
  37. switch (arguments.length) {
  38. case 1:
  39. {
  40. if ((typeof first === 'undefined' ? 'undefined' : (0, _typeof3.default)(first)) === 'object' && typeof first.toSQL !== 'function') {
  41. var keys = (0, _keys2.default)(first);
  42. var i = -1;
  43. var method = bool === 'or' ? 'orOn' : 'on';
  44. while (++i < keys.length) {
  45. this[method](keys[i], first[keys[i]]);
  46. }
  47. return this;
  48. } else {
  49. data = { type: 'onRaw', value: first, bool: bool };
  50. }
  51. break;
  52. }
  53. case 2:
  54. data = { type: 'onBasic', column: first, operator: '=', value: operator, bool: bool };break;
  55. default:
  56. data = { type: 'onBasic', column: first, operator: operator, value: second, bool: bool };
  57. }
  58. this.clauses.push(data);
  59. return this;
  60. },
  61. // Adds a "using" clause to the current join.
  62. using: function using(column) {
  63. return this.clauses.push({ type: 'onUsing', column: column, bool: this._bool() });
  64. },
  65. /*// Adds an "and on" clause to the current join object.
  66. andOn() {
  67. return this.on.apply(this, arguments);
  68. },*/
  69. // Adds an "or on" clause to the current join object.
  70. orOn: function orOn(first, operator, second) {
  71. return this._bool('or').on.apply(this, arguments);
  72. },
  73. onBetween: function onBetween(column, values) {
  74. (0, _assert2.default)(Array.isArray(values), 'The second argument to onBetween must be an array.');
  75. (0, _assert2.default)(values.length === 2, 'You must specify 2 values for the onBetween clause');
  76. this.clauses.push({
  77. type: 'onBetween',
  78. column: column,
  79. value: values,
  80. bool: this._bool(),
  81. not: this._not()
  82. });
  83. return this;
  84. },
  85. onNotBetween: function onNotBetween(column, values) {
  86. return this._not(true).onBetween(column, values);
  87. },
  88. orOnBetween: function orOnBetween(column, values) {
  89. return this._bool('or').onBetween(column, values);
  90. },
  91. orOnNotBetween: function orOnNotBetween(column, values) {
  92. return this._bool('or')._not(true).onBetween(column, values);
  93. },
  94. onIn: function onIn(column, values) {
  95. if (Array.isArray(values) && values.length === 0) return this.where(this._not());
  96. this.clauses.push({
  97. type: 'onIn',
  98. column: column,
  99. value: values,
  100. not: this._not(),
  101. bool: this._bool()
  102. });
  103. return this;
  104. },
  105. onNotIn: function onNotIn(column, values) {
  106. return this._not(true).onIn(column, values);
  107. },
  108. orOnIn: function orOnIn(column, values) {
  109. return this._bool('or').onIn(column, values);
  110. },
  111. orOnNotIn: function orOnNotIn(column, values) {
  112. return this._bool('or')._not(true).onIn(column, values);
  113. },
  114. onNull: function onNull(column) {
  115. this.clauses.push({
  116. type: 'onNull',
  117. column: column,
  118. not: this._not(),
  119. bool: this._bool()
  120. });
  121. return this;
  122. },
  123. orOnNull: function orOnNull(callback) {
  124. return this._bool('or').onNull(callback);
  125. },
  126. onNotNull: function onNotNull(callback) {
  127. return this._not(true).onNull(callback);
  128. },
  129. orOnNotNull: function orOnNotNull(callback) {
  130. return this._not(true)._bool('or').onNull(callback);
  131. },
  132. onExists: function onExists(callback) {
  133. this.clauses.push({
  134. type: 'onExists',
  135. value: callback,
  136. not: this._not(),
  137. bool: this._bool()
  138. });
  139. return this;
  140. },
  141. orOnExists: function orOnExists(callback) {
  142. return this._bool('or').onExists(callback);
  143. },
  144. onNotExists: function onNotExists(callback) {
  145. return this._not(true).onExists(callback);
  146. },
  147. orOnNotExists: function orOnNotExists(callback) {
  148. return this._not(true)._bool('or').onExists(callback);
  149. },
  150. // Explicitly set the type of join, useful within a function when creating a grouped join.
  151. type: function type(_type) {
  152. this.joinType = _type;
  153. return this;
  154. },
  155. _bool: function _bool(bool) {
  156. if (arguments.length === 1) {
  157. this._boolFlag = bool;
  158. return this;
  159. }
  160. var ret = this._boolFlag || 'and';
  161. this._boolFlag = 'and';
  162. return ret;
  163. },
  164. _not: function _not(val) {
  165. if (arguments.length === 1) {
  166. this._notFlag = val;
  167. return this;
  168. }
  169. var ret = this._notFlag;
  170. this._notFlag = false;
  171. return ret;
  172. }
  173. });
  174. Object.defineProperty(JoinClause.prototype, 'or', {
  175. get: function get() {
  176. return this._bool('or');
  177. }
  178. });
  179. JoinClause.prototype.andOn = JoinClause.prototype.on;
  180. JoinClause.prototype.andOnIn = JoinClause.prototype.onIn;
  181. JoinClause.prototype.andOnNotIn = JoinClause.prototype.onNotIn;
  182. JoinClause.prototype.andOnNull = JoinClause.prototype.onNull;
  183. JoinClause.prototype.andOnNotNull = JoinClause.prototype.onNotNull;
  184. JoinClause.prototype.andOnExists = JoinClause.prototype.onExists;
  185. JoinClause.prototype.andOnNotExists = JoinClause.prototype.onNotExists;
  186. JoinClause.prototype.andOnBetween = JoinClause.prototype.onBetween;
  187. JoinClause.prototype.andOnNotBetween = JoinClause.prototype.onNotBetween;
  188. exports.default = JoinClause;
  189. module.exports = exports['default'];