| 1165 | @private |
| 1166 | */ |
| 1167 | selectQuery(tableName, options, model) { |
| 1168 | options = options || {}; |
| 1169 | const limit = options.limit; |
| 1170 | const mainQueryItems = []; |
| 1171 | const subQueryItems = []; |
| 1172 | const subQuery = options.subQuery === undefined ? limit && options.hasMultiAssociation : options.subQuery; |
| 1173 | const attributes = { |
| 1174 | main: options.attributes && options.attributes.slice(), |
| 1175 | subQuery: null |
| 1176 | }; |
| 1177 | const mainTable = { |
| 1178 | name: tableName, |
| 1179 | quotedName: null, |
| 1180 | as: null, |
| 1181 | model |
| 1182 | }; |
| 1183 | const topLevelInfo = { |
| 1184 | names: mainTable, |
| 1185 | options, |
| 1186 | subQuery |
| 1187 | }; |
| 1188 | let mainJoinQueries = []; |
| 1189 | let subJoinQueries = []; |
| 1190 | let query; |
| 1191 | |
| 1192 | class="cm">// Aliases can be passed through subqueries and we don't want to reset them |
| 1193 | if (this.options.minifyAliases && !options.aliasesMapping) { |
| 1194 | options.aliasesMapping = new Map(); |
| 1195 | options.aliasesByTable = {}; |
| 1196 | options.includeAliases = new Map(); |
| 1197 | } |
| 1198 | |
| 1199 | class="cm">// resolve table name options |
| 1200 | if (options.tableAs) { |
| 1201 | mainTable.as = this.quoteIdentifier(options.tableAs); |
| 1202 | } else if (!Array.isArray(mainTable.name) && mainTable.model) { |
| 1203 | mainTable.as = this.quoteIdentifier(mainTable.model.name); |
| 1204 | } |
| 1205 | |
| 1206 | mainTable.quotedName = !Array.isArray(mainTable.name) ? this.quoteTable(mainTable.name) : tableName.map(t => { |
| 1207 | return Array.isArray(t) ? this.quoteTable(t[0], t[1]) : this.quoteTable(t, true); |
| 1208 | }).join(class="st">', '); |
| 1209 | |
| 1210 | if (subQuery && attributes.main) { |
| 1211 | for (const keyAtt of mainTable.model.primaryKeyAttributes) { |
| 1212 | class="cm">// Check if mainAttributes contain the primary key of the model either as a field or an aliased field |
| 1213 | if (!attributes.main.some(attr => keyAtt === attr || keyAtt === attr[0] || keyAtt === attr[1])) { |
| 1214 | attributes.main.push(mainTable.model.rawAttributes[keyAtt].field ? [keyAtt, mainTable.model.rawAttributes[keyAtt].field] : keyAtt); |
| 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | attributes.main = this.escapeAttributes(attributes.main, options, mainTable.as); |
| 1220 | attributes.main = attributes.main || (options.include ? [`${mainTable.as}.*`] : [class="st">'*']); |
| 1221 | |
| 1222 | class="cm">// If subquery, we add the mainAttributes to the subQuery and set the mainAttributes to select * from subquery |
| 1223 | if (subQuery || options.groupedLimit) { |
| 1224 | class="cm">// We need primary keys |