query-generator.js 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653
  1. 'use strict';
  2. const util = require('util');
  3. const _ = require('lodash');
  4. const uuidv4 = require('uuid').v4;
  5. const semver = require('semver');
  6. const Utils = require('../../utils');
  7. const deprecations = require('../../utils/deprecations');
  8. const SqlString = require('../../sql-string');
  9. const DataTypes = require('../../data-types');
  10. const Model = require('../../model');
  11. const Association = require('../../associations/base');
  12. const BelongsTo = require('../../associations/belongs-to');
  13. const BelongsToMany = require('../../associations/belongs-to-many');
  14. const HasMany = require('../../associations/has-many');
  15. const Op = require('../../operators');
  16. const sequelizeError = require('../../errors');
  17. const IndexHints = require('../../index-hints');
  18. const QuoteHelper = require('./query-generator/helpers/quote');
  19. /**
  20. * Abstract Query Generator
  21. *
  22. * @private
  23. */
  24. class QueryGenerator {
  25. constructor(options) {
  26. if (!options.sequelize) throw new Error('QueryGenerator initialized without options.sequelize');
  27. if (!options._dialect) throw new Error('QueryGenerator initialized without options._dialect');
  28. this.sequelize = options.sequelize;
  29. this.options = options.sequelize.options;
  30. // dialect name
  31. this.dialect = options._dialect.name;
  32. this._dialect = options._dialect;
  33. }
  34. extractTableDetails(tableName, options) {
  35. options = options || {};
  36. tableName = tableName || {};
  37. return {
  38. schema: tableName.schema || options.schema || 'public',
  39. tableName: _.isPlainObject(tableName) ? tableName.tableName : tableName,
  40. delimiter: tableName.delimiter || options.delimiter || '.'
  41. };
  42. }
  43. addSchema(param) {
  44. if (!param._schema) return param.tableName || param;
  45. const self = this;
  46. return {
  47. tableName: param.tableName || param,
  48. table: param.tableName || param,
  49. name: param.name || param,
  50. schema: param._schema,
  51. delimiter: param._schemaDelimiter || '.',
  52. toString() {
  53. return self.quoteTable(this);
  54. }
  55. };
  56. }
  57. dropSchema(tableName, options) {
  58. return this.dropTableQuery(tableName, options);
  59. }
  60. describeTableQuery(tableName, schema, schemaDelimiter) {
  61. const table = this.quoteTable(
  62. this.addSchema({
  63. tableName,
  64. _schema: schema,
  65. _schemaDelimiter: schemaDelimiter
  66. })
  67. );
  68. return `DESCRIBE ${table};`;
  69. }
  70. dropTableQuery(tableName) {
  71. return `DROP TABLE IF EXISTS ${this.quoteTable(tableName)};`;
  72. }
  73. renameTableQuery(before, after) {
  74. return `ALTER TABLE ${this.quoteTable(before)} RENAME TO ${this.quoteTable(after)};`;
  75. }
  76. /**
  77. * Returns an insert into command
  78. *
  79. * @param {string} table
  80. * @param {Object} valueHash attribute value pairs
  81. * @param {Object} modelAttributes
  82. * @param {Object} [options]
  83. *
  84. * @private
  85. */
  86. insertQuery(table, valueHash, modelAttributes, options) {
  87. options = options || {};
  88. _.defaults(options, this.options);
  89. const modelAttributeMap = {};
  90. const fields = [];
  91. const values = [];
  92. const bind = [];
  93. const quotedTable = this.quoteTable(table);
  94. const bindParam = options.bindParam === undefined ? this.bindParam(bind) : options.bindParam;
  95. let query;
  96. let valueQuery = '';
  97. let emptyQuery = '';
  98. let outputFragment = '';
  99. let identityWrapperRequired = false;
  100. let tmpTable = ''; //tmpTable declaration for trigger
  101. if (modelAttributes) {
  102. _.each(modelAttributes, (attribute, key) => {
  103. modelAttributeMap[key] = attribute;
  104. if (attribute.field) {
  105. modelAttributeMap[attribute.field] = attribute;
  106. }
  107. });
  108. }
  109. if (this._dialect.supports['DEFAULT VALUES']) {
  110. emptyQuery += ' DEFAULT VALUES';
  111. } else if (this._dialect.supports['VALUES ()']) {
  112. emptyQuery += ' VALUES ()';
  113. }
  114. if (this._dialect.supports.returnValues && options.returning) {
  115. if (this._dialect.supports.returnValues.returning) {
  116. valueQuery += ' RETURNING *';
  117. emptyQuery += ' RETURNING *';
  118. } else if (this._dialect.supports.returnValues.output) {
  119. outputFragment = ' OUTPUT INSERTED.*';
  120. //To capture output rows when there is a trigger on MSSQL DB
  121. if (modelAttributes && options.hasTrigger && this._dialect.supports.tmpTableTrigger) {
  122. let tmpColumns = '';
  123. let outputColumns = '';
  124. for (const modelKey in modelAttributes) {
  125. const attribute = modelAttributes[modelKey];
  126. if (!(attribute.type instanceof DataTypes.VIRTUAL)) {
  127. if (tmpColumns.length > 0) {
  128. tmpColumns += ',';
  129. outputColumns += ',';
  130. }
  131. tmpColumns += `${this.quoteIdentifier(attribute.field)} ${attribute.type.toSql()}`;
  132. outputColumns += `INSERTED.${this.quoteIdentifier(attribute.field)}`;
  133. }
  134. }
  135. tmpTable = `declare @tmp table (${tmpColumns});`;
  136. outputFragment = ` OUTPUT ${outputColumns} into @tmp`;
  137. const selectFromTmp = ';select * from @tmp';
  138. valueQuery += selectFromTmp;
  139. emptyQuery += selectFromTmp;
  140. }
  141. }
  142. }
  143. if (_.get(this, ['sequelize', 'options', 'dialectOptions', 'prependSearchPath']) || options.searchPath) {
  144. // Not currently supported with search path (requires output of multiple queries)
  145. options.bindParam = false;
  146. }
  147. if (this._dialect.supports.EXCEPTION && options.exception) {
  148. // Not currently supported with bind parameters (requires output of multiple queries)
  149. options.bindParam = false;
  150. }
  151. valueHash = Utils.removeNullValuesFromHash(valueHash, this.options.omitNull);
  152. for (const key in valueHash) {
  153. if (Object.prototype.hasOwnProperty.call(valueHash, key)) {
  154. const value = valueHash[key];
  155. fields.push(this.quoteIdentifier(key));
  156. // SERIALS' can't be NULL in postgresql, use DEFAULT where supported
  157. if (modelAttributeMap && modelAttributeMap[key] && modelAttributeMap[key].autoIncrement === true && !value) {
  158. if (!this._dialect.supports.autoIncrement.defaultValue) {
  159. fields.splice(-1, 1);
  160. } else if (this._dialect.supports.DEFAULT) {
  161. values.push('DEFAULT');
  162. } else {
  163. values.push(this.escape(null));
  164. }
  165. } else {
  166. if (modelAttributeMap && modelAttributeMap[key] && modelAttributeMap[key].autoIncrement === true) {
  167. identityWrapperRequired = true;
  168. }
  169. if (value instanceof Utils.SequelizeMethod || options.bindParam === false) {
  170. values.push(this.escape(value, modelAttributeMap && modelAttributeMap[key] || undefined, { context: 'INSERT' }));
  171. } else {
  172. values.push(this.format(value, modelAttributeMap && modelAttributeMap[key] || undefined, { context: 'INSERT' }, bindParam));
  173. }
  174. }
  175. }
  176. }
  177. const replacements = {
  178. ignoreDuplicates: options.ignoreDuplicates ? this._dialect.supports.inserts.ignoreDuplicates : '',
  179. onConflictDoNothing: options.ignoreDuplicates ? this._dialect.supports.inserts.onConflictDoNothing : '',
  180. attributes: fields.join(','),
  181. output: outputFragment,
  182. values: values.join(','),
  183. tmpTable
  184. };
  185. valueQuery = `${tmpTable}INSERT${replacements.ignoreDuplicates} INTO ${quotedTable} (${replacements.attributes})${replacements.output} VALUES (${replacements.values})${replacements.onConflictDoNothing}${valueQuery}`;
  186. emptyQuery = `${tmpTable}INSERT${replacements.ignoreDuplicates} INTO ${quotedTable}${replacements.output}${replacements.onConflictDoNothing}${emptyQuery}`;
  187. if (this._dialect.supports.EXCEPTION && options.exception) {
  188. // Mostly for internal use, so we expect the user to know what he's doing!
  189. // pg_temp functions are private per connection, so we never risk this function interfering with another one.
  190. if (semver.gte(this.sequelize.options.databaseVersion, '9.2.0')) {
  191. // >= 9.2 - Use a UUID but prefix with 'func_' (numbers first not allowed)
  192. const delimiter = `$func_${uuidv4().replace(/-/g, '')}$`;
  193. options.exception = 'WHEN unique_violation THEN GET STACKED DIAGNOSTICS sequelize_caught_exception = PG_EXCEPTION_DETAIL;';
  194. valueQuery = `${`CREATE OR REPLACE FUNCTION pg_temp.testfunc(OUT response ${quotedTable}, OUT sequelize_caught_exception text) RETURNS RECORD AS ${delimiter}` +
  195. ' BEGIN '}${valueQuery} INTO response; EXCEPTION ${options.exception} END ${delimiter
  196. } LANGUAGE plpgsql; SELECT (testfunc.response).*, testfunc.sequelize_caught_exception FROM pg_temp.testfunc(); DROP FUNCTION IF EXISTS pg_temp.testfunc()`;
  197. } else {
  198. options.exception = 'WHEN unique_violation THEN NULL;';
  199. valueQuery = `CREATE OR REPLACE FUNCTION pg_temp.testfunc() RETURNS SETOF ${quotedTable} AS $body$ BEGIN RETURN QUERY ${valueQuery}; EXCEPTION ${options.exception} END; $body$ LANGUAGE plpgsql; SELECT * FROM pg_temp.testfunc(); DROP FUNCTION IF EXISTS pg_temp.testfunc();`;
  200. }
  201. }
  202. if (this._dialect.supports['ON DUPLICATE KEY'] && options.onDuplicate) {
  203. valueQuery += ` ON DUPLICATE KEY ${options.onDuplicate}`;
  204. emptyQuery += ` ON DUPLICATE KEY ${options.onDuplicate}`;
  205. }
  206. query = `${replacements.attributes.length ? valueQuery : emptyQuery};`;
  207. if (identityWrapperRequired && this._dialect.supports.autoIncrement.identityInsert) {
  208. query = `SET IDENTITY_INSERT ${quotedTable} ON; ${query} SET IDENTITY_INSERT ${quotedTable} OFF;`;
  209. }
  210. // Used by Postgres upsertQuery and calls to here with options.exception set to true
  211. const result = { query };
  212. if (options.bindParam !== false) {
  213. result.bind = bind;
  214. }
  215. return result;
  216. }
  217. /**
  218. * Returns an insert into command for multiple values.
  219. *
  220. * @param {string} tableName
  221. * @param {Object} fieldValueHashes
  222. * @param {Object} options
  223. * @param {Object} fieldMappedAttributes
  224. *
  225. * @private
  226. */
  227. bulkInsertQuery(tableName, fieldValueHashes, options, fieldMappedAttributes) {
  228. options = options || {};
  229. fieldMappedAttributes = fieldMappedAttributes || {};
  230. const tuples = [];
  231. const serials = {};
  232. const allAttributes = [];
  233. let onDuplicateKeyUpdate = '';
  234. for (const fieldValueHash of fieldValueHashes) {
  235. _.forOwn(fieldValueHash, (value, key) => {
  236. if (!allAttributes.includes(key)) {
  237. allAttributes.push(key);
  238. }
  239. if (
  240. fieldMappedAttributes[key]
  241. && fieldMappedAttributes[key].autoIncrement === true
  242. ) {
  243. serials[key] = true;
  244. }
  245. });
  246. }
  247. for (const fieldValueHash of fieldValueHashes) {
  248. const values = allAttributes.map(key => {
  249. if (
  250. this._dialect.supports.bulkDefault
  251. && serials[key] === true
  252. ) {
  253. return fieldValueHash[key] || 'DEFAULT';
  254. }
  255. return this.escape(fieldValueHash[key], fieldMappedAttributes[key], { context: 'INSERT' });
  256. });
  257. tuples.push(`(${values.join(',')})`);
  258. }
  259. if (this._dialect.supports.inserts.updateOnDuplicate && options.updateOnDuplicate) {
  260. if (this._dialect.supports.inserts.updateOnDuplicate == ' ON CONFLICT DO UPDATE SET') { // postgres / sqlite
  261. // If no conflict target columns were specified, use the primary key names from options.upsertKeys
  262. const conflictKeys = options.upsertKeys.map(attr => this.quoteIdentifier(attr));
  263. const updateKeys = options.updateOnDuplicate.map(attr => `${this.quoteIdentifier(attr)}=EXCLUDED.${this.quoteIdentifier(attr)}`);
  264. onDuplicateKeyUpdate = ` ON CONFLICT (${conflictKeys.join(',')}) DO UPDATE SET ${updateKeys.join(',')}`;
  265. } else { // mysql / maria
  266. const valueKeys = options.updateOnDuplicate.map(attr => `${this.quoteIdentifier(attr)}=VALUES(${this.quoteIdentifier(attr)})`);
  267. onDuplicateKeyUpdate = `${this._dialect.supports.inserts.updateOnDuplicate} ${valueKeys.join(',')}`;
  268. }
  269. }
  270. const ignoreDuplicates = options.ignoreDuplicates ? this._dialect.supports.inserts.ignoreDuplicates : '';
  271. const attributes = allAttributes.map(attr => this.quoteIdentifier(attr)).join(',');
  272. const onConflictDoNothing = options.ignoreDuplicates ? this._dialect.supports.inserts.onConflictDoNothing : '';
  273. let returning = '';
  274. if (this._dialect.supports.returnValues && Array.isArray(options.returning)) {
  275. const fields = options.returning.map(field => this.quoteIdentifier(field)).join(',');
  276. returning += ` RETURNING ${fields}`;
  277. } else {
  278. returning += this._dialect.supports.returnValues && options.returning ? ' RETURNING *' : '';
  279. }
  280. return `INSERT${ignoreDuplicates} INTO ${this.quoteTable(tableName)} (${attributes}) VALUES ${tuples.join(',')}${onDuplicateKeyUpdate}${onConflictDoNothing}${returning};`;
  281. }
  282. /**
  283. * Returns an update query
  284. *
  285. * @param {string} tableName
  286. * @param {Object} attrValueHash
  287. * @param {Object} where A hash with conditions (e.g. {name: 'foo'}) OR an ID as integer
  288. * @param {Object} options
  289. * @param {Object} attributes
  290. *
  291. * @private
  292. */
  293. updateQuery(tableName, attrValueHash, where, options, attributes) {
  294. options = options || {};
  295. _.defaults(options, this.options);
  296. attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, options.omitNull, options);
  297. const values = [];
  298. const bind = [];
  299. const modelAttributeMap = {};
  300. let outputFragment = '';
  301. let tmpTable = ''; // tmpTable declaration for trigger
  302. let selectFromTmp = ''; // Select statement for trigger
  303. let suffix = '';
  304. if (_.get(this, ['sequelize', 'options', 'dialectOptions', 'prependSearchPath']) || options.searchPath) {
  305. // Not currently supported with search path (requires output of multiple queries)
  306. options.bindParam = false;
  307. }
  308. const bindParam = options.bindParam === undefined ? this.bindParam(bind) : options.bindParam;
  309. if (this._dialect.supports['LIMIT ON UPDATE'] && options.limit) {
  310. if (this.dialect !== 'mssql') {
  311. suffix = ` LIMIT ${this.escape(options.limit)} `;
  312. }
  313. }
  314. if (this._dialect.supports.returnValues) {
  315. if (this._dialect.supports.returnValues.output) {
  316. // we always need this for mssql
  317. outputFragment = ' OUTPUT INSERTED.*';
  318. //To capture output rows when there is a trigger on MSSQL DB
  319. if (attributes && options.hasTrigger && this._dialect.supports.tmpTableTrigger) {
  320. let tmpColumns = '';
  321. let outputColumns = '';
  322. for (const modelKey in attributes) {
  323. const attribute = attributes[modelKey];
  324. if (!(attribute.type instanceof DataTypes.VIRTUAL)) {
  325. if (tmpColumns.length > 0) {
  326. tmpColumns += ',';
  327. outputColumns += ',';
  328. }
  329. tmpColumns += `${this.quoteIdentifier(attribute.field)} ${attribute.type.toSql()}`;
  330. outputColumns += `INSERTED.${this.quoteIdentifier(attribute.field)}`;
  331. }
  332. }
  333. tmpTable = `declare @tmp table (${tmpColumns}); `;
  334. outputFragment = ` OUTPUT ${outputColumns} into @tmp`;
  335. selectFromTmp = ';select * from @tmp';
  336. suffix += selectFromTmp;
  337. }
  338. } else if (this._dialect.supports.returnValues && options.returning) {
  339. // ensure that the return output is properly mapped to model fields.
  340. options.mapToModel = true;
  341. suffix += ' RETURNING *';
  342. }
  343. }
  344. if (attributes) {
  345. _.each(attributes, (attribute, key) => {
  346. modelAttributeMap[key] = attribute;
  347. if (attribute.field) {
  348. modelAttributeMap[attribute.field] = attribute;
  349. }
  350. });
  351. }
  352. for (const key in attrValueHash) {
  353. if (modelAttributeMap && modelAttributeMap[key] &&
  354. modelAttributeMap[key].autoIncrement === true &&
  355. !this._dialect.supports.autoIncrement.update) {
  356. // not allowed to update identity column
  357. continue;
  358. }
  359. const value = attrValueHash[key];
  360. if (value instanceof Utils.SequelizeMethod || options.bindParam === false) {
  361. values.push(`${this.quoteIdentifier(key)}=${this.escape(value, modelAttributeMap && modelAttributeMap[key] || undefined, { context: 'UPDATE' })}`);
  362. } else {
  363. values.push(`${this.quoteIdentifier(key)}=${this.format(value, modelAttributeMap && modelAttributeMap[key] || undefined, { context: 'UPDATE' }, bindParam)}`);
  364. }
  365. }
  366. const whereOptions = _.defaults({ bindParam }, options);
  367. if (values.length === 0) {
  368. return '';
  369. }
  370. const query = `${tmpTable}UPDATE ${this.quoteTable(tableName)} SET ${values.join(',')}${outputFragment} ${this.whereQuery(where, whereOptions)}${suffix}`.trim();
  371. // Used by Postgres upsertQuery and calls to here with options.exception set to true
  372. const result = { query };
  373. if (options.bindParam !== false) {
  374. result.bind = bind;
  375. }
  376. return result;
  377. }
  378. /**
  379. * Returns an update query using arithmetic operator
  380. *
  381. * @param {string} operator String with the arithmetic operator (e.g. '+' or '-')
  382. * @param {string} tableName Name of the table
  383. * @param {Object} attrValueHash A hash with attribute-value-pairs
  384. * @param {Object} where A hash with conditions (e.g. {name: 'foo'}) OR an ID as integer
  385. * @param {Object} options
  386. * @param {Object} attributes
  387. */
  388. arithmeticQuery(operator, tableName, attrValueHash, where, options, attributes) {
  389. options = options || {};
  390. _.defaults(options, { returning: true });
  391. attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull);
  392. const values = [];
  393. let outputFragment = '';
  394. let returningFragment = '';
  395. if (this._dialect.supports.returnValues && options.returning) {
  396. if (this._dialect.supports.returnValues.returning) {
  397. options.mapToModel = true;
  398. returningFragment = 'RETURNING *';
  399. } else if (this._dialect.supports.returnValues.output) {
  400. outputFragment = ' OUTPUT INSERTED.*';
  401. }
  402. }
  403. for (const key in attrValueHash) {
  404. const value = attrValueHash[key];
  405. values.push(`${this.quoteIdentifier(key)}=${this.quoteIdentifier(key)}${operator} ${this.escape(value)}`);
  406. }
  407. attributes = attributes || {};
  408. for (const key in attributes) {
  409. const value = attributes[key];
  410. values.push(`${this.quoteIdentifier(key)}=${this.escape(value)}`);
  411. }
  412. return `UPDATE ${this.quoteTable(tableName)} SET ${values.join(',')}${outputFragment} ${this.whereQuery(where)} ${returningFragment}`.trim();
  413. }
  414. /*
  415. Returns an add index query.
  416. Parameters:
  417. - tableName -> Name of an existing table, possibly with schema.
  418. - options:
  419. - type: UNIQUE|FULLTEXT|SPATIAL
  420. - name: The name of the index. Default is <table>_<attr1>_<attr2>
  421. - fields: An array of attributes as string or as hash.
  422. If the attribute is a hash, it must have the following content:
  423. - name: The name of the attribute/column
  424. - length: An integer. Optional
  425. - order: 'ASC' or 'DESC'. Optional
  426. - parser
  427. - using
  428. - operator
  429. - concurrently: Pass CONCURRENT so other operations run while the index is created
  430. - rawTablename, the name of the table, without schema. Used to create the name of the index
  431. @private
  432. */
  433. addIndexQuery(tableName, attributes, options, rawTablename) {
  434. options = options || {};
  435. if (!Array.isArray(attributes)) {
  436. options = attributes;
  437. attributes = undefined;
  438. } else {
  439. options.fields = attributes;
  440. }
  441. options.prefix = options.prefix || rawTablename || tableName;
  442. if (options.prefix && typeof options.prefix === 'string') {
  443. options.prefix = options.prefix.replace(/\./g, '_');
  444. options.prefix = options.prefix.replace(/("|')/g, '');
  445. }
  446. const fieldsSql = options.fields.map(field => {
  447. if (typeof field === 'string') {
  448. return this.quoteIdentifier(field);
  449. }
  450. if (field instanceof Utils.SequelizeMethod) {
  451. return this.handleSequelizeMethod(field);
  452. }
  453. let result = '';
  454. if (field.attribute) {
  455. field.name = field.attribute;
  456. }
  457. if (!field.name) {
  458. throw new Error(`The following index field has no name: ${util.inspect(field)}`);
  459. }
  460. result += this.quoteIdentifier(field.name);
  461. if (this._dialect.supports.index.collate && field.collate) {
  462. result += ` COLLATE ${this.quoteIdentifier(field.collate)}`;
  463. }
  464. if (this._dialect.supports.index.length && field.length) {
  465. result += `(${field.length})`;
  466. }
  467. if (field.order) {
  468. result += ` ${field.order}`;
  469. }
  470. return result;
  471. });
  472. if (!options.name) {
  473. // Mostly for cases where addIndex is called directly by the user without an options object (for example in migrations)
  474. // All calls that go through sequelize should already have a name
  475. options = Utils.nameIndex(options, options.prefix);
  476. }
  477. options = Model._conformIndex(options);
  478. if (!this._dialect.supports.index.type) {
  479. delete options.type;
  480. }
  481. if (options.where) {
  482. options.where = this.whereQuery(options.where);
  483. }
  484. if (typeof tableName === 'string') {
  485. tableName = this.quoteIdentifiers(tableName);
  486. } else {
  487. tableName = this.quoteTable(tableName);
  488. }
  489. const concurrently = this._dialect.supports.index.concurrently && options.concurrently ? 'CONCURRENTLY' : undefined;
  490. let ind;
  491. if (this._dialect.supports.indexViaAlter) {
  492. ind = [
  493. 'ALTER TABLE',
  494. tableName,
  495. concurrently,
  496. 'ADD'
  497. ];
  498. } else {
  499. ind = ['CREATE'];
  500. }
  501. ind = ind.concat(
  502. options.unique ? 'UNIQUE' : '',
  503. options.type, 'INDEX',
  504. !this._dialect.supports.indexViaAlter ? concurrently : undefined,
  505. this.quoteIdentifiers(options.name),
  506. this._dialect.supports.index.using === 1 && options.using ? `USING ${options.using}` : '',
  507. !this._dialect.supports.indexViaAlter ? `ON ${tableName}` : undefined,
  508. this._dialect.supports.index.using === 2 && options.using ? `USING ${options.using}` : '',
  509. `(${fieldsSql.join(', ')}${options.operator ? ` ${options.operator}` : ''})`,
  510. this._dialect.supports.index.parser && options.parser ? `WITH PARSER ${options.parser}` : undefined,
  511. this._dialect.supports.index.where && options.where ? options.where : undefined
  512. );
  513. return _.compact(ind).join(' ');
  514. }
  515. addConstraintQuery(tableName, options) {
  516. options = options || {};
  517. const constraintSnippet = this.getConstraintSnippet(tableName, options);
  518. if (typeof tableName === 'string') {
  519. tableName = this.quoteIdentifiers(tableName);
  520. } else {
  521. tableName = this.quoteTable(tableName);
  522. }
  523. return `ALTER TABLE ${tableName} ADD ${constraintSnippet};`;
  524. }
  525. getConstraintSnippet(tableName, options) {
  526. let constraintSnippet, constraintName;
  527. const fieldsSql = options.fields.map(field => {
  528. if (typeof field === 'string') {
  529. return this.quoteIdentifier(field);
  530. }
  531. if (field instanceof Utils.SequelizeMethod) {
  532. return this.handleSequelizeMethod(field);
  533. }
  534. if (field.attribute) {
  535. field.name = field.attribute;
  536. }
  537. if (!field.name) {
  538. throw new Error(`The following index field has no name: ${field}`);
  539. }
  540. return this.quoteIdentifier(field.name);
  541. });
  542. const fieldsSqlQuotedString = fieldsSql.join(', ');
  543. const fieldsSqlString = fieldsSql.join('_');
  544. switch (options.type.toUpperCase()) {
  545. case 'UNIQUE':
  546. constraintName = this.quoteIdentifier(options.name || `${tableName}_${fieldsSqlString}_uk`);
  547. constraintSnippet = `CONSTRAINT ${constraintName} UNIQUE (${fieldsSqlQuotedString})`;
  548. break;
  549. case 'CHECK':
  550. options.where = this.whereItemsQuery(options.where);
  551. constraintName = this.quoteIdentifier(options.name || `${tableName}_${fieldsSqlString}_ck`);
  552. constraintSnippet = `CONSTRAINT ${constraintName} CHECK (${options.where})`;
  553. break;
  554. case 'DEFAULT':
  555. if (options.defaultValue === undefined) {
  556. throw new Error('Default value must be specifed for DEFAULT CONSTRAINT');
  557. }
  558. if (this._dialect.name !== 'mssql') {
  559. throw new Error('Default constraints are supported only for MSSQL dialect.');
  560. }
  561. constraintName = this.quoteIdentifier(options.name || `${tableName}_${fieldsSqlString}_df`);
  562. constraintSnippet = `CONSTRAINT ${constraintName} DEFAULT (${this.escape(options.defaultValue)}) FOR ${fieldsSql[0]}`;
  563. break;
  564. case 'PRIMARY KEY':
  565. constraintName = this.quoteIdentifier(options.name || `${tableName}_${fieldsSqlString}_pk`);
  566. constraintSnippet = `CONSTRAINT ${constraintName} PRIMARY KEY (${fieldsSqlQuotedString})`;
  567. break;
  568. case 'FOREIGN KEY':
  569. const references = options.references;
  570. if (!references || !references.table || !references.field) {
  571. throw new Error('references object with table and field must be specified');
  572. }
  573. constraintName = this.quoteIdentifier(options.name || `${tableName}_${fieldsSqlString}_${references.table}_fk`);
  574. const referencesSnippet = `${this.quoteTable(references.table)} (${this.quoteIdentifier(references.field)})`;
  575. constraintSnippet = `CONSTRAINT ${constraintName} `;
  576. constraintSnippet += `FOREIGN KEY (${fieldsSqlQuotedString}) REFERENCES ${referencesSnippet}`;
  577. if (options.onUpdate) {
  578. constraintSnippet += ` ON UPDATE ${options.onUpdate.toUpperCase()}`;
  579. }
  580. if (options.onDelete) {
  581. constraintSnippet += ` ON DELETE ${options.onDelete.toUpperCase()}`;
  582. }
  583. break;
  584. default: throw new Error(`${options.type} is invalid.`);
  585. }
  586. return constraintSnippet;
  587. }
  588. removeConstraintQuery(tableName, constraintName) {
  589. if (typeof tableName === 'string') {
  590. tableName = this.quoteIdentifiers(tableName);
  591. } else {
  592. tableName = this.quoteTable(tableName);
  593. }
  594. return `ALTER TABLE ${tableName} DROP CONSTRAINT ${this.quoteIdentifiers(constraintName)}`;
  595. }
  596. /*
  597. Quote an object based on its type. This is a more general version of quoteIdentifiers
  598. Strings: should proxy to quoteIdentifiers
  599. Arrays:
  600. * Expects array in the form: [<model> (optional), <model> (optional),... String, String (optional)]
  601. Each <model> can be a model, or an object {model: Model, as: String}, matching include, or an
  602. association object, or the name of an association.
  603. * Zero or more models can be included in the array and are used to trace a path through the tree of
  604. included nested associations. This produces the correct table name for the ORDER BY/GROUP BY SQL
  605. and quotes it.
  606. * If a single string is appended to end of array, it is quoted.
  607. If two strings appended, the 1st string is quoted, the 2nd string unquoted.
  608. Objects:
  609. * If raw is set, that value should be returned verbatim, without quoting
  610. * If fn is set, the string should start with the value of fn, starting paren, followed by
  611. the values of cols (which is assumed to be an array), quoted and joined with ', ',
  612. unless they are themselves objects
  613. * If direction is set, should be prepended
  614. Currently this function is only used for ordering / grouping columns and Sequelize.col(), but it could
  615. potentially also be used for other places where we want to be able to call SQL functions (e.g. as default values)
  616. @private
  617. */
  618. quote(collection, parent, connector) {
  619. // init
  620. const validOrderOptions = [
  621. 'ASC',
  622. 'DESC',
  623. 'ASC NULLS LAST',
  624. 'DESC NULLS LAST',
  625. 'ASC NULLS FIRST',
  626. 'DESC NULLS FIRST',
  627. 'NULLS FIRST',
  628. 'NULLS LAST'
  629. ];
  630. // default
  631. connector = connector || '.';
  632. // just quote as identifiers if string
  633. if (typeof collection === 'string') {
  634. return this.quoteIdentifiers(collection);
  635. }
  636. if (Array.isArray(collection)) {
  637. // iterate through the collection and mutate objects into associations
  638. collection.forEach((item, index) => {
  639. const previous = collection[index - 1];
  640. let previousAssociation;
  641. let previousModel;
  642. // set the previous as the parent when previous is undefined or the target of the association
  643. if (!previous && parent !== undefined) {
  644. previousModel = parent;
  645. } else if (previous && previous instanceof Association) {
  646. previousAssociation = previous;
  647. previousModel = previous.target;
  648. }
  649. // if the previous item is a model, then attempt getting an association
  650. if (previousModel && previousModel.prototype instanceof Model) {
  651. let model;
  652. let as;
  653. if (typeof item === 'function' && item.prototype instanceof Model) {
  654. // set
  655. model = item;
  656. } else if (_.isPlainObject(item) && item.model && item.model.prototype instanceof Model) {
  657. // set
  658. model = item.model;
  659. as = item.as;
  660. }
  661. if (model) {
  662. // set the as to either the through name or the model name
  663. if (!as && previousAssociation && previousAssociation instanceof Association && previousAssociation.through && previousAssociation.through.model === model) {
  664. // get from previous association
  665. item = new Association(previousModel, model, {
  666. as: model.name
  667. });
  668. } else {
  669. // get association from previous model
  670. item = previousModel.getAssociationForAlias(model, as);
  671. // attempt to use the model name if the item is still null
  672. if (!item) {
  673. item = previousModel.getAssociationForAlias(model, model.name);
  674. }
  675. }
  676. // make sure we have an association
  677. if (!(item instanceof Association)) {
  678. throw new Error(util.format('Unable to find a valid association for model, \'%s\'', model.name));
  679. }
  680. }
  681. }
  682. if (typeof item === 'string') {
  683. // get order index
  684. const orderIndex = validOrderOptions.indexOf(item.toUpperCase());
  685. // see if this is an order
  686. if (index > 0 && orderIndex !== -1) {
  687. item = this.sequelize.literal(` ${validOrderOptions[orderIndex]}`);
  688. } else if (previousModel && previousModel.prototype instanceof Model) {
  689. // only go down this path if we have preivous model and check only once
  690. if (previousModel.associations !== undefined && previousModel.associations[item]) {
  691. // convert the item to an association
  692. item = previousModel.associations[item];
  693. } else if (previousModel.rawAttributes !== undefined && previousModel.rawAttributes[item] && item !== previousModel.rawAttributes[item].field) {
  694. // convert the item attribute from its alias
  695. item = previousModel.rawAttributes[item].field;
  696. } else if (
  697. item.includes('.')
  698. && previousModel.rawAttributes !== undefined
  699. ) {
  700. const itemSplit = item.split('.');
  701. if (previousModel.rawAttributes[itemSplit[0]].type instanceof DataTypes.JSON) {
  702. // just quote identifiers for now
  703. const identifier = this.quoteIdentifiers(`${previousModel.name}.${previousModel.rawAttributes[itemSplit[0]].field}`);
  704. // get path
  705. const path = itemSplit.slice(1);
  706. // extract path
  707. item = this.jsonPathExtractionQuery(identifier, path);
  708. // literal because we don't want to append the model name when string
  709. item = this.sequelize.literal(item);
  710. }
  711. }
  712. }
  713. }
  714. collection[index] = item;
  715. }, this);
  716. // loop through array, adding table names of models to quoted
  717. const collectionLength = collection.length;
  718. const tableNames = [];
  719. let item;
  720. let i = 0;
  721. for (i = 0; i < collectionLength - 1; i++) {
  722. item = collection[i];
  723. if (typeof item === 'string' || item._modelAttribute || item instanceof Utils.SequelizeMethod) {
  724. break;
  725. } else if (item instanceof Association) {
  726. tableNames[i] = item.as;
  727. }
  728. }
  729. // start building sql
  730. let sql = '';
  731. if (i > 0) {
  732. sql += `${this.quoteIdentifier(tableNames.join(connector))}.`;
  733. } else if (typeof collection[0] === 'string' && parent) {
  734. sql += `${this.quoteIdentifier(parent.name)}.`;
  735. }
  736. // loop through everything past i and append to the sql
  737. collection.slice(i).forEach(collectionItem => {
  738. sql += this.quote(collectionItem, parent, connector);
  739. }, this);
  740. return sql;
  741. }
  742. if (collection._modelAttribute) {
  743. return `${this.quoteTable(collection.Model.name)}.${this.quoteIdentifier(collection.fieldName)}`;
  744. }
  745. if (collection instanceof Utils.SequelizeMethod) {
  746. return this.handleSequelizeMethod(collection);
  747. }
  748. if (_.isPlainObject(collection) && collection.raw) {
  749. // simple objects with raw is no longer supported
  750. throw new Error('The `{raw: "..."}` syntax is no longer supported. Use `sequelize.literal` instead.');
  751. }
  752. throw new Error(`Unknown structure passed to order / group: ${util.inspect(collection)}`);
  753. }
  754. /**
  755. * Split a list of identifiers by "." and quote each part
  756. *
  757. * @param {string} identifier
  758. * @param {boolean} force
  759. *
  760. * @returns {string}
  761. */
  762. quoteIdentifier(identifier, force) {
  763. return QuoteHelper.quoteIdentifier(this.dialect, identifier, {
  764. force,
  765. quoteIdentifiers: this.options.quoteIdentifiers
  766. });
  767. }
  768. quoteIdentifiers(identifiers) {
  769. if (identifiers.includes('.')) {
  770. identifiers = identifiers.split('.');
  771. const head = identifiers.slice(0, identifiers.length - 1).join('->');
  772. const tail = identifiers[identifiers.length - 1];
  773. return `${this.quoteIdentifier(head)}.${this.quoteIdentifier(tail)}`;
  774. }
  775. return this.quoteIdentifier(identifiers);
  776. }
  777. quoteAttribute(attribute, model) {
  778. if (model && attribute in model.rawAttributes) {
  779. return this.quoteIdentifier(attribute);
  780. }
  781. return this.quoteIdentifiers(attribute);
  782. }
  783. /**
  784. * Quote table name with optional alias and schema attribution
  785. *
  786. * @param {string|Object} param table string or object
  787. * @param {string|boolean} alias alias name
  788. *
  789. * @returns {string}
  790. */
  791. quoteTable(param, alias) {
  792. let table = '';
  793. if (alias === true) {
  794. alias = param.as || param.name || param;
  795. }
  796. if (_.isObject(param)) {
  797. if (this._dialect.supports.schemas) {
  798. if (param.schema) {
  799. table += `${this.quoteIdentifier(param.schema)}.`;
  800. }
  801. table += this.quoteIdentifier(param.tableName);
  802. } else {
  803. if (param.schema) {
  804. table += param.schema + (param.delimiter || '.');
  805. }
  806. table += param.tableName;
  807. table = this.quoteIdentifier(table);
  808. }
  809. } else {
  810. table = this.quoteIdentifier(param);
  811. }
  812. if (alias) {
  813. table += ` AS ${this.quoteIdentifier(alias)}`;
  814. }
  815. return table;
  816. }
  817. /*
  818. Escape a value (e.g. a string, number or date)
  819. @private
  820. */
  821. escape(value, field, options) {
  822. options = options || {};
  823. if (value !== null && value !== undefined) {
  824. if (value instanceof Utils.SequelizeMethod) {
  825. return this.handleSequelizeMethod(value);
  826. }
  827. if (field && field.type) {
  828. this.validate(value, field, options);
  829. if (field.type.stringify) {
  830. // Users shouldn't have to worry about these args - just give them a function that takes a single arg
  831. const simpleEscape = escVal => SqlString.escape(escVal, this.options.timezone, this.dialect);
  832. value = field.type.stringify(value, { escape: simpleEscape, field, timezone: this.options.timezone, operation: options.operation });
  833. if (field.type.escape === false) {
  834. // The data-type already did the required escaping
  835. return value;
  836. }
  837. }
  838. }
  839. }
  840. return SqlString.escape(value, this.options.timezone, this.dialect);
  841. }
  842. bindParam(bind) {
  843. return value => {
  844. bind.push(value);
  845. return `$${bind.length}`;
  846. };
  847. }
  848. /*
  849. Returns a bind parameter representation of a value (e.g. a string, number or date)
  850. @private
  851. */
  852. format(value, field, options, bindParam) {
  853. options = options || {};
  854. if (value !== null && value !== undefined) {
  855. if (value instanceof Utils.SequelizeMethod) {
  856. throw new Error('Cannot pass SequelizeMethod as a bind parameter - use escape instead');
  857. }
  858. if (field && field.type) {
  859. this.validate(value, field, options);
  860. if (field.type.bindParam) {
  861. return field.type.bindParam(value, { escape: _.identity, field, timezone: this.options.timezone, operation: options.operation, bindParam });
  862. }
  863. }
  864. }
  865. return bindParam(value);
  866. }
  867. /*
  868. Validate a value against a field specification
  869. @private
  870. */
  871. validate(value, field, options) {
  872. if (this.typeValidation && field.type.validate && value) {
  873. try {
  874. if (options.isList && Array.isArray(value)) {
  875. for (const item of value) {
  876. field.type.validate(item, options);
  877. }
  878. } else {
  879. field.type.validate(value, options);
  880. }
  881. } catch (error) {
  882. if (error instanceof sequelizeError.ValidationError) {
  883. error.errors.push(new sequelizeError.ValidationErrorItem(
  884. error.message,
  885. 'Validation error',
  886. field.fieldName,
  887. value,
  888. null,
  889. `${field.type.key} validator`
  890. ));
  891. }
  892. throw error;
  893. }
  894. }
  895. }
  896. isIdentifierQuoted(identifier) {
  897. return QuoteHelper.isIdentifierQuoted(identifier);
  898. }
  899. /**
  900. * Generates an SQL query that extract JSON property of given path.
  901. *
  902. * @param {string} column The JSON column
  903. * @param {string|Array<string>} [path] The path to extract (optional)
  904. * @returns {string} The generated sql query
  905. * @private
  906. */
  907. jsonPathExtractionQuery(column, path) {
  908. let paths = _.toPath(path);
  909. let pathStr;
  910. const quotedColumn = this.isIdentifierQuoted(column)
  911. ? column
  912. : this.quoteIdentifier(column);
  913. switch (this.dialect) {
  914. case 'mysql':
  915. case 'mariadb':
  916. case 'sqlite':
  917. /**
  918. * Non digit sub paths need to be quoted as ECMAScript identifiers
  919. * https://bugs.mysql.com/bug.php?id=81896
  920. */
  921. if (this.dialect === 'mysql') {
  922. paths = paths.map(subPath => {
  923. return /\D/.test(subPath)
  924. ? Utils.addTicks(subPath, '"')
  925. : subPath;
  926. });
  927. }
  928. pathStr = this.escape(['$']
  929. .concat(paths)
  930. .join('.')
  931. .replace(/\.(\d+)(?:(?=\.)|$)/g, (__, digit) => `[${digit}]`));
  932. if (this.dialect === 'sqlite') {
  933. return `json_extract(${quotedColumn},${pathStr})`;
  934. }
  935. return `json_unquote(json_extract(${quotedColumn},${pathStr}))`;
  936. case 'postgres':
  937. pathStr = this.escape(`{${paths.join(',')}}`);
  938. return `(${quotedColumn}#>>${pathStr})`;
  939. default:
  940. throw new Error(`Unsupported ${this.dialect} for JSON operations`);
  941. }
  942. }
  943. /*
  944. Returns a query for selecting elements in the table <tableName>.
  945. Options:
  946. - attributes -> An array of attributes (e.g. ['name', 'birthday']). Default: *
  947. - where -> A hash with conditions (e.g. {name: 'foo'})
  948. OR an ID as integer
  949. - order -> e.g. 'id DESC'
  950. - group
  951. - limit -> The maximum count you want to get.
  952. - offset -> An offset value to start from. Only useable with limit!
  953. @private
  954. */
  955. selectQuery(tableName, options, model) {
  956. options = options || {};
  957. const limit = options.limit;
  958. const mainQueryItems = [];
  959. const subQueryItems = [];
  960. const subQuery = options.subQuery === undefined ? limit && options.hasMultiAssociation : options.subQuery;
  961. const attributes = {
  962. main: options.attributes && options.attributes.slice(),
  963. subQuery: null
  964. };
  965. const mainTable = {
  966. name: tableName,
  967. quotedName: null,
  968. as: null,
  969. model
  970. };
  971. const topLevelInfo = {
  972. names: mainTable,
  973. options,
  974. subQuery
  975. };
  976. let mainJoinQueries = [];
  977. let subJoinQueries = [];
  978. let query;
  979. // Aliases can be passed through subqueries and we don't want to reset them
  980. if (this.options.minifyAliases && !options.aliasesMapping) {
  981. options.aliasesMapping = new Map();
  982. options.aliasesByTable = {};
  983. }
  984. // resolve table name options
  985. if (options.tableAs) {
  986. mainTable.as = this.quoteIdentifier(options.tableAs);
  987. } else if (!Array.isArray(mainTable.name) && mainTable.model) {
  988. mainTable.as = this.quoteIdentifier(mainTable.model.name);
  989. }
  990. mainTable.quotedName = !Array.isArray(mainTable.name) ? this.quoteTable(mainTable.name) : tableName.map(t => {
  991. return Array.isArray(t) ? this.quoteTable(t[0], t[1]) : this.quoteTable(t, true);
  992. }).join(', ');
  993. if (subQuery && attributes.main) {
  994. for (const keyAtt of mainTable.model.primaryKeyAttributes) {
  995. // Check if mainAttributes contain the primary key of the model either as a field or an aliased field
  996. if (!attributes.main.some(attr => keyAtt === attr || keyAtt === attr[0] || keyAtt === attr[1])) {
  997. attributes.main.push(mainTable.model.rawAttributes[keyAtt].field ? [keyAtt, mainTable.model.rawAttributes[keyAtt].field] : keyAtt);
  998. }
  999. }
  1000. }
  1001. attributes.main = this.escapeAttributes(attributes.main, options, mainTable.as);
  1002. attributes.main = attributes.main || (options.include ? [`${mainTable.as}.*`] : ['*']);
  1003. // If subquery, we add the mainAttributes to the subQuery and set the mainAttributes to select * from subquery
  1004. if (subQuery || options.groupedLimit) {
  1005. // We need primary keys
  1006. attributes.subQuery = attributes.main;
  1007. attributes.main = [`${mainTable.as || mainTable.quotedName}.*`];
  1008. }
  1009. if (options.include) {
  1010. for (const include of options.include) {
  1011. if (include.separate) {
  1012. continue;
  1013. }
  1014. const joinQueries = this.generateInclude(include, { externalAs: mainTable.as, internalAs: mainTable.as }, topLevelInfo);
  1015. subJoinQueries = subJoinQueries.concat(joinQueries.subQuery);
  1016. mainJoinQueries = mainJoinQueries.concat(joinQueries.mainQuery);
  1017. if (joinQueries.attributes.main.length > 0) {
  1018. attributes.main = _.uniq(attributes.main.concat(joinQueries.attributes.main));
  1019. }
  1020. if (joinQueries.attributes.subQuery.length > 0) {
  1021. attributes.subQuery = _.uniq(attributes.subQuery.concat(joinQueries.attributes.subQuery));
  1022. }
  1023. }
  1024. }
  1025. if (subQuery) {
  1026. subQueryItems.push(this.selectFromTableFragment(options, mainTable.model, attributes.subQuery, mainTable.quotedName, mainTable.as));
  1027. subQueryItems.push(subJoinQueries.join(''));
  1028. } else {
  1029. if (options.groupedLimit) {
  1030. if (!mainTable.as) {
  1031. mainTable.as = mainTable.quotedName;
  1032. }
  1033. const where = Object.assign({}, options.where);
  1034. let groupedLimitOrder,
  1035. whereKey,
  1036. include,
  1037. groupedTableName = mainTable.as;
  1038. if (typeof options.groupedLimit.on === 'string') {
  1039. whereKey = options.groupedLimit.on;
  1040. } else if (options.groupedLimit.on instanceof HasMany) {
  1041. whereKey = options.groupedLimit.on.foreignKeyField;
  1042. }
  1043. if (options.groupedLimit.on instanceof BelongsToMany) {
  1044. // BTM includes needs to join the through table on to check ID
  1045. groupedTableName = options.groupedLimit.on.manyFromSource.as;
  1046. const groupedLimitOptions = Model._validateIncludedElements({
  1047. include: [{
  1048. association: options.groupedLimit.on.manyFromSource,
  1049. duplicating: false, // The UNION'ed query may contain duplicates, but each sub-query cannot
  1050. required: true,
  1051. where: Object.assign({
  1052. [Op.placeholder]: true
  1053. }, options.groupedLimit.through && options.groupedLimit.through.where)
  1054. }],
  1055. model
  1056. });
  1057. // Make sure attributes from the join table are mapped back to models
  1058. options.hasJoin = true;
  1059. options.hasMultiAssociation = true;
  1060. options.includeMap = Object.assign(groupedLimitOptions.includeMap, options.includeMap);
  1061. options.includeNames = groupedLimitOptions.includeNames.concat(options.includeNames || []);
  1062. include = groupedLimitOptions.include;
  1063. if (Array.isArray(options.order)) {
  1064. // We need to make sure the order by attributes are available to the parent query
  1065. options.order.forEach((order, i) => {
  1066. if (Array.isArray(order)) {
  1067. order = order[0];
  1068. }
  1069. let alias = `subquery_order_${i}`;
  1070. options.attributes.push([order, alias]);
  1071. // We don't want to prepend model name when we alias the attributes, so quote them here
  1072. alias = this.sequelize.literal(this.quote(alias));
  1073. if (Array.isArray(options.order[i])) {
  1074. options.order[i][0] = alias;
  1075. } else {
  1076. options.order[i] = alias;
  1077. }
  1078. });
  1079. groupedLimitOrder = options.order;
  1080. }
  1081. } else {
  1082. // Ordering is handled by the subqueries, so ordering the UNION'ed result is not needed
  1083. groupedLimitOrder = options.order;
  1084. delete options.order;
  1085. where[Op.placeholder] = true;
  1086. }
  1087. // Caching the base query and splicing the where part into it is consistently > twice
  1088. // as fast than generating from scratch each time for values.length >= 5
  1089. const baseQuery = `SELECT * FROM (${this.selectQuery(
  1090. tableName,
  1091. {
  1092. attributes: options.attributes,
  1093. offset: options.offset,
  1094. limit: options.groupedLimit.limit,
  1095. order: groupedLimitOrder,
  1096. aliasesMapping: options.aliasesMapping,
  1097. aliasesByTable: options.aliasesByTable,
  1098. where,
  1099. include,
  1100. model
  1101. },
  1102. model
  1103. ).replace(/;$/, '')}) AS sub`; // Every derived table must have its own alias
  1104. const placeHolder = this.whereItemQuery(Op.placeholder, true, { model });
  1105. const splicePos = baseQuery.indexOf(placeHolder);
  1106. mainQueryItems.push(this.selectFromTableFragment(options, mainTable.model, attributes.main, `(${
  1107. options.groupedLimit.values.map(value => {
  1108. let groupWhere;
  1109. if (whereKey) {
  1110. groupWhere = {
  1111. [whereKey]: value
  1112. };
  1113. }
  1114. if (include) {
  1115. groupWhere = {
  1116. [options.groupedLimit.on.foreignIdentifierField]: value
  1117. };
  1118. }
  1119. return Utils.spliceStr(baseQuery, splicePos, placeHolder.length, this.getWhereConditions(groupWhere, groupedTableName));
  1120. }).join(
  1121. this._dialect.supports['UNION ALL'] ? ' UNION ALL ' : ' UNION '
  1122. )
  1123. })`, mainTable.as));
  1124. } else {
  1125. mainQueryItems.push(this.selectFromTableFragment(options, mainTable.model, attributes.main, mainTable.quotedName, mainTable.as));
  1126. }
  1127. mainQueryItems.push(mainJoinQueries.join(''));
  1128. }
  1129. // Add WHERE to sub or main query
  1130. if (Object.prototype.hasOwnProperty.call(options, 'where') && !options.groupedLimit) {
  1131. options.where = this.getWhereConditions(options.where, mainTable.as || tableName, model, options);
  1132. if (options.where) {
  1133. if (subQuery) {
  1134. subQueryItems.push(` WHERE ${options.where}`);
  1135. } else {
  1136. mainQueryItems.push(` WHERE ${options.where}`);
  1137. // Walk the main query to update all selects
  1138. mainQueryItems.forEach((value, key) => {
  1139. if (value.startsWith('SELECT')) {
  1140. mainQueryItems[key] = this.selectFromTableFragment(options, model, attributes.main, mainTable.quotedName, mainTable.as, options.where);
  1141. }
  1142. });
  1143. }
  1144. }
  1145. }
  1146. // Add GROUP BY to sub or main query
  1147. if (options.group) {
  1148. options.group = Array.isArray(options.group) ? options.group.map(t => this.aliasGrouping(t, model, mainTable.as, options)).join(', ') : this.aliasGrouping(options.group, model, mainTable.as, options);
  1149. if (subQuery) {
  1150. subQueryItems.push(` GROUP BY ${options.group}`);
  1151. } else {
  1152. mainQueryItems.push(` GROUP BY ${options.group}`);
  1153. }
  1154. }
  1155. // Add HAVING to sub or main query
  1156. if (Object.prototype.hasOwnProperty.call(options, 'having')) {
  1157. options.having = this.getWhereConditions(options.having, tableName, model, options, false);
  1158. if (options.having) {
  1159. if (subQuery) {
  1160. subQueryItems.push(` HAVING ${options.having}`);
  1161. } else {
  1162. mainQueryItems.push(` HAVING ${options.having}`);
  1163. }
  1164. }
  1165. }
  1166. // Add ORDER to sub or main query
  1167. if (options.order) {
  1168. const orders = this.getQueryOrders(options, model, subQuery);
  1169. if (orders.mainQueryOrder.length) {
  1170. mainQueryItems.push(` ORDER BY ${orders.mainQueryOrder.join(', ')}`);
  1171. }
  1172. if (orders.subQueryOrder.length) {
  1173. subQueryItems.push(` ORDER BY ${orders.subQueryOrder.join(', ')}`);
  1174. }
  1175. }
  1176. // Add LIMIT, OFFSET to sub or main query
  1177. const limitOrder = this.addLimitAndOffset(options, mainTable.model);
  1178. if (limitOrder && !options.groupedLimit) {
  1179. if (subQuery) {
  1180. subQueryItems.push(limitOrder);
  1181. } else {
  1182. mainQueryItems.push(limitOrder);
  1183. }
  1184. }
  1185. if (subQuery) {
  1186. query = `SELECT ${attributes.main.join(', ')} FROM (${subQueryItems.join('')}) AS ${mainTable.as}${mainJoinQueries.join('')}${mainQueryItems.join('')}`;
  1187. } else {
  1188. query = mainQueryItems.join('');
  1189. }
  1190. if (options.lock && this._dialect.supports.lock) {
  1191. let lock = options.lock;
  1192. if (typeof options.lock === 'object') {
  1193. lock = options.lock.level;
  1194. }
  1195. if (this._dialect.supports.lockKey && (lock === 'KEY SHARE' || lock === 'NO KEY UPDATE')) {
  1196. query += ` FOR ${lock}`;
  1197. } else if (lock === 'SHARE') {
  1198. query += ` ${this._dialect.supports.forShare}`;
  1199. } else {
  1200. query += ' FOR UPDATE';
  1201. }
  1202. if (this._dialect.supports.lockOf && options.lock.of && options.lock.of.prototype instanceof Model) {
  1203. query += ` OF ${this.quoteTable(options.lock.of.name)}`;
  1204. }
  1205. if (this._dialect.supports.skipLocked && options.skipLocked) {
  1206. query += ' SKIP LOCKED';
  1207. }
  1208. }
  1209. return `${query};`;
  1210. }
  1211. aliasGrouping(field, model, tableName, options) {
  1212. const src = Array.isArray(field) ? field[0] : field;
  1213. return this.quote(this._getAliasForField(tableName, src, options) || src, model);
  1214. }
  1215. escapeAttributes(attributes, options, mainTableAs) {
  1216. return attributes && attributes.map(attr => {
  1217. let addTable = true;
  1218. if (attr instanceof Utils.SequelizeMethod) {
  1219. return this.handleSequelizeMethod(attr);
  1220. }
  1221. if (Array.isArray(attr)) {
  1222. if (attr.length !== 2) {
  1223. throw new Error(`${JSON.stringify(attr)} is not a valid attribute definition. Please use the following format: ['attribute definition', 'alias']`);
  1224. }
  1225. attr = attr.slice();
  1226. if (attr[0] instanceof Utils.SequelizeMethod) {
  1227. attr[0] = this.handleSequelizeMethod(attr[0]);
  1228. addTable = false;
  1229. } else if (!attr[0].includes('(') && !attr[0].includes(')')) {
  1230. attr[0] = this.quoteIdentifier(attr[0]);
  1231. } else {
  1232. deprecations.noRawAttributes();
  1233. }
  1234. let alias = attr[1];
  1235. if (this.options.minifyAliases) {
  1236. alias = this._getMinifiedAlias(alias, mainTableAs, options);
  1237. }
  1238. attr = [attr[0], this.quoteIdentifier(alias)].join(' AS ');
  1239. } else {
  1240. attr = !attr.includes(Utils.TICK_CHAR) && !attr.includes('"')
  1241. ? this.quoteAttribute(attr, options.model)
  1242. : this.escape(attr);
  1243. }
  1244. if (!_.isEmpty(options.include) && !attr.includes('.') && addTable) {
  1245. attr = `${mainTableAs}.${attr}`;
  1246. }
  1247. return attr;
  1248. });
  1249. }
  1250. generateInclude(include, parentTableName, topLevelInfo) {
  1251. const joinQueries = {
  1252. mainQuery: [],
  1253. subQuery: []
  1254. };
  1255. const mainChildIncludes = [];
  1256. const subChildIncludes = [];
  1257. let requiredMismatch = false;
  1258. const includeAs = {
  1259. internalAs: include.as,
  1260. externalAs: include.as
  1261. };
  1262. const attributes = {
  1263. main: [],
  1264. subQuery: []
  1265. };
  1266. let joinQuery;
  1267. topLevelInfo.options.keysEscaped = true;
  1268. if (topLevelInfo.names.name !== parentTableName.externalAs && topLevelInfo.names.as !== parentTableName.externalAs) {
  1269. includeAs.internalAs = `${parentTableName.internalAs}->${include.as}`;
  1270. includeAs.externalAs = `${parentTableName.externalAs}.${include.as}`;
  1271. }
  1272. // includeIgnoreAttributes is used by aggregate functions
  1273. if (topLevelInfo.options.includeIgnoreAttributes !== false) {
  1274. include.model._expandAttributes(include);
  1275. Utils.mapFinderOptions(include, include.model);
  1276. const includeAttributes = include.attributes.map(attr => {
  1277. let attrAs = attr;
  1278. let verbatim = false;
  1279. if (Array.isArray(attr) && attr.length === 2) {
  1280. if (attr[0] instanceof Utils.SequelizeMethod && (
  1281. attr[0] instanceof Utils.Literal ||
  1282. attr[0] instanceof Utils.Cast ||
  1283. attr[0] instanceof Utils.Fn
  1284. )) {
  1285. verbatim = true;
  1286. }
  1287. attr = attr.map(attr => attr instanceof Utils.SequelizeMethod ? this.handleSequelizeMethod(attr) : attr);
  1288. attrAs = attr[1];
  1289. attr = attr[0];
  1290. }
  1291. if (attr instanceof Utils.Literal) {
  1292. return attr.val; // We trust the user to rename the field correctly
  1293. }
  1294. if (attr instanceof Utils.Cast || attr instanceof Utils.Fn) {
  1295. throw new Error(
  1296. 'Tried to select attributes using Sequelize.cast or Sequelize.fn without specifying an alias for the result, during eager loading. ' +
  1297. 'This means the attribute will not be added to the returned instance'
  1298. );
  1299. }
  1300. let prefix;
  1301. if (verbatim === true) {
  1302. prefix = attr;
  1303. } else if (/#>>|->>/.test(attr)) {
  1304. prefix = `(${this.quoteIdentifier(includeAs.internalAs)}.${attr.replace(/\(|\)/g, '')})`;
  1305. } else if (/json_extract\(/.test(attr)) {
  1306. prefix = attr.replace(/json_extract\(/i, `json_extract(${this.quoteIdentifier(includeAs.internalAs)}.`);
  1307. } else {
  1308. prefix = `${this.quoteIdentifier(includeAs.internalAs)}.${this.quoteIdentifier(attr)}`;
  1309. }
  1310. let alias = `${includeAs.externalAs}.${attrAs}`;
  1311. if (this.options.minifyAliases) {
  1312. alias = this._getMinifiedAlias(alias, includeAs.internalAs, topLevelInfo.options);
  1313. }
  1314. return `${prefix} AS ${this.quoteIdentifier(alias, true)}`;
  1315. });
  1316. if (include.subQuery && topLevelInfo.subQuery) {
  1317. for (const attr of includeAttributes) {
  1318. attributes.subQuery.push(attr);
  1319. }
  1320. } else {
  1321. for (const attr of includeAttributes) {
  1322. attributes.main.push(attr);
  1323. }
  1324. }
  1325. }
  1326. //through
  1327. if (include.through) {
  1328. joinQuery = this.generateThroughJoin(include, includeAs, parentTableName.internalAs, topLevelInfo);
  1329. } else {
  1330. this._generateSubQueryFilter(include, includeAs, topLevelInfo);
  1331. joinQuery = this.generateJoin(include, topLevelInfo);
  1332. }
  1333. // handle possible new attributes created in join
  1334. if (joinQuery.attributes.main.length > 0) {
  1335. attributes.main = attributes.main.concat(joinQuery.attributes.main);
  1336. }
  1337. if (joinQuery.attributes.subQuery.length > 0) {
  1338. attributes.subQuery = attributes.subQuery.concat(joinQuery.attributes.subQuery);
  1339. }
  1340. if (include.include) {
  1341. for (const childInclude of include.include) {
  1342. if (childInclude.separate || childInclude._pseudo) {
  1343. continue;
  1344. }
  1345. const childJoinQueries = this.generateInclude(childInclude, includeAs, topLevelInfo);
  1346. if (include.required === false && childInclude.required === true) {
  1347. requiredMismatch = true;
  1348. }
  1349. // if the child is a sub query we just give it to the
  1350. if (childInclude.subQuery && topLevelInfo.subQuery) {
  1351. subChildIncludes.push(childJoinQueries.subQuery);
  1352. }
  1353. if (childJoinQueries.mainQuery) {
  1354. mainChildIncludes.push(childJoinQueries.mainQuery);
  1355. }
  1356. if (childJoinQueries.attributes.main.length > 0) {
  1357. attributes.main = attributes.main.concat(childJoinQueries.attributes.main);
  1358. }
  1359. if (childJoinQueries.attributes.subQuery.length > 0) {
  1360. attributes.subQuery = attributes.subQuery.concat(childJoinQueries.attributes.subQuery);
  1361. }
  1362. }
  1363. }
  1364. if (include.subQuery && topLevelInfo.subQuery) {
  1365. if (requiredMismatch && subChildIncludes.length > 0) {
  1366. joinQueries.subQuery.push(` ${joinQuery.join} ( ${joinQuery.body}${subChildIncludes.join('')} ) ON ${joinQuery.condition}`);
  1367. } else {
  1368. joinQueries.subQuery.push(` ${joinQuery.join} ${joinQuery.body} ON ${joinQuery.condition}`);
  1369. if (subChildIncludes.length > 0) {
  1370. joinQueries.subQuery.push(subChildIncludes.join(''));
  1371. }
  1372. }
  1373. joinQueries.mainQuery.push(mainChildIncludes.join(''));
  1374. } else {
  1375. if (requiredMismatch && mainChildIncludes.length > 0) {
  1376. joinQueries.mainQuery.push(` ${joinQuery.join} ( ${joinQuery.body}${mainChildIncludes.join('')} ) ON ${joinQuery.condition}`);
  1377. } else {
  1378. joinQueries.mainQuery.push(` ${joinQuery.join} ${joinQuery.body} ON ${joinQuery.condition}`);
  1379. if (mainChildIncludes.length > 0) {
  1380. joinQueries.mainQuery.push(mainChildIncludes.join(''));
  1381. }
  1382. }
  1383. joinQueries.subQuery.push(subChildIncludes.join(''));
  1384. }
  1385. return {
  1386. mainQuery: joinQueries.mainQuery.join(''),
  1387. subQuery: joinQueries.subQuery.join(''),
  1388. attributes
  1389. };
  1390. }
  1391. _getMinifiedAlias(alias, tableName, options) {
  1392. // We do not want to re-alias in case of a subquery
  1393. if (options.aliasesByTable[`${tableName}${alias}`]) {
  1394. return options.aliasesByTable[`${tableName}${alias}`];
  1395. }
  1396. // Do not alias custom suquery_orders
  1397. if (alias.match(/subquery_order_[0-9]/)) {
  1398. return alias;
  1399. }
  1400. const minifiedAlias = `_${options.aliasesMapping.size}`;
  1401. options.aliasesMapping.set(minifiedAlias, alias);
  1402. options.aliasesByTable[`${tableName}${alias}`] = minifiedAlias;
  1403. return minifiedAlias;
  1404. }
  1405. _getAliasForField(tableName, field, options) {
  1406. if (this.options.minifyAliases) {
  1407. if (options.aliasesByTable[`${tableName}${field}`]) {
  1408. return options.aliasesByTable[`${tableName}${field}`];
  1409. }
  1410. }
  1411. return null;
  1412. }
  1413. generateJoin(include, topLevelInfo) {
  1414. const association = include.association;
  1415. const parent = include.parent;
  1416. const parentIsTop = !!parent && !include.parent.association && include.parent.model.name === topLevelInfo.options.model.name;
  1417. let $parent;
  1418. let joinWhere;
  1419. /* Attributes for the left side */
  1420. const left = association.source;
  1421. const attrLeft = association instanceof BelongsTo ?
  1422. association.identifier :
  1423. association.sourceKeyAttribute || left.primaryKeyAttribute;
  1424. const fieldLeft = association instanceof BelongsTo ?
  1425. association.identifierField :
  1426. left.rawAttributes[association.sourceKeyAttribute || left.primaryKeyAttribute].field;
  1427. let asLeft;
  1428. /* Attributes for the right side */
  1429. const right = include.model;
  1430. const tableRight = right.getTableName();
  1431. const fieldRight = association instanceof BelongsTo ?
  1432. right.rawAttributes[association.targetIdentifier || right.primaryKeyAttribute].field :
  1433. association.identifierField;
  1434. let asRight = include.as;
  1435. while (($parent = $parent && $parent.parent || include.parent) && $parent.association) {
  1436. if (asLeft) {
  1437. asLeft = `${$parent.as}->${asLeft}`;
  1438. } else {
  1439. asLeft = $parent.as;
  1440. }
  1441. }
  1442. if (!asLeft) asLeft = parent.as || parent.model.name;
  1443. else asRight = `${asLeft}->${asRight}`;
  1444. let joinOn = `${this.quoteTable(asLeft)}.${this.quoteIdentifier(fieldLeft)}`;
  1445. const subqueryAttributes = [];
  1446. if (topLevelInfo.options.groupedLimit && parentIsTop || topLevelInfo.subQuery && include.parent.subQuery && !include.subQuery) {
  1447. if (parentIsTop) {
  1448. // The main model attributes is not aliased to a prefix
  1449. const tableName = this.quoteTable(parent.as || parent.model.name);
  1450. // Check for potential aliased JOIN condition
  1451. joinOn = this._getAliasForField(tableName, attrLeft, topLevelInfo.options) || `${tableName}.${this.quoteIdentifier(attrLeft)}`;
  1452. if (topLevelInfo.subQuery) {
  1453. subqueryAttributes.push(`${tableName}.${this.quoteIdentifier(fieldLeft)}`);
  1454. }
  1455. } else {
  1456. const joinSource = `${asLeft.replace(/->/g, '.')}.${attrLeft}`;
  1457. // Check for potential aliased JOIN condition
  1458. joinOn = this._getAliasForField(asLeft, joinSource, topLevelInfo.options) || this.quoteIdentifier(joinSource);
  1459. }
  1460. }
  1461. joinOn += ` = ${this.quoteIdentifier(asRight)}.${this.quoteIdentifier(fieldRight)}`;
  1462. if (include.on) {
  1463. joinOn = this.whereItemsQuery(include.on, {
  1464. prefix: this.sequelize.literal(this.quoteIdentifier(asRight)),
  1465. model: include.model
  1466. });
  1467. }
  1468. if (include.where) {
  1469. joinWhere = this.whereItemsQuery(include.where, {
  1470. prefix: this.sequelize.literal(this.quoteIdentifier(asRight)),
  1471. model: include.model
  1472. });
  1473. if (joinWhere) {
  1474. if (include.or) {
  1475. joinOn += ` OR ${joinWhere}`;
  1476. } else {
  1477. joinOn += ` AND ${joinWhere}`;
  1478. }
  1479. }
  1480. }
  1481. return {
  1482. join: include.required ? 'INNER JOIN' : include.right && this._dialect.supports['RIGHT JOIN'] ? 'RIGHT OUTER JOIN' : 'LEFT OUTER JOIN',
  1483. body: this.quoteTable(tableRight, asRight),
  1484. condition: joinOn,
  1485. attributes: {
  1486. main: [],
  1487. subQuery: subqueryAttributes
  1488. }
  1489. };
  1490. }
  1491. generateThroughJoin(include, includeAs, parentTableName, topLevelInfo) {
  1492. const through = include.through;
  1493. const throughTable = through.model.getTableName();
  1494. const throughAs = `${includeAs.internalAs}->${through.as}`;
  1495. const externalThroughAs = `${includeAs.externalAs}.${through.as}`;
  1496. const throughAttributes = through.attributes.map(attr => {
  1497. let alias = `${externalThroughAs}.${Array.isArray(attr) ? attr[1] : attr}`;
  1498. if (this.options.minifyAliases) {
  1499. alias = this._getMinifiedAlias(alias, throughAs, topLevelInfo.options);
  1500. }
  1501. return `${this.quoteIdentifier(throughAs)}.${this.quoteIdentifier(Array.isArray(attr) ? attr[0] : attr)
  1502. } AS ${
  1503. this.quoteIdentifier(alias)}`;
  1504. });
  1505. const association = include.association;
  1506. const parentIsTop = !include.parent.association && include.parent.model.name === topLevelInfo.options.model.name;
  1507. const tableSource = parentTableName;
  1508. const identSource = association.identifierField;
  1509. const tableTarget = includeAs.internalAs;
  1510. const identTarget = association.foreignIdentifierField;
  1511. const attrTarget = association.targetKeyField;
  1512. const joinType = include.required ? 'INNER JOIN' : include.right && this._dialect.supports['RIGHT JOIN'] ? 'RIGHT OUTER JOIN' : 'LEFT OUTER JOIN';
  1513. let joinBody;
  1514. let joinCondition;
  1515. const attributes = {
  1516. main: [],
  1517. subQuery: []
  1518. };
  1519. let attrSource = association.sourceKey;
  1520. let sourceJoinOn;
  1521. let targetJoinOn;
  1522. let throughWhere;
  1523. let targetWhere;
  1524. if (topLevelInfo.options.includeIgnoreAttributes !== false) {
  1525. // Through includes are always hasMany, so we need to add the attributes to the mainAttributes no matter what (Real join will never be executed in subquery)
  1526. for (const attr of throughAttributes) {
  1527. attributes.main.push(attr);
  1528. }
  1529. }
  1530. // Figure out if we need to use field or attribute
  1531. if (!topLevelInfo.subQuery) {
  1532. attrSource = association.sourceKeyField;
  1533. }
  1534. if (topLevelInfo.subQuery && !include.subQuery && !include.parent.subQuery && include.parent.model !== topLevelInfo.options.mainModel) {
  1535. attrSource = association.sourceKeyField;
  1536. }
  1537. // Filter statement for left side of through
  1538. // Used by both join and subquery where
  1539. // If parent include was in a subquery need to join on the aliased attribute
  1540. if (topLevelInfo.subQuery && !include.subQuery && include.parent.subQuery && !parentIsTop) {
  1541. // If we are minifying aliases and our JOIN target has been minified, we need to use the alias instead of the original column name
  1542. const joinSource = this._getAliasForField(tableSource, `${tableSource}.${attrSource}`, topLevelInfo.options) || `${tableSource}.${attrSource}`;
  1543. sourceJoinOn = `${this.quoteIdentifier(joinSource)} = `;
  1544. } else {
  1545. // If we are minifying aliases and our JOIN target has been minified, we need to use the alias instead of the original column name
  1546. const aliasedSource = this._getAliasForField(tableSource, attrSource, topLevelInfo.options) || attrSource;
  1547. sourceJoinOn = `${this.quoteTable(tableSource)}.${this.quoteIdentifier(aliasedSource)} = `;
  1548. }
  1549. sourceJoinOn += `${this.quoteIdentifier(throughAs)}.${this.quoteIdentifier(identSource)}`;
  1550. // Filter statement for right side of through
  1551. // Used by both join and subquery where
  1552. targetJoinOn = `${this.quoteIdentifier(tableTarget)}.${this.quoteIdentifier(attrTarget)} = `;
  1553. targetJoinOn += `${this.quoteIdentifier(throughAs)}.${this.quoteIdentifier(identTarget)}`;
  1554. if (through.where) {
  1555. throughWhere = this.getWhereConditions(through.where, this.sequelize.literal(this.quoteIdentifier(throughAs)), through.model);
  1556. }
  1557. if (this._dialect.supports.joinTableDependent) {
  1558. // Generate a wrapped join so that the through table join can be dependent on the target join
  1559. joinBody = `( ${this.quoteTable(throughTable, throughAs)} INNER JOIN ${this.quoteTable(include.model.getTableName(), includeAs.internalAs)} ON ${targetJoinOn}`;
  1560. if (throughWhere) {
  1561. joinBody += ` AND ${throughWhere}`;
  1562. }
  1563. joinBody += ')';
  1564. joinCondition = sourceJoinOn;
  1565. } else {
  1566. // Generate join SQL for left side of through
  1567. joinBody = `${this.quoteTable(throughTable, throughAs)} ON ${sourceJoinOn} ${joinType} ${this.quoteTable(include.model.getTableName(), includeAs.internalAs)}`;
  1568. joinCondition = targetJoinOn;
  1569. if (throughWhere) {
  1570. joinCondition += ` AND ${throughWhere}`;
  1571. }
  1572. }
  1573. if (include.where || include.through.where) {
  1574. if (include.where) {
  1575. targetWhere = this.getWhereConditions(include.where, this.sequelize.literal(this.quoteIdentifier(includeAs.internalAs)), include.model, topLevelInfo.options);
  1576. if (targetWhere) {
  1577. joinCondition += ` AND ${targetWhere}`;
  1578. }
  1579. }
  1580. }
  1581. this._generateSubQueryFilter(include, includeAs, topLevelInfo);
  1582. return {
  1583. join: joinType,
  1584. body: joinBody,
  1585. condition: joinCondition,
  1586. attributes
  1587. };
  1588. }
  1589. /*
  1590. * Generates subQueryFilter - a select nested in the where clause of the subQuery.
  1591. * For a given include a query is generated that contains all the way from the subQuery
  1592. * table to the include table plus everything that's in required transitive closure of the
  1593. * given include.
  1594. */
  1595. _generateSubQueryFilter(include, includeAs, topLevelInfo) {
  1596. if (!topLevelInfo.subQuery || !include.subQueryFilter) {
  1597. return;
  1598. }
  1599. if (!topLevelInfo.options.where) {
  1600. topLevelInfo.options.where = {};
  1601. }
  1602. let parent = include;
  1603. let child = include;
  1604. let nestedIncludes = this._getRequiredClosure(include).include;
  1605. let query;
  1606. while ((parent = parent.parent)) { // eslint-disable-line
  1607. if (parent.parent && !parent.required) {
  1608. return; // only generate subQueryFilter if all the parents of this include are required
  1609. }
  1610. if (parent.subQueryFilter) {
  1611. // the include is already handled as this parent has the include on its required closure
  1612. // skip to prevent duplicate subQueryFilter
  1613. return;
  1614. }
  1615. nestedIncludes = [Object.assign({}, child, { include: nestedIncludes, attributes: [] })];
  1616. child = parent;
  1617. }
  1618. const topInclude = nestedIncludes[0];
  1619. const topParent = topInclude.parent;
  1620. const topAssociation = topInclude.association;
  1621. topInclude.association = undefined;
  1622. if (topInclude.through && Object(topInclude.through.model) === topInclude.through.model) {
  1623. query = this.selectQuery(topInclude.through.model.getTableName(), {
  1624. attributes: [topInclude.through.model.primaryKeyField],
  1625. include: Model._validateIncludedElements({
  1626. model: topInclude.through.model,
  1627. include: [{
  1628. association: topAssociation.toTarget,
  1629. required: true,
  1630. where: topInclude.where,
  1631. include: topInclude.include
  1632. }]
  1633. }).include,
  1634. model: topInclude.through.model,
  1635. where: {
  1636. [Op.and]: [
  1637. this.sequelize.literal([
  1638. `${this.quoteTable(topParent.model.name)}.${this.quoteIdentifier(topParent.model.primaryKeyField)}`,
  1639. `${this.quoteIdentifier(topInclude.through.model.name)}.${this.quoteIdentifier(topAssociation.identifierField)}`
  1640. ].join(' = ')),
  1641. topInclude.through.where
  1642. ]
  1643. },
  1644. limit: 1,
  1645. includeIgnoreAttributes: false
  1646. }, topInclude.through.model);
  1647. } else {
  1648. const isBelongsTo = topAssociation.associationType === 'BelongsTo';
  1649. const sourceField = isBelongsTo ? topAssociation.identifierField : topAssociation.sourceKeyField || topParent.model.primaryKeyField;
  1650. const targetField = isBelongsTo ? topAssociation.sourceKeyField || topInclude.model.primaryKeyField : topAssociation.identifierField;
  1651. const join = [
  1652. `${this.quoteIdentifier(topInclude.as)}.${this.quoteIdentifier(targetField)}`,
  1653. `${this.quoteTable(topParent.as || topParent.model.name)}.${this.quoteIdentifier(sourceField)}`
  1654. ].join(' = ');
  1655. query = this.selectQuery(topInclude.model.getTableName(), {
  1656. attributes: [targetField],
  1657. include: Model._validateIncludedElements(topInclude).include,
  1658. model: topInclude.model,
  1659. where: {
  1660. [Op.and]: [
  1661. topInclude.where,
  1662. { [Op.join]: this.sequelize.literal(join) }
  1663. ]
  1664. },
  1665. limit: 1,
  1666. tableAs: topInclude.as,
  1667. includeIgnoreAttributes: false
  1668. }, topInclude.model);
  1669. }
  1670. if (!topLevelInfo.options.where[Op.and]) {
  1671. topLevelInfo.options.where[Op.and] = [];
  1672. }
  1673. topLevelInfo.options.where[`__${includeAs.internalAs}`] = this.sequelize.literal([
  1674. '(',
  1675. query.replace(/;$/, ''),
  1676. ')',
  1677. 'IS NOT NULL'
  1678. ].join(' '));
  1679. }
  1680. /*
  1681. * For a given include hierarchy creates a copy of it where only the required includes
  1682. * are preserved.
  1683. */
  1684. _getRequiredClosure(include) {
  1685. const copy = Object.assign({}, include, { attributes: [], include: [] });
  1686. if (Array.isArray(include.include)) {
  1687. copy.include = include.include
  1688. .filter(i => i.required)
  1689. .map(inc => this._getRequiredClosure(inc));
  1690. }
  1691. return copy;
  1692. }
  1693. getQueryOrders(options, model, subQuery) {
  1694. const mainQueryOrder = [];
  1695. const subQueryOrder = [];
  1696. if (Array.isArray(options.order)) {
  1697. for (let order of options.order) {
  1698. // wrap if not array
  1699. if (!Array.isArray(order)) {
  1700. order = [order];
  1701. }
  1702. if (
  1703. subQuery
  1704. && Array.isArray(order)
  1705. && order[0]
  1706. && !(order[0] instanceof Association)
  1707. && !(typeof order[0] === 'function' && order[0].prototype instanceof Model)
  1708. && !(typeof order[0].model === 'function' && order[0].model.prototype instanceof Model)
  1709. && !(typeof order[0] === 'string' && model && model.associations !== undefined && model.associations[order[0]])
  1710. ) {
  1711. subQueryOrder.push(this.quote(order, model, '->'));
  1712. }
  1713. if (subQuery) {
  1714. // Handle case where sub-query renames attribute we want to order by,
  1715. // see https://github.com/sequelize/sequelize/issues/8739
  1716. const subQueryAttribute = options.attributes.find(a => Array.isArray(a) && a[0] === order[0] && a[1]);
  1717. if (subQueryAttribute) {
  1718. const modelName = this.quoteIdentifier(model.name);
  1719. order[0] = new Utils.Col(this._getAliasForField(modelName, subQueryAttribute[1], options) || subQueryAttribute[1]);
  1720. }
  1721. }
  1722. mainQueryOrder.push(this.quote(order, model, '->'));
  1723. }
  1724. } else if (options.order instanceof Utils.SequelizeMethod) {
  1725. const sql = this.quote(options.order, model, '->');
  1726. if (subQuery) {
  1727. subQueryOrder.push(sql);
  1728. }
  1729. mainQueryOrder.push(sql);
  1730. } else {
  1731. throw new Error('Order must be type of array or instance of a valid sequelize method.');
  1732. }
  1733. return { mainQueryOrder, subQueryOrder };
  1734. }
  1735. selectFromTableFragment(options, model, attributes, tables, mainTableAs) {
  1736. let fragment = `SELECT ${attributes.join(', ')} FROM ${tables}`;
  1737. if (mainTableAs) {
  1738. fragment += ` AS ${mainTableAs}`;
  1739. }
  1740. if (options.indexHints && this._dialect.supports.indexHints) {
  1741. for (const hint of options.indexHints) {
  1742. if (IndexHints[hint.type]) {
  1743. fragment += ` ${IndexHints[hint.type]} INDEX (${hint.values.map(indexName => this.quoteIdentifiers(indexName)).join(',')})`;
  1744. }
  1745. }
  1746. }
  1747. return fragment;
  1748. }
  1749. /**
  1750. * Returns an SQL fragment for adding result constraints.
  1751. *
  1752. * @param {Object} options An object with selectQuery options.
  1753. * @returns {string} The generated sql query.
  1754. * @private
  1755. */
  1756. addLimitAndOffset(options) {
  1757. let fragment = '';
  1758. /* eslint-disable */
  1759. if (options.offset != null && options.limit == null) {
  1760. fragment += ' LIMIT ' + this.escape(options.offset) + ', ' + 10000000000000;
  1761. } else if (options.limit != null) {
  1762. if (options.offset != null) {
  1763. fragment += ' LIMIT ' + this.escape(options.offset) + ', ' + this.escape(options.limit);
  1764. } else {
  1765. fragment += ' LIMIT ' + this.escape(options.limit);
  1766. }
  1767. }
  1768. /* eslint-enable */
  1769. return fragment;
  1770. }
  1771. handleSequelizeMethod(smth, tableName, factory, options, prepend) {
  1772. let result;
  1773. if (Object.prototype.hasOwnProperty.call(this.OperatorMap, smth.comparator)) {
  1774. smth.comparator = this.OperatorMap[smth.comparator];
  1775. }
  1776. if (smth instanceof Utils.Where) {
  1777. let value = smth.logic;
  1778. let key;
  1779. if (smth.attribute instanceof Utils.SequelizeMethod) {
  1780. key = this.getWhereConditions(smth.attribute, tableName, factory, options, prepend);
  1781. } else {
  1782. key = `${this.quoteTable(smth.attribute.Model.name)}.${this.quoteIdentifier(smth.attribute.field || smth.attribute.fieldName)}`;
  1783. }
  1784. if (value && value instanceof Utils.SequelizeMethod) {
  1785. value = this.getWhereConditions(value, tableName, factory, options, prepend);
  1786. if (value === 'NULL') {
  1787. if (smth.comparator === '=') {
  1788. smth.comparator = 'IS';
  1789. }
  1790. if (smth.comparator === '!=') {
  1791. smth.comparator = 'IS NOT';
  1792. }
  1793. }
  1794. return [key, value].join(` ${smth.comparator} `);
  1795. }
  1796. if (_.isPlainObject(value)) {
  1797. return this.whereItemQuery(smth.attribute, value, {
  1798. model: factory
  1799. });
  1800. }
  1801. if (typeof value === 'boolean') {
  1802. value = this.booleanValue(value);
  1803. } else {
  1804. value = this.escape(value);
  1805. }
  1806. if (value === 'NULL') {
  1807. if (smth.comparator === '=') {
  1808. smth.comparator = 'IS';
  1809. }
  1810. if (smth.comparator === '!=') {
  1811. smth.comparator = 'IS NOT';
  1812. }
  1813. }
  1814. return [key, value].join(` ${smth.comparator} `);
  1815. }
  1816. if (smth instanceof Utils.Literal) {
  1817. return smth.val;
  1818. }
  1819. if (smth instanceof Utils.Cast) {
  1820. if (smth.val instanceof Utils.SequelizeMethod) {
  1821. result = this.handleSequelizeMethod(smth.val, tableName, factory, options, prepend);
  1822. } else if (_.isPlainObject(smth.val)) {
  1823. result = this.whereItemsQuery(smth.val);
  1824. } else {
  1825. result = this.escape(smth.val);
  1826. }
  1827. return `CAST(${result} AS ${smth.type.toUpperCase()})`;
  1828. }
  1829. if (smth instanceof Utils.Fn) {
  1830. return `${smth.fn}(${smth.args.map(arg => {
  1831. if (arg instanceof Utils.SequelizeMethod) {
  1832. return this.handleSequelizeMethod(arg, tableName, factory, options, prepend);
  1833. }
  1834. if (_.isPlainObject(arg)) {
  1835. return this.whereItemsQuery(arg);
  1836. }
  1837. return this.escape(arg);
  1838. }).join(', ')})`;
  1839. }
  1840. if (smth instanceof Utils.Col) {
  1841. if (Array.isArray(smth.col) && !factory) {
  1842. throw new Error('Cannot call Sequelize.col() with array outside of order / group clause');
  1843. }
  1844. if (smth.col.startsWith('*')) {
  1845. return '*';
  1846. }
  1847. return this.quote(smth.col, factory);
  1848. }
  1849. return smth.toString(this, factory);
  1850. }
  1851. whereQuery(where, options) {
  1852. const query = this.whereItemsQuery(where, options);
  1853. if (query && query.length) {
  1854. return `WHERE ${query}`;
  1855. }
  1856. return '';
  1857. }
  1858. whereItemsQuery(where, options, binding) {
  1859. if (
  1860. where === null ||
  1861. where === undefined ||
  1862. Utils.getComplexSize(where) === 0
  1863. ) {
  1864. // NO OP
  1865. return '';
  1866. }
  1867. if (typeof where === 'string') {
  1868. throw new Error('Support for `{where: \'raw query\'}` has been removed.');
  1869. }
  1870. const items = [];
  1871. binding = binding || 'AND';
  1872. if (binding[0] !== ' ') binding = ` ${binding} `;
  1873. if (_.isPlainObject(where)) {
  1874. Utils.getComplexKeys(where).forEach(prop => {
  1875. const item = where[prop];
  1876. items.push(this.whereItemQuery(prop, item, options));
  1877. });
  1878. } else {
  1879. items.push(this.whereItemQuery(undefined, where, options));
  1880. }
  1881. return items.length && items.filter(item => item && item.length).join(binding) || '';
  1882. }
  1883. whereItemQuery(key, value, options = {}) {
  1884. if (value === undefined) {
  1885. throw new Error(`WHERE parameter "${key}" has invalid "undefined" value`);
  1886. }
  1887. if (typeof key === 'string' && key.includes('.') && options.model) {
  1888. const keyParts = key.split('.');
  1889. if (options.model.rawAttributes[keyParts[0]] && options.model.rawAttributes[keyParts[0]].type instanceof DataTypes.JSON) {
  1890. const tmp = {};
  1891. const field = options.model.rawAttributes[keyParts[0]];
  1892. _.set(tmp, keyParts.slice(1), value);
  1893. return this.whereItemQuery(field.field || keyParts[0], tmp, Object.assign({ field }, options));
  1894. }
  1895. }
  1896. const field = this._findField(key, options);
  1897. const fieldType = field && field.type || options.type;
  1898. const isPlainObject = _.isPlainObject(value);
  1899. const isArray = !isPlainObject && Array.isArray(value);
  1900. key = this.OperatorsAliasMap && this.OperatorsAliasMap[key] || key;
  1901. if (isPlainObject) {
  1902. value = this._replaceAliases(value);
  1903. }
  1904. const valueKeys = isPlainObject && Utils.getComplexKeys(value);
  1905. if (key === undefined) {
  1906. if (typeof value === 'string') {
  1907. return value;
  1908. }
  1909. if (isPlainObject && valueKeys.length === 1) {
  1910. return this.whereItemQuery(valueKeys[0], value[valueKeys[0]], options);
  1911. }
  1912. }
  1913. if (value === null) {
  1914. const opValue = options.bindParam ? 'NULL' : this.escape(value, field);
  1915. return this._joinKeyValue(key, opValue, this.OperatorMap[Op.is], options.prefix);
  1916. }
  1917. if (!value) {
  1918. const opValue = options.bindParam ? this.format(value, field, options, options.bindParam) : this.escape(value, field);
  1919. return this._joinKeyValue(key, opValue, this.OperatorMap[Op.eq], options.prefix);
  1920. }
  1921. if (value instanceof Utils.SequelizeMethod && !(key !== undefined && value instanceof Utils.Fn)) {
  1922. return this.handleSequelizeMethod(value);
  1923. }
  1924. // Convert where: [] to Op.and if possible, else treat as literal/replacements
  1925. if (key === undefined && isArray) {
  1926. if (Utils.canTreatArrayAsAnd(value)) {
  1927. key = Op.and;
  1928. } else {
  1929. throw new Error('Support for literal replacements in the `where` object has been removed.');
  1930. }
  1931. }
  1932. if (key === Op.or || key === Op.and || key === Op.not) {
  1933. return this._whereGroupBind(key, value, options);
  1934. }
  1935. if (value[Op.or]) {
  1936. return this._whereBind(this.OperatorMap[Op.or], key, value[Op.or], options);
  1937. }
  1938. if (value[Op.and]) {
  1939. return this._whereBind(this.OperatorMap[Op.and], key, value[Op.and], options);
  1940. }
  1941. if (isArray && fieldType instanceof DataTypes.ARRAY) {
  1942. const opValue = options.bindParam ? this.format(value, field, options, options.bindParam) : this.escape(value, field);
  1943. return this._joinKeyValue(key, opValue, this.OperatorMap[Op.eq], options.prefix);
  1944. }
  1945. if (isPlainObject && fieldType instanceof DataTypes.JSON && options.json !== false) {
  1946. return this._whereJSON(key, value, options);
  1947. }
  1948. // If multiple keys we combine the different logic conditions
  1949. if (isPlainObject && valueKeys.length > 1) {
  1950. return this._whereBind(this.OperatorMap[Op.and], key, value, options);
  1951. }
  1952. if (isArray) {
  1953. return this._whereParseSingleValueObject(key, field, Op.in, value, options);
  1954. }
  1955. if (isPlainObject) {
  1956. if (this.OperatorMap[valueKeys[0]]) {
  1957. return this._whereParseSingleValueObject(key, field, valueKeys[0], value[valueKeys[0]], options);
  1958. }
  1959. return this._whereParseSingleValueObject(key, field, this.OperatorMap[Op.eq], value, options);
  1960. }
  1961. if (key === Op.placeholder) {
  1962. const opValue = options.bindParam ? this.format(value, field, options, options.bindParam) : this.escape(value, field);
  1963. return this._joinKeyValue(this.OperatorMap[key], opValue, this.OperatorMap[Op.eq], options.prefix);
  1964. }
  1965. const opValue = options.bindParam ? this.format(value, field, options, options.bindParam) : this.escape(value, field);
  1966. return this._joinKeyValue(key, opValue, this.OperatorMap[Op.eq], options.prefix);
  1967. }
  1968. _findField(key, options) {
  1969. if (options.field) {
  1970. return options.field;
  1971. }
  1972. if (options.model && options.model.rawAttributes && options.model.rawAttributes[key]) {
  1973. return options.model.rawAttributes[key];
  1974. }
  1975. if (options.model && options.model.fieldRawAttributesMap && options.model.fieldRawAttributesMap[key]) {
  1976. return options.model.fieldRawAttributesMap[key];
  1977. }
  1978. }
  1979. // OR/AND/NOT grouping logic
  1980. _whereGroupBind(key, value, options) {
  1981. const binding = key === Op.or ? this.OperatorMap[Op.or] : this.OperatorMap[Op.and];
  1982. const outerBinding = key === Op.not ? 'NOT ' : '';
  1983. if (Array.isArray(value)) {
  1984. value = value.map(item => {
  1985. let itemQuery = this.whereItemsQuery(item, options, this.OperatorMap[Op.and]);
  1986. if (itemQuery && itemQuery.length && (Array.isArray(item) || _.isPlainObject(item)) && Utils.getComplexSize(item) > 1) {
  1987. itemQuery = `(${itemQuery})`;
  1988. }
  1989. return itemQuery;
  1990. }).filter(item => item && item.length);
  1991. value = value.length && value.join(binding);
  1992. } else {
  1993. value = this.whereItemsQuery(value, options, binding);
  1994. }
  1995. // Op.or: [] should return no data.
  1996. // Op.not of no restriction should also return no data
  1997. if ((key === Op.or || key === Op.not) && !value) {
  1998. return '0 = 1';
  1999. }
  2000. return value ? `${outerBinding}(${value})` : undefined;
  2001. }
  2002. _whereBind(binding, key, value, options) {
  2003. if (_.isPlainObject(value)) {
  2004. value = Utils.getComplexKeys(value).map(prop => {
  2005. const item = value[prop];
  2006. return this.whereItemQuery(key, { [prop]: item }, options);
  2007. });
  2008. } else {
  2009. value = value.map(item => this.whereItemQuery(key, item, options));
  2010. }
  2011. value = value.filter(item => item && item.length);
  2012. return value.length ? `(${value.join(binding)})` : undefined;
  2013. }
  2014. _whereJSON(key, value, options) {
  2015. const items = [];
  2016. let baseKey = this.quoteIdentifier(key);
  2017. if (options.prefix) {
  2018. if (options.prefix instanceof Utils.Literal) {
  2019. baseKey = `${this.handleSequelizeMethod(options.prefix)}.${baseKey}`;
  2020. } else {
  2021. baseKey = `${this.quoteTable(options.prefix)}.${baseKey}`;
  2022. }
  2023. }
  2024. Utils.getOperators(value).forEach(op => {
  2025. const where = {
  2026. [op]: value[op]
  2027. };
  2028. items.push(this.whereItemQuery(key, where, Object.assign({}, options, { json: false })));
  2029. });
  2030. _.forOwn(value, (item, prop) => {
  2031. this._traverseJSON(items, baseKey, prop, item, [prop]);
  2032. });
  2033. const result = items.join(this.OperatorMap[Op.and]);
  2034. return items.length > 1 ? `(${result})` : result;
  2035. }
  2036. _traverseJSON(items, baseKey, prop, item, path) {
  2037. let cast;
  2038. if (path[path.length - 1].includes('::')) {
  2039. const tmp = path[path.length - 1].split('::');
  2040. cast = tmp[1];
  2041. path[path.length - 1] = tmp[0];
  2042. }
  2043. const pathKey = this.jsonPathExtractionQuery(baseKey, path);
  2044. if (_.isPlainObject(item)) {
  2045. Utils.getOperators(item).forEach(op => {
  2046. const value = this._toJSONValue(item[op]);
  2047. items.push(this.whereItemQuery(this._castKey(pathKey, value, cast), { [op]: value }));
  2048. });
  2049. _.forOwn(item, (value, itemProp) => {
  2050. this._traverseJSON(items, baseKey, itemProp, value, path.concat([itemProp]));
  2051. });
  2052. return;
  2053. }
  2054. item = this._toJSONValue(item);
  2055. items.push(this.whereItemQuery(this._castKey(pathKey, item, cast), { [Op.eq]: item }));
  2056. }
  2057. _toJSONValue(value) {
  2058. return value;
  2059. }
  2060. _castKey(key, value, cast, json) {
  2061. cast = cast || this._getJsonCast(Array.isArray(value) ? value[0] : value);
  2062. if (cast) {
  2063. return new Utils.Literal(this.handleSequelizeMethod(new Utils.Cast(new Utils.Literal(key), cast, json)));
  2064. }
  2065. return new Utils.Literal(key);
  2066. }
  2067. _getJsonCast(value) {
  2068. if (typeof value === 'number') {
  2069. return 'double precision';
  2070. }
  2071. if (value instanceof Date) {
  2072. return 'timestamptz';
  2073. }
  2074. if (typeof value === 'boolean') {
  2075. return 'boolean';
  2076. }
  2077. return;
  2078. }
  2079. _joinKeyValue(key, value, comparator, prefix) {
  2080. if (!key) {
  2081. return value;
  2082. }
  2083. if (comparator === undefined) {
  2084. throw new Error(`${key} and ${value} has no comparator`);
  2085. }
  2086. key = this._getSafeKey(key, prefix);
  2087. return [key, value].join(` ${comparator} `);
  2088. }
  2089. _getSafeKey(key, prefix) {
  2090. if (key instanceof Utils.SequelizeMethod) {
  2091. key = this.handleSequelizeMethod(key);
  2092. return this._prefixKey(this.handleSequelizeMethod(key), prefix);
  2093. }
  2094. if (Utils.isColString(key)) {
  2095. key = key.substr(1, key.length - 2).split('.');
  2096. if (key.length > 2) {
  2097. key = [
  2098. // join the tables by -> to match out internal namings
  2099. key.slice(0, -1).join('->'),
  2100. key[key.length - 1]
  2101. ];
  2102. }
  2103. return key.map(identifier => this.quoteIdentifier(identifier)).join('.');
  2104. }
  2105. return this._prefixKey(this.quoteIdentifier(key), prefix);
  2106. }
  2107. _prefixKey(key, prefix) {
  2108. if (prefix) {
  2109. if (prefix instanceof Utils.Literal) {
  2110. return [this.handleSequelizeMethod(prefix), key].join('.');
  2111. }
  2112. return [this.quoteTable(prefix), key].join('.');
  2113. }
  2114. return key;
  2115. }
  2116. _whereParseSingleValueObject(key, field, prop, value, options) {
  2117. if (prop === Op.not) {
  2118. if (Array.isArray(value)) {
  2119. prop = Op.notIn;
  2120. } else if (value !== null && value !== true && value !== false) {
  2121. prop = Op.ne;
  2122. }
  2123. }
  2124. let comparator = this.OperatorMap[prop] || this.OperatorMap[Op.eq];
  2125. switch (prop) {
  2126. case Op.in:
  2127. case Op.notIn:
  2128. if (value instanceof Utils.Literal) {
  2129. return this._joinKeyValue(key, value.val, comparator, options.prefix);
  2130. }
  2131. if (value.length) {
  2132. return this._joinKeyValue(key, `(${value.map(item => this.escape(item, field)).join(', ')})`, comparator, options.prefix);
  2133. }
  2134. if (comparator === this.OperatorMap[Op.in]) {
  2135. return this._joinKeyValue(key, '(NULL)', comparator, options.prefix);
  2136. }
  2137. return '';
  2138. case Op.any:
  2139. case Op.all:
  2140. comparator = `${this.OperatorMap[Op.eq]} ${comparator}`;
  2141. if (value[Op.values]) {
  2142. return this._joinKeyValue(key, `(VALUES ${value[Op.values].map(item => `(${this.escape(item)})`).join(', ')})`, comparator, options.prefix);
  2143. }
  2144. return this._joinKeyValue(key, `(${this.escape(value, field)})`, comparator, options.prefix);
  2145. case Op.between:
  2146. case Op.notBetween:
  2147. return this._joinKeyValue(key, `${this.escape(value[0], field)} AND ${this.escape(value[1], field)}`, comparator, options.prefix);
  2148. case Op.raw:
  2149. throw new Error('The `$raw` where property is no longer supported. Use `sequelize.literal` instead.');
  2150. case Op.col:
  2151. comparator = this.OperatorMap[Op.eq];
  2152. value = value.split('.');
  2153. if (value.length > 2) {
  2154. value = [
  2155. // join the tables by -> to match out internal namings
  2156. value.slice(0, -1).join('->'),
  2157. value[value.length - 1]
  2158. ];
  2159. }
  2160. return this._joinKeyValue(key, value.map(identifier => this.quoteIdentifier(identifier)).join('.'), comparator, options.prefix);
  2161. case Op.startsWith:
  2162. comparator = this.OperatorMap[Op.like];
  2163. return this._joinKeyValue(key, this.escape(`${value}%`), comparator, options.prefix);
  2164. case Op.endsWith:
  2165. comparator = this.OperatorMap[Op.like];
  2166. return this._joinKeyValue(key, this.escape(`%${value}`), comparator, options.prefix);
  2167. case Op.substring:
  2168. comparator = this.OperatorMap[Op.like];
  2169. return this._joinKeyValue(key, this.escape(`%${value}%`), comparator, options.prefix);
  2170. }
  2171. const escapeOptions = {
  2172. acceptStrings: comparator.includes(this.OperatorMap[Op.like])
  2173. };
  2174. if (_.isPlainObject(value)) {
  2175. if (value[Op.col]) {
  2176. return this._joinKeyValue(key, this.whereItemQuery(null, value), comparator, options.prefix);
  2177. }
  2178. if (value[Op.any]) {
  2179. escapeOptions.isList = true;
  2180. return this._joinKeyValue(key, `(${this.escape(value[Op.any], field, escapeOptions)})`, `${comparator} ${this.OperatorMap[Op.any]}`, options.prefix);
  2181. }
  2182. if (value[Op.all]) {
  2183. escapeOptions.isList = true;
  2184. return this._joinKeyValue(key, `(${this.escape(value[Op.all], field, escapeOptions)})`, `${comparator} ${this.OperatorMap[Op.all]}`, options.prefix);
  2185. }
  2186. }
  2187. if (value === null && comparator === this.OperatorMap[Op.eq]) {
  2188. return this._joinKeyValue(key, this.escape(value, field, escapeOptions), this.OperatorMap[Op.is], options.prefix);
  2189. }
  2190. if (value === null && comparator === this.OperatorMap[Op.ne]) {
  2191. return this._joinKeyValue(key, this.escape(value, field, escapeOptions), this.OperatorMap[Op.not], options.prefix);
  2192. }
  2193. return this._joinKeyValue(key, this.escape(value, field, escapeOptions), comparator, options.prefix);
  2194. }
  2195. /*
  2196. Takes something and transforms it into values of a where condition.
  2197. @private
  2198. */
  2199. getWhereConditions(smth, tableName, factory, options, prepend) {
  2200. const where = {};
  2201. if (Array.isArray(tableName)) {
  2202. tableName = tableName[0];
  2203. if (Array.isArray(tableName)) {
  2204. tableName = tableName[1];
  2205. }
  2206. }
  2207. options = options || {};
  2208. if (prepend === undefined) {
  2209. prepend = true;
  2210. }
  2211. if (smth && smth instanceof Utils.SequelizeMethod) { // Checking a property is cheaper than a lot of instanceof calls
  2212. return this.handleSequelizeMethod(smth, tableName, factory, options, prepend);
  2213. }
  2214. if (_.isPlainObject(smth)) {
  2215. return this.whereItemsQuery(smth, {
  2216. model: factory,
  2217. prefix: prepend && tableName,
  2218. type: options.type
  2219. });
  2220. }
  2221. if (typeof smth === 'number') {
  2222. let primaryKeys = factory ? Object.keys(factory.primaryKeys) : [];
  2223. if (primaryKeys.length > 0) {
  2224. // Since we're just a number, assume only the first key
  2225. primaryKeys = primaryKeys[0];
  2226. } else {
  2227. primaryKeys = 'id';
  2228. }
  2229. where[primaryKeys] = smth;
  2230. return this.whereItemsQuery(where, {
  2231. model: factory,
  2232. prefix: prepend && tableName
  2233. });
  2234. }
  2235. if (typeof smth === 'string') {
  2236. return this.whereItemsQuery(smth, {
  2237. model: factory,
  2238. prefix: prepend && tableName
  2239. });
  2240. }
  2241. if (Buffer.isBuffer(smth)) {
  2242. return this.escape(smth);
  2243. }
  2244. if (Array.isArray(smth)) {
  2245. if (smth.length === 0 || smth.length > 0 && smth[0].length === 0) return '1=1';
  2246. if (Utils.canTreatArrayAsAnd(smth)) {
  2247. const _smth = { [Op.and]: smth };
  2248. return this.getWhereConditions(_smth, tableName, factory, options, prepend);
  2249. }
  2250. throw new Error('Support for literal replacements in the `where` object has been removed.');
  2251. }
  2252. if (smth === null) {
  2253. return this.whereItemsQuery(smth, {
  2254. model: factory,
  2255. prefix: prepend && tableName
  2256. });
  2257. }
  2258. return '1=1';
  2259. }
  2260. // A recursive parser for nested where conditions
  2261. parseConditionObject(conditions, path) {
  2262. path = path || [];
  2263. return _.reduce(conditions, (result, value, key) => {
  2264. if (_.isObject(value)) {
  2265. return result.concat(this.parseConditionObject(value, path.concat(key))); // Recursively parse objects
  2266. }
  2267. result.push({ path: path.concat(key), value });
  2268. return result;
  2269. }, []);
  2270. }
  2271. booleanValue(value) {
  2272. return value;
  2273. }
  2274. }
  2275. Object.assign(QueryGenerator.prototype, require('./query-generator/operators'));
  2276. Object.assign(QueryGenerator.prototype, require('./query-generator/transaction'));
  2277. module.exports = QueryGenerator;