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