提交 f177d0de 编写于 作者: S serge-rider

#3398 PG comments DDL fix


Former-commit-id: 8c67b694
上级 6fea9ded
......@@ -232,7 +232,9 @@ public class EntityEditor extends MultiPageDatabaseEditor
// Flush all nested object editors and result containers
for (IEditorPart editor : editorMap.values()) {
if (editor instanceof ObjectPropertiesEditor || editor instanceof IResultSetContainer) {
editor.doSave(monitor);
if (editor.isDirty()) {
editor.doSave(monitor);
}
}
if (monitor.isCanceled()) {
return;
......@@ -418,9 +420,11 @@ public class EntityEditor extends MultiPageDatabaseEditor
DBeaverUI.syncExec(() -> DBUserInterface.getInstance().showError("Validation", e.getMessage()));
return IDialogConstants.CANCEL_ID;
}
Map<String, Object> options = new HashMap<>();
options.put(DBPScriptObject.OPTION_OBJECT_SAVE, true);
script.append(SQLUtils.generateScript(
commandContext.getExecutionContext().getDataSource(),
command.getPersistActions(DBPScriptObject.EMPTY_OPTIONS),
command.getPersistActions(options),
false));
}
if (script.length() == 0) {
......@@ -945,7 +949,9 @@ public class EntityEditor extends MultiPageDatabaseEditor
try {
final DBECommandContext commandContext = getCommandContext();
if (commandContext != null && commandContext.isDirty()) {
success = saveCommandContext(monitor, DBPScriptObject.EMPTY_OPTIONS);
Map<String, Object> options = new HashMap<>();
options.put(DBPScriptObject.OPTION_OBJECT_SAVE, true);
success = saveCommandContext(monitor, options);
} else {
success = true;
}
......
......@@ -293,7 +293,9 @@ public class ObjectPropertiesEditor extends AbstractDatabaseObjectEditor<DBSObje
public void doSave(IProgressMonitor monitor)
{
for (ISaveablePart sp : nestedSaveable) {
sp.doSave(monitor);
if (sp.isDirty()) {
sp.doSave(monitor);
}
}
}
......
......@@ -53,7 +53,9 @@ public abstract class PostgreTableManagerBase extends SQLTableManager<PostgreTab
} else {
comment = table.getDescription();
}
boolean showComments = CommonUtils.getOption(options, PostgreConstants.OPTION_DDL_SHOW_COLUMN_COMMENTS);
boolean showComments =
CommonUtils.getOption(options, PostgreConstants.OPTION_DDL_SHOW_COLUMN_COMMENTS) ||
CommonUtils.getOption(options, DBPScriptObject.OPTION_OBJECT_SAVE);
if (showComments && comment != null) {
actions.add(new SQLDatabasePersistAction(
"Comment table",
......
......@@ -18,16 +18,25 @@ package org.jkiss.dbeaver.ext.postgresql.edit;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.ext.postgresql.PostgreConstants;
import org.jkiss.dbeaver.ext.postgresql.PostgreUtils;
import org.jkiss.dbeaver.ext.postgresql.model.*;
import org.jkiss.dbeaver.model.DBConstants;
import org.jkiss.dbeaver.model.DBPEvaluationContext;
import org.jkiss.dbeaver.model.DBPScriptObject;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.edit.DBECommandContext;
import org.jkiss.dbeaver.model.edit.DBEPersistAction;
import org.jkiss.dbeaver.model.impl.DBSObjectCache;
import org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction;
import org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.sql.SQLUtils;
import org.jkiss.dbeaver.model.struct.DBSEntityAssociation;
import org.jkiss.utils.CommonUtils;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
......@@ -61,9 +70,6 @@ public class PostgreViewManager extends PostgreTableManagerBase {
if (CommonUtils.isEmpty(object.getName())) {
throw new DBException("View name cannot be empty");
}
if (CommonUtils.isEmpty(((PostgreViewBase) object).getSource())) {
throw new DBException("View definition cannot be empty");
}
}
@Override
......@@ -103,12 +109,15 @@ public class PostgreViewManager extends PostgreTableManagerBase {
protected void createOrReplaceViewQuery(List<DBEPersistAction> actions, PostgreViewBase view)
{
String sql = view.getSource().trim();
if (!sql.toLowerCase(Locale.ENGLISH).startsWith("create")) {
sql = "CREATE OR REPLACE VIEW " + DBUtils.getObjectFullName(view, DBPEvaluationContext.DDL) + " AS\n" + sql;
if (!CommonUtils.isEmpty(view.getSource())) {
// Source may be empty if it wasn't yet read. Then it definitely wasn't changed
String sql = view.getSource().trim();
if (!sql.toLowerCase(Locale.ENGLISH).startsWith("create")) {
sql = "CREATE OR REPLACE VIEW " + DBUtils.getObjectFullName(view, DBPEvaluationContext.DDL) + " AS\n" + sql;
}
actions.add(
new SQLDatabasePersistAction("Create view", sql));
}
actions.add(
new SQLDatabasePersistAction("Create view", sql));
}
}
......
......@@ -18,18 +18,27 @@ package org.jkiss.dbeaver.ext.postgresql.model;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.ext.postgresql.PostgreConstants;
import org.jkiss.dbeaver.ext.postgresql.PostgreUtils;
import org.jkiss.dbeaver.model.DBPEvaluationContext;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.edit.DBEPersistAction;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.DBObjectNameCaseTransformer;
import org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
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.struct.DBSObject;
import org.jkiss.dbeaver.model.struct.rdb.DBSTableIndex;
import org.jkiss.utils.CommonUtils;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
......@@ -95,7 +104,24 @@ public abstract class PostgreViewBase extends PostgreTableReal
source = "";
}
}
return source;
List<DBEPersistAction> actions = new ArrayList<>();
if (CommonUtils.getOption(options, PostgreConstants.OPTION_DDL_SHOW_COLUMN_COMMENTS) && getDescription() != null) {
actions.add(
new SQLDatabasePersistAction("Comment",
"COMMENT ON VIEW " + getFullyQualifiedName(DBPEvaluationContext.DDL) + " IS " + SQLUtils.quoteString(this, getDescription())));
}
if (CommonUtils.getOption(options, PostgreConstants.OPTION_DDL_SHOW_PERMISSIONS)) {
PostgreUtils.getObjectGrantPermissionActions(monitor, this, actions, options);
}
StringBuilder ddl = new StringBuilder(source);
if (!actions.isEmpty()) {
ddl.append("\n\n").append(SQLUtils.generateScript(
getDataSource(), actions.toArray(new DBEPersistAction[actions.size()]), false));
}
return ddl.toString();
}
protected String readExtraDefinition(JDBCSession session, Map<String, Object> options) throws DBException {
......@@ -110,4 +136,9 @@ public abstract class PostgreViewBase extends PostgreTableReal
public abstract String getViewType();
@Override
public DBSObject refreshObject(DBRProgressMonitor monitor) throws DBException {
this.source = null;
return super.refreshObject(monitor);
}
}
......@@ -46,6 +46,9 @@ public interface DBPScriptObject extends DBPObject {
// nested objects (columns, constraints, etc) which can be embedded in parent object declaration (tables)
String OPTION_EMBEDDED_SOURCE = "embedded.source";
// Means that result script will be used for object save
String OPTION_OBJECT_SAVE = "object.save";
Map<String, Object> EMPTY_OPTIONS = Collections.unmodifiableMap(new HashMap<>());
String getObjectDefinitionText(DBRProgressMonitor monitor, Map<String, Object> options)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册