RelationJoinBuilder.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. const Promise = require('bluebird');
  2. const Selection = require('../select/Selection');
  3. const { uniqBy, values } = require('../../../utils/objectUtils');
  4. const { Type: ValidationErrorType } = require('../../../model/ValidationError');
  5. const ID_LENGTH_LIMIT = 63;
  6. const RELATION_RECURSION_LIMIT = 64;
  7. class RelationJoinBuilder {
  8. constructor({ modelClass, expression, filters = Object.create(null) }) {
  9. this.rootModelClass = modelClass;
  10. this.expression = expression;
  11. this.filters = filters;
  12. this.allRelations = null;
  13. this.pathInfo = new Map();
  14. this.encodings = new Map();
  15. this.decodings = new Map();
  16. this.encIdx = 0;
  17. this.opt = {
  18. minimize: false,
  19. separator: ':',
  20. aliases: {}
  21. };
  22. }
  23. setOptions(opt) {
  24. this.opt = Object.assign(this.opt, opt);
  25. }
  26. /**
  27. * Fetches the column information needed for building the select clauses.
  28. * This must be called before calling `build`. `buildJoinOnly` can be called
  29. * without this since it doesn't build selects.
  30. */
  31. fetchColumnInfo(builder) {
  32. const allModelClasses = findAllModels(this.expression, this.rootModelClass);
  33. return Promise.map(
  34. allModelClasses,
  35. ModelClass => ModelClass.fetchTableMetadata({ parentBuilder: builder }),
  36. {
  37. concurrency: this.rootModelClass.concurrency
  38. }
  39. );
  40. }
  41. buildJoinOnly(builder) {
  42. this.doBuild({
  43. expr: this.expression,
  44. builder,
  45. modelClass: builder.modelClass(),
  46. joinOperation: this.opt.joinOperation || 'leftJoin',
  47. parentInfo: null,
  48. relation: null,
  49. noSelects: true,
  50. path: ''
  51. });
  52. }
  53. build(builder) {
  54. const tableName = builder.tableNameFor(this.rootModelClass);
  55. const tableAlias = builder.tableRefFor(this.rootModelClass);
  56. if (tableName === tableAlias) {
  57. builder.table(tableName);
  58. } else {
  59. builder.table(`${tableName} as ${tableAlias}`);
  60. }
  61. this.doBuild({
  62. expr: this.expression,
  63. builder,
  64. modelClass: builder.modelClass(),
  65. joinOperation: this.opt.joinOperation || 'leftJoin',
  66. selectFilterQuery: builder.clone(),
  67. parentInfo: null,
  68. relation: null,
  69. path: ''
  70. });
  71. }
  72. rowsToTree(rows) {
  73. if (!Array.isArray(rows) || rows.length === 0) {
  74. return rows;
  75. }
  76. const keyInfoByPath = this.createKeyInfo(rows);
  77. const pathInfo = Array.from(this.pathInfo.values());
  78. const tree = Object.create(null);
  79. const stack = new Map();
  80. for (let i = 0, lr = rows.length; i < lr; ++i) {
  81. const row = rows[i];
  82. let curBranch = tree;
  83. for (let j = 0, lp = pathInfo.length; j < lp; ++j) {
  84. const pInfo = pathInfo[j];
  85. const id = pInfo.idGetter(row);
  86. let model;
  87. if (id === null) {
  88. continue;
  89. }
  90. if (pInfo.relation) {
  91. const parentModel = stack.get(pInfo.encParentPath);
  92. curBranch = pInfo.getBranch(parentModel);
  93. if (!curBranch) {
  94. curBranch = pInfo.createBranch(parentModel);
  95. }
  96. }
  97. model = pInfo.getModelFromBranch(curBranch, id);
  98. if (!model) {
  99. model = createModel(row, pInfo, keyInfoByPath);
  100. pInfo.setModelToBranch(curBranch, id, model);
  101. }
  102. stack.set(pInfo.encPath, model);
  103. }
  104. }
  105. return this.finalize(pathInfo[0], values(tree));
  106. }
  107. createKeyInfo(rows) {
  108. const keys = Object.keys(rows[0]);
  109. const keyInfo = new Map();
  110. for (let i = 0, l = keys.length; i < l; ++i) {
  111. const key = keys[i];
  112. const sepIdx = key.lastIndexOf(this.sep);
  113. let col, pInfo;
  114. if (sepIdx === -1) {
  115. pInfo = this.pathInfo.get('');
  116. col = key;
  117. } else {
  118. const encPath = key.substr(0, sepIdx);
  119. const path = this.decode(encPath);
  120. col = key.substr(sepIdx + this.sep.length);
  121. pInfo = this.pathInfo.get(path);
  122. }
  123. if (!pInfo.omitCols.has(col)) {
  124. let infoArr = keyInfo.get(pInfo.encPath);
  125. if (!infoArr) {
  126. infoArr = [];
  127. keyInfo.set(pInfo.encPath, infoArr);
  128. }
  129. infoArr.push({ pInfo, key, col });
  130. }
  131. }
  132. return keyInfo;
  133. }
  134. finalize(pInfo, models) {
  135. const relNames = Array.from(pInfo.children.keys());
  136. if (Array.isArray(models)) {
  137. for (let m = 0, lm = models.length; m < lm; ++m) {
  138. this.finalizeOne(pInfo, relNames, models[m]);
  139. }
  140. } else if (models) {
  141. this.finalizeOne(pInfo, relNames, models);
  142. }
  143. return models;
  144. }
  145. finalizeOne(pInfo, relNames, model) {
  146. for (let r = 0, lr = relNames.length; r < lr; ++r) {
  147. const relName = relNames[r];
  148. const branch = model[relName];
  149. const childPathInfo = pInfo.children.get(relName);
  150. const finalized = childPathInfo.finalizeBranch(branch, model);
  151. this.finalize(childPathInfo, finalized);
  152. }
  153. }
  154. doBuild({
  155. expr,
  156. builder,
  157. relation,
  158. modelClass,
  159. selectFilterQuery,
  160. joinOperation,
  161. parentInfo,
  162. noSelects,
  163. path
  164. }) {
  165. if (!this.allRelations) {
  166. this.allRelations = findAllRelations(this.expression, this.rootModelClass);
  167. }
  168. const info = this.createPathInfo({
  169. modelClass,
  170. path,
  171. expr,
  172. relation,
  173. parentInfo
  174. });
  175. this.pathInfo.set(path, info);
  176. if (!noSelects) {
  177. this.buildSelects({
  178. builder,
  179. selectFilterQuery,
  180. modelClass,
  181. relation,
  182. info
  183. });
  184. }
  185. forEachExpr(expr, modelClass, (childExpr, relation) => {
  186. const nextPath = this.joinPath(path, childExpr.$name);
  187. const encNextPath = this.encode(nextPath);
  188. const encJoinTablePath = relation.joinTable ? this.encode(joinTableForPath(nextPath)) : null;
  189. const ownerTable = info.encPath || undefined;
  190. const filterQuery = createFilterQuery({
  191. builder,
  192. modelClass,
  193. relation,
  194. expr: childExpr,
  195. filters: this.filters
  196. });
  197. const relatedJoinSelectQuery = createRelatedJoinFromQuery({
  198. filterQuery,
  199. relation,
  200. allRelations: this.allRelations
  201. });
  202. relation.join(builder, {
  203. ownerTable,
  204. joinOperation,
  205. relatedTableAlias: encNextPath,
  206. joinTableAlias: encJoinTablePath,
  207. relatedJoinSelectQuery
  208. });
  209. // Apply relation.modify since it may also contains selections. Don't move this
  210. // to the createFilterQuery function because relatedJoinSelectQuery is cloned
  211. // from the return value of that function and we don't want relation.modify
  212. // to be called twice for it.
  213. filterQuery.modify(relation.modify);
  214. this.doBuild({
  215. expr: childExpr,
  216. builder,
  217. modelClass: relation.relatedModelClass,
  218. joinOperation,
  219. relation,
  220. parentInfo: info,
  221. noSelects,
  222. path: nextPath,
  223. selectFilterQuery: filterQuery
  224. });
  225. });
  226. }
  227. createPathInfo({ modelClass, path, expr, relation, parentInfo }) {
  228. const encPath = this.encode(path);
  229. let info;
  230. if (relation && relation.isOneToOne()) {
  231. info = new OneToOnePathInfo();
  232. } else {
  233. info = new PathInfo();
  234. }
  235. info.path = path;
  236. info.encPath = encPath;
  237. info.parentPath = parentInfo && parentInfo.path;
  238. info.encParentPath = parentInfo && parentInfo.encPath;
  239. info.modelClass = modelClass;
  240. info.relation = relation;
  241. info.idGetter = this.createIdGetter(modelClass, encPath);
  242. info.relationAlias = expr.$name;
  243. if (parentInfo) {
  244. parentInfo.children.set(expr.$name, info);
  245. }
  246. return info;
  247. }
  248. buildSelects({ builder, selectFilterQuery, modelClass, relation, info }) {
  249. const selects = [];
  250. const idCols = modelClass.getIdColumnArray();
  251. const rootTable = builder.tableRefFor(this.rootModelClass);
  252. const isSelectFilterQuerySubQuery = !!info.encPath;
  253. let selections = selectFilterQuery.findAllSelections();
  254. const selectAllIndex = selections.findIndex(isSelectAll);
  255. // If there are no explicit selects, or there is a `select *` item,
  256. // we need to select all columns using the schema information
  257. // in `modelClass.tableMetadata()`.
  258. if (selections.length === 0 || selectAllIndex !== -1) {
  259. const table = builder.tableNameFor(modelClass);
  260. selections.splice(selectAllIndex, 1);
  261. selections = modelClass
  262. .tableMetadata({ table })
  263. .columns.map(it => new Selection(null, it))
  264. .concat(selections);
  265. }
  266. // Id columns always need to be selected so that we are able to construct
  267. // the tree structure from the flat columns.
  268. for (let i = 0, l = idCols.length; i < l; ++i) {
  269. const idCol = idCols[i];
  270. if (!selections.some(it => it.name === idCol)) {
  271. info.omitCols.add(idCol);
  272. selections.unshift(new Selection(null, idCol));
  273. }
  274. }
  275. for (let i = 0, l = selections.length; i < l; ++i) {
  276. const selection = selections[i];
  277. // If `selections` come from a subquery, we need to use the possible alias instead
  278. // of the column name because that's what the root query sees instead of the real
  279. // column name.
  280. const col = isSelectFilterQuerySubQuery ? selection.name : selection.column;
  281. const name = selection.name;
  282. const fullCol = `${info.encPath || rootTable}.${col}`;
  283. const alias = this.joinPath(info.encPath, name);
  284. if (!builder.hasSelectionAs(fullCol, alias, true)) {
  285. checkAliasLength(modelClass, alias);
  286. selects.push(`${fullCol} as ${alias}`);
  287. }
  288. }
  289. if (relation && relation.joinTableExtras) {
  290. const joinTable = this.encode(joinTableForPath(info.path));
  291. for (let i = 0, l = relation.joinTableExtras.length; i < l; ++i) {
  292. const extra = relation.joinTableExtras[i];
  293. const filterPassed = selectFilterQuery.hasSelection(extra.joinTableCol);
  294. if (filterPassed) {
  295. const fullCol = `${joinTable}.${extra.joinTableCol}`;
  296. if (!builder.hasSelection(fullCol, true)) {
  297. const alias = this.joinPath(info.encPath, extra.aliasCol);
  298. checkAliasLength(modelClass, alias);
  299. selects.push(`${fullCol} as ${alias}`);
  300. }
  301. }
  302. }
  303. }
  304. builder.select(selects);
  305. }
  306. encode(path) {
  307. if (!this.opt.minimize) {
  308. let encPath = this.encodings.get(path);
  309. if (!encPath) {
  310. const parts = path.split(this.sep);
  311. // Don't encode the root.
  312. if (!path) {
  313. encPath = path;
  314. } else {
  315. encPath = parts.map(part => this.opt.aliases[part] || part).join(this.sep);
  316. }
  317. this.encodings.set(path, encPath);
  318. this.decodings.set(encPath, path);
  319. }
  320. return encPath;
  321. } else {
  322. let encPath = this.encodings.get(path);
  323. if (!encPath) {
  324. // Don't encode the root.
  325. if (!path) {
  326. encPath = path;
  327. } else {
  328. encPath = this.nextEncodedPath();
  329. }
  330. this.encodings.set(path, encPath);
  331. this.decodings.set(encPath, path);
  332. }
  333. return encPath;
  334. }
  335. }
  336. decode(path) {
  337. return this.decodings.get(path);
  338. }
  339. nextEncodedPath() {
  340. return `_t${++this.encIdx}`;
  341. }
  342. createIdGetter(modelClass, path) {
  343. const idCols = modelClass.getIdColumnArray().map(col => this.joinPath(path, col));
  344. if (idCols.length === 1) {
  345. return createSingleIdGetter(idCols);
  346. } else if (idCols.length === 2) {
  347. return createTwoIdGetter(idCols);
  348. } else if (idCols.length === 3) {
  349. return createThreeIdGetter(idCols);
  350. } else {
  351. return createNIdGetter(idCols);
  352. }
  353. }
  354. get sep() {
  355. return this.opt.separator;
  356. }
  357. joinPath(path, nextPart) {
  358. if (path) {
  359. return `${path}${this.sep}${nextPart}`;
  360. } else {
  361. return nextPart;
  362. }
  363. }
  364. }
  365. function findAllModels(expr, modelClass) {
  366. const modelClasses = [];
  367. findAllModelsImpl(expr, modelClass, modelClasses);
  368. return uniqBy(modelClasses, getTableName);
  369. }
  370. function getTableName(modelClass) {
  371. return modelClass.getTableName();
  372. }
  373. function findAllModelsImpl(expr, modelClass, models) {
  374. models.push(modelClass);
  375. forEachExpr(expr, modelClass, (childExpr, relation) => {
  376. findAllModelsImpl(childExpr, relation.relatedModelClass, models);
  377. });
  378. }
  379. function findAllRelations(expr, modelClass) {
  380. const relations = [];
  381. findAllRelationsImpl(expr, modelClass, relations);
  382. return uniqBy(relations);
  383. }
  384. function strictEqual(lhs, rhs) {
  385. return lhs === rhs;
  386. }
  387. function findAllRelationsImpl(expr, modelClass, relations) {
  388. forEachExpr(expr, modelClass, (childExpr, relation) => {
  389. relations.push(relation);
  390. findAllRelationsImpl(childExpr, relation.relatedModelClass, relations);
  391. });
  392. }
  393. function forEachExpr(expr, modelClass, callback) {
  394. const relations = modelClass.getRelations();
  395. if (expr.isAllRecursive || expr.maxRecursionDepth > RELATION_RECURSION_LIMIT) {
  396. throw modelClass.createValidationError({
  397. type: ValidationErrorType.RelationExpression,
  398. message: `recursion depth of eager expression ${expr.toString()} too big for JoinEagerAlgorithm`
  399. });
  400. }
  401. expr.forEachChildExpression(relations, callback);
  402. }
  403. function createSingleIdGetter(idCols) {
  404. const idCol = idCols[0];
  405. return row => {
  406. const val = row[idCol];
  407. if (isNullOrUndefined(val)) {
  408. return null;
  409. } else {
  410. return `id:${val}`;
  411. }
  412. };
  413. }
  414. function createTwoIdGetter(idCols) {
  415. const idCol1 = idCols[0];
  416. const idCol2 = idCols[1];
  417. return row => {
  418. const val1 = row[idCol1];
  419. const val2 = row[idCol2];
  420. if (isNullOrUndefined(val1) || isNullOrUndefined(val2)) {
  421. return null;
  422. } else {
  423. return `id:${val1},${val2}`;
  424. }
  425. };
  426. }
  427. function createThreeIdGetter(idCols) {
  428. const idCol1 = idCols[0];
  429. const idCol2 = idCols[1];
  430. const idCol3 = idCols[2];
  431. return row => {
  432. const val1 = row[idCol1];
  433. const val2 = row[idCol2];
  434. const val3 = row[idCol3];
  435. if (isNullOrUndefined(val1) || isNullOrUndefined(val2) || isNullOrUndefined(val3)) {
  436. return null;
  437. } else {
  438. return `id:${val1},${val2},${val3}`;
  439. }
  440. };
  441. }
  442. function createNIdGetter(idCols) {
  443. return row => {
  444. let id = 'id:';
  445. for (let i = 0, l = idCols.length; i < l; ++i) {
  446. const val = row[idCols[i]];
  447. if (isNullOrUndefined(val)) {
  448. return null;
  449. }
  450. id += (i > 0 ? ',' : '') + val;
  451. }
  452. return id;
  453. };
  454. }
  455. function isNullOrUndefined(val) {
  456. return val === null || val === undefined;
  457. }
  458. function createFilterQuery({ builder, modelClass, expr, filters, relation }) {
  459. const modelNamedFilters = relation.relatedModelClass.namedFilters || {};
  460. const filterQuery = relation.relatedModelClass.query().childQueryOf(builder);
  461. for (let i = 0, l = expr.$modify.length; i < l; ++i) {
  462. const filterName = expr.$modify[i];
  463. const filter = filters[filterName] || modelNamedFilters[filterName];
  464. if (typeof filter !== 'function') {
  465. throw modelClass.createValidationError({
  466. type: ValidationErrorType.RelationExpression,
  467. message: `could not find filter "${filterName}" for relation "${relation.name}"`
  468. });
  469. }
  470. filter(filterQuery);
  471. }
  472. return filterQuery;
  473. }
  474. function createRelatedJoinFromQuery({ filterQuery, relation, allRelations }) {
  475. const relatedJoinFromQuery = filterQuery.clone();
  476. const tableRef = filterQuery.tableRefFor(relation.relatedModelClass);
  477. const allForeignKeys = findAllForeignKeysForModel({
  478. modelClass: relation.relatedModelClass,
  479. allRelations
  480. });
  481. return relatedJoinFromQuery.select(
  482. allForeignKeys
  483. .filter(col => {
  484. return !relatedJoinFromQuery.hasSelectionAs(col, col);
  485. })
  486. .map(col => {
  487. return `${tableRef}.${col}`;
  488. })
  489. );
  490. }
  491. function findAllForeignKeysForModel({ modelClass, allRelations }) {
  492. const foreignKeys = modelClass.getIdColumnArray().slice();
  493. allRelations.forEach(rel => {
  494. if (rel.relatedModelClass === modelClass) {
  495. rel.relatedProp.cols.forEach(col => foreignKeys.push(col));
  496. }
  497. if (rel.ownerModelClass === modelClass) {
  498. rel.ownerProp.cols.forEach(col => foreignKeys.push(col));
  499. }
  500. });
  501. return uniqBy(foreignKeys);
  502. }
  503. function createModel(row, pInfo, keyInfoByPath) {
  504. const keyInfo = keyInfoByPath.get(pInfo.encPath);
  505. const json = {};
  506. for (let k = 0, lk = keyInfo.length; k < lk; ++k) {
  507. const kInfo = keyInfo[k];
  508. json[kInfo.col] = row[kInfo.key];
  509. }
  510. return pInfo.modelClass.fromDatabaseJson(json);
  511. }
  512. function joinTableForPath(path) {
  513. return path + '_join';
  514. }
  515. function checkAliasLength(modelClass, alias) {
  516. if (alias.length > ID_LENGTH_LIMIT) {
  517. throw modelClass.createValidationError({
  518. type: ValidationErrorType.RelationExpression,
  519. message: `identifier ${alias} is over ${ID_LENGTH_LIMIT} characters long and would be truncated by the database engine.`
  520. });
  521. }
  522. }
  523. function isSelectAll(selection) {
  524. return selection.column === '*';
  525. }
  526. class PathInfo {
  527. constructor() {
  528. this.path = null;
  529. this.encPath = null;
  530. this.encParentPath = null;
  531. this.modelClass = null;
  532. this.relation = null;
  533. this.omitCols = new Set();
  534. this.children = new Map();
  535. this.idGetter = null;
  536. this.relationAlias = null;
  537. }
  538. createBranch(parentModel) {
  539. const branch = Object.create(null);
  540. parentModel[this.relationAlias] = branch;
  541. return branch;
  542. }
  543. getBranch(parentModel) {
  544. return parentModel[this.relationAlias];
  545. }
  546. getModelFromBranch(branch, id) {
  547. return branch[id];
  548. }
  549. setModelToBranch(branch, id, model) {
  550. branch[id] = model;
  551. }
  552. finalizeBranch(branch, parentModel) {
  553. const relModels = values(branch);
  554. parentModel[this.relationAlias] = relModels;
  555. return relModels;
  556. }
  557. }
  558. class OneToOnePathInfo extends PathInfo {
  559. createBranch(parentModel) {
  560. return parentModel;
  561. }
  562. getBranch(parentModel) {
  563. return parentModel;
  564. }
  565. getModelFromBranch(branch, id) {
  566. return branch[this.relationAlias];
  567. }
  568. setModelToBranch(branch, id, model) {
  569. branch[this.relationAlias] = model;
  570. }
  571. finalizeBranch(branch, parentModel) {
  572. parentModel[this.relationAlias] = branch || null;
  573. return branch || null;
  574. }
  575. }
  576. module.exports = RelationJoinBuilder;