Skip to content

Commit

Permalink
feat(SQLite): triggers management
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Nov 19, 2021
1 parent 7671c58 commit f40e9c5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 86 deletions.
71 changes: 7 additions & 64 deletions src/common/customizations/sqlite.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,27 @@
module.exports = {
// Defaults
defaultPort: null,
defaultUser: null,
defaultDatabase: null,
// Core
database: false,
collations: false,
engines: false,
connectionSchema: false,
fileConnection: true,
// Tools
processesList: false,
usersManagement: false,
variables: false,
// Structure
schemas: false,
tables: false,
views: false,
triggers: false,
triggerFunctions: false,
routines: false,
functions: false,
schedulers: false,
schemas: true,
tables: true,
views: true,
triggers: true,
// Settings
elementsWrapper: '',
stringsWrapper: '"',
tableAdd: true,
viewAdd: true,
triggerAdd: false,
triggerFunctionAdd: false,
routineAdd: false,
functionAdd: false,
schedulerAdd: false,
databaseEdit: false,
triggerAdd: true,
schemaEdit: false,
tableSettings: true,
tableArray: false,
tableRealCount: true,
viewSettings: true,
triggerSettings: false,
triggerFunctionSettings: false,
routineSettings: false,
functionSettings: false,
schedulerSettings: false,
triggerSettings: true,
indexes: true,
foreigns: true,
sortableFields: true,
unsigned: false,
nullable: true,
nullablePrimary: true,
zerofill: false,
tableOptions: false,
autoIncrement: false,
comment: false,
collation: false,
definer: false,
onUpdate: false,
viewAlgorithm: false,
viewSqlSecurity: false,
viewUpdateOption: false,
procedureDeterministic: false,
procedureDataAccess: false,
procedureSql: false,
procedureContext: false,
procedureLanguage: false,
functionDeterministic: false,
functionDataAccess: false,
functionSql: false,
functionContext: false,
functionLanguage: false,
triggerSql: false,
triggerStatementInCreation: false,
triggerMultipleEvents: false,
triggerTableInName: false,
triggerUpdateColumns: false,
triggerOnlyRename: false,
triggerEnableDisable: false,
triggerFunctionSql: false,
triggerFunctionlanguages: false,
parametersLength: false,
languages: false,
triggerSql: 'BEGIN\r\n\r\nEND',
readOnlyMode: true
};
38 changes: 16 additions & 22 deletions src/main/libs/clients/SQLiteClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class SQLiteClient extends AntaresCore {
tablesArr.push(...tables);
}

let { rows: triggers } = await this.raw(`SELECT * FROM "${db.name}".sqlite_master WHERE type='trigger' AND name NOT LIKE 'sqlite_%'`);
let { rows: triggers } = await this.raw(`SELECT * FROM "${db.name}".sqlite_master WHERE type='trigger'`);
if (triggers.length) {
triggers = triggers.map(trigger => {
trigger.Db = db.name;
Expand Down Expand Up @@ -99,15 +99,8 @@ export class SQLiteClient extends AntaresCore {
// TRIGGERS
const remappedTriggers = triggersArr.filter(trigger => trigger.Db === db.name).map(trigger => {
return {
name: trigger.Trigger,
statement: trigger.Statement,
timing: trigger.Timing,
definer: trigger.Definer,
event: trigger.Event,
table: trigger.Table,
sqlMode: trigger.sql_mode,
created: trigger.Created,
charset: trigger.character_set_client
name: trigger.name,
table: trigger.tbl_name
};
});

Expand Down Expand Up @@ -310,11 +303,13 @@ export class SQLiteClient extends AntaresCore {
*/
async alterView (params) {
const { view } = params;

return await Promise.all([
this.dropView({ schema: view.schema, view: view.oldName }),
this.createView(view)
]);
try {
await this.dropView({ schema: view.schema, view: view.oldName });
await this.createView(view);
}
catch (err) {
return Promise.reject(err);
}
}

/**
Expand All @@ -335,17 +330,16 @@ export class SQLiteClient extends AntaresCore {
* @memberof SQLiteClient
*/
async getTriggerInformations ({ schema, trigger }) {
const sql = `SHOW CREATE TRIGGER \`${schema}\`.\`${trigger}\``;
const sql = `SELECT "sql" FROM "${schema}".sqlite_master WHERE "type"='trigger' AND name='${trigger}'`;
const results = await this.raw(sql);

return results.rows.map(row => {
return {
definer: row['SQL Original Statement'].match(/(?<=DEFINER=).*?(?=\s)/gs)[0],
sql: row['SQL Original Statement'].match(/(BEGIN|begin)(.*)(END|end)/gs)[0],
name: row.Trigger,
table: row['SQL Original Statement'].match(/(?<=ON `).*?(?=`)/gs)[0],
activation: row['SQL Original Statement'].match(/(BEFORE|AFTER)/gs)[0],
event: row['SQL Original Statement'].match(/(INSERT|UPDATE|DELETE)/gs)[0]
sql: row.sql.match(/(BEGIN|begin)(.*)(END|end)/gs)[0],
name: trigger,
table: row.sql.match(/(?<=ON `).*?(?=`)/gs)[0],
activation: row.sql.match(/(BEFORE|AFTER)/gs)[0],
event: row.sql.match(/(INSERT|UPDATE|DELETE)/gs)[0]
};
})[0];
}
Expand Down

0 comments on commit f40e9c5

Please sign in to comment.