提交 454743d6 编写于 作者: S Serge Rider 提交者: GitHub

Merge pull request #9832 from dbeaver/#9653-postgre-trigger

#9653 Postgre trigger refactor

Former-commit-id: bde4cba1
......@@ -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<PostgreTrigger> {
protected static final Log log = Log.getLog(PostgreTriggerConfigurator.class);
@Override
......@@ -63,20 +60,8 @@ public class PostgreTriggerConfigurator implements DBEObjectConfigurator<Postgre
if (!editPage.edit()) {
return null;
}
try {
trigger.setName(editPage.getEntityName());
trigger.setFunction(editPage.selectedFunction);
String procName = "X";
PostgreProcedure function = trigger.getFunction(monitor);
if (function != null) {
procName = function.getFullQualifiedSignature();
}
trigger.setObjectDefinitionText("CREATE TRIGGER " + DBUtils.getQuotedIdentifier(trigger) + "\n"
+ "BEFORE UPDATE" + " " + "\n" + "ON " + trigger.getTable().getFullyQualifiedName(DBPEvaluationContext.DDL)
+ " FOR EACH ROW" + "\n" + "EXECUTE PROCEDURE " + (function == null ? procName : function.getFullyQualifiedName(DBPEvaluationContext.DDL))+ "()\n");
} catch (DBException e) {
log.error(e);
}
trigger.setName(editPage.getEntityName());
trigger.setFunction(editPage.selectedFunction);
return trigger;
}
}.execute();
......@@ -87,18 +72,11 @@ public class PostgreTriggerConfigurator implements DBEObjectConfigurator<Postgre
PostgreTrigger trigger;
CSmartSelector functionCombo;
PostgreProcedure selectedFunction;
Text processIdText;
public TriggerEditPage editPage;
public TriggerEditPage(PostgreTrigger trigger) {
super(trigger.getDataSource(), DBSEntityType.TRIGGER);
this.trigger = trigger;
}
public TriggerEditPage getEditPage() {
return editPage;
}
@Override
protected Control createPageContents(Composite parent) {
......@@ -142,8 +120,8 @@ public class PostgreTriggerConfigurator implements DBEObjectConfigurator<Postgre
: navigatorModel.getNodeByObject(selectedFunction);
DBNNode node = DBWorkbench.getPlatformUI().selectObject(parent.getShell(),
"Select function for ", dsNode, curNode,
new Class[] { DBSInstance.class, DBSObjectContainer.class, PostgreProcedure.class },
new Class[] { PostgreProcedure.class }, null);
new Class[]{ DBSInstance.class, DBSObjectContainer.class, PostgreProcedure.class },
new Class[]{ PostgreProcedure.class }, null);
if (node instanceof DBNDatabaseNode
&& ((DBNDatabaseNode) node).getObject() instanceof PostgreProcedure) {
functionCombo.removeAll();
......@@ -151,13 +129,9 @@ public class PostgreTriggerConfigurator implements DBEObjectConfigurator<Postgre
functionCombo.addItem(selectedFunction);
functionCombo.select(selectedFunction);
}
}
}
}
}
}
}
......@@ -76,16 +76,29 @@ public class PostgreTriggerManager extends SQLTriggerManager<PostgreTrigger, Pos
@Override
protected void createOrReplaceTriggerQuery(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> 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<DBEPersistAction> actions, ObjectDeleteCommand command, Map<String, Object> 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<DBEPersistAction> actions, ObjectDeleteCommand command, Map<String, Object> options) {
actions.add(new SQLDatabasePersistAction(
"Drop trigger",
"DROP TRIGGER " + DBUtils.getQuotedIdentifier(command.getObject()) + " ON " + command.getObject().getTable().getFullyQualifiedName(DBPEvaluationContext.DDL)
));
}
}
......@@ -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;
......@@ -66,15 +64,13 @@ public class PostgreTrigger implements DBSTrigger, DBSEntityElement, DBPQualifie
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;
protected String name;
public PostgreTrigger(
DBRProgressMonitor monitor,
......@@ -157,11 +153,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 +250,32 @@ public class PostgreTrigger implements DBSTrigger, DBSEntityElement, DBPQualifie
}
@Override
@Property(hidden = true, editable = true, updatable = true, order = -1)
public String getObjectDefinitionText(DBRProgressMonitor monitor, Map<String, Object> 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<String, Object> 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
......
......@@ -280,6 +280,7 @@ public abstract class SQLEditorNested<T extends DBSObject>
});
return Status.OK_STATUS;
} catch (Exception e) {
log.error(e);
sourceText = "/* ERROR WHILE READING SOURCE:\n\n" + e.getMessage() + "\n*/";
return Status.CANCEL_STATUS;
} finally {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册