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

#3189 Triggers in DDL. SQL script generation enhancement. Comment lines

上级 e1ad93ed
......@@ -27,6 +27,7 @@ import org.jkiss.dbeaver.model.edit.DBEObjectRenamer;
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.impl.sql.edit.struct.SQLTableManager;
import org.jkiss.dbeaver.model.messages.ModelMessages;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
......@@ -34,6 +35,7 @@ import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.sql.SQLUtils;
import org.jkiss.utils.CommonUtils;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
......@@ -86,31 +88,62 @@ public class PostgreTableManager extends SQLTableManager<PostgreTableBase, Postg
@Override
protected void addObjectExtraActions(List<DBEPersistAction> actions, NestedObjectCommand<PostgreTableBase, PropertyHandler> command, Map<String, Object> options) {
PostgreTableBase table = command.getObject();
// Add comments
if (!CommonUtils.isEmpty(command.getObject().getDescription())) {
if (!CommonUtils.isEmpty(table.getDescription())) {
actions.add(new SQLDatabasePersistAction(
"Comment table",
"COMMENT ON TABLE " + command.getObject().getFullyQualifiedName(DBPEvaluationContext.DDL) +
" IS " + SQLUtils.quoteString(command.getObject(), command.getObject().getDescription())));
"COMMENT ON TABLE " + table.getFullyQualifiedName(DBPEvaluationContext.DDL) +
" IS " + SQLUtils.quoteString(table, table.getDescription())));
}
DBRProgressMonitor monitor = new VoidProgressMonitor();
try {
for (PostgreTableColumn column : command.getObject().getAttributes(monitor)) {
if (!CommonUtils.isEmpty(column.getDescription())) {
actions.add(new SQLDatabasePersistAction("Set column comment", "COMMENT ON COLUMN " +
DBUtils.getObjectFullName(command.getObject(), DBPEvaluationContext.DDL) + "." + DBUtils.getQuotedIdentifier(column) +
" IS " + SQLUtils.quoteString(column, column.getDescription())));
{
// 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"));
}
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;
}
}
}
for (PostgrePermission permission : command.getObject().getPermissions(monitor)) {
if (permission.hasAllPrivileges(command.getObject())) {
Collections.addAll(actions,
new PostgreCommandGrantPrivilege(permission.getOwner(), true, permission, PostgrePrivilegeType.ALL)
.getPersistActions(options));
} else {
for (PostgrePermission.ObjectPermission op : permission.getPermissions()) {
PostgreCommandGrantPrivilege grant = new PostgreCommandGrantPrivilege(permission.getOwner(), true, permission, op.getPrivilegeType());
Collections.addAll(actions, grant.getPersistActions(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)));
}
}
}
{
// 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 {
for (PostgrePermission.ObjectPermission op : permission.getPermissions()) {
PostgreCommandGrantPrivilege grant = new PostgreCommandGrantPrivilege(permission.getOwner(), true, permission, op.getPrivilegeType());
Collections.addAll(actions, grant.getPersistActions(options));
}
}
}
}
}
......
......@@ -29,7 +29,8 @@ public interface DBEPersistAction {
INITIALIZER,
NORMAL,
OPTIONAL,
FINALIZER
FINALIZER,
COMMENT
}
String getTitle();
......
......@@ -163,6 +163,10 @@ public abstract class AbstractCommandContext implements DBECommandContext {
DBException error = null;
for (PersistInfo persistInfo : cmd.persistActions) {
DBEPersistAction.ActionType actionType = persistInfo.action.getType();
if (actionType == DBEPersistAction.ActionType.COMMENT) {
// Skip pure comments
continue;
}
if (persistInfo.executed && actionType == DBEPersistAction.ActionType.NORMAL) {
continue;
}
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.model.impl.edit;
import org.jkiss.dbeaver.model.sql.SQLDataSource;
/**
* Comment action
*/
public class SQLDatabasePersistActionComment extends SQLDatabasePersistAction {
public SQLDatabasePersistActionComment(SQLDataSource dataSource, String comment) {
super("Comment",
"\n" + dataSource.getSQLDialect().getSingleLineComments()[0] + " " + comment + "\n",
ActionType.COMMENT,
false);
}
}
......@@ -806,7 +806,14 @@ public final class SQLUtils {
script.append(delimiter).append(lineSeparator);
}
script.append(scriptLine);
script.append(" ").append(delimiter).append(lineSeparator);
if (action.getType() != DBEPersistAction.ActionType.COMMENT) {
char lastChar = scriptLine.charAt(scriptLine.length() - 1);
if (!Character.isWhitespace(lastChar) && !Character.isLetterOrDigit(lastChar)) {
script.append(" ");
}
script.append(delimiter);
}
script.append(lineSeparator);
if (action.isComplex() && redefiner != null) {
script.append(redefiner).append(" ").append(sqlDialect.getScriptDelimiter()).append(lineSeparator);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册