(sql, parameters)
| 47 | } |
| 48 | |
| 49 | async run(sql, parameters) { |
| 50 | const { connection } = this; |
| 51 | |
| 52 | if (!_.isEmpty(this.options.searchPath)) { |
| 53 | sql = this.sequelize.getQueryInterface().queryGenerator.setSearchPath(this.options.searchPath) + sql; |
| 54 | } |
| 55 | |
| 56 | if (this.sequelize.options.minifyAliases && this.options.includeAliases) { |
| 57 | _.toPairs(this.options.includeAliases) |
| 58 | class="cm">// Sorting to replace the longest aliases first to prevent alias collision |
| 59 | .sort((a, b) => b[1].length - a[1].length) |
| 60 | .forEach(([alias, original]) => { |
| 61 | const reg = new RegExp(_.escapeRegExp(original), class="st">'g'); |
| 62 | |
| 63 | sql = sql.replace(reg, alias); |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | this.sql = sql; |
| 68 | |
| 69 | const query = parameters && parameters.length |
| 70 | ? new Promise((resolve, reject) => connection.query(sql, parameters, (error, result) => error ? reject(error) : resolve(result))) |
| 71 | : new Promise((resolve, reject) => connection.query(sql, (error, result) => error ? reject(error) : resolve(result))); |
| 72 | |
| 73 | const complete = this._logQuery(sql, debug, parameters); |
| 74 | |
| 75 | let queryResult; |
| 76 | const errForStack = new Error(); |
| 77 | |
| 78 | try { |
| 79 | queryResult = await query; |
| 80 | } catch (err) { |
| 81 | class="cm">// set the client so that it will be reaped if the connection resets while executing |
| 82 | if (err.code === class="st">'ECONNRESET') { |
| 83 | connection._invalid = true; |
| 84 | } |
| 85 | |
| 86 | err.sql = sql; |
| 87 | err.parameters = parameters; |
| 88 | throw this.formatError(err, errForStack.stack); |
| 89 | } |
| 90 | |
| 91 | complete(); |
| 92 | |
| 93 | let rows = Array.isArray(queryResult) |
| 94 | ? queryResult.reduce((allRows, r) => allRows.concat(r.rows || []), []) |
| 95 | : queryResult.rows; |
| 96 | const rowCount = Array.isArray(queryResult) |
| 97 | ? queryResult.reduce( |
| 98 | (count, r) => Number.isFinite(r.rowCount) ? count + r.rowCount : count, |
| 99 | 0 |
| 100 | ) |
| 101 | : queryResult.rowCount || 0; |
| 102 | |
| 103 | if (this.sequelize.options.minifyAliases && this.options.aliasesMapping) { |
| 104 | rows = rows |
| 105 | .map(row => _.toPairs(row) |
| 106 | .reduce((acc, [key, value]) => { |
nothing calls this directly
no test coverage detected