( statement: SQLStatementType, options?: QueryOptions, )
| 132 | +multipleStatements?: boolean, |
| 133 | }; |
| 134 | async function dbQuery( |
| 135 | statement: SQLStatementType, |
| 136 | options?: QueryOptions, |
| 137 | ): Promise<QueryResults> { |
| 138 | const triesLeft = options?.triesLeft ?? 2; |
| 139 | const multipleStatements = options?.multipleStatements ?? false; |
| 140 | |
| 141 | let connection; |
| 142 | if (connectionContext.migrationsActive) { |
| 143 | connection = await getMigrationConnection(); |
| 144 | } |
| 145 | if (multipleStatements) { |
| 146 | connection = await getMultipleStatementsConnection(); |
| 147 | } |
| 148 | if (!connection) { |
| 149 | connection = await loadPool(); |
| 150 | } |
| 151 | |
| 152 | const timeoutID = setTimeout( |
| 153 | () => databaseMonitor.reportLaggingQuery(statement.sql), |
| 154 | queryWarnTime, |
| 155 | ); |
| 156 | const scriptContext = getScriptContext(); |
| 157 | try { |
| 158 | const sql = statement.sql.trim(); |
| 159 | if ( |
| 160 | scriptContext && |
| 161 | scriptContext.dryRun && |
| 162 | (sql.startsWith('INSERT') || |
| 163 | sql.startsWith('DELETE') || |
| 164 | sql.startsWith('UPDATE')) |
| 165 | ) { |
| 166 | console.log(rawSQL(statement)); |
| 167 | return ([fakeResult]: any); |
| 168 | } |
| 169 | return await connection.query(statement); |
| 170 | } catch (e) { |
| 171 | if (e.errno === MYSQL_DEADLOCK_ERROR_CODE && triesLeft > 0) { |
| 172 | console.log('deadlock occurred, trying again', e); |
| 173 | return await dbQuery(statement, { ...options, triesLeft: triesLeft - 1 }); |
| 174 | } |
| 175 | e.query = statement.sql; |
| 176 | throw e; |
| 177 | } finally { |
| 178 | clearTimeout(timeoutID); |
| 179 | if (multipleStatements) { |
| 180 | connection.end(); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | function rawSQL(statement: SQLStatementType): string { |
| 186 | return mysql.format(statement.sql, statement.values); |
no test coverage detected