MCPcopy
hub / github.com/sequelize/sequelize / run

Method run

lib/dialects/postgres/query.js:49–301  ·  lib/dialects/postgres/query.js::Query.run
(sql, parameters)

Source from the content-addressed store, hash-verified

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]) => {

Callers

nothing calls this directly

Calls 15

formatErrorMethod · 0.95
isShowIndexesQueryMethod · 0.95
isForeignKeysQueryMethod · 0.95
isShowOrDescribeQueryMethod · 0.95
setSearchPathMethod · 0.80
getQueryInterfaceMethod · 0.80
_logQueryMethod · 0.80
fromArrayMethod · 0.80
isSelectQueryMethod · 0.80
handleSelectQueryMethod · 0.80
isVersionQueryMethod · 0.80
isInsertQueryMethod · 0.80

Tested by

no test coverage detected