QueryBuilderBase.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. const QueryBuilderOperationSupport = require('./QueryBuilderOperationSupport');
  2. const { isSqlite, isMsSql } = require('../utils/knexUtils');
  3. const KnexOperation = require('./operations/KnexOperation');
  4. const SelectOperation = require('./operations/select/SelectOperation');
  5. const ReturningOperation = require('./operations/ReturningOperation');
  6. const WhereCompositeOperation = require('./operations/WhereCompositeOperation');
  7. const WhereInCompositeOperation = require('./operations/whereInComposite/WhereInCompositeOperation');
  8. const WhereInCompositeSqliteOperation = require('./operations/whereInComposite/WhereInCompositeSqliteOperation');
  9. const WhereInCompositeMsSqlOperation = require('./operations/whereInComposite/WhereInCompositeMsSqlOperation');
  10. const WhereJsonPostgresOperation = require('./operations/jsonApi/WhereJsonPostgresOperation');
  11. const WhereJsonHasPostgresOperation = require('./operations/jsonApi/WhereJsonHasPostgresOperation');
  12. const WhereJsonNotObjectPostgresOperation = require('./operations/jsonApi/WhereJsonNotObjectPostgresOperation');
  13. const SelectSelector = SelectOperation;
  14. const WhereSelector = /^(where|orWhere|andWhere)/;
  15. const OrderBySelector = /orderBy/;
  16. const JoinSelector = /(join|joinRaw)$/i;
  17. const FromSelector = /^(from|into|table)$/;
  18. class QueryBuilderBase extends QueryBuilderOperationSupport {
  19. static get SelectSelector() {
  20. return SelectSelector;
  21. }
  22. static get WhereSelector() {
  23. return WhereSelector;
  24. }
  25. static get JoinSelector() {
  26. return JoinSelector;
  27. }
  28. static get FromSelector() {
  29. return FromSelector;
  30. }
  31. static get OrderBySelector() {
  32. return OrderBySelector;
  33. }
  34. modify() {
  35. const func = arguments[0];
  36. if (!func) {
  37. return this;
  38. }
  39. if (arguments.length === 1) {
  40. func.call(this, this);
  41. } else {
  42. const args = new Array(arguments.length);
  43. args[0] = this;
  44. for (let i = 1, l = args.length; i < l; ++i) {
  45. args[i] = arguments[i];
  46. }
  47. func.apply(this, args);
  48. }
  49. return this;
  50. }
  51. transacting(trx) {
  52. this._context.knex = trx || null;
  53. return this;
  54. }
  55. clearSelect() {
  56. return this.clear(SelectSelector);
  57. }
  58. clearWhere() {
  59. return this.clear(WhereSelector);
  60. }
  61. clearOrder() {
  62. return this.clear(OrderBySelector);
  63. }
  64. select() {
  65. return this.addOperation(new SelectOperation('select'), arguments);
  66. }
  67. insert() {
  68. return this.addOperation(new KnexOperation('insert'), arguments);
  69. }
  70. update() {
  71. return this.addOperation(new KnexOperation('update'), arguments);
  72. }
  73. delete() {
  74. return this.addOperation(new KnexOperation('delete'), arguments);
  75. }
  76. del() {
  77. return this.addOperation(new KnexOperation('delete'), arguments);
  78. }
  79. forUpdate() {
  80. return this.addOperation(new KnexOperation('forUpdate'), arguments);
  81. }
  82. forShare() {
  83. return this.addOperation(new KnexOperation('forShare'), arguments);
  84. }
  85. as() {
  86. return this.addOperation(new KnexOperation('as'), arguments);
  87. }
  88. columns() {
  89. return this.addOperation(new SelectOperation('columns'), arguments);
  90. }
  91. column() {
  92. return this.addOperation(new SelectOperation('column'), arguments);
  93. }
  94. from() {
  95. return this.addOperation(new KnexOperation('from'), arguments);
  96. }
  97. fromJS() {
  98. return this.addOperation(new KnexOperation('fromJS'), arguments);
  99. }
  100. into() {
  101. return this.addOperation(new KnexOperation('into'), arguments);
  102. }
  103. withSchema() {
  104. return this.addOperation(new KnexOperation('withSchema'), arguments);
  105. }
  106. table() {
  107. return this.addOperation(new KnexOperation('table'), arguments);
  108. }
  109. distinct() {
  110. return this.addOperation(new SelectOperation('distinct'), arguments);
  111. }
  112. join() {
  113. return this.addOperation(new KnexOperation('join'), arguments);
  114. }
  115. joinRaw() {
  116. return this.addOperation(new KnexOperation('joinRaw'), arguments);
  117. }
  118. innerJoin() {
  119. return this.addOperation(new KnexOperation('innerJoin'), arguments);
  120. }
  121. leftJoin() {
  122. return this.addOperation(new KnexOperation('leftJoin'), arguments);
  123. }
  124. leftOuterJoin() {
  125. return this.addOperation(new KnexOperation('leftOuterJoin'), arguments);
  126. }
  127. rightJoin() {
  128. return this.addOperation(new KnexOperation('rightJoin'), arguments);
  129. }
  130. rightOuterJoin() {
  131. return this.addOperation(new KnexOperation('rightOuterJoin'), arguments);
  132. }
  133. outerJoin() {
  134. return this.addOperation(new KnexOperation('outerJoin'), arguments);
  135. }
  136. fullOuterJoin() {
  137. return this.addOperation(new KnexOperation('fullOuterJoin'), arguments);
  138. }
  139. crossJoin() {
  140. return this.addOperation(new KnexOperation('crossJoin'), arguments);
  141. }
  142. where() {
  143. return this.addOperation(new KnexOperation('where'), arguments);
  144. }
  145. andWhere() {
  146. return this.addOperation(new KnexOperation('andWhere'), arguments);
  147. }
  148. orWhere() {
  149. return this.addOperation(new KnexOperation('orWhere'), arguments);
  150. }
  151. whereNot() {
  152. return this.addOperation(new KnexOperation('whereNot'), arguments);
  153. }
  154. orWhereNot() {
  155. return this.addOperation(new KnexOperation('orWhereNot'), arguments);
  156. }
  157. whereRaw() {
  158. return this.addOperation(new KnexOperation('whereRaw'), arguments);
  159. }
  160. whereWrapped() {
  161. return this.addOperation(new KnexOperation('whereWrapped'), arguments);
  162. }
  163. havingWrapped() {
  164. return this.addOperation(new KnexOperation('havingWrapped'), arguments);
  165. }
  166. orWhereRaw() {
  167. return this.addOperation(new KnexOperation('orWhereRaw'), arguments);
  168. }
  169. whereExists() {
  170. return this.addOperation(new KnexOperation('whereExists'), arguments);
  171. }
  172. orWhereExists() {
  173. return this.addOperation(new KnexOperation('orWhereExists'), arguments);
  174. }
  175. whereNotExists() {
  176. return this.addOperation(new KnexOperation('whereNotExists'), arguments);
  177. }
  178. orWhereNotExists() {
  179. return this.addOperation(new KnexOperation('orWhereNotExists'), arguments);
  180. }
  181. whereIn() {
  182. return this.addOperation(new KnexOperation('whereIn'), arguments);
  183. }
  184. orWhereIn() {
  185. return this.addOperation(new KnexOperation('orWhereIn'), arguments);
  186. }
  187. whereNotIn() {
  188. return this.addOperation(new KnexOperation('whereNotIn'), arguments);
  189. }
  190. orWhereNotIn() {
  191. return this.addOperation(new KnexOperation('orWhereNotIn'), arguments);
  192. }
  193. whereNull() {
  194. return this.addOperation(new KnexOperation('whereNull'), arguments);
  195. }
  196. orWhereNull() {
  197. return this.addOperation(new KnexOperation('orWhereNull'), arguments);
  198. }
  199. whereNotNull() {
  200. return this.addOperation(new KnexOperation('whereNotNull'), arguments);
  201. }
  202. orWhereNotNull() {
  203. return this.addOperation(new KnexOperation('orWhereNotNull'), arguments);
  204. }
  205. whereBetween() {
  206. return this.addOperation(new KnexOperation('whereBetween'), arguments);
  207. }
  208. andWhereBetween() {
  209. return this.addOperation(new KnexOperation('andWhereBetween'), arguments);
  210. }
  211. whereNotBetween() {
  212. return this.addOperation(new KnexOperation('whereNotBetween'), arguments);
  213. }
  214. andWhereNotBetween() {
  215. return this.addOperation(new KnexOperation('andWhereNotBetween'), arguments);
  216. }
  217. orWhereBetween() {
  218. return this.addOperation(new KnexOperation('orWhereBetween'), arguments);
  219. }
  220. orWhereNotBetween() {
  221. return this.addOperation(new KnexOperation('orWhereNotBetween'), arguments);
  222. }
  223. groupBy() {
  224. return this.addOperation(new KnexOperation('groupBy'), arguments);
  225. }
  226. groupByRaw() {
  227. return this.addOperation(new KnexOperation('groupByRaw'), arguments);
  228. }
  229. orderBy() {
  230. return this.addOperation(new KnexOperation('orderBy'), arguments);
  231. }
  232. orderByRaw() {
  233. return this.addOperation(new KnexOperation('orderByRaw'), arguments);
  234. }
  235. union() {
  236. return this.addOperation(new KnexOperation('union'), arguments);
  237. }
  238. unionAll() {
  239. return this.addOperation(new KnexOperation('unionAll'), arguments);
  240. }
  241. having() {
  242. return this.addOperation(new KnexOperation('having'), arguments);
  243. }
  244. orHaving() {
  245. return this.addOperation(new KnexOperation('orHaving'), arguments);
  246. }
  247. havingIn() {
  248. return this.addOperation(new KnexOperation('havingIn'), arguments);
  249. }
  250. orHavingIn() {
  251. return this.addOperation(new KnexOperation('havingIn'), arguments);
  252. }
  253. havingNotIn() {
  254. return this.addOperation(new KnexOperation('havingNotIn'), arguments);
  255. }
  256. orHavingNotIn() {
  257. return this.addOperation(new KnexOperation('orHavingNotIn'), arguments);
  258. }
  259. havingNull() {
  260. return this.addOperation(new KnexOperation('havingNull'), arguments);
  261. }
  262. orHavingNull() {
  263. return this.addOperation(new KnexOperation('orHavingNull'), arguments);
  264. }
  265. havingNotNull() {
  266. return this.addOperation(new KnexOperation('havingNotNull'), arguments);
  267. }
  268. orHavingNotNull() {
  269. return this.addOperation(new KnexOperation('orHavingNotNull'), arguments);
  270. }
  271. havingExists() {
  272. return this.addOperation(new KnexOperation('havingExists'), arguments);
  273. }
  274. orHavingExists() {
  275. return this.addOperation(new KnexOperation('orHavingExists'), arguments);
  276. }
  277. havingNotExists() {
  278. return this.addOperation(new KnexOperation('havingNotExists'), arguments);
  279. }
  280. orHavingNotExists() {
  281. return this.addOperation(new KnexOperation('orHavingNotExists'), arguments);
  282. }
  283. havingBetween() {
  284. return this.addOperation(new KnexOperation('havingBetween'), arguments);
  285. }
  286. orHavingBetween() {
  287. return this.addOperation(new KnexOperation('havingBetween'), arguments);
  288. }
  289. havingNotBetween() {
  290. return this.addOperation(new KnexOperation('havingNotBetween'), arguments);
  291. }
  292. orHavingNotBetween() {
  293. return this.addOperation(new KnexOperation('havingNotBetween'), arguments);
  294. }
  295. havingRaw() {
  296. return this.addOperation(new KnexOperation('havingRaw'), arguments);
  297. }
  298. orHavingRaw() {
  299. return this.addOperation(new KnexOperation('orHavingRaw'), arguments);
  300. }
  301. offset() {
  302. return this.addOperation(new KnexOperation('offset'), arguments);
  303. }
  304. limit() {
  305. return this.addOperation(new KnexOperation('limit'), arguments);
  306. }
  307. count() {
  308. return this.addOperation(new SelectOperation('count'), arguments);
  309. }
  310. countDistinct() {
  311. return this.addOperation(new SelectOperation('countDistinct'), arguments);
  312. }
  313. min() {
  314. return this.addOperation(new SelectOperation('min'), arguments);
  315. }
  316. max() {
  317. return this.addOperation(new SelectOperation('max'), arguments);
  318. }
  319. sum() {
  320. return this.addOperation(new SelectOperation('sum'), arguments);
  321. }
  322. sumDistinct() {
  323. return this.addOperation(new SelectOperation('sumDistinct'), arguments);
  324. }
  325. avg() {
  326. return this.addOperation(new SelectOperation('avg'), arguments);
  327. }
  328. avgDistinct() {
  329. return this.addOperation(new SelectOperation('avgDistinct'), arguments);
  330. }
  331. debug() {
  332. return this.addOperation(new KnexOperation('debug'), arguments);
  333. }
  334. returning() {
  335. return this.addOperation(new ReturningOperation('returning'), arguments);
  336. }
  337. truncate() {
  338. return this.addOperation(new KnexOperation('truncate'), arguments);
  339. }
  340. connection() {
  341. return this.addOperation(new KnexOperation('connection'), arguments);
  342. }
  343. options() {
  344. return this.addOperation(new KnexOperation('options'), arguments);
  345. }
  346. columnInfo() {
  347. return this.addOperation(new KnexOperation('columnInfo'), arguments);
  348. }
  349. off() {
  350. return this.addOperation(new KnexOperation('off'), arguments);
  351. }
  352. timeout() {
  353. return this.addOperation(new KnexOperation('timeout'), arguments);
  354. }
  355. with() {
  356. return this.addOperation(new KnexOperation('with'), arguments);
  357. }
  358. withRaw() {
  359. return this.addOperation(new KnexOperation('withRaw'), arguments);
  360. }
  361. withWrapped() {
  362. return this.addOperation(new KnexOperation('withWrapped'), arguments);
  363. }
  364. whereComposite() {
  365. return this.addOperation(new WhereCompositeOperation('whereComposite'), arguments);
  366. }
  367. whereInComposite() {
  368. let operation = null;
  369. if (isSqlite(this.knex())) {
  370. operation = new WhereInCompositeSqliteOperation('whereInComposite');
  371. } else if (isMsSql(this.knex())) {
  372. operation = new WhereInCompositeMsSqlOperation('whereInComposite');
  373. } else {
  374. operation = new WhereInCompositeOperation('whereInComposite');
  375. }
  376. return this.addOperation(operation, arguments);
  377. }
  378. whereNotInComposite() {
  379. let operation = null;
  380. if (isSqlite(this.knex())) {
  381. operation = new WhereInCompositeSqliteOperation('whereNotInComposite', { prefix: 'not' });
  382. } else if (isMsSql(this.knex())) {
  383. operation = new WhereInCompositeMsSqlOperation('whereNotInComposite', { prefix: 'not' });
  384. } else {
  385. operation = new WhereInCompositeOperation('whereNotInComposite', { prefix: 'not' });
  386. }
  387. return this.addOperation(operation, arguments);
  388. }
  389. whereJsonSupersetOf() {
  390. return this.addOperation(
  391. new WhereJsonPostgresOperation('whereJsonSupersetOf', { operator: '@>', bool: 'and' }),
  392. arguments
  393. );
  394. }
  395. orWhereJsonSupersetOf() {
  396. return this.addOperation(
  397. new WhereJsonPostgresOperation('orWhereJsonSupersetOf', { operator: '@>', bool: 'or' }),
  398. arguments
  399. );
  400. }
  401. whereJsonNotSupersetOf() {
  402. return this.addOperation(
  403. new WhereJsonPostgresOperation('whereJsonNotSupersetOf', {
  404. operator: '@>',
  405. bool: 'and',
  406. prefix: 'not'
  407. }),
  408. arguments
  409. );
  410. }
  411. orWhereJsonNotSupersetOf() {
  412. return this.addOperation(
  413. new WhereJsonPostgresOperation('orWhereJsonNotSupersetOf', {
  414. operator: '@>',
  415. bool: 'or',
  416. prefix: 'not'
  417. }),
  418. arguments
  419. );
  420. }
  421. whereJsonSubsetOf() {
  422. return this.addOperation(
  423. new WhereJsonPostgresOperation('whereJsonSubsetOf', { operator: '<@', bool: 'and' }),
  424. arguments
  425. );
  426. }
  427. orWhereJsonSubsetOf(fieldExpression, jsonObjectOrFieldExpression) {
  428. return this.addOperation(
  429. new WhereJsonPostgresOperation('orWhereJsonSubsetOf', { operator: '<@', bool: 'or' }),
  430. arguments
  431. );
  432. }
  433. whereJsonNotSubsetOf() {
  434. return this.addOperation(
  435. new WhereJsonPostgresOperation('whereJsonNotSubsetOf', {
  436. operator: '<@',
  437. bool: 'and',
  438. prefix: 'not'
  439. }),
  440. arguments
  441. );
  442. }
  443. orWhereJsonNotSubsetOf(fieldExpression, jsonObjectOrFieldExpression) {
  444. return this.addOperation(
  445. new WhereJsonPostgresOperation('orWhereJsonNotSubsetOf', {
  446. operator: '<@',
  447. bool: 'or',
  448. prefix: 'not'
  449. }),
  450. arguments
  451. );
  452. }
  453. whereJsonNotArray() {
  454. return this.addOperation(
  455. new WhereJsonNotObjectPostgresOperation('whereJsonNotArray', {
  456. bool: 'and',
  457. compareValue: []
  458. }),
  459. arguments
  460. );
  461. }
  462. orWhereJsonNotArray() {
  463. return this.addOperation(
  464. new WhereJsonNotObjectPostgresOperation('orWhereJsonNotArray', {
  465. bool: 'or',
  466. compareValue: []
  467. }),
  468. arguments
  469. );
  470. }
  471. whereJsonNotObject() {
  472. return this.addOperation(
  473. new WhereJsonNotObjectPostgresOperation('whereJsonNotObject', {
  474. bool: 'and',
  475. compareValue: {}
  476. }),
  477. arguments
  478. );
  479. }
  480. orWhereJsonNotObject() {
  481. return this.addOperation(
  482. new WhereJsonNotObjectPostgresOperation('orWhereJsonNotObject', {
  483. bool: 'or',
  484. compareValue: {}
  485. }),
  486. arguments
  487. );
  488. }
  489. whereJsonHasAny() {
  490. return this.addOperation(
  491. new WhereJsonHasPostgresOperation('whereJsonHasAny', { bool: 'and', operator: '?|' }),
  492. arguments
  493. );
  494. }
  495. orWhereJsonHasAny() {
  496. return this.addOperation(
  497. new WhereJsonHasPostgresOperation('orWhereJsonHasAny', { bool: 'or', operator: '?|' }),
  498. arguments
  499. );
  500. }
  501. whereJsonHasAll() {
  502. return this.addOperation(
  503. new WhereJsonHasPostgresOperation('whereJsonHasAll', { bool: 'and', operator: '?&' }),
  504. arguments
  505. );
  506. }
  507. orWhereJsonHasAll() {
  508. return this.addOperation(
  509. new WhereJsonHasPostgresOperation('orWhereJsonHasAll', { bool: 'or', operator: '?&' }),
  510. arguments
  511. );
  512. }
  513. whereJsonIsArray(fieldExpression) {
  514. return this.whereJsonSupersetOf(fieldExpression, []);
  515. }
  516. orWhereJsonIsArray(fieldExpression) {
  517. return this.orWhereJsonSupersetOf(fieldExpression, []);
  518. }
  519. whereJsonIsObject(fieldExpression) {
  520. return this.whereJsonSupersetOf(fieldExpression, {});
  521. }
  522. orWhereJsonIsObject(fieldExpression) {
  523. return this.orWhereJsonSupersetOf(fieldExpression, {});
  524. }
  525. }
  526. Object.defineProperties(QueryBuilderBase.prototype, {
  527. isObjectionQueryBuilderBase: {
  528. enumerable: false,
  529. writable: false,
  530. value: true
  531. }
  532. });
  533. module.exports = QueryBuilderBase;