/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
* Copyright (C) 2011-2012 Eugene Fradkin (eugene.fradkin@gmail.com)
*
* 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.oracle.edit;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.ext.oracle.model.OracleDataType;
import org.jkiss.dbeaver.ext.oracle.model.OracleTableBase;
import org.jkiss.dbeaver.ext.oracle.model.OracleTableColumn;
import org.jkiss.dbeaver.model.DBPDataKind;
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.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSDataType;
import org.jkiss.dbeaver.model.struct.DBSObject;
import java.sql.Types;
import java.util.List;
/**
* Oracle table column manager
*/
public class OracleTableColumnManager extends SQLTableColumnManager<OracleTableColumn, OracleTableBase> implements DBEObjectRenamer<OracleTableColumn> {
@Nullable
@Override
public DBSObjectCache<? extends DBSObject, OracleTableColumn> getObjectsCache(OracleTableColumn object)
{
return object.getParentObject().getContainer().tableCache.getChildrenCache(object.getParentObject());
}
protected ColumnModifier[] getSupportedModifiers(OracleTableColumn column)
{
return new ColumnModifier[] {DataTypeModifier, DefaultModifier, NullNotNullModifierConditional};
}
@Override
protected OracleTableColumn createDatabaseObject(DBRProgressMonitor monitor, DBECommandContext context, OracleTableBase parent, Object copyFrom)
{
DBSDataType columnType = findBestDataType(parent.getDataSource(), "varchar2"); //$NON-NLS-1$
final OracleTableColumn column = new OracleTableColumn(parent);
column.setName(getNewColumnName(monitor, context, parent));
column.setDataType((OracleDataType) columnType);
column.setTypeName(columnType == null ? "INTEGER" : columnType.getName()); //$NON-NLS-1$
column.setMaxLength(columnType != null && columnType.getDataKind() == DBPDataKind.STRING ? 100 : 0);
column.setValueType(columnType == null ? Types.INTEGER : columnType.getTypeID());
column.setOrdinalPosition(-1);
return column;
}
@Override
protected void addObjectModifyActions(List<DBEPersistAction> actionList, ObjectChangeCommand command)
{
final OracleTableColumn column = command.getObject();
boolean hasComment = command.getProperty("comment") != null;
if (!hasComment || command.getProperties().size() > 1) {
actionList.add(new SQLDatabasePersistAction(
"Modify column",
"ALTER TABLE " + column.getTable().getFullyQualifiedName(DBPEvaluationContext.DDL) + //$NON-NLS-1$
" MODIFY " + getNestedDeclaration(column.getTable(), command))); //$NON-NLS-1$
}
if (hasComment) {
actionList.add(new SQLDatabasePersistAction(
"Comment column",
"COMMENT ON COLUMN " + column.getTable().getFullyQualifiedName(DBPEvaluationContext.DDL) + "." + DBUtils.getQuotedIdentifier(column) +
" IS '" + column.getComment(new VoidProgressMonitor()) + "'"));
}
}
@Override
public void renameObject(DBECommandContext commandContext, OracleTableColumn object, String newName) throws DBException {
processObjectRename(commandContext, object, newName);
}
@Override
protected void addObjectRenameActions(List<DBEPersistAction> actions, ObjectRenameCommand command)
{
final OracleTableColumn 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()))
);
}
}