feat(SQLite): triggers management

This commit is contained in:
Fabio Di Stasio 2021-11-19 15:36:07 +01:00
parent 7671c585f5
commit f40e9c592e
2 changed files with 23 additions and 86 deletions

View File

@ -1,84 +1,27 @@
module.exports = { module.exports = {
// Defaults
defaultPort: null,
defaultUser: null,
defaultDatabase: null,
// Core // Core
database: false,
collations: false,
engines: false,
connectionSchema: false,
fileConnection: true, fileConnection: true,
// Tools
processesList: false,
usersManagement: false,
variables: false,
// Structure // Structure
schemas: false, schemas: true,
tables: false, tables: true,
views: false, views: true,
triggers: false, triggers: true,
triggerFunctions: false,
routines: false,
functions: false,
schedulers: false,
// Settings // Settings
elementsWrapper: '', elementsWrapper: '',
stringsWrapper: '"', stringsWrapper: '"',
tableAdd: true, tableAdd: true,
viewAdd: true, viewAdd: true,
triggerAdd: false, triggerAdd: true,
triggerFunctionAdd: false,
routineAdd: false,
functionAdd: false,
schedulerAdd: false,
databaseEdit: false,
schemaEdit: false, schemaEdit: false,
tableSettings: true, tableSettings: true,
tableArray: false,
tableRealCount: true, tableRealCount: true,
viewSettings: true, viewSettings: true,
triggerSettings: false, triggerSettings: true,
triggerFunctionSettings: false,
routineSettings: false,
functionSettings: false,
schedulerSettings: false,
indexes: true, indexes: true,
foreigns: true, foreigns: true,
sortableFields: true, sortableFields: true,
unsigned: false,
nullable: true, nullable: true,
nullablePrimary: true, nullablePrimary: true,
zerofill: false, triggerSql: 'BEGIN\r\n\r\nEND',
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,
readOnlyMode: true readOnlyMode: true
}; };

View File

@ -71,7 +71,7 @@ export class SQLiteClient extends AntaresCore {
tablesArr.push(...tables); 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) { if (triggers.length) {
triggers = triggers.map(trigger => { triggers = triggers.map(trigger => {
trigger.Db = db.name; trigger.Db = db.name;
@ -99,15 +99,8 @@ export class SQLiteClient extends AntaresCore {
// TRIGGERS // TRIGGERS
const remappedTriggers = triggersArr.filter(trigger => trigger.Db === db.name).map(trigger => { const remappedTriggers = triggersArr.filter(trigger => trigger.Db === db.name).map(trigger => {
return { return {
name: trigger.Trigger, name: trigger.name,
statement: trigger.Statement, table: trigger.tbl_name
timing: trigger.Timing,
definer: trigger.Definer,
event: trigger.Event,
table: trigger.Table,
sqlMode: trigger.sql_mode,
created: trigger.Created,
charset: trigger.character_set_client
}; };
}); });
@ -310,11 +303,13 @@ export class SQLiteClient extends AntaresCore {
*/ */
async alterView (params) { async alterView (params) {
const { view } = params; const { view } = params;
try {
return await Promise.all([ await this.dropView({ schema: view.schema, view: view.oldName });
this.dropView({ schema: view.schema, view: view.oldName }), await this.createView(view);
this.createView(view) }
]); catch (err) {
return Promise.reject(err);
}
} }
/** /**
@ -335,17 +330,16 @@ export class SQLiteClient extends AntaresCore {
* @memberof SQLiteClient * @memberof SQLiteClient
*/ */
async getTriggerInformations ({ schema, trigger }) { 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); const results = await this.raw(sql);
return results.rows.map(row => { return results.rows.map(row => {
return { return {
definer: row['SQL Original Statement'].match(/(?<=DEFINER=).*?(?=\s)/gs)[0], sql: row.sql.match(/(BEGIN|begin)(.*)(END|end)/gs)[0],
sql: row['SQL Original Statement'].match(/(BEGIN|begin)(.*)(END|end)/gs)[0], name: trigger,
name: row.Trigger, table: row.sql.match(/(?<=ON `).*?(?=`)/gs)[0],
table: row['SQL Original Statement'].match(/(?<=ON `).*?(?=`)/gs)[0], activation: row.sql.match(/(BEFORE|AFTER)/gs)[0],
activation: row['SQL Original Statement'].match(/(BEFORE|AFTER)/gs)[0], event: row.sql.match(/(INSERT|UPDATE|DELETE)/gs)[0]
event: row['SQL Original Statement'].match(/(INSERT|UPDATE|DELETE)/gs)[0]
}; };
})[0]; })[0];
} }