use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class RenameConstantAction method execGutsRenameColumn.
// do necessary work for rename column at execute time.
private void execGutsRenameColumn(TableDescriptor td, Activation activation) throws StandardException {
ColumnDescriptor columnDescriptor = null;
int columnPosition = 0;
ConstraintDescriptorList constraintDescriptorList;
ConstraintDescriptor constraintDescriptor;
LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
DataDictionary dd = lcc.getDataDictionary();
DependencyManager dm = dd.getDependencyManager();
TransactionController tc = lcc.getTransactionExecute();
/* get the column descriptor for column to be renamed and
* using it's position in the table, set the referenced
* column map of the table indicating which column is being
* renamed. Dependency Manager uses this to find out the
* dependents on the column.
*/
columnDescriptor = td.getColumnDescriptor(oldObjectName);
if (columnDescriptor.isAutoincrement())
columnDescriptor.setAutoinc_create_or_modify_Start_Increment(ColumnDefinitionNode.CREATE_AUTOINCREMENT);
columnPosition = columnDescriptor.getPosition();
FormatableBitSet toRename = new FormatableBitSet(td.getColumnDescriptorList().size() + 1);
toRename.set(columnPosition);
td.setReferencedColumnMap(toRename);
dm.invalidateFor(td, DependencyManager.RENAME, lcc);
// look for foreign key dependency on the column.
constraintDescriptorList = dd.getConstraintDescriptors(td);
for (int index = 0; index < constraintDescriptorList.size(); index++) {
constraintDescriptor = constraintDescriptorList.elementAt(index);
int[] referencedColumns = constraintDescriptor.getReferencedColumns();
int numRefCols = referencedColumns.length;
for (int j = 0; j < numRefCols; j++) {
if ((referencedColumns[j] == columnPosition) && (constraintDescriptor instanceof ReferencedKeyConstraintDescriptor))
dm.invalidateFor(constraintDescriptor, DependencyManager.RENAME, lcc);
}
}
// Drop the column
dd.dropColumnDescriptor(td.getUUID(), oldObjectName, tc);
columnDescriptor.setColumnName(newObjectName);
dd.addDescriptor(columnDescriptor, td, DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
// Need to do following to reload the cache so that table
// descriptor now has new column name
td = dd.getTableDescriptor(td.getObjectID());
}
use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class MatchingClauseNode method buildFullColumnList.
/**
* <p>
* Build the full column list for a table.
* </p>
*/
private ResultColumnList buildFullColumnList(TableDescriptor td) throws StandardException {
ResultColumnList result = new ResultColumnList(getContextManager());
ColumnDescriptorList cdl = td.getColumnDescriptorList();
int cdlSize = cdl.size();
for (int index = 0; index < cdlSize; index++) {
ColumnDescriptor colDesc = cdl.elementAt(index);
ColumnReference columnRef = new ColumnReference(colDesc.getColumnName(), null, getContextManager());
ResultColumn resultColumn = new ResultColumn(columnRef, null, getContextManager());
result.addResultColumn(resultColumn);
}
return result;
}
use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class MatchingClauseNode method buildThenColumnsForInsert.
/**
* <p>
* Construct the row in the temporary table which drives an INSERT action.
* Unlike a DELETE, whose temporary row is just a list of copied columns, the
* temporary row for INSERT may contain complex expressions which must
* be code-generated later on.
* </p>
*/
private void buildThenColumnsForInsert(FromList fullFromList, FromTable targetTable, ResultColumnList fullRow, ResultColumnList insertColumns, ResultColumnList insertValues) throws StandardException {
//
// Don't add USAGE privilege on user-defined types just because we're
// building the THEN columns.
//
boolean wasSkippingTypePrivileges = getCompilerContext().skipTypePrivileges(true);
TableDescriptor td = targetTable.getTableDescriptor();
_thenColumns = fullRow.copyListAndObjects();
//
for (int i = 0; i < _thenColumns.size(); i++) {
ResultColumn origRC = _thenColumns.elementAt(i);
String columnName = origRC.getName();
ColumnDescriptor cd = td.getColumnDescriptor(columnName);
boolean changed = false;
//
if (!origRC.isAutoincrement() && (origRC.getExpression() instanceof VirtualColumnNode)) {
origRC.setExpression(new UntypedNullConstantNode(getContextManager()));
}
if (cd.hasGenerationClause()) {
origRC.setExpression(new UntypedNullConstantNode(getContextManager()));
continue;
}
for (int ic = 0; ic < insertColumns.size(); ic++) {
ResultColumn icRC = insertColumns.elementAt(ic);
if (columnName.equals(icRC.getName())) {
ResultColumn newRC = null;
// replace DEFAULT for a generated or identity column
ResultColumn valueRC = insertValues.elementAt(ic);
if (valueRC.wasDefaultColumn() || (valueRC.getExpression() instanceof UntypedNullConstantNode)) {
if (!cd.isAutoincrement()) {
//
// Eliminate column references under identity columns. They
// will mess up the code generation.
//
ValueNode expr = origRC.getExpression();
if (expr instanceof ColumnReference) {
origRC.setExpression(new UntypedNullConstantNode(getContextManager()));
}
continue;
}
newRC = makeAutoGenRC(targetTable, origRC, i + 1);
} else {
newRC = valueRC.cloneMe();
newRC.setType(origRC.getTypeServices());
}
newRC.setVirtualColumnId(origRC.getVirtualColumnId());
_thenColumns.setElementAt(newRC, i);
changed = true;
break;
}
}
// plug in defaults if we haven't done so already
if (!changed) {
DefaultInfoImpl defaultInfo = (DefaultInfoImpl) cd.getDefaultInfo();
if ((defaultInfo != null) && !defaultInfo.isGeneratedColumn() && !cd.isAutoincrement()) {
_thenColumns.setDefault(origRC, cd, defaultInfo);
changed = true;
}
}
// set the result column name correctly for buildThenColumnSignature()
ResultColumn finalRC = _thenColumns.elementAt(i);
finalRC.setName(cd.getColumnName());
}
// end loop through _thenColumns
getCompilerContext().skipTypePrivileges(wasSkippingTypePrivileges);
}
use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class MatchingClauseNode method buildThenColumnsForUpdate.
/**
* <p>
* Construct the row in the temporary table which drives an UPDATE action.
* Unlike a DELETE, whose temporary row is just a list of copied columns, the
* temporary row for UPDATE may contain complex expressions which must
* be code-generated later on.
* </p>
*/
private void buildThenColumnsForUpdate(FromList fullFromList, FromTable targetTable, ResultColumnList fullRow, ResultColumnList beforeRow, ResultColumnList afterValues) throws StandardException {
TableDescriptor td = targetTable.getTableDescriptor();
HashSet<String> changedColumns = getChangedColumnNames();
HashSet<String> changedGeneratedColumns = getChangedGeneratedColumnNames(td, changedColumns);
_thenColumns = fullRow.copyListAndObjects();
//
for (int i = 0; i < _thenColumns.size(); i++) {
ResultColumn origRC = _thenColumns.elementAt(i);
boolean isAfterColumn = (i >= beforeRow.size());
// skip the final RowLocation column of an UPDATE
boolean isRowLocation = isRowLocation(origRC);
ValueNode origExpr = origRC.getExpression();
if (isRowLocation) {
continue;
}
String columnName = origRC.getName();
ColumnDescriptor cd = td.getColumnDescriptor(columnName);
boolean changed = false;
//
if (cd.isAutoincrement() && (origRC.getExpression() instanceof NumericConstantNode)) {
DataValueDescriptor numericValue = ((NumericConstantNode) origRC.getExpression()).getValue();
if (numericValue == null) {
ResultColumn newRC = makeAutoGenRC(targetTable, origRC, i + 1);
newRC.setVirtualColumnId(origRC.getVirtualColumnId());
_thenColumns.setElementAt(newRC, i);
continue;
}
}
//
if (!origRC.isAutoincrement() && (origRC.getExpression() instanceof VirtualColumnNode)) {
origRC.setExpression(new UntypedNullConstantNode(getContextManager()));
}
//
if (cd.hasGenerationClause()) {
if (isAfterColumn && changedGeneratedColumns.contains(columnName)) {
// Set the expression to something that won't choke ResultColumnList.generateEvaluatedRow().
// The value will be a Java null at execution time, which will cause the value
// to be re-generated.
origRC.setExpression(new UntypedNullConstantNode(getContextManager()));
} else {
ColumnReference cr = new ColumnReference(columnName, targetTable.getTableName(), getContextManager());
origRC.setExpression(cr);
// remove the column descriptor in order to turn off hasGenerationClause()
origRC.setColumnDescriptor(null, null);
}
continue;
}
if (isAfterColumn) {
for (int ic = 0; ic < beforeRow.size(); ic++) {
ResultColumn icRC = beforeRow.elementAt(ic);
if (columnName.equals(icRC.getName())) {
ResultColumn newRC = null;
// replace DEFAULT for a generated or identity column
ResultColumn valueRC = afterValues.elementAt(ic);
if (valueRC.wasDefaultColumn() || (valueRC.getExpression() instanceof UntypedNullConstantNode)) {
if (!cd.isAutoincrement()) {
//
// Eliminate column references under identity columns. They
// will mess up the code generation.
//
ValueNode expr = origRC.getExpression();
if (expr instanceof ColumnReference) {
origRC.setExpression(new UntypedNullConstantNode(getContextManager()));
}
continue;
}
newRC = makeAutoGenRC(targetTable, origRC, i + 1);
} else {
newRC = valueRC.cloneMe();
newRC.setType(origRC.getTypeServices());
}
newRC.setVirtualColumnId(origRC.getVirtualColumnId());
_thenColumns.setElementAt(newRC, i);
changed = true;
break;
}
}
}
// plug in defaults if we haven't done so already
if (!changed) {
DefaultInfoImpl defaultInfo = (DefaultInfoImpl) cd.getDefaultInfo();
if ((defaultInfo != null) && !defaultInfo.isGeneratedColumn() && !cd.isAutoincrement()) {
_thenColumns.setDefault(origRC, cd, defaultInfo);
changed = true;
}
}
// set the result column name correctly for buildThenColumnSignature()
ResultColumn finalRC = _thenColumns.elementAt(i);
finalRC.setName(cd.getColumnName());
//
// Turn off the autogenerated bit for identity columns so that
// ResultColumnList.generateEvaluatedRow() doesn't try to compile
// code to generate values for the before images in UPDATE rows.
// This logic will probably need to be revisited as part of fixing derby-6414.
//
finalRC.resetAutoincrementGenerated();
}
// end loop through _thenColumns
}
use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class FromVTI method createResultColumnsForTableFunction.
/**
* Add result columns for a Derby-style Table Function
*/
private void createResultColumnsForTableFunction(TypeDescriptor td) throws StandardException {
String[] columnNames = td.getRowColumnNames();
TypeDescriptor[] types = td.getRowTypes();
for (int i = 0; i < columnNames.length; i++) {
String columnName = columnNames[i];
DataTypeDescriptor dtd = DataTypeDescriptor.getType(types[i]);
ResultColumn rc = getResultColumns().addColumn(exposedName, columnName, dtd);
//
// Stuff a column descriptor into the ResultColumn. We do this so that
// getColumnPosition() will return the column position within the
// table function's shape. Later on, projection may remove columns
// from the ResultColumnList. We don't want getColumnPosition() to say
// that the column position is the index into the abbreviated ResultColumnList.
// See DERBY-6040.
//
ColumnDescriptor coldesc = new ColumnDescriptor(columnName, i + 1, dtd, (DataValueDescriptor) null, (DefaultInfo) null, (UUID) null, (UUID) null, 0L, 0L, 0L, false);
rc.setColumnDescriptor(null, coldesc);
}
}
Aggregations