feat(Firebird SQL): trigger add/edit/delete support

This commit is contained in:
Fabio Di Stasio 2022-11-16 15:16:12 +01:00
parent 7d1967a609
commit 8e422e3f07
3 changed files with 64 additions and 16 deletions

View File

@ -48,6 +48,9 @@ export const customizations: Customizations = {
tableSettings: true, tableSettings: true,
viewAdd: true, viewAdd: true,
viewSettings: true, viewSettings: true,
triggerAdd: true,
triggerMultipleEvents: true,
triggerSql: 'BEGIN\r\n\r\nEND',
indexes: true, indexes: true,
foreigns: true, foreigns: true,
nullable: true nullable: true

View File

@ -203,8 +203,8 @@ export class FirebirdSQLClient extends AntaresCore {
// TRIGGERS // TRIGGERS
const remappedTriggers = triggersArr.map(trigger => { const remappedTriggers = triggersArr.map(trigger => {
return { return {
name: trigger.NAME, name: trigger.NAME.trim(),
table: trigger.RELATION, table: trigger.RELATION.trim(),
statement: trigger.SOURCE statement: trigger.SOURCE
}; };
}); });
@ -651,23 +651,49 @@ export class FirebirdSQLClient extends AntaresCore {
return await this.raw(sql); return await this.raw(sql);
} }
async getTriggerInformations ({ schema, trigger }: { schema: string; trigger: string }) { async getTriggerInformations ({ trigger }: { schema: string; trigger: string }) {
const sql = `SELECT "sql" FROM "${schema}".sqlite_master WHERE "type"='trigger' AND name='${trigger}'`; const sql = `
SELECT
RDB$TRIGGER_NAME as name,
RDB$RELATION_NAME as relation,
RDB$TRIGGER_SOURCE as sql,
RDB$TRIGGER_TYPE as type
FROM RDB$TRIGGERS
WHERE RDB$SYSTEM_FLAG=0
AND RDB$TRIGGER_NAME = '${trigger}';
`;
const results = await this.raw(sql); const results = await this.raw(sql);
const eventsMap = new Map([
[1, ['INSERT']],
[2, ['INSERT']],
[3, ['UPDATE']],
[4, ['UPDATE']],
[5, ['DELETE']],
[6, ['DELETE']],
[17, ['INSERT', 'UPDATE']],
[18, ['INSERT', 'UPDATE']],
[25, ['INSERT', 'DELETE']],
[26, ['INSERT', 'DELETE']],
[27, ['UPDATE', 'DELETE']],
[28, ['UPDATE', 'DELETE']],
[113, ['INSERT', 'UPDATE', 'DELETE']],
[114, ['INSERT', 'UPDATE', 'DELETE']]
]);
return results.rows.map(row => { return results.rows.map(row => {
return { return {
sql: row.sql.match(/(BEGIN|begin)(.*)(END|end)/gs)[0], sql: row.SQL.match(/(BEGIN|begin)(.*)(END|end)/gs)[0],
name: trigger, name: trigger,
table: row.sql.match(/(?<=ON `).*?(?=`)/gs)[0], table: row.RELATION.trim(),
activation: row.sql.match(/(BEFORE|AFTER)/gs)[0], activation: row.TYPE%2 ? 'BEFORE' : 'AFTER',
event: row.sql.match(/(INSERT|UPDATE|DELETE)/gs)[0] event: eventsMap.get(row.TYPE)
}; };
})[0]; })[0];
} }
async dropTrigger (params: { schema: string; trigger: string }) { async dropTrigger (params: { schema: string; trigger: string }) {
const sql = `DROP TRIGGER \`${params.schema}\`.\`${params.trigger}\``; const sql = `DROP TRIGGER "${params.trigger}"`;
return await this.raw(sql); return await this.raw(sql);
} }
@ -687,13 +713,19 @@ export class FirebirdSQLClient extends AntaresCore {
} }
async createTrigger (params: antares.CreateTriggerParams) { async createTrigger (params: antares.CreateTriggerParams) {
const sql = `CREATE ${params.definer ? `DEFINER=${params.definer} ` : ''}TRIGGER \`${params.schema}\`.\`${params.name}\` ${params.activation} ${params.event} ON \`${params.table}\` FOR EACH ROW ${params.sql}`; const eventsString = Array.isArray(params.event) ? params.event.join(' OR ') : params.event;
const sql = `
CREATE TRIGGER "${params.name}" FOR "${params.table}"
${params.activation} ${eventsString}
AS ${params.sql}
`;
return await this.raw(sql, { split: false }); return await this.raw(sql, { split: false });
} }
async getEngines () { async getEngines () {
return { return {
name: 'SQLite', name: 'Firebird',
support: 'YES', support: 'YES',
comment: '', comment: '',
isDefault: true isDefault: true
@ -954,7 +986,7 @@ export class FirebirdSQLClient extends AntaresCore {
}); });
if (args.details) { if (args.details) {
if (remappedFields.length) { if (remappedFields?.length) {
paramsArr = remappedFields.map(field => { paramsArr = remappedFields.map(field => {
return { return {
table: field.orgTable, table: field.orgTable,
@ -978,8 +1010,10 @@ export class FirebirdSQLClient extends AntaresCore {
}); });
} }
catch (err) { catch (err) {
if (args.autocommit) if (args.autocommit) {
this._runningConnections.delete(args.tabUid); this._runningConnections.delete(args.tabUid);
(connection as firebird.Database).detach();
}
this.destroy(); this.destroy();
reject(err); reject(err);
@ -990,8 +1024,10 @@ export class FirebirdSQLClient extends AntaresCore {
keysArr = keysArr ? [...keysArr, ...response] : response; keysArr = keysArr ? [...keysArr, ...response] : response;
} }
catch (err) { catch (err) {
if (args.autocommit) if (args.autocommit) {
this._runningConnections.delete(args.tabUid); this._runningConnections.delete(args.tabUid);
(connection as firebird.Database).detach();
}
this.destroy(); this.destroy();
reject(err); reject(err);
@ -1003,6 +1039,8 @@ export class FirebirdSQLClient extends AntaresCore {
catch (err) { catch (err) {
reject(err); reject(err);
this.destroy(); this.destroy();
if (args.autocommit)
(connection as firebird.Database).detach();
} }
timeStop = new Date(); timeStop = new Date();
@ -1020,6 +1058,7 @@ export class FirebirdSQLClient extends AntaresCore {
resultsArr.push({ rows, report, fields, keys, duration }); resultsArr.push({ rows, report, fields, keys, duration });
} }
if (args.autocommit)
(connection as firebird.Database).detach(); (connection as firebird.Database).detach();
const result = resultsArr.length === 1 ? resultsArr[0] : resultsArr; const result = resultsArr.length === 1 ? resultsArr[0] : resultsArr;

View File

@ -275,7 +275,13 @@ const saveContentListener = () => {
}; };
watch(() => props.isSelected, (val) => { watch(() => props.isSelected, (val) => {
if (val) changeBreadcrumbs({ schema: props.schema }); if (val) {
changeBreadcrumbs({ schema: props.schema });
setTimeout(() => {
resizeQueryEditor();
}, 50);
}
}); });
watch(isChanged, (val) => { watch(isChanged, (val) => {