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

#3024 PG: constraint comments (read/write, table DDL)

上级 386f020d
......@@ -47,7 +47,7 @@ meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreAttribute.dataType.name=Data
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreAttribute.dataType.description=
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreAttribute.defaultValue.name=Default
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreAttribute.defaultValue.description=
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreAttribute.description.name=Description
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreAttribute.description.name=Comment
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreAttribute.description.description=
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreAttribute.maxLength.name=Length
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreAttribute.maxLength.description=
......@@ -204,7 +204,7 @@ meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreIndex.checkXMin.name=Check X
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreIndex.checkXMin.description=
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreIndex.clustered.name=Clustered
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreIndex.clustered.description=
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreIndex.description.name=Description
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreIndex.description.name=Comment
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreIndex.description.description=
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreIndex.exclusion.name=Exclusion
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreIndex.exclusion.description=
......@@ -294,7 +294,7 @@ meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreSchema.database.name=Database
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreSchema.database.description=
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreSchema.name.name=Name
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreSchema.name.description=
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreSchema.description.name=Description
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreSchema.description.name=Comment
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreSchema.description.description=Schema comment
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreSchema.owner.name=Owner
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreSchema.owner.description=
......@@ -320,10 +320,12 @@ meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreTableBase.objectId.name=ID
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreTableBase.objectId.description=
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreTableBase.owner.name=Owner
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreTableBase.owner.description=Table owner (creator)
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreTableBase.description.name=Description
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreTableBase.description.name=Comment
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreTableBase.description.description=
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreTableConstraintBase.objectDefinitionText.name=DDL
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreTableConstraintBase.objectDefinitionText.description=Constraint definition text
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreTableConstraintBase.description.name=Comment
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreTableConstraintBase.description.description=Constraint comment
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreTableConstraint.source.name=Expression
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreTableConstraint.source.description=Expression of CHECK constraints
meta.org.jkiss.dbeaver.ext.postgresql.model.PostgreTableConstraintColumn.attribute.name=Attribute
......
......@@ -20,13 +20,19 @@ import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
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.DBECommandAbstract;
import org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction;
import org.jkiss.dbeaver.model.impl.sql.edit.struct.SQLConstraintManager;
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.DBSEntityAttribute;
import org.jkiss.dbeaver.model.struct.DBSEntityConstraintType;
import org.jkiss.dbeaver.ui.UITask;
......@@ -34,6 +40,7 @@ import org.jkiss.dbeaver.ui.editors.object.struct.EditConstraintPage;
import org.jkiss.utils.CommonUtils;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
......@@ -123,6 +130,21 @@ public class PostgreConstraintManager extends SQLConstraintManager<PostgreTableC
}
}
@Override
protected void addObjectModifyActions(List<DBEPersistAction> actionList, ObjectChangeCommand command, Map<String, Object> options)
{
if (command.getProperty(DBConstants.PROP_ID_DESCRIPTION) != null) {
addConstraintCommentAction(actionList, command.getObject());
}
}
static void addConstraintCommentAction(List<DBEPersistAction> actionList, PostgreTableConstraintBase constr) {
actionList.add(new SQLDatabasePersistAction(
"Comment sequence",
"COMMENT ON CONSTRAINT " + DBUtils.getQuotedIdentifier(constr) + " ON " + constr.getTable().getFullyQualifiedName(DBPEvaluationContext.DDL) +
" IS " + SQLUtils.quoteString(constr, constr.getDescription())));
}
@Override
protected String getDropConstraintPattern(PostgreTableConstraintBase constraint)
{
......
......@@ -140,10 +140,14 @@ public class PostgreIndexManager extends SQLIndexManager<PostgreIndex, PostgreTa
super.addObjectCreateActions(actions, command, options);
}
if (!CommonUtils.isEmpty(index.getDescription())) {
actions.add(new SQLDatabasePersistAction(
"Comment index",
"COMMENT ON INDEX " + index.getFullyQualifiedName(DBPEvaluationContext.DDL) +
" IS " + SQLUtils.quoteString(index, index.getDescription())));
addIndexCommentAction(actions, index);
}
}
static void addIndexCommentAction(List<DBEPersistAction> actions, PostgreIndex index) {
actions.add(new SQLDatabasePersistAction(
"Comment index",
"COMMENT ON INDEX " + index.getFullyQualifiedName(DBPEvaluationContext.DDL) +
" IS " + SQLUtils.quoteString(index, index.getDescription())));
}
}
......@@ -20,6 +20,7 @@ import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.ext.postgresql.PostgreConstants;
import org.jkiss.dbeaver.ext.postgresql.model.*;
import org.jkiss.dbeaver.model.DBConstants;
import org.jkiss.dbeaver.model.DBPEvaluationContext;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.edit.DBECommandContext;
......@@ -213,13 +214,17 @@ public class PostgreTableColumnManager extends SQLTableColumnManager<PostgreTabl
actionList.add(new SQLDatabasePersistAction("Set column default", prefix + "SET DEFAULT " + column.getDefaultValue()));
}
}
if (command.getProperty("description") != null) {
actionList.add(new SQLDatabasePersistAction("Set column comment", "COMMENT ON COLUMN " +
DBUtils.getObjectFullName(column.getTable(), DBPEvaluationContext.DDL) + "." + DBUtils.getQuotedIdentifier(column) +
" IS " + SQLUtils.quoteString(column, CommonUtils.notEmpty(column.getDescription()))));
if (command.getProperty(DBConstants.PROP_ID_DESCRIPTION) != null) {
addColumnCommentAction(actionList, column);
}
}
static void addColumnCommentAction(List<DBEPersistAction> actionList, PostgreAttribute column) {
actionList.add(new SQLDatabasePersistAction("Set column comment", "COMMENT ON COLUMN " +
DBUtils.getObjectFullName(column.getTable(), DBPEvaluationContext.DDL) + "." + DBUtils.getQuotedIdentifier(column) +
" IS " + SQLUtils.quoteString(column, CommonUtils.notEmpty(column.getDescription()))));
}
@Override
public void renameObject(DBECommandContext commandContext, PostgreTableColumn object, String newName) throws DBException {
processObjectRename(commandContext, object, newName);
......
......@@ -53,54 +53,68 @@ public abstract class PostgreTableManagerBase extends SQLTableManager<PostgreTab
" IS " + SQLUtils.quoteString(table, table.getDescription())));
}
DBRProgressMonitor monitor = new VoidProgressMonitor();
try {
if (isDDL) {
// Column comments
boolean hasComments = false;
for (PostgreTableColumn column : table.getAttributes(monitor)) {
if (!CommonUtils.isEmpty(column.getDescription())) {
if (!hasComments) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Column comments"));
if (isDDL) {
try {
{
// Column comments
boolean hasComments = false;
for (PostgreTableColumn column : table.getAttributes(monitor)) {
if (!CommonUtils.isEmpty(column.getDescription())) {
if (!hasComments) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Column comments"));
}
PostgreTableColumnManager.addColumnCommentAction(actions, column);
hasComments = true;
}
actions.add(new SQLDatabasePersistAction("Set column comment", "COMMENT ON COLUMN " +
DBUtils.getObjectFullName(table, DBPEvaluationContext.DDL) + "." + DBUtils.getQuotedIdentifier(column) +
" IS " + SQLUtils.quoteString(column, column.getDescription())));
hasComments = true;
}
}
}
// Triggers
if (isDDL && table instanceof PostgreTableReal) {
Collection<PostgreTrigger> triggers = ((PostgreTableReal) table).getTriggers(monitor);
if (!CommonUtils.isEmpty(triggers)) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Table Triggers"));
{
// Constraint comments
boolean hasComments = false;
for (PostgreTableConstraintBase constr : table.getConstraints(monitor)) {
if (!CommonUtils.isEmpty(constr.getDescription())) {
if (!hasComments) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Constraint comments"));
}
PostgreConstraintManager.addConstraintCommentAction(actions, constr);
hasComments = true;
}
}
}
for (PostgreTrigger trigger : triggers) {
actions.add(new SQLDatabasePersistAction("Create trigger", trigger.getObjectDefinitionText(monitor, options)));
// Triggers
if (table instanceof PostgreTableReal) {
Collection<PostgreTrigger> triggers = ((PostgreTableReal) table).getTriggers(monitor);
if (!CommonUtils.isEmpty(triggers)) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Table Triggers"));
for (PostgreTrigger trigger : triggers) {
actions.add(new SQLDatabasePersistAction("Create trigger", trigger.getObjectDefinitionText(monitor, options)));
}
}
}
}
if (isDDL && CommonUtils.getOption(options, PostgreConstants.OPTION_DDL_SHOW_PERMISSIONS)) {
// Permissions
Collection<PostgrePermission> permissions = table.getPermissions(monitor);
if (!CommonUtils.isEmpty(permissions)) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Permissions"));
for (PostgrePermission permission : permissions) {
if (permission.hasAllPrivileges(table)) {
Collections.addAll(actions,
new PostgreCommandGrantPrivilege(permission.getOwner(), true, permission, PostgrePrivilegeType.ALL)
.getPersistActions(options));
} else {
PostgreCommandGrantPrivilege grant = new PostgreCommandGrantPrivilege(permission.getOwner(), true, permission, permission.getPrivileges());
Collections.addAll(actions, grant.getPersistActions(options));
if (CommonUtils.getOption(options, PostgreConstants.OPTION_DDL_SHOW_PERMISSIONS)) {
// Permissions
Collection<PostgrePermission> permissions = table.getPermissions(monitor);
if (!CommonUtils.isEmpty(permissions)) {
actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Permissions"));
for (PostgrePermission permission : permissions) {
if (permission.hasAllPrivileges(table)) {
Collections.addAll(actions,
new PostgreCommandGrantPrivilege(permission.getOwner(), true, permission, PostgrePrivilegeType.ALL)
.getPersistActions(options));
} else {
PostgreCommandGrantPrivilege grant = new PostgreCommandGrantPrivilege(permission.getOwner(), true, permission, permission.getPrivileges());
Collections.addAll(actions, grant.getPersistActions(options));
}
}
}
}
} catch (DBException e) {
log.error(e);
}
} catch (DBException e) {
log.error(e);
}
}
}
......@@ -500,10 +500,11 @@ public class PostgreSchema implements DBSSchema, DBPNamedObject2, DBPSaveableObj
@Override
protected JDBCStatement prepareObjectsStatement(JDBCSession session, PostgreSchema schema, PostgreTableBase forParent) throws SQLException {
StringBuilder sql = new StringBuilder(
"SELECT c.oid,c.*,t.relname as tabrelname,rt.relnamespace as refnamespace" +
"SELECT c.oid,c.*,t.relname as tabrelname,rt.relnamespace as refnamespace,d.description" +
"\nFROM pg_catalog.pg_constraint c" +
"\nINNER JOIN pg_catalog.pg_class t ON t.oid=c.conrelid" +
"\nLEFT OUTER JOIN pg_catalog.pg_class rt ON rt.oid=c.confrelid" +
"\nLEFT OUTER JOIN pg_catalog.pg_description d ON d.objoid=c.oid AND d.objsubid=0" +
"\nWHERE ");
if (forParent == null) {
sql.append("t.relnamespace=?");
......
......@@ -17,6 +17,7 @@
package org.jkiss.dbeaver.ext.postgresql.model;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPEvaluationContext;
......@@ -48,6 +49,7 @@ public abstract class PostgreTableConstraintBase extends JDBCTableConstraint<Pos
super(table, name, null, constraintType, true);
this.oid = JDBCUtils.safeGetLong(resultSet, "oid");
this.description = JDBCUtils.safeGetString(resultSet, "description");
}
public PostgreTableConstraintBase(PostgreTableBase table, String constraintName, DBSEntityConstraintType constraintType) {
......@@ -82,6 +84,14 @@ public abstract class PostgreTableConstraintBase extends JDBCTableConstraint<Pos
return oid;
}
@Property(viewable = true, editable = true, updatable = true, order = 100)
@Nullable
@Override
public String getDescription()
{
return super.getDescription();
}
abstract void cacheAttributes(DBRProgressMonitor monitor, List<? extends PostgreTableConstraintColumn> children, boolean secondPass);
@Override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册