feat(SQLite): views management

This commit is contained in:
Fabio Di Stasio 2021-11-19 13:13:35 +01:00
parent 93b4a7063b
commit 7671c585f5
3 changed files with 22 additions and 26 deletions

View File

@ -26,7 +26,7 @@ module.exports = {
elementsWrapper: '', elementsWrapper: '',
stringsWrapper: '"', stringsWrapper: '"',
tableAdd: true, tableAdd: true,
viewAdd: false, viewAdd: true,
triggerAdd: false, triggerAdd: false,
triggerFunctionAdd: false, triggerFunctionAdd: false,
routineAdd: false, routineAdd: false,
@ -37,7 +37,7 @@ module.exports = {
tableSettings: true, tableSettings: true,
tableArray: false, tableArray: false,
tableRealCount: true, tableRealCount: true,
viewSettings: false, viewSettings: true,
triggerSettings: false, triggerSettings: false,
triggerFunctionSettings: false, triggerFunctionSettings: false,
routineSettings: false, routineSettings: false,

View File

@ -56,7 +56,13 @@ export class SQLiteClient extends AntaresCore {
for (const db of filteredDatabases) { for (const db of filteredDatabases) {
if (!schemas.has(db.name)) continue; if (!schemas.has(db.name)) continue;
let { rows: tables } = await this.raw(`SELECT * FROM "${db.name}".sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name`); let { rows: tables } = await this.raw(`
SELECT *
FROM "${db.name}".sqlite_master
WHERE type IN ('table', 'view')
AND name NOT LIKE 'sqlite_%'
ORDER BY name
`);
if (tables.length) { if (tables.length) {
tables = tables.map(table => { tables = tables.map(table => {
table.Db = db.name; table.Db = db.name;
@ -274,17 +280,13 @@ export class SQLiteClient extends AntaresCore {
* @memberof SQLiteClient * @memberof SQLiteClient
*/ */
async getViewInformations ({ schema, view }) { async getViewInformations ({ schema, view }) {
const sql = `SHOW CREATE VIEW \`${schema}\`.\`${view}\``; const sql = `SELECT "sql" FROM "${schema}".sqlite_master WHERE "type"='view' AND name='${view}'`;
const results = await this.raw(sql); const results = await this.raw(sql);
return results.rows.map(row => { return results.rows.map(row => {
return { return {
algorithm: row['Create View'].match(/(?<=CREATE ALGORITHM=).*?(?=\s)/gs)[0], sql: row.sql.match(/(?<=AS ).*?$/gs)[0],
definer: row['Create View'].match(/(?<=DEFINER=).*?(?=\s)/gs)[0], name: view
security: row['Create View'].match(/(?<=SQL SECURITY ).*?(?=\s)/gs)[0],
updateOption: row['Create View'].match(/(?<=WITH ).*?(?=\s)/gs) ? row['Create View'].match(/(?<=WITH ).*?(?=\s)/gs)[0] : '',
sql: row['Create View'].match(/(?<=AS ).*?$/gs)[0],
name: row.View
}; };
})[0]; })[0];
} }
@ -296,7 +298,7 @@ export class SQLiteClient extends AntaresCore {
* @memberof SQLiteClient * @memberof SQLiteClient
*/ */
async dropView (params) { async dropView (params) {
const sql = `DROP VIEW \`${params.schema}\`.\`${params.view}\``; const sql = `DROP VIEW "${params.schema}"."${params.view}"`;
return await this.raw(sql); return await this.raw(sql);
} }
@ -308,17 +310,11 @@ export class SQLiteClient extends AntaresCore {
*/ */
async alterView (params) { async alterView (params) {
const { view } = params; const { view } = params;
let sql = `
USE \`${view.schema}\`;
ALTER ALGORITHM = ${view.algorithm}${view.definer ? ` DEFINER=${view.definer}` : ''}
SQL SECURITY ${view.security}
VIEW \`${view.schema}\`.\`${view.oldName}\` AS ${view.sql} ${view.updateOption ? `WITH ${view.updateOption} CHECK OPTION` : ''}
`;
if (view.name !== view.oldName) return await Promise.all([
sql += `; RENAME TABLE \`${view.schema}\`.\`${view.oldName}\` TO \`${view.schema}\`.\`${view.name}\``; this.dropView({ schema: view.schema, view: view.oldName }),
this.createView(view)
return await this.raw(sql); ]);
} }
/** /**
@ -328,7 +324,7 @@ export class SQLiteClient extends AntaresCore {
* @memberof SQLiteClient * @memberof SQLiteClient
*/ */
async createView (params) { async createView (params) {
const sql = `CREATE ALGORITHM = ${params.algorithm} ${params.definer ? `DEFINER=${params.definer} ` : ''}SQL SECURITY ${params.security} VIEW \`${params.schema}\`.\`${params.name}\` AS ${params.sql} ${params.updateOption ? `WITH ${params.updateOption} CHECK OPTION` : ''}`; const sql = `CREATE VIEW "${params.schema}"."${params.name}" AS ${params.sql}`;
return await this.raw(sql); return await this.raw(sql);
} }

View File

@ -107,14 +107,14 @@ else {
mainWindow = await createMainWindow(); mainWindow = await createMainWindow();
createAppMenu(); createAppMenu();
if (isDevelopment) // if (isDevelopment)
mainWindow.webContents.openDevTools(); // mainWindow.webContents.openDevTools();
process.on('uncaughtException', (error) => { process.on('uncaughtException', error => {
mainWindow.webContents.send('unhandled-exception', error); mainWindow.webContents.send('unhandled-exception', error);
}); });
process.on('unhandledRejection', (error) => { process.on('unhandledRejection', error => {
mainWindow.webContents.send('unhandled-exception', error); mainWindow.webContents.send('unhandled-exception', error);
}); });
}); });