/* * DBeaver - Universal Database Manager * Copyright (C) 2016-2016 Karl Griesser (fullref@gmail.com) * 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.ext.exasol.manager; import org.jkiss.code.Nullable; import org.jkiss.dbeaver.DBException; import org.jkiss.dbeaver.ext.exasol.model.ExasolTable; import org.jkiss.dbeaver.ext.exasol.model.ExasolTableBase; import org.jkiss.dbeaver.ext.exasol.model.ExasolTableColumn; import org.jkiss.dbeaver.model.DBPEvaluationContext; import org.jkiss.dbeaver.model.DBUtils; import org.jkiss.dbeaver.model.edit.DBECommandContext; 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.sql.edit.struct.SQLTableColumnManager; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; import org.jkiss.dbeaver.model.struct.DBSObject; import org.jkiss.utils.CommonUtils; import java.math.BigDecimal; import java.util.List; /** * Exasol Table Column Manager * * @author Karl Griesser */ public class ExasolTableColumnManager extends SQLTableColumnManager<ExasolTableColumn, ExasolTableBase> implements DBEObjectRenamer<ExasolTableColumn> { private static final String SQL_ALTER = "ALTER TABLE %s MODIFY COLUMN %s "; private static final String SQL_COMMENT = "COMMENT ON COLUMN %s.%s IS '%s'"; private static final String CMD_ALTER = "Alter Column"; private static final String CMD_COMMENT = "Comment on Column"; // ----------------- // Business Contract // ----------------- @Nullable @Override public DBSObjectCache<? extends DBSObject, ExasolTableColumn> getObjectsCache(ExasolTableColumn object) { return object.getParentObject().getContainer().getTableCache().getChildrenCache((ExasolTable) object.getParentObject()); } @Override public boolean canEditObject(ExasolTableColumn object) { // Edit is only availabe for ExasolTable and not for other kinds of tables (View, MQTs, Nicknames..) ExasolTableBase exasolTableBase = object.getParentObject(); if ((exasolTableBase != null) & (exasolTableBase.getClass().equals(ExasolTable.class))) { return true; } else { return false; } } // ------ // Create // ------ @Override protected ExasolTableColumn createDatabaseObject(DBRProgressMonitor monitor, DBECommandContext context, ExasolTableBase parent, Object copyFrom) { ExasolTableColumn column = new ExasolTableColumn(parent); column.setName(getNewColumnName(monitor, context, parent)); return column; } // ----- // Alter // ----- @Override protected void addObjectModifyActions(List<DBEPersistAction> actionList, ObjectChangeCommand command) { ExasolTableColumn exasolColumn = command.getObject(); if (!command.getProperties().isEmpty()) { // build nullability tring String nullability = ""; if (exasolColumn.isOriRequired() != null && exasolColumn.isOriRequired() != exasolColumn.isRequired()) nullability = exasolColumn.isRequired() ? "NOT NULL" : "NULL"; final String deltaSQL = exasolColumn.getName() + " " + exasolColumn.getFormatType() + " " + (exasolColumn.getDefaultValue() == null ? "" : " DEFAULT " + exasolColumn.getDefaultValue()) + " " + formatIdentiy(exasolColumn.isAutoGenerated(), exasolColumn.getIdentityValue()) + " " + nullability; if (!deltaSQL.isEmpty()) { String sqlAlterColumn = String.format(SQL_ALTER, exasolColumn.getTable().getFullyQualifiedName(DBPEvaluationContext.DDL), deltaSQL); actionList.add(new SQLDatabasePersistAction(CMD_ALTER, sqlAlterColumn)); } } // Comment DBEPersistAction commentAction = buildCommentAction(exasolColumn); if (commentAction != null) { actionList.add(commentAction); } } private String formatIdentiy(Boolean isAutoGenerated,BigDecimal identityValue) { String ret = ""; if (isAutoGenerated) { ret = "IDENTITY "; if (identityValue != null) { ret = ret + identityValue.toString() + " "; } } return ret; } // ------- // Helpers // ------- private DBEPersistAction buildCommentAction(ExasolTableColumn exasolColumn) { if (CommonUtils.isNotEmpty(exasolColumn.getDescription())) { String tableName = exasolColumn.getTable().getFullyQualifiedName(DBPEvaluationContext.DDL); String columnName = exasolColumn.getName(); String comment = exasolColumn.getDescription(); String commentSQL = String.format(SQL_COMMENT, tableName, columnName, comment); return new SQLDatabasePersistAction(CMD_COMMENT, commentSQL); } else { return null; } } @Override public void renameObject(DBECommandContext commandContext, ExasolTableColumn object, String newName) throws DBException { processObjectRename(commandContext, object, newName); } @Override protected void addObjectRenameActions(List<DBEPersistAction> actions, ObjectRenameCommand command) { final ExasolTableColumn column = command.getObject(); actions.add( new SQLDatabasePersistAction( "Rename column", "ALTER TABLE " + column.getTable().getFullyQualifiedName(DBPEvaluationContext.DDL) + " RENAME COLUMN " + DBUtils.getQuotedIdentifier(column.getDataSource(), command.getOldName()) + " TO " + DBUtils.getQuotedIdentifier(column.getDataSource(), command.getNewName())) ); } }