MCPcopy Create free account
hub / github.com/CommE2E/comm / dbQuery

Function dbQuery

keyserver/src/database/database.js:134–183  ·  view source on GitHub ↗
(
  statement: SQLStatementType,
  options?: QueryOptions,
)

Source from the content-addressed store, hash-verified

132 +multipleStatements?: boolean,
133};
134async 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
185function rawSQL(statement: SQLStatementType): string {
186 return mysql.format(statement.sql, statement.values);

Callers 15

createOrUpdatePublicLinkFunction · 0.90
createEntryFunction · 0.90
persistFreshOlmSessionFunction · 0.90
createUploadsFunction · 0.90
createSessionFunction · 0.90
createIDsFunction · 0.90
createMessagesFunction · 0.90
updateRepliesCountFunction · 0.90
postMessageSendFunction · 0.90
updateLatestMessagesFunction · 0.90

Calls 7

getScriptContextFunction · 0.90
getMigrationConnectionFunction · 0.85
loadPoolFunction · 0.85
rawSQLFunction · 0.85
endMethod · 0.80
logMethod · 0.45

Tested by

no test coverage detected