use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class ResultSetNode method genNewRCForInsert.
/**
* Generate the RC/expression for an unspecified column in an insert.
* Use the default if one exists.
*
* @param targetTD Target TableDescriptor if the target is not a VTI, null if a VTI.
* @param targetVTI Target description if it is a VTI, null if not a VTI
* @param columnNumber The column number
* @param dataDictionary The DataDictionary
* @return The RC/expression for the unspecified column.
*
* @exception StandardException Thrown on error
*/
private ResultColumn genNewRCForInsert(TableDescriptor targetTD, FromVTI targetVTI, int columnNumber, DataDictionary dataDictionary) throws StandardException {
ResultColumn newResultColumn;
if (targetVTI != null) {
newResultColumn = targetVTI.getResultColumns().getResultColumn(columnNumber);
newResultColumn = newResultColumn.cloneMe();
newResultColumn.setExpressionToNullNode();
} else {
// column position is 1-based, index is 0-based.
ColumnDescriptor colDesc = targetTD.getColumnDescriptor(columnNumber);
DataTypeDescriptor colType = colDesc.getType();
// Check for defaults
DefaultInfoImpl defaultInfo = (DefaultInfoImpl) colDesc.getDefaultInfo();
// if it have defaultInfo and not be autoincrement.
if (defaultInfo != null && !colDesc.isAutoincrement()) {
// RESOLVEPARAMETER - skip the tree if we have the value
/*
if (defaultInfo.getDefaultValue() != null)
{
}
else
*/
{
if (colDesc.hasGenerationClause()) {
// later on we will revisit the generated columns and bind
// their generation clauses
newResultColumn = createGeneratedColumn(targetTD, colDesc);
} else {
// Generate the tree for the default
String defaultText = defaultInfo.getDefaultText();
ValueNode defaultTree = parseDefault(defaultText);
defaultTree = defaultTree.bindExpression(getFromList(), (SubqueryList) null, (List<AggregateNode>) null);
newResultColumn = new ResultColumn(defaultTree.getTypeServices(), defaultTree, getContextManager());
}
DefaultDescriptor defaultDescriptor = colDesc.getDefaultDescriptor(dataDictionary);
if (SanityManager.DEBUG) {
SanityManager.ASSERT(defaultDescriptor != null, "defaultDescriptor expected to be non-null");
}
getCompilerContext().createDependency(defaultDescriptor);
}
} else if (colDesc.isAutoincrement()) {
newResultColumn = new ResultColumn(colDesc, null, getContextManager());
newResultColumn.setAutoincrementGenerated();
} else {
newResultColumn = new ResultColumn(colType, getNullNode(colType), getContextManager());
}
}
// Mark the new RC as generated for an unmatched column in an insert
newResultColumn.markGeneratedForUnmatchedColumnInInsert();
return newResultColumn;
}
use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class MergeNode method addColumnPrivilege.
/**
* <p>
* Add SELECT privilege on the indicated column.
* </p>
*/
private void addColumnPrivilege(ColumnReference cr) throws StandardException {
CompilerContext cc = getCompilerContext();
ResultColumn rc = cr.getSource();
if (rc != null) {
ColumnDescriptor colDesc = rc.getColumnDescriptor();
if (colDesc != null) {
cc.pushCurrentPrivType(Authorizer.SELECT_PRIV);
cc.addRequiredColumnPriv(colDesc);
cc.popCurrentPrivType();
}
}
}
use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class ModifyColumnNode method useExistingCollation.
/**
* If the column being modified is of character string type, then it should
* get its collation from the corresponding column in the TableDescriptor.
* This will ensure that at alter table time, the existing character string
* type columns do not loose their collation type. If the alter table is
* doing a drop column, then we do not need to worry about collation info.
*
* @param td Table Descriptor that holds the column which is being altered
* @throws StandardException
*/
void useExistingCollation(TableDescriptor td) throws StandardException {
ColumnDescriptor cd;
// First verify that the column exists
cd = td.getColumnDescriptor(name);
if (cd == null) {
throw StandardException.newException(SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE, name, td.getName());
}
// no need to worry about collation info
if (getType() != null) {
if (getType().getTypeId().isStringTypeId()) {
setCollationType(cd.getType().getCollationType());
}
}
}
use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class MatchingClauseNode method bindInsertValues.
/**
* Bind the values in the INSERT list
*/
private void bindInsertValues(FromList fullFromList, FromTable targetTable) throws StandardException {
TableDescriptor td = targetTable.getTableDescriptor();
// construct a full insert column list if insert columns weren't specified
if (_insertColumns == null) {
_insertColumns = buildFullColumnList(td);
}
if (_insertColumns.size() != _insertValues.size()) {
throw StandardException.newException(SQLState.LANG_DB2_INVALID_COLS_SPECIFIED);
}
// forbid illegal values for identity columns
for (int i = 0; i < _insertValues.size(); i++) {
ResultColumn rc = _insertValues.elementAt(i);
String columnName = _insertColumns.elementAt(i).getName();
ValueNode expr = rc.getExpression();
ColumnDescriptor cd = td.getColumnDescriptor(columnName);
// the InsertNode
if (cd == null) {
continue;
}
// DEFAULT is the only value allowed for a GENERATED ALWAYS AS IDENTITY column
if (cd.isAutoincAlways() && !(expr instanceof DefaultNode)) {
throw StandardException.newException(SQLState.LANG_AI_CANNOT_MODIFY_AI, columnName);
}
// NULL is illegal as the value for any identity column
if (cd.isAutoincrement() && (expr instanceof UntypedNullConstantNode)) {
throw StandardException.newException(SQLState.LANG_NULL_INTO_NON_NULL, columnName);
}
}
// needed to make the SelectNode bind
_insertValues.replaceOrForbidDefaults(targetTable.getTableDescriptor(), _insertColumns, true);
bindExpressions(_insertValues, fullFromList);
}
use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class RenameNode method renameColumnBind.
// do any checking needs to be done at bind time for rename column
private void renameColumnBind(DataDictionary dd) throws StandardException {
ColumnDescriptor columnDescriptor = td.getColumnDescriptor(oldObjectName);
/* Verify that old column name does exist in the table */
if (columnDescriptor == null)
throw StandardException.newException(SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE, oldObjectName, getFullName());
/* Verify that new column name does not exist in the table */
ColumnDescriptor cd = td.getColumnDescriptor(newObjectName);
if (cd != null)
throw descriptorExistsException(cd, td);
//
// You cannot rename a column which is referenced by the generation
// clause of a generated column.
//
ColumnDescriptorList generatedColumns = td.getGeneratedColumns();
int generatedColumnCount = generatedColumns.size();
for (int i = 0; i < generatedColumnCount; i++) {
ColumnDescriptor gc = generatedColumns.elementAt(i);
String[] referencedColumns = gc.getDefaultInfo().getReferencedColumnNames();
int refColCount = referencedColumns.length;
for (int j = 0; j < refColCount; j++) {
String refName = referencedColumns[j];
if (oldObjectName.equals(refName)) {
throw StandardException.newException(SQLState.LANG_GEN_COL_BAD_RENAME, oldObjectName, gc.getColumnName());
}
}
}
/* Verify that there are no check constraints using the column being renamed */
ConstraintDescriptorList constraintDescriptorList = dd.getConstraintDescriptors(td);
int size = constraintDescriptorList == null ? 0 : constraintDescriptorList.size();
ConstraintDescriptor constraintDescriptor;
ColumnDescriptorList checkConstraintCDL;
int checkConstraintCDLSize;
// go through all the constraints defined on the table
for (int index = 0; index < size; index++) {
constraintDescriptor = constraintDescriptorList.elementAt(index);
// renamed is not used in it's sql
if (constraintDescriptor.getConstraintType() == DataDictionary.CHECK_CONSTRAINT) {
checkConstraintCDL = constraintDescriptor.getColumnDescriptors();
checkConstraintCDLSize = checkConstraintCDL.size();
for (int index2 = 0; index2 < checkConstraintCDLSize; index2++) if (checkConstraintCDL.elementAt(index2) == columnDescriptor)
throw StandardException.newException(SQLState.LANG_RENAME_COLUMN_WILL_BREAK_CHECK_CONSTRAINT, oldObjectName, constraintDescriptor.getConstraintName());
}
}
}
Aggregations