Search in sources :

Example 36 with ColumnDescriptor

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

the class T_ConsistencyChecker method getIndexTemplateRow.

/* Get a template row for the specified index */
private ExecRow getIndexTemplateRow(RowLocation baseRL) throws StandardException {
    int[] baseColumnPositions;
    int baseColumns = 0;
    ExecRow indexScanTemplate;
    baseColumnPositions = id.getIndexDescriptor().baseColumnPositions();
    baseColumns = baseColumnPositions.length;
    FormatableBitSet indexColsBitSet = new FormatableBitSet();
    for (int i = 0; i < baseColumns; i++) {
        indexColsBitSet.grow(baseColumnPositions[i]);
        indexColsBitSet.set(baseColumnPositions[i] - 1);
    }
    /* Get a row template */
    indexScanTemplate = lcc.getLanguageConnectionFactory().getExecutionFactory().getValueRow(baseColumns + 1);
    /* Fill the row with nulls of the correct type */
    for (int column = 0; column < baseColumns; column++) {
        /* Column positions in the data dictionary are one-based */
        ColumnDescriptor cd = td.getColumnDescriptor(baseColumnPositions[column]);
        DataTypeDescriptor dts = cd.getType();
        indexScanTemplate.setColumn(column + 1, dts.getNull());
    }
    /* Set the row location in the last column of the index row */
    indexScanTemplate.setColumn(baseColumns + 1, baseRL);
    return indexScanTemplate;
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet)

Example 37 with ColumnDescriptor

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

the class T_ConsistencyChecker method getHeapRowOfNulls.

/* Get a heap row full of nulls */
private ExecRow getHeapRowOfNulls() throws StandardException {
    ConglomerateController baseCC;
    ExecRow baseRow;
    /* Open the heap for reading */
    baseCC = tc.openConglomerate(td.getHeapConglomerateId(), false, 0, TransactionController.MODE_TABLE, TransactionController.ISOLATION_SERIALIZABLE);
    /* Get a row template for the base table */
    baseRow = lcc.getLanguageConnectionFactory().getExecutionFactory().getValueRow(td.getNumberOfColumns());
    /* Fill the row with nulls of the correct type */
    ColumnDescriptorList cdl = td.getColumnDescriptorList();
    int cdlSize = cdl.size();
    for (int index = 0; index < cdlSize; index++) {
        ColumnDescriptor cd = (ColumnDescriptor) cdl.elementAt(index);
        DataTypeDescriptor dts = cd.getType();
        baseRow.setColumn(cd.getPosition(), dts.getNull());
    }
    baseCC.close();
    return baseRow;
}
Also used : ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) ConglomerateController(org.apache.derby.iapi.store.access.ConglomerateController) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow)

Example 38 with ColumnDescriptor

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

the class InsertResultSet method getOldStyleBulkInsertValue.

/**
 * Identity generation logic for bulk-insert used in pre-10.11 databases.
 *
 * @param index   0-based index into aiCache
 */
private void getOldStyleBulkInsertValue(int index, long increment) throws StandardException {
    NumberDataValue dvd;
    int columnPosition = index + 1;
    ColumnDescriptor cd = td.getColumnDescriptor(columnPosition);
    long ret;
    // System.out.println("in bulk insert");
    if (aiCache[index].isNull()) {
        long startValue;
        if (bulkInsertReplace) {
            startValue = cd.getAutoincStart();
        } else {
            dvd = dd.getSetAutoincrementValue(constants.autoincRowLocation[index], tc, false, (NumberDataValue) aiCache[index], true);
            startValue = dvd.getLong();
        }
        lcc.autoincrementCreateCounter(td.getSchemaName(), td.getName(), cd.getColumnName(), Long.valueOf(startValue), increment, columnPosition);
    }
    ret = lcc.nextAutoincrementValue(td.getSchemaName(), td.getName(), cd.getColumnName());
    aiCache[columnPosition - 1].setValue(ret);
}
Also used : ResultColumnDescriptor(org.apache.derby.iapi.sql.ResultColumnDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) NumberDataValue(org.apache.derby.iapi.types.NumberDataValue)

Example 39 with ColumnDescriptor

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

the class InsertResultSet method normalInsertCore.

// Do the work for a "normal" insert
private void normalInsertCore(LanguageConnectionContext lcc, boolean firstExecute) throws StandardException {
    boolean setUserIdentity = constants.hasAutoincrement() && isSingleRowResultSet();
    ExecRow deferredRowBuffer;
    long user_autoinc = 0;
    /* Get or re-use the row changer.
		 */
    if (firstExecute) {
        rowChanger = lcc.getLanguageConnectionFactory().getExecutionFactory().getRowChanger(heapConglom, constants.heapSCOCI, heapDCOCI, constants.irgs, constants.indexCIDS, constants.indexSCOCIs, indexDCOCIs, // number of columns in partial row meaningless for insert
        0, tc, // Changed column ids
        null, constants.getStreamStorableHeapColIds(), activation);
        rowChanger.setIndexNames(constants.indexNames);
    }
    /* decode lock mode for the execution isolation level */
    int lockMode = decodeLockMode(constants.lockMode);
    rowChanger.open(lockMode);
    /* The source does not know whether or not we are doing a
		 * deferred mode insert.  If we are, then we must clear the
		 * index scan info from the activation so that the row changer
		 * does not re-use that information (which won't be valid for
		 * a deferred mode insert).
		 */
    if (constants.deferred) {
        activation.clearIndexScanInfo();
    }
    if (fkInfoArray != null) {
        if (fkChecker == null) {
            fkChecker = new RISetChecker(lcc, tc, fkInfoArray);
        } else {
            fkChecker.reopen();
        }
    }
    if (firstExecute && constants.deferred) {
        Properties properties = new Properties();
        // Get the properties on the old heap
        rowChanger.getHeapConglomerateController().getInternalTablePropertySet(properties);
        /*
			** If deferred we save a copy of the entire row.
			*/
        rowHolder = new TemporaryRowHolderImpl(activation, properties, resultDescription);
        rowChanger.setRowHolder(rowHolder);
    }
    firstExecuteSpecialHandlingAutoGen(firstExecute, rowChanger, constants.targetUUID);
    while (row != null) {
        // auto-generated key columns.
        if (activation.getAutoGeneratedKeysResultsetMode() && autoGeneratedKeysColumnIndexes.length > 0) {
            autoGeneratedKeysRowsHolder.insert(getCompactRow(row, autoGeneratedKeysColumnIndexes));
        }
        // fill in columns that are computed from expressions on other columns
        evaluateGenerationClauses(generationClauses, activation, sourceResultSet, row, false);
        /*
			** If we're doing a deferred insert, insert into the temporary
			** conglomerate.  Otherwise, insert directly into the permanent
			** conglomerates using the rowChanger.
			*/
        if (constants.deferred) {
            rowHolder.insert(row);
        } else {
            // Immediate mode violations will throw, so we only ever
            // see false here with deferred constraint mode for one or more
            // of the constraints being checked.
            boolean allOk = evaluateCheckConstraints();
            if (fkChecker != null) {
                fkChecker.doFKCheck(activation, row);
            }
            // Objectify any streaming columns that are indexed.
            if (constants.irgs.length > 0) {
                DataValueDescriptor[] rowArray = row.getRowArray();
                for (int i = 0; i < rowArray.length; i++) {
                    // System.out.println("checking " + i);
                    if (!constants.indexedCols[i]) {
                        continue;
                    }
                    if (rowArray[i] instanceof StreamStorable)
                        rowArray[i].getObject();
                }
            }
            if (allOk) {
                rowChanger.insertRow(row, false);
            } else {
                RowLocation offendingRow = rowChanger.insertRow(row, true);
                deferredChecks = DeferredConstraintsMemory.rememberCheckViolations(lcc, constants.targetUUID, schemaName, tableName, deferredChecks, violatingCheckConstraints, offendingRow, new CheckInfo[1]);
            }
        }
        rowCount++;
        if (setUserIdentity) {
            dd = lcc.getDataDictionary();
            td = dd.getTableDescriptor(constants.targetUUID);
            int maxColumns = td.getMaxColumnID();
            int col;
            for (col = 1; col <= maxColumns; col++) {
                ColumnDescriptor cd = td.getColumnDescriptor(col);
                if (cd.isAutoincrement()) {
                    break;
                }
            }
            if (col <= maxColumns) {
                DataValueDescriptor dvd = row.cloneColumn(col);
                user_autoinc = dvd.getLong();
            }
        }
        // No need to do a next on a single row source
        if (constants.singleRowSource) {
            row = null;
        } else {
            row = getNextRowCore(sourceResultSet);
        }
    }
    /*
		** If it's a deferred insert, scan the temporary conglomerate and
		** insert the rows into the permanent conglomerates using rowChanger.
		*/
    if (constants.deferred) {
        if (triggerInfo != null) {
            Vector<AutoincrementCounter> v = null;
            if (aiCache != null) {
                v = new Vector<AutoincrementCounter>();
                for (int i = 0; i < aiCache.length; i++) {
                    String s, t, c;
                    if (aiCache[i] == null)
                        continue;
                    Long initialValue = lcc.lastAutoincrementValue((s = constants.getSchemaName()), (t = constants.getTableName()), (c = constants.getColumnName(i)));
                    AutoincrementCounter aic = new AutoincrementCounter(initialValue, constants.getAutoincIncrement(i), aiCache[i].getLong(), s, t, c, i + 1);
                    v.addElement(aic);
                }
            }
            if (triggerActivator == null) {
                triggerActivator = new TriggerEventActivator(lcc, constants.targetUUID, triggerInfo, TriggerExecutionContext.INSERT_EVENT, activation, v);
            } else {
                triggerActivator.reopen();
            }
            // fire BEFORE trigger, do this before checking constraints
            triggerActivator.notifyEvent(TriggerEvents.BEFORE_INSERT, (CursorResultSet) null, rowHolder.getResultSet(), (int[]) null);
        }
        CursorResultSet rs = rowHolder.getResultSet();
        try {
            rs.open();
            while ((deferredRowBuffer = rs.getNextRow()) != null) {
                // we have to set the source row so the check constraint
                // sees the correct row.
                sourceResultSet.setCurrentRow(deferredRowBuffer);
                boolean allOk = evaluateCheckConstraints();
                if (allOk) {
                    rowChanger.insertRow(deferredRowBuffer, false);
                } else {
                    RowLocation offendingRow = rowChanger.insertRow(deferredRowBuffer, true);
                    deferredChecks = DeferredConstraintsMemory.rememberCheckViolations(lcc, constants.targetUUID, schemaName, tableName, deferredChecks, violatingCheckConstraints, offendingRow, new CheckInfo[1]);
                }
            }
        } finally {
            sourceResultSet.clearCurrentRow();
            rs.close();
        }
        if (fkChecker != null) {
            /*
				** Second scan to make sure all the foreign key
				** constraints are ok.  We have to do this after
				** we have completed the inserts in case of self
				** referencing constraints.
				*/
            rs = rowHolder.getResultSet();
            try {
                rs.open();
                while ((deferredRowBuffer = rs.getNextRow()) != null) {
                    fkChecker.doFKCheck(activation, deferredRowBuffer);
                }
            } finally {
                rs.close();
            }
        }
        // fire AFTER trigger
        if (triggerActivator != null) {
            triggerActivator.notifyEvent(TriggerEvents.AFTER_INSERT, (CursorResultSet) null, rowHolder.getResultSet(), (int[]) null);
        }
    }
    if (rowHolder != null) {
        rowHolder.close();
    // rowHolder kept across opens
    }
    if (fkChecker != null) {
        fkChecker.close();
        fkChecker = null;
    }
    if (setIdentity)
        lcc.setIdentityValue(identityVal);
    else /*
                 * find the value of the identity column from the user inserted value
                 * and do a lcc.setIdentityValue(<user_value>);
                 */
    if (setUserIdentity) {
        lcc.setIdentityValue(user_autoinc);
    }
}
Also used : CursorResultSet(org.apache.derby.iapi.sql.execute.CursorResultSet) ResultColumnDescriptor(org.apache.derby.iapi.sql.ResultColumnDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) Properties(java.util.Properties) LanguageProperties(org.apache.derby.iapi.sql.LanguageProperties) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) StreamStorable(org.apache.derby.iapi.services.io.StreamStorable) CheckInfo(org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.CheckInfo) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) RowLocation(org.apache.derby.iapi.types.RowLocation)

Example 40 with ColumnDescriptor

use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor 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)

Aggregations

ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)79 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)23 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)20 ResultColumnDescriptor (org.apache.derby.iapi.sql.ResultColumnDescriptor)19 UUID (org.apache.derby.catalog.UUID)15 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)14 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)10 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)8 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)8 TransactionController (org.apache.derby.iapi.store.access.TransactionController)8 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)8 SQLLongint (org.apache.derby.iapi.types.SQLLongint)8 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)7 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)7 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)7 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)7 RowLocation (org.apache.derby.iapi.types.RowLocation)7 DefaultInfoImpl (org.apache.derby.catalog.types.DefaultInfoImpl)6 ConstraintDescriptorList (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList)6 DefaultDescriptor (org.apache.derby.iapi.sql.dictionary.DefaultDescriptor)6