From 6fe24992156c8e7eb0919476bb3312aef585b93a Mon Sep 17 00:00:00 2001 From: ShadelessFox Date: Mon, 21 Sep 2020 19:04:24 +0300 Subject: [PATCH] #9653 Refactor trigger creation and modification --- .../ui/config/PostgreTriggerConfigurator.java | 36 ++------- .../edit/PostgreTriggerManager.java | 29 ++++++-- .../ext/postgresql/model/PostgreTrigger.java | 73 +++++++------------ 3 files changed, 53 insertions(+), 85 deletions(-) diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.ui/src/org/jkiss/dbeaver/ext/postgresql/ui/config/PostgreTriggerConfigurator.java b/plugins/org.jkiss.dbeaver.ext.postgresql.ui/src/org/jkiss/dbeaver/ext/postgresql/ui/config/PostgreTriggerConfigurator.java index 4836ad0686..46e56413b3 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.ui/src/org/jkiss/dbeaver/ext/postgresql/ui/config/PostgreTriggerConfigurator.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.ui/src/org/jkiss/dbeaver/ext/postgresql/ui/config/PostgreTriggerConfigurator.java @@ -24,13 +24,10 @@ import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Text; -import org.jkiss.dbeaver.DBException; import org.jkiss.dbeaver.Log; import org.jkiss.dbeaver.ext.postgresql.model.PostgreProcedure; import org.jkiss.dbeaver.ext.postgresql.model.PostgreTrigger; import org.jkiss.dbeaver.model.DBIcon; -import org.jkiss.dbeaver.model.DBPEvaluationContext; -import org.jkiss.dbeaver.model.DBUtils; import org.jkiss.dbeaver.model.edit.DBEObjectConfigurator; import org.jkiss.dbeaver.model.navigator.DBNDatabaseNode; import org.jkiss.dbeaver.model.navigator.DBNModel; @@ -50,7 +47,7 @@ import org.jkiss.dbeaver.ui.editors.object.struct.EntityEditPage; * Postgre sequence configurator */ public class PostgreTriggerConfigurator implements DBEObjectConfigurator { - + protected static final Log log = Log.getLog(PostgreTriggerConfigurator.class); @Override @@ -63,20 +60,8 @@ public class PostgreTriggerConfigurator implements DBEObjectConfigurator actions, PostgreTrigger trigger, boolean create) { - actions.add(new SQLDatabasePersistAction("Create trigger", trigger.getBody(), true)); + if (!create) + return; + + try { + actions.add(new SQLDatabasePersistAction( + "Create trigger", + "CREATE TRIGGER " + DBUtils.getQuotedIdentifier(trigger) + + "\n AFTER INSERT" + + "\n ON " + trigger.getTable().getFullyQualifiedName(DBPEvaluationContext.DDL) + + "\n FOR EACH ROW" + + "\n EXECUTE PROCEDURE " + trigger.getFunction(monitor).getFullyQualifiedName(DBPEvaluationContext.DDL) + "()", + true + )); + } catch (DBException e) { + log.error(e); + } } @Override - protected void addObjectDeleteActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List actions, ObjectDeleteCommand command, Map options) - { - actions.add( - new SQLDatabasePersistAction("Drop trigger", - "DROP TRIGGER " + DBUtils.getQuotedIdentifier(command.getObject()) + " ON " + command.getObject().getTable().getFullyQualifiedName(DBPEvaluationContext.DDL)) - ); + protected void addObjectDeleteActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List actions, ObjectDeleteCommand command, Map options) { + actions.add(new SQLDatabasePersistAction( + "Drop trigger", + "DROP TRIGGER " + DBUtils.getQuotedIdentifier(command.getObject()) + " ON " + command.getObject().getTable().getFullyQualifiedName(DBPEvaluationContext.DDL) + )); } - } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreTrigger.java b/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreTrigger.java index 5751eafff2..e10e0dcd92 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreTrigger.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreTrigger.java @@ -20,7 +20,6 @@ import org.jkiss.code.NotNull; import org.jkiss.code.Nullable; import org.jkiss.dbeaver.DBException; import org.jkiss.dbeaver.Log; -import org.jkiss.dbeaver.ext.postgresql.PostgreConstants; import org.jkiss.dbeaver.ext.postgresql.PostgreUtils; import org.jkiss.dbeaver.model.*; import org.jkiss.dbeaver.model.exec.DBCException; @@ -30,7 +29,6 @@ import org.jkiss.dbeaver.model.meta.IPropertyValueTransformer; import org.jkiss.dbeaver.model.meta.Property; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; import org.jkiss.dbeaver.model.sql.SQLUtils; -import org.jkiss.dbeaver.model.sql.format.SQLFormatUtils; import org.jkiss.dbeaver.model.struct.DBSActionTiming; import org.jkiss.dbeaver.model.struct.DBSEntityElement; import org.jkiss.dbeaver.model.struct.DBSObjectState; @@ -47,34 +45,29 @@ import java.util.Map; /** * PostgreTrigger */ -public class PostgreTrigger implements DBSTrigger, DBSEntityElement, DBPQualifiedObject, PostgreObject, PostgreScriptObject, DBPStatefulObject, DBPScriptObjectExt2 -{ - private static final Log log = Log.getLog(PostgreTrigger.class); - +public class PostgreTrigger implements DBSTrigger, DBSEntityElement, DBPQualifiedObject, PostgreObject, PostgreScriptObject, DBPStatefulObject, DBPScriptObjectExt2 { /* Bits within tgtype */ - public static final int TRIGGER_TYPE_ROW = (1 << 0); - public static final int TRIGGER_TYPE_BEFORE = (1 << 1); - public static final int TRIGGER_TYPE_INSERT = (1 << 2); - public static final int TRIGGER_TYPE_DELETE = (1 << 3); - public static final int TRIGGER_TYPE_UPDATE = (1 << 4); - public static final int TRIGGER_TYPE_TRUNCATE = (1 << 5); - public static final int TRIGGER_TYPE_INSTEAD = (1 << 6); - + public static final int TRIGGER_TYPE_ROW = (1 << 0); + public static final int TRIGGER_TYPE_BEFORE = (1 << 1); + public static final int TRIGGER_TYPE_INSERT = (1 << 2); + public static final int TRIGGER_TYPE_DELETE = (1 << 3); + public static final int TRIGGER_TYPE_UPDATE = (1 << 4); + public static final int TRIGGER_TYPE_TRUNCATE = (1 << 5); + public static final int TRIGGER_TYPE_INSTEAD = (1 << 6); + private static final Log log = Log.getLog(PostgreTrigger.class); + protected String name; + protected String description; private PostgreTableReal table; private long objectId; private String enabledState; private String whenExpression; private long functionSchemaId; private long functionId; - private String body; - - protected String name; private DBSActionTiming actionTiming; private DBSManipulationType[] manipulationTypes; private PostgreTriggerType type; private boolean persisted; private PostgreTableColumn[] columnRefs; - protected String description; public PostgreTrigger( DBRProgressMonitor monitor, @@ -157,11 +150,6 @@ public class PostgreTrigger implements DBSTrigger, DBSEntityElement, DBPQualifie this.name = name; } - public String getBody() - { - return body; - } - @Property(viewable = true, order = 2) public DBSActionTiming getActionTiming() { return actionTiming; @@ -259,39 +247,32 @@ public class PostgreTrigger implements DBSTrigger, DBSEntityElement, DBPQualifie } @Override - @Property(hidden = true, editable = true, updatable = true, order = -1) - public String getObjectDefinitionText(DBRProgressMonitor monitor, Map options) throws DBException - { - if (body == null) { - StringBuilder ddl = new StringBuilder(); - ddl.append("-- DROP TRIGGER ").append(DBUtils.getQuotedIdentifier(this)).append(" ON ") + public String getObjectDefinitionText(DBRProgressMonitor monitor, Map options) throws DBException { + StringBuilder ddl = new StringBuilder(); + + ddl.append("-- DROP TRIGGER ") + .append(DBUtils.getQuotedIdentifier(this)).append(" ON ") .append(getTable().getFullyQualifiedName(DBPEvaluationContext.DDL)).append(";\n\n"); - try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Read trigger definition")) { - String triggerSource = JDBCUtils.queryString(session, "SELECT pg_catalog.pg_get_triggerdef(?)", objectId); - if (triggerSource != null) { - triggerSource = SQLFormatUtils.formatSQL(getDataSource(), triggerSource); - ddl.append(triggerSource).append(";"); - } - } catch (SQLException e) { - throw new DBException(e, getDataSource()); - } + ddl.append("CREATE TRIGGER ").append(DBUtils.getQuotedIdentifier(this)) + .append("\n AFTER INSERT") + .append("\n ON ").append(table.getFullyQualifiedName(DBPEvaluationContext.DDL)) + .append("\n FOR EACH ROW") + .append("\n EXECUTE PROCEDURE ").append(getFunction(monitor).getFullyQualifiedName(DBPEvaluationContext.DDL)).append("();\n"); - if (!CommonUtils.isEmpty(getDescription()) && CommonUtils.getOption(options, DBPScriptObject.OPTION_INCLUDE_COMMENTS)) { - ddl.append("\n").append("\nCOMMENT ON TRIGGER ").append(DBUtils.getQuotedIdentifier(this)) + if (!CommonUtils.isEmpty(getDescription()) && CommonUtils.getOption(options, DBPScriptObject.OPTION_INCLUDE_COMMENTS)) { + ddl.append("\nCOMMENT ON TRIGGER ").append(DBUtils.getQuotedIdentifier(this)) .append(" ON ").append(getTable().getFullyQualifiedName(DBPEvaluationContext.DDL)) .append(" IS ") .append(SQLUtils.quoteString(this, getDescription())).append(";"); - } - this.body = ddl.toString(); } - return body; + + return ddl.toString(); } @Override - public void setObjectDefinitionText(String sourceText) throws DBException - { - body = sourceText; + public void setObjectDefinitionText(String sourceText) throws DBException { + throw new DBException("Trigger DDL is read-only"); } @Override -- GitLab