Search in sources :

Example 1 with DefaultDescriptor

use of org.apache.derby.iapi.sql.dictionary.DefaultDescriptor 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;
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) ResultColumnDescriptor(org.apache.derby.iapi.sql.ResultColumnDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) DefaultDescriptor(org.apache.derby.iapi.sql.dictionary.DefaultDescriptor) DefaultInfoImpl(org.apache.derby.catalog.types.DefaultInfoImpl)

Example 2 with DefaultDescriptor

use of org.apache.derby.iapi.sql.dictionary.DefaultDescriptor in project derby by apache.

the class AlterTableConstantAction method modifyColumnDefault.

/**
 * Workhorse for modifying the default value of a column.
 *
 * @param       ix 		the index of the column specfication in the ALTER
 *						statement-- currently we allow only one.
 * @exception	StandardException, thrown on error.
 */
private void modifyColumnDefault(int ix) throws StandardException {
    ColumnDescriptor columnDescriptor = td.getColumnDescriptor(columnInfo[ix].name);
    int columnPosition = columnDescriptor.getPosition();
    // Clean up after the old default, if non-null
    if (columnDescriptor.hasNonNullDefault()) {
        // Invalidate off of the old default
        DefaultDescriptor defaultDescriptor = new DefaultDescriptor(dd, columnInfo[ix].oldDefaultUUID, td.getUUID(), columnPosition);
        dm.invalidateFor(defaultDescriptor, DependencyManager.MODIFY_COLUMN_DEFAULT, lcc);
        // Drop any dependencies
        dm.clearDependencies(lcc, defaultDescriptor);
    }
    UUID defaultUUID = columnInfo[ix].newDefaultUUID;
    /* Generate a UUID for the default, if one exists
		 * and there is no default id yet.
		 */
    if (columnInfo[ix].defaultInfo != null && defaultUUID == null) {
        defaultUUID = dd.getUUIDFactory().createUUID();
    }
    /* Get a ColumnDescriptor reflecting the new default */
    columnDescriptor = new ColumnDescriptor(columnInfo[ix].name, columnPosition, columnInfo[ix].dataType, columnInfo[ix].defaultValue, columnInfo[ix].defaultInfo, td, defaultUUID, columnInfo[ix].autoincStart, columnInfo[ix].autoincInc, columnInfo[ix].autoinc_create_or_modify_Start_Increment, columnInfo[ix].autoincCycle);
    // Update the ColumnDescriptor with new default info
    dd.dropColumnDescriptor(td.getUUID(), columnInfo[ix].name, tc);
    dd.addDescriptor(columnDescriptor, td, DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
    if (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_INCREMENT) {
        // adding an autoincrement default-- calculate the maximum value
        // of the autoincrement column.
        long maxValue = getColumnMax(td, columnInfo[ix].name, columnInfo[ix].autoincInc);
        dd.setAutoincrementValue(tc, td.getUUID(), columnInfo[ix].name, maxValue, true);
    } else if (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_RESTART) {
        dd.setAutoincrementValue(tc, td.getUUID(), columnInfo[ix].name, columnInfo[ix].autoincStart, false);
    }
    if ((columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_INCREMENT) || (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_RESTART) || (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_CYCLE)) {
        // 
        if (dd.checkVersion(DataDictionary.DD_VERSION_DERBY_10_11, null)) {
            Long currentValue = null;
            // are just changing the increment. see DERBY-6579.
            if ((columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_INCREMENT) || (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_CYCLE)) {
                currentValue = dd.peekAtIdentity(td.getSchemaName(), td.getName());
            }
            if (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_CYCLE) {
                if (columnInfo[ix].autoincCycle) {
                    // ALTER TABLE ALTER COLUMN $columnName SET CYCLE
                    if (currentValue == null) {
                        // 
                        // If the current value is NULL, then the sequence generator
                        // is exhausted and it must have been a NO CYCLE generator,
                        // which we are changing to CYCLE.
                        // According to the 2016 SQL Standard, section 4.27.2
                        // (Operations involving sequence generators),
                        // the next value of the sequence generator should be the minimum value
                        // (for an ascending sequence generator) or the maximum value
                        // (for a descending sequence generator). See DERBY-6961.
                        // This logic will have to change in the future if we
                        // let users configure the maximum and minimum values of identity columns.
                        // 
                        int topOrBottom = (columnInfo[ix].autoincInc > 0) ? RANGE_BOTTOM : RANGE_TOP;
                        currentValue = getRangeBound(columnInfo[ix].dataType, topOrBottom);
                    }
                } else {
                    // ALTER TABLE ALTER COLUMN $columnName SET NO CYCLE
                    // 
                    // If we are just about to issue the rollover value,
                    // set it to NULL in order to prevent cycling.
                    int topOrBottom = (columnInfo[ix].autoincInc > 0) ? RANGE_BOTTOM : RANGE_TOP;
                    Long rolloverValue = getRangeBound(columnInfo[ix].dataType, topOrBottom);
                    if ((currentValue != null) && (currentValue.equals(rolloverValue))) {
                        currentValue = null;
                    }
                }
            }
            DropTableConstantAction.dropIdentitySequence(dd, td, activation);
            // recreate the sequence
            String sequenceName = TableDescriptor.makeSequenceName(td.getUUID());
            CreateSequenceConstantAction csca = CreateTableConstantAction.makeCSCA(columnInfo[ix], sequenceName);
            csca.executeConstantAction(activation);
            // reset the current value of the sequence generator as necessary
            if ((columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_INCREMENT) || (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_CYCLE)) {
                SequenceDescriptor sequence = dd.getSequenceDescriptor(dd.getSystemSchemaDescriptor(), sequenceName);
                RowLocation[] rowLocation = new RowLocation[1];
                SequenceDescriptor[] sequenceDescriptor = new SequenceDescriptor[1];
                dd.computeSequenceRowLocation(tc, sequence.getUUID().toString(), rowLocation, sequenceDescriptor);
                dd.updateCurrentSequenceValue(tc, rowLocation[0], true, null, currentValue);
            }
        }
    }
}
Also used : ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) DefaultDescriptor(org.apache.derby.iapi.sql.dictionary.DefaultDescriptor) UUID(org.apache.derby.catalog.UUID) SequenceDescriptor(org.apache.derby.iapi.sql.dictionary.SequenceDescriptor) RowLocation(org.apache.derby.iapi.types.RowLocation)

Example 3 with DefaultDescriptor

use of org.apache.derby.iapi.sql.dictionary.DefaultDescriptor in project derby by apache.

the class DDLConstantAction method addColumnDependencies.

/**
 * Add dependencies of a column on providers. These can arise if a generated column depends
 * on a user created function.
 */
protected void addColumnDependencies(LanguageConnectionContext lcc, DataDictionary dd, TableDescriptor td, ColumnInfo ci) throws StandardException {
    ProviderInfo[] providers = ci.providers;
    if (providers != null) {
        DependencyManager dm = dd.getDependencyManager();
        ContextManager cm = lcc.getContextManager();
        int providerCount = providers.length;
        ColumnDescriptor cd = td.getColumnDescriptor(ci.name);
        DefaultDescriptor defDesc = cd.getDefaultDescriptor(dd);
        for (int px = 0; px < providerCount; px++) {
            ProviderInfo pi = providers[px];
            DependableFinder finder = pi.getDependableFinder();
            UUID providerID = pi.getObjectId();
            Provider provider = (Provider) finder.getDependable(dd, providerID);
            dm.addDependency(defDesc, provider, cm);
        }
    // end loop through providers
    }
}
Also used : DependableFinder(org.apache.derby.catalog.DependableFinder) ProviderInfo(org.apache.derby.iapi.sql.depend.ProviderInfo) ContextManager(org.apache.derby.iapi.services.context.ContextManager) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) DefaultDescriptor(org.apache.derby.iapi.sql.dictionary.DefaultDescriptor) UUID(org.apache.derby.catalog.UUID) Provider(org.apache.derby.iapi.sql.depend.Provider)

Example 4 with DefaultDescriptor

use of org.apache.derby.iapi.sql.dictionary.DefaultDescriptor in project derby by apache.

the class DropTableConstantAction method executeConstantAction.

// INTERFACE METHODS
/**
 *	This is the guts of the Execution-time logic for DROP TABLE.
 *
 *	@see ConstantAction#executeConstantAction
 *
 * @exception StandardException		Thrown on failure
 */
public void executeConstantAction(Activation activation) throws StandardException {
    TableDescriptor td;
    UUID tableID;
    ConglomerateDescriptor[] cds;
    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    DependencyManager dm = dd.getDependencyManager();
    TransactionController tc = lcc.getTransactionExecute();
    if ((sd != null) && sd.getSchemaName().equals(SchemaDescriptor.STD_DECLARED_GLOBAL_TEMPORARY_TABLES_SCHEMA_NAME)) {
        // check if this is a temp table before checking data dictionary
        td = lcc.getTableDescriptorForDeclaredGlobalTempTable(tableName);
        if (// td null here means it is not a temporary table. Look for table in physical SESSION schema
        td == null)
            td = dd.getTableDescriptor(tableName, sd, tc);
        if (// td null means tableName is not a temp table and it is not a physical table in SESSION schema
        td == null) {
            throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, fullTableName);
        }
        if (td.getTableType() == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) {
            dm.invalidateFor(td, DependencyManager.DROP_TABLE, lcc);
            tc.dropConglomerate(td.getHeapConglomerateId());
            lcc.dropDeclaredGlobalTempTable(tableName);
            return;
        }
    }
    /* Lock the table before we access the data dictionary
		 * to prevent deadlocks.
		 *
		 * Note that for DROP TABLE replayed at Targets during REFRESH,
		 * the conglomerateNumber will be 0. That's ok. During REFRESH,
		 * we don't need to lock the conglomerate.
		 */
    if (conglomerateNumber != 0) {
        lockTableForDDL(tc, conglomerateNumber, true);
    }
    /*
		** Inform the data dictionary that we are about to write to it.
		** There are several calls to data dictionary "get" methods here
		** that might be done in "read" mode in the data dictionary, but
		** it seemed safer to do this whole operation in "write" mode.
		**
		** We tell the data dictionary we're done writing at the end of
		** the transaction.
		*/
    dd.startWriting(lcc);
    /* Get the table descriptor. */
    td = dd.getTableDescriptor(tableId);
    if (td == null) {
        throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, fullTableName);
    }
    /* Get an exclusive table lock on the table. */
    long heapId = td.getHeapConglomerateId();
    lockTableForDDL(tc, heapId, true);
    /* Drop the triggers */
    for (TriggerDescriptor trd : dd.getTriggerDescriptors(td)) {
        trd.drop(lcc);
    }
    /* Drop all defaults */
    ColumnDescriptorList cdl = td.getColumnDescriptorList();
    for (ColumnDescriptor cd : cdl) {
        // 
        if (cd.isAutoincrement() && dd.checkVersion(DataDictionary.DD_VERSION_DERBY_10_11, null)) {
            dropIdentitySequence(dd, td, activation);
        }
        // any dependencies
        if (cd.getDefaultInfo() != null) {
            DefaultDescriptor defaultDesc = cd.getDefaultDescriptor(dd);
            dm.clearDependencies(lcc, defaultDesc);
        }
    }
    /* Drop the columns */
    dd.dropAllColumnDescriptors(tableId, tc);
    /* Drop all table and column permission descriptors */
    dd.dropAllTableAndColPermDescriptors(tableId, tc);
    /* Drop the constraints */
    dropAllConstraintDescriptors(td, activation);
    /*
		** Drop all the conglomerates.  Drop the heap last, because the
		** store needs it for locking the indexes when they are dropped.
		*/
    cds = td.getConglomerateDescriptors();
    long[] dropped = new long[cds.length - 1];
    int numDropped = 0;
    for (int index = 0; index < cds.length; index++) {
        ConglomerateDescriptor cd = cds[index];
        /* if it's for an index, since similar indexes share one
			 * conglomerate, we only drop the conglomerate once
			 */
        if (cd.getConglomerateNumber() != heapId) {
            long thisConglom = cd.getConglomerateNumber();
            int i;
            for (i = 0; i < numDropped; i++) {
                if (dropped[i] == thisConglom)
                    break;
            }
            if (// not dropped
            i == numDropped) {
                dropped[numDropped++] = thisConglom;
                tc.dropConglomerate(thisConglom);
                dd.dropStatisticsDescriptors(td.getUUID(), cd.getUUID(), tc);
            }
        }
    }
    /* Prepare all dependents to invalidate.  (This is there chance
		 * to say that they can't be invalidated.  For example, an open
		 * cursor referencing a table/view that the user is attempting to
		 * drop.) If no one objects, then invalidate any dependent objects.
		 * We check for invalidation before we drop the table descriptor
		 * since the table descriptor may be looked up as part of
		 * decoding tuples in SYSDEPENDS.
		 */
    dm.invalidateFor(td, DependencyManager.DROP_TABLE, lcc);
    // 
    // The table itself can depend on the user defined types of its columns.
    // Drop all of those dependencies now.
    // 
    adjustUDTDependencies(lcc, dd, td, null, true);
    /* Drop the table */
    dd.dropTableDescriptor(td, sd, tc);
    /* Drop the conglomerate descriptors */
    dd.dropAllConglomerateDescriptors(td, tc);
    /* Drop the store element at last, to prevent dangling reference
		 * for open cursor, beetle 4393.
		 */
    tc.dropConglomerate(heapId);
}
Also used : ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor) TriggerDescriptor(org.apache.derby.iapi.sql.dictionary.TriggerDescriptor) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) UUID(org.apache.derby.catalog.UUID) TransactionController(org.apache.derby.iapi.store.access.TransactionController) DefaultDescriptor(org.apache.derby.iapi.sql.dictionary.DefaultDescriptor)

Example 5 with DefaultDescriptor

use of org.apache.derby.iapi.sql.dictionary.DefaultDescriptor in project derby by apache.

the class ResultColumnList method setDefault.

/**
 * Set the default in a ResultColumn
 */
void setDefault(ResultColumn rc, ColumnDescriptor cd, DefaultInfoImpl defaultInfo) throws StandardException {
    /* Query is dependent on the DefaultDescriptor */
    DefaultDescriptor defaultDescriptor = cd.getDefaultDescriptor(getDataDictionary());
    getCompilerContext().createDependency(defaultDescriptor);
    rc.setExpression(DefaultNode.parseDefault(defaultInfo.getDefaultText(), getLanguageConnectionContext(), getCompilerContext()));
}
Also used : DefaultDescriptor(org.apache.derby.iapi.sql.dictionary.DefaultDescriptor)

Aggregations

DefaultDescriptor (org.apache.derby.iapi.sql.dictionary.DefaultDescriptor)7 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)6 UUID (org.apache.derby.catalog.UUID)3 DefaultInfoImpl (org.apache.derby.catalog.types.DefaultInfoImpl)2 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)2 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)2 DependableFinder (org.apache.derby.catalog.DependableFinder)1 ContextManager (org.apache.derby.iapi.services.context.ContextManager)1 ResultColumnDescriptor (org.apache.derby.iapi.sql.ResultColumnDescriptor)1 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)1 Provider (org.apache.derby.iapi.sql.depend.Provider)1 ProviderInfo (org.apache.derby.iapi.sql.depend.ProviderInfo)1 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)1 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)1 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)1 SequenceDescriptor (org.apache.derby.iapi.sql.dictionary.SequenceDescriptor)1 TriggerDescriptor (org.apache.derby.iapi.sql.dictionary.TriggerDescriptor)1 TransactionController (org.apache.derby.iapi.store.access.TransactionController)1 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)1 RowLocation (org.apache.derby.iapi.types.RowLocation)1