query.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. 'use strict';
  2. const _ = require('lodash');
  3. const SqlString = require('../../sql-string');
  4. const QueryTypes = require('../../query-types');
  5. const Dot = require('dottie');
  6. const deprecations = require('../../utils/deprecations');
  7. const uuid = require('uuid').v4;
  8. class AbstractQuery {
  9. constructor(connection, sequelize, options) {
  10. this.uuid = uuid();
  11. this.connection = connection;
  12. this.instance = options.instance;
  13. this.model = options.model;
  14. this.sequelize = sequelize;
  15. this.options = Object.assign({
  16. plain: false,
  17. raw: false,
  18. // eslint-disable-next-line no-console
  19. logging: console.log
  20. }, options || {});
  21. this.checkLoggingOption();
  22. }
  23. /**
  24. * rewrite query with parameters
  25. *
  26. * Examples:
  27. *
  28. * query.formatBindParameters('select $1 as foo', ['fooval']);
  29. *
  30. * query.formatBindParameters('select $foo as foo', { foo: 'fooval' });
  31. *
  32. * Options
  33. * skipUnescape: bool, skip unescaping $$
  34. * skipValueReplace: bool, do not replace (but do unescape $$). Check correct syntax and if all values are available
  35. *
  36. * @param {string} sql
  37. * @param {Object|Array} values
  38. * @param {string} dialect
  39. * @param {Function} [replacementFunc]
  40. * @param {Object} [options]
  41. * @private
  42. */
  43. static formatBindParameters(sql, values, dialect, replacementFunc, options) {
  44. if (!values) {
  45. return [sql, []];
  46. }
  47. options = options || {};
  48. if (typeof replacementFunc !== 'function') {
  49. options = replacementFunc || {};
  50. replacementFunc = undefined;
  51. }
  52. if (!replacementFunc) {
  53. if (options.skipValueReplace) {
  54. replacementFunc = (match, key, values) => {
  55. if (values[key] !== undefined) {
  56. return match;
  57. }
  58. return undefined;
  59. };
  60. } else {
  61. replacementFunc = (match, key, values, timeZone, dialect) => {
  62. if (values[key] !== undefined) {
  63. return SqlString.escape(values[key], timeZone, dialect);
  64. }
  65. return undefined;
  66. };
  67. }
  68. } else if (options.skipValueReplace) {
  69. const origReplacementFunc = replacementFunc;
  70. replacementFunc = (match, key, values, timeZone, dialect, options) => {
  71. if (origReplacementFunc(match, key, values, timeZone, dialect, options) !== undefined) {
  72. return match;
  73. }
  74. return undefined;
  75. };
  76. }
  77. const timeZone = null;
  78. const list = Array.isArray(values);
  79. sql = sql.replace(/\$(\$|\w+)/g, (match, key) => {
  80. if ('$' === key) {
  81. return options.skipUnescape ? match : key;
  82. }
  83. let replVal;
  84. if (list) {
  85. if (key.match(/^[1-9]\d*$/)) {
  86. key = key - 1;
  87. replVal = replacementFunc(match, key, values, timeZone, dialect, options);
  88. }
  89. } else if (!key.match(/^\d*$/)) {
  90. replVal = replacementFunc(match, key, values, timeZone, dialect, options);
  91. }
  92. if (replVal === undefined) {
  93. throw new Error(`Named bind parameter "${match}" has no value in the given object.`);
  94. }
  95. return replVal;
  96. });
  97. return [sql, []];
  98. }
  99. /**
  100. * Execute the passed sql query.
  101. *
  102. * Examples:
  103. *
  104. * query.run('SELECT 1')
  105. *
  106. * @private
  107. */
  108. run() {
  109. throw new Error('The run method wasn\'t overwritten!');
  110. }
  111. /**
  112. * Check the logging option of the instance and print deprecation warnings.
  113. *
  114. * @private
  115. */
  116. checkLoggingOption() {
  117. if (this.options.logging === true) {
  118. deprecations.noTrueLogging();
  119. // eslint-disable-next-line no-console
  120. this.options.logging = console.log;
  121. }
  122. }
  123. /**
  124. * Get the attributes of an insert query, which contains the just inserted id.
  125. *
  126. * @returns {string} The field name.
  127. * @private
  128. */
  129. getInsertIdField() {
  130. return 'insertId';
  131. }
  132. getUniqueConstraintErrorMessage(field) {
  133. let message = field ? `${field} must be unique` : 'Must be unique';
  134. if (field && this.model) {
  135. for (const key of Object.keys(this.model.uniqueKeys)) {
  136. if (this.model.uniqueKeys[key].fields.includes(field.replace(/"/g, ''))) {
  137. if (this.model.uniqueKeys[key].msg) {
  138. message = this.model.uniqueKeys[key].msg;
  139. }
  140. }
  141. }
  142. }
  143. return message;
  144. }
  145. isRawQuery() {
  146. return this.options.type === QueryTypes.RAW;
  147. }
  148. isVersionQuery() {
  149. return this.options.type === QueryTypes.VERSION;
  150. }
  151. isUpsertQuery() {
  152. return this.options.type === QueryTypes.UPSERT;
  153. }
  154. isInsertQuery(results, metaData) {
  155. let result = true;
  156. if (this.options.type === QueryTypes.INSERT) {
  157. return true;
  158. }
  159. // is insert query if sql contains insert into
  160. result = result && this.sql.toLowerCase().startsWith('insert into');
  161. // is insert query if no results are passed or if the result has the inserted id
  162. result = result && (!results || Object.prototype.hasOwnProperty.call(results, this.getInsertIdField()));
  163. // is insert query if no metadata are passed or if the metadata has the inserted id
  164. result = result && (!metaData || Object.prototype.hasOwnProperty.call(metaData, this.getInsertIdField()));
  165. return result;
  166. }
  167. handleInsertQuery(results, metaData) {
  168. if (this.instance) {
  169. // add the inserted row id to the instance
  170. const autoIncrementAttribute = this.model.autoIncrementAttribute;
  171. let id = null;
  172. id = id || results && results[this.getInsertIdField()];
  173. id = id || metaData && metaData[this.getInsertIdField()];
  174. this.instance[autoIncrementAttribute] = id;
  175. }
  176. }
  177. isShowTablesQuery() {
  178. return this.options.type === QueryTypes.SHOWTABLES;
  179. }
  180. handleShowTablesQuery(results) {
  181. return _.flatten(results.map(resultSet => _.values(resultSet)));
  182. }
  183. isShowIndexesQuery() {
  184. return this.options.type === QueryTypes.SHOWINDEXES;
  185. }
  186. isShowConstraintsQuery() {
  187. return this.options.type === QueryTypes.SHOWCONSTRAINTS;
  188. }
  189. isDescribeQuery() {
  190. return this.options.type === QueryTypes.DESCRIBE;
  191. }
  192. isSelectQuery() {
  193. return this.options.type === QueryTypes.SELECT;
  194. }
  195. isBulkUpdateQuery() {
  196. return this.options.type === QueryTypes.BULKUPDATE;
  197. }
  198. isBulkDeleteQuery() {
  199. return this.options.type === QueryTypes.BULKDELETE;
  200. }
  201. isForeignKeysQuery() {
  202. return this.options.type === QueryTypes.FOREIGNKEYS;
  203. }
  204. isUpdateQuery() {
  205. return this.options.type === QueryTypes.UPDATE;
  206. }
  207. handleSelectQuery(results) {
  208. let result = null;
  209. // Map raw fields to names if a mapping is provided
  210. if (this.options.fieldMap) {
  211. const fieldMap = this.options.fieldMap;
  212. results = results.map(result => _.reduce(fieldMap, (result, name, field) => {
  213. if (result[field] !== undefined && name !== field) {
  214. result[name] = result[field];
  215. delete result[field];
  216. }
  217. return result;
  218. }, result));
  219. }
  220. // Raw queries
  221. if (this.options.raw) {
  222. result = results.map(result => {
  223. let o = {};
  224. for (const key in result) {
  225. if (Object.prototype.hasOwnProperty.call(result, key)) {
  226. o[key] = result[key];
  227. }
  228. }
  229. if (this.options.nest) {
  230. o = Dot.transform(o);
  231. }
  232. return o;
  233. });
  234. // Queries with include
  235. } else if (this.options.hasJoin === true) {
  236. results = AbstractQuery._groupJoinData(results, {
  237. model: this.model,
  238. includeMap: this.options.includeMap,
  239. includeNames: this.options.includeNames
  240. }, {
  241. checkExisting: this.options.hasMultiAssociation
  242. });
  243. result = this.model.bulkBuild(results, {
  244. isNewRecord: false,
  245. include: this.options.include,
  246. includeNames: this.options.includeNames,
  247. includeMap: this.options.includeMap,
  248. includeValidated: true,
  249. attributes: this.options.originalAttributes || this.options.attributes,
  250. raw: true
  251. });
  252. // Regular queries
  253. } else {
  254. result = this.model.bulkBuild(results, {
  255. isNewRecord: false,
  256. raw: true,
  257. attributes: this.options.originalAttributes || this.options.attributes
  258. });
  259. }
  260. // return the first real model instance if options.plain is set (e.g. Model.find)
  261. if (this.options.plain) {
  262. result = result.length === 0 ? null : result[0];
  263. }
  264. return result;
  265. }
  266. isShowOrDescribeQuery() {
  267. let result = false;
  268. result = result || this.sql.toLowerCase().startsWith('show');
  269. result = result || this.sql.toLowerCase().startsWith('describe');
  270. return result;
  271. }
  272. isCallQuery() {
  273. return this.sql.toLowerCase().startsWith('call');
  274. }
  275. /**
  276. * @param {string} sql
  277. * @param {Function} debugContext
  278. * @param {Array|Object} parameters
  279. * @protected
  280. * @returns {Function} A function to call after the query was completed.
  281. */
  282. _logQuery(sql, debugContext, parameters) {
  283. const { connection, options } = this;
  284. const benchmark = this.sequelize.options.benchmark || options.benchmark;
  285. const logQueryParameters = this.sequelize.options.logQueryParameters || options.logQueryParameters;
  286. const startTime = Date.now();
  287. let logParameter = '';
  288. if (logQueryParameters && parameters) {
  289. const delimiter = sql.endsWith(';') ? '' : ';';
  290. let paramStr;
  291. if (Array.isArray(parameters)) {
  292. paramStr = parameters.map(p=>JSON.stringify(p)).join(', ');
  293. } else {
  294. paramStr = JSON.stringify(parameters);
  295. }
  296. logParameter = `${delimiter} ${paramStr}`;
  297. }
  298. const fmt = `(${connection.uuid || 'default'}): ${sql}${logParameter}`;
  299. const msg = `Executing ${fmt}`;
  300. debugContext(msg);
  301. if (!benchmark) {
  302. this.sequelize.log(`Executing ${fmt}`, options);
  303. }
  304. return () => {
  305. const afterMsg = `Executed ${fmt}`;
  306. debugContext(afterMsg);
  307. if (benchmark) {
  308. this.sequelize.log(afterMsg, Date.now() - startTime, options);
  309. }
  310. };
  311. }
  312. /**
  313. * The function takes the result of the query execution and groups
  314. * the associated data by the callee.
  315. *
  316. * Example:
  317. * groupJoinData([
  318. * {
  319. * some: 'data',
  320. * id: 1,
  321. * association: { foo: 'bar', id: 1 }
  322. * }, {
  323. * some: 'data',
  324. * id: 1,
  325. * association: { foo: 'bar', id: 2 }
  326. * }, {
  327. * some: 'data',
  328. * id: 1,
  329. * association: { foo: 'bar', id: 3 }
  330. * }
  331. * ])
  332. *
  333. * Result:
  334. * Something like this:
  335. *
  336. * [
  337. * {
  338. * some: 'data',
  339. * id: 1,
  340. * association: [
  341. * { foo: 'bar', id: 1 },
  342. * { foo: 'bar', id: 2 },
  343. * { foo: 'bar', id: 3 }
  344. * ]
  345. * }
  346. * ]
  347. *
  348. * @param {Array} rows
  349. * @param {Object} includeOptions
  350. * @param {Object} options
  351. * @private
  352. */
  353. static _groupJoinData(rows, includeOptions, options) {
  354. /*
  355. * Assumptions
  356. * ID is not necessarily the first field
  357. * All fields for a level is grouped in the same set (i.e. Panel.id, Task.id, Panel.title is not possible)
  358. * Parent keys will be seen before any include/child keys
  359. * Previous set won't necessarily be parent set (one parent could have two children, one child would then be previous set for the other)
  360. */
  361. /*
  362. * Author (MH) comment: This code is an unreadable mess, but it's performant.
  363. * groupJoinData is a performance critical function so we prioritize perf over readability.
  364. */
  365. if (!rows.length) {
  366. return [];
  367. }
  368. // Generic looping
  369. let i;
  370. let length;
  371. let $i;
  372. let $length;
  373. // Row specific looping
  374. let rowsI;
  375. let row;
  376. const rowsLength = rows.length;
  377. // Key specific looping
  378. let keys;
  379. let key;
  380. let keyI;
  381. let keyLength;
  382. let prevKey;
  383. let values;
  384. let topValues;
  385. let topExists;
  386. const checkExisting = options.checkExisting;
  387. // If we don't have to deduplicate we can pre-allocate the resulting array
  388. let itemHash;
  389. let parentHash;
  390. let topHash;
  391. const results = checkExisting ? [] : new Array(rowsLength);
  392. const resultMap = {};
  393. const includeMap = {};
  394. // Result variables for the respective functions
  395. let $keyPrefix;
  396. let $keyPrefixString;
  397. let $prevKeyPrefixString; // eslint-disable-line
  398. let $prevKeyPrefix;
  399. let $lastKeyPrefix;
  400. let $current;
  401. let $parent;
  402. // Map each key to an include option
  403. let previousPiece;
  404. const buildIncludeMap = piece => {
  405. if (Object.prototype.hasOwnProperty.call($current.includeMap, piece)) {
  406. includeMap[key] = $current = $current.includeMap[piece];
  407. if (previousPiece) {
  408. previousPiece = `${previousPiece}.${piece}`;
  409. } else {
  410. previousPiece = piece;
  411. }
  412. includeMap[previousPiece] = $current;
  413. }
  414. };
  415. // Calculate the string prefix of a key ('User.Results' for 'User.Results.id')
  416. const keyPrefixStringMemo = {};
  417. const keyPrefixString = (key, memo) => {
  418. if (!Object.prototype.hasOwnProperty.call(memo, key)) {
  419. memo[key] = key.substr(0, key.lastIndexOf('.'));
  420. }
  421. return memo[key];
  422. };
  423. // Removes the prefix from a key ('id' for 'User.Results.id')
  424. const removeKeyPrefixMemo = {};
  425. const removeKeyPrefix = key => {
  426. if (!Object.prototype.hasOwnProperty.call(removeKeyPrefixMemo, key)) {
  427. const index = key.lastIndexOf('.');
  428. removeKeyPrefixMemo[key] = key.substr(index === -1 ? 0 : index + 1);
  429. }
  430. return removeKeyPrefixMemo[key];
  431. };
  432. // Calculates the array prefix of a key (['User', 'Results'] for 'User.Results.id')
  433. const keyPrefixMemo = {};
  434. const keyPrefix = key => {
  435. // We use a double memo and keyPrefixString so that different keys with the same prefix will receive the same array instead of differnet arrays with equal values
  436. if (!Object.prototype.hasOwnProperty.call(keyPrefixMemo, key)) {
  437. const prefixString = keyPrefixString(key, keyPrefixStringMemo);
  438. if (!Object.prototype.hasOwnProperty.call(keyPrefixMemo, prefixString)) {
  439. keyPrefixMemo[prefixString] = prefixString ? prefixString.split('.') : [];
  440. }
  441. keyPrefixMemo[key] = keyPrefixMemo[prefixString];
  442. }
  443. return keyPrefixMemo[key];
  444. };
  445. // Calcuate the last item in the array prefix ('Results' for 'User.Results.id')
  446. const lastKeyPrefixMemo = {};
  447. const lastKeyPrefix = key => {
  448. if (!Object.prototype.hasOwnProperty.call(lastKeyPrefixMemo, key)) {
  449. const prefix = keyPrefix(key);
  450. const length = prefix.length;
  451. lastKeyPrefixMemo[key] = !length ? '' : prefix[length - 1];
  452. }
  453. return lastKeyPrefixMemo[key];
  454. };
  455. const getUniqueKeyAttributes = model => {
  456. let uniqueKeyAttributes = _.chain(model.uniqueKeys);
  457. uniqueKeyAttributes = uniqueKeyAttributes
  458. .result(`${uniqueKeyAttributes.findKey()}.fields`)
  459. .map(field => _.findKey(model.attributes, chr => chr.field === field))
  460. .value();
  461. return uniqueKeyAttributes;
  462. };
  463. const stringify = obj => obj instanceof Buffer ? obj.toString('hex') : obj;
  464. let primaryKeyAttributes;
  465. let uniqueKeyAttributes;
  466. let prefix;
  467. for (rowsI = 0; rowsI < rowsLength; rowsI++) {
  468. row = rows[rowsI];
  469. // Keys are the same for all rows, so only need to compute them on the first row
  470. if (rowsI === 0) {
  471. keys = Object.keys(row);
  472. keyLength = keys.length;
  473. }
  474. if (checkExisting) {
  475. topExists = false;
  476. // Compute top level hash key (this is usually just the primary key values)
  477. $length = includeOptions.model.primaryKeyAttributes.length;
  478. topHash = '';
  479. if ($length === 1) {
  480. topHash = stringify(row[includeOptions.model.primaryKeyAttributes[0]]);
  481. }
  482. else if ($length > 1) {
  483. for ($i = 0; $i < $length; $i++) {
  484. topHash += stringify(row[includeOptions.model.primaryKeyAttributes[$i]]);
  485. }
  486. }
  487. else if (!_.isEmpty(includeOptions.model.uniqueKeys)) {
  488. uniqueKeyAttributes = getUniqueKeyAttributes(includeOptions.model);
  489. for ($i = 0; $i < uniqueKeyAttributes.length; $i++) {
  490. topHash += row[uniqueKeyAttributes[$i]];
  491. }
  492. }
  493. }
  494. topValues = values = {};
  495. $prevKeyPrefix = undefined;
  496. for (keyI = 0; keyI < keyLength; keyI++) {
  497. key = keys[keyI];
  498. // The string prefix isn't actualy needed
  499. // We use it so keyPrefix for different keys will resolve to the same array if they have the same prefix
  500. // TODO: Find a better way?
  501. $keyPrefixString = keyPrefixString(key, keyPrefixStringMemo);
  502. $keyPrefix = keyPrefix(key);
  503. // On the first row we compute the includeMap
  504. if (rowsI === 0 && !Object.prototype.hasOwnProperty.call(includeMap, key)) {
  505. if (!$keyPrefix.length) {
  506. includeMap[key] = includeMap[''] = includeOptions;
  507. } else {
  508. $current = includeOptions;
  509. previousPiece = undefined;
  510. $keyPrefix.forEach(buildIncludeMap);
  511. }
  512. }
  513. // End of key set
  514. if ($prevKeyPrefix !== undefined && $prevKeyPrefix !== $keyPrefix) {
  515. if (checkExisting) {
  516. // Compute hash key for this set instance
  517. // TODO: Optimize
  518. length = $prevKeyPrefix.length;
  519. $parent = null;
  520. parentHash = null;
  521. if (length) {
  522. for (i = 0; i < length; i++) {
  523. prefix = $parent ? `${$parent}.${$prevKeyPrefix[i]}` : $prevKeyPrefix[i];
  524. primaryKeyAttributes = includeMap[prefix].model.primaryKeyAttributes;
  525. $length = primaryKeyAttributes.length;
  526. itemHash = prefix;
  527. if ($length === 1) {
  528. itemHash += stringify(row[`${prefix}.${primaryKeyAttributes[0]}`]);
  529. }
  530. else if ($length > 1) {
  531. for ($i = 0; $i < $length; $i++) {
  532. itemHash += stringify(row[`${prefix}.${primaryKeyAttributes[$i]}`]);
  533. }
  534. }
  535. else if (!_.isEmpty(includeMap[prefix].model.uniqueKeys)) {
  536. uniqueKeyAttributes = getUniqueKeyAttributes(includeMap[prefix].model);
  537. for ($i = 0; $i < uniqueKeyAttributes.length; $i++) {
  538. itemHash += row[`${prefix}.${uniqueKeyAttributes[$i]}`];
  539. }
  540. }
  541. if (!parentHash) {
  542. parentHash = topHash;
  543. }
  544. itemHash = parentHash + itemHash;
  545. $parent = prefix;
  546. if (i < length - 1) {
  547. parentHash = itemHash;
  548. }
  549. }
  550. } else {
  551. itemHash = topHash;
  552. }
  553. if (itemHash === topHash) {
  554. if (!resultMap[itemHash]) {
  555. resultMap[itemHash] = values;
  556. } else {
  557. topExists = true;
  558. }
  559. } else if (!resultMap[itemHash]) {
  560. $parent = resultMap[parentHash];
  561. $lastKeyPrefix = lastKeyPrefix(prevKey);
  562. if (includeMap[prevKey].association.isSingleAssociation) {
  563. if ($parent) {
  564. $parent[$lastKeyPrefix] = resultMap[itemHash] = values;
  565. }
  566. } else {
  567. if (!$parent[$lastKeyPrefix]) {
  568. $parent[$lastKeyPrefix] = [];
  569. }
  570. $parent[$lastKeyPrefix].push(resultMap[itemHash] = values);
  571. }
  572. }
  573. // Reset values
  574. values = {};
  575. } else {
  576. // If checkExisting is false it's because there's only 1:1 associations in this query
  577. // However we still need to map onto the appropriate parent
  578. // For 1:1 we map forward, initializing the value object on the parent to be filled in the next iterations of the loop
  579. $current = topValues;
  580. length = $keyPrefix.length;
  581. if (length) {
  582. for (i = 0; i < length; i++) {
  583. if (i === length - 1) {
  584. values = $current[$keyPrefix[i]] = {};
  585. }
  586. $current = $current[$keyPrefix[i]] || {};
  587. }
  588. }
  589. }
  590. }
  591. // End of iteration, set value and set prev values (for next iteration)
  592. values[removeKeyPrefix(key)] = row[key];
  593. prevKey = key;
  594. $prevKeyPrefix = $keyPrefix;
  595. $prevKeyPrefixString = $keyPrefixString;
  596. }
  597. if (checkExisting) {
  598. length = $prevKeyPrefix.length;
  599. $parent = null;
  600. parentHash = null;
  601. if (length) {
  602. for (i = 0; i < length; i++) {
  603. prefix = $parent ? `${$parent}.${$prevKeyPrefix[i]}` : $prevKeyPrefix[i];
  604. primaryKeyAttributes = includeMap[prefix].model.primaryKeyAttributes;
  605. $length = primaryKeyAttributes.length;
  606. itemHash = prefix;
  607. if ($length === 1) {
  608. itemHash += stringify(row[`${prefix}.${primaryKeyAttributes[0]}`]);
  609. }
  610. else if ($length > 0) {
  611. for ($i = 0; $i < $length; $i++) {
  612. itemHash += stringify(row[`${prefix}.${primaryKeyAttributes[$i]}`]);
  613. }
  614. }
  615. else if (!_.isEmpty(includeMap[prefix].model.uniqueKeys)) {
  616. uniqueKeyAttributes = getUniqueKeyAttributes(includeMap[prefix].model);
  617. for ($i = 0; $i < uniqueKeyAttributes.length; $i++) {
  618. itemHash += row[`${prefix}.${uniqueKeyAttributes[$i]}`];
  619. }
  620. }
  621. if (!parentHash) {
  622. parentHash = topHash;
  623. }
  624. itemHash = parentHash + itemHash;
  625. $parent = prefix;
  626. if (i < length - 1) {
  627. parentHash = itemHash;
  628. }
  629. }
  630. } else {
  631. itemHash = topHash;
  632. }
  633. if (itemHash === topHash) {
  634. if (!resultMap[itemHash]) {
  635. resultMap[itemHash] = values;
  636. } else {
  637. topExists = true;
  638. }
  639. } else if (!resultMap[itemHash]) {
  640. $parent = resultMap[parentHash];
  641. $lastKeyPrefix = lastKeyPrefix(prevKey);
  642. if (includeMap[prevKey].association.isSingleAssociation) {
  643. if ($parent) {
  644. $parent[$lastKeyPrefix] = resultMap[itemHash] = values;
  645. }
  646. } else {
  647. if (!$parent[$lastKeyPrefix]) {
  648. $parent[$lastKeyPrefix] = [];
  649. }
  650. $parent[$lastKeyPrefix].push(resultMap[itemHash] = values);
  651. }
  652. }
  653. if (!topExists) {
  654. results.push(topValues);
  655. }
  656. } else {
  657. results[rowsI] = topValues;
  658. }
  659. }
  660. return results;
  661. }
  662. }
  663. module.exports = AbstractQuery;
  664. module.exports.AbstractQuery = AbstractQuery;
  665. module.exports.default = AbstractQuery;