sqlite FK analyser, query runs in transaction

This commit is contained in:
Jan Prochazka 2021-05-06 14:11:51 +02:00
parent e251459512
commit 615397f332
2 changed files with 67 additions and 33 deletions

View File

@ -46,6 +46,23 @@ class Analyser extends DatabaseAnalyser {
}; };
} }
const fklist = await this.driver.query(this.pool, `pragma foreign_key_list('${tableName}')`);
tableObj.foreignKeys = _.values(_.groupBy(fklist.rows, 'id')).map((fkcols) => {
const fkcol = fkcols[0];
const fk = {
pureName: tableName,
refTableName: fkcol.table,
columns: fkcols.map((col) => ({
columnName: col.from,
refColumnName: col.to,
})),
updateAction: fkcol.on_update,
deleteAction: fkcol.on_delete,
constraintName: `FK_${tableName}_${fkcol.id}`,
};
return fk;
});
// console.log(info); // console.log(info);
} }

View File

@ -6,42 +6,35 @@ const { identify } = require('sql-query-identifier');
let Database; let Database;
function runStreamItem(client, sql, options) { function runStreamItem(client, sql, options, rowCounter) {
try { const stmt = client.prepare(sql);
console.log('RUN SQL ITEM', sql); if (stmt.reader) {
const stmt = client.prepare(sql); const columns = stmt.columns();
if (stmt.reader) { // const rows = stmt.all();
const columns = stmt.columns();
// const rows = stmt.all();
options.recordset( options.recordset(
columns.map((col) => ({ columns.map((col) => ({
columnName: col.name, columnName: col.name,
dataType: col.type, dataType: col.type,
})) }))
); );
for (const row of stmt.iterate()) { for (const row of stmt.iterate()) {
options.row(row); options.row(row);
} }
} else { } else {
const info = stmt.run(); const info = stmt.run();
rowCounter.count += info.changes;
if (!rowCounter.date) rowCounter.date = new Date().getTime();
if (new Date().getTime() > rowCounter.date > 1000) {
options.info({ options.info({
message: `${info.changes} rows affected`, message: `${rowCounter.count} rows affected`,
time: new Date(), time: new Date(),
severity: 'info', severity: 'info',
}); });
rowCounter.count = 0;
rowCounter.date = null;
} }
} catch (error) {
console.log('ERROR', error);
const { message, lineNumber, procName } = error;
options.info({
message,
line: lineNumber,
procedure: procName,
time: new Date(),
severity: 'error',
});
} }
} }
@ -68,12 +61,36 @@ const driver = {
}; };
}, },
async stream(client, sql, options) { async stream(client, sql, options) {
// console.log('CP1', sql);
const sqlSplitted = identify(sql, { dialect: 'sqlite', strict: false }); const sqlSplitted = identify(sql, { dialect: 'sqlite', strict: false });
// console.log('CP2', sqlSplitted);
for (const sqlItem of sqlSplitted) { const rowCounter = { count: 0, date: null };
runStreamItem(client, sqlItem.text, options);
const inTransaction = client.transaction(() => {
for (const sqlItem of sqlSplitted) {
runStreamItem(client, sqlItem.text, options, rowCounter);
}
if (rowCounter.date) {
options.info({
message: `${rowCounter.count} rows affected`,
time: new Date(),
severity: 'info',
});
}
});
try {
inTransaction();
} catch (error) {
console.log('ERROR', error);
const { message, lineNumber, procName } = error;
options.info({
message,
line: lineNumber,
procedure: procName,
time: new Date(),
severity: 'error',
});
} }
options.done(); options.done();