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

Method run

lib/dialects/sqlite/query.js:219–283  ·  view source on GitHub ↗
(sql, parameters)

Source from the content-addressed store, hash-verified

217 }
218
219 async run(sql, parameters) {
220 const conn = this.connection;
221 this.sql = sql;
222 const method = this.getDatabaseMethod();
223 const complete = this._logQuery(sql, debug, parameters);
224
225 return new Promise((resolve, reject) => conn.serialize(async () => {
226 const columnTypes = {};
227 const errForStack = new Error();
228 const executeSql = () => {
229 if (sql.startsWith('-- ')) {
230 return resolve();
231 }
232 const query = this;
233 // cannot use arrow function here because the function is bound to the statement
234 function afterExecute(executionError, results) {
235 try {
236 complete();
237 // `this` is passed from sqlite, we have no control over this.
238 // eslint-disable-next-line no-invalid-this
239 resolve(query._handleQueryResponse(this, columnTypes, executionError, results, errForStack.stack));
240 return;
241 } catch (error) {
242 reject(error);
243 }
244 }
245
246 if (!parameters) parameters = [];
247 conn[method](sql, parameters, afterExecute);
248
249 return null;
250 };
251
252 if (this.getDatabaseMethod() === 'all') {
253 let tableNames = [];
254 if (this.options && this.options.tableNames) {
255 tableNames = this.options.tableNames;
256 } else if (/FROM `(.*?)`/i.exec(this.sql)) {
257 tableNames.push(/FROM `(.*?)`/i.exec(this.sql)[1]);
258 }
259
260 // If we already have the metadata for the table, there's no need to ask for it again
261 tableNames = tableNames.filter(tableName => !(tableName in columnTypes) && tableName !== 'sqlite_master');
262
263 if (!tableNames.length) {
264 return executeSql();
265 }
266 await Promise.all(tableNames.map(tableName =>
267 new Promise(resolve => {
268 tableName = tableName.replace(/`/g, '');
269 columnTypes[tableName] = {};
270
271 conn.all(`PRAGMA table_info(\`${tableName}\`)`, (err, results) => {
272 if (!err) {
273 for (const result of results) {
274 columnTypes[tableName][result.name] = result.type;
275 }
276 }

Callers 2

getConnectionMethod · 0.45

Calls 3

getDatabaseMethodMethod · 0.95
_logQueryMethod · 0.80
allMethod · 0.80

Tested by

no test coverage detected