Search in sources :

Example 21 with ExecRow

use of org.apache.derby.iapi.sql.execute.ExecRow in project derby by apache.

the class DataDictionaryImpl method getConstraintDescriptorViaHeap.

/**
 * Return a (single or list of) catalog row descriptor(s) from
 * SYSCONSTRAINTS through a heap scan
 *
 * @param scanQualifiers			qualifiers
 * @param ti						The TabInfoImpl to use
 * @param parentTupleDescriptor		The parentDescriptor, if applicable.
 * @param list						The list to build, if supplied.
 *									If null, then caller expects a single descriptor
 *
 * @return	The last matching descriptor
 *
 * @exception StandardException		Thrown on error
 */
protected TupleDescriptor getConstraintDescriptorViaHeap(ScanQualifier[][] scanQualifiers, TabInfoImpl ti, TupleDescriptor parentTupleDescriptor, ConstraintDescriptorList list) throws StandardException {
    SYSCONSTRAINTSRowFactory rf = (SYSCONSTRAINTSRowFactory) ti.getCatalogRowFactory();
    ExecRow outRow;
    ScanController scanController;
    TransactionController tc;
    ConstraintDescriptor cd = null;
    // Get the current transaction controller
    tc = getTransactionCompile();
    outRow = rf.makeEmptyRow();
    /*
		** Table scan
		*/
    scanController = tc.openScan(// conglomerate to open
    ti.getHeapConglomerate(), // don't hold open across commit
    false, // for read
    0, TransactionController.MODE_TABLE, TransactionController.ISOLATION_REPEATABLE_READ, // all fields as objects
    (FormatableBitSet) null, // start position - first row
    (DataValueDescriptor[]) null, // startSearchOperation - none
    0, // scanQualifier,
    scanQualifiers, // stop position -through last row
    (DataValueDescriptor[]) null, // stopSearchOperation - none
    0);
    try {
        while (scanController.fetchNext(outRow.getRowArray())) {
            SubConstraintDescriptor subCD = null;
            switch(rf.getConstraintType(outRow)) {
                case DataDictionary.PRIMARYKEY_CONSTRAINT:
                case DataDictionary.FOREIGNKEY_CONSTRAINT:
                case DataDictionary.UNIQUE_CONSTRAINT:
                    subCD = getSubKeyConstraint(rf.getConstraintId(outRow), rf.getConstraintType(outRow));
                    break;
                case DataDictionary.CHECK_CONSTRAINT:
                    subCD = getSubCheckConstraint(rf.getConstraintId(outRow));
                    break;
                default:
                    if (SanityManager.DEBUG) {
                        SanityManager.THROWASSERT("unexpected value from " + " rf.getConstraintType(outRow) " + rf.getConstraintType(outRow));
                    }
            }
            if (SanityManager.DEBUG) {
                SanityManager.ASSERT(subCD != null, "subCD is expected to be non-null");
            }
            cd = (ConstraintDescriptor) rf.buildDescriptor(outRow, subCD, this);
            /* If dList is null, then caller only wants a single descriptor - we're done
				 * else just add the current descriptor to the list.
				 */
            if (list == null) {
                break;
            } else {
                list.add(cd);
            }
        }
    } finally {
        scanController.close();
    }
    return cd;
}
Also used : ScanController(org.apache.derby.iapi.store.access.ScanController) SubConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.SubConstraintDescriptor) ConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) KeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.KeyConstraintDescriptor) SubKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.SubKeyConstraintDescriptor) CheckConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.CheckConstraintDescriptor) ForeignKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor) SubCheckConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.SubCheckConstraintDescriptor) SubConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.SubConstraintDescriptor) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) TransactionController(org.apache.derby.iapi.store.access.TransactionController)

Example 22 with ExecRow

use of org.apache.derby.iapi.sql.execute.ExecRow in project derby by apache.

the class DataDictionaryImpl method rewriteSYSCOLPERMSforAlterTable.

/**
 * Workhorse for ALTER TABLE-driven mods to SYSCOLPERMS
 *
 * This method finds all the SYSCOLPERMS rows for this table. Then it
 * iterates through each row, either adding a new column to the end of
 * the table, or dropping a column from the table, as appropriate. It
 * updates each SYSCOLPERMS row to store the new COLUMNS value.
 *
 * @param tableID	The UUID of the table being altered
 * @param tc		TransactionController for the transaction
 * @param columnDescriptor   Dropped column info, or null if adding
 *
 * @exception StandardException		Thrown on error
 */
private void rewriteSYSCOLPERMSforAlterTable(UUID tableID, TransactionController tc, ColumnDescriptor columnDescriptor) throws StandardException {
    // In Derby authorization mode, permission catalogs may not be present
    if (!usesSqlAuthorization)
        return;
    /* This method has 2 steps to it. First get all the ColPermsDescriptor   
		for given tableid. And next step is to go back to SYSCOLPERMS to find
		unique row corresponding to each of ColPermsDescriptor and update the
		"COLUMNS" column in SYSCOLPERMS. The reason for this 2 step process is
		that SYSCOLPERMS has a non-unique row on "TABLEID" column and hence   
		we can't get a unique handle on each of the affected row in SYSCOLPERMS
		using just the "TABLEID" column */
    // First get all the ColPermsDescriptor for the given tableid from
    // SYSCOLPERMS using getDescriptorViaIndex().
    // all ColPermsDescriptor for given tableid
    List<ColPermsDescriptor> permissionDescriptorsList;
    DataValueDescriptor tableIDOrderable = getIDValueAsCHAR(tableID);
    TabInfoImpl ti = getNonCoreTI(SYSCOLPERMS_CATALOG_NUM);
    SYSCOLPERMSRowFactory rf = (SYSCOLPERMSRowFactory) ti.getCatalogRowFactory();
    ExecIndexRow keyRow = exFactory.getIndexableRow(1);
    keyRow.setColumn(1, tableIDOrderable);
    permissionDescriptorsList = newSList();
    getDescriptorViaIndex(SYSCOLPERMSRowFactory.TABLEID_INDEX_NUM, keyRow, (ScanQualifier[][]) null, ti, (TupleDescriptor) null, permissionDescriptorsList, ColPermsDescriptor.class, false);
    /* Next, using each of the ColPermDescriptor's uuid, get the unique row 
		in SYSCOLPERMS and adjust the "COLUMNS" column in SYSCOLPERMS to 
		accomodate the added or dropped column in the tableid*/
    ExecRow curRow;
    ExecIndexRow uuidKey;
    // Not updating any indexes on SYSCOLPERMS
    boolean[] bArray = new boolean[SYSCOLPERMSRowFactory.TOTAL_NUM_OF_INDEXES];
    int[] colsToUpdate = { SYSCOLPERMSRowFactory.COLUMNS_COL_NUM };
    for (ColPermsDescriptor colPermsDescriptor : permissionDescriptorsList) {
        removePermEntryInCache(colPermsDescriptor);
        uuidKey = rf.buildIndexKeyRow(rf.COLPERMSID_INDEX_NUM, colPermsDescriptor);
        curRow = ti.getRow(tc, uuidKey, rf.COLPERMSID_INDEX_NUM);
        FormatableBitSet columns = (FormatableBitSet) curRow.getColumn(SYSCOLPERMSRowFactory.COLUMNS_COL_NUM).getObject();
        // for the dropped column.
        if (columnDescriptor == null) {
            int currentLength = columns.getLength();
            columns.grow(currentLength + 1);
        } else {
            FormatableBitSet modifiedColumns = new FormatableBitSet(columns);
            modifiedColumns.shrink(columns.getLength() - 1);
            // FormatableBitSet count from 0.
            for (int i = columnDescriptor.getPosition() - 1; i < modifiedColumns.getLength(); i++) {
                if (columns.isSet(i + 1))
                    modifiedColumns.set(i);
                else
                    modifiedColumns.clear(i);
            }
            columns = modifiedColumns;
        }
        curRow.setColumn(SYSCOLPERMSRowFactory.COLUMNS_COL_NUM, new UserType((Object) columns));
        ti.updateRow(uuidKey, curRow, SYSCOLPERMSRowFactory.COLPERMSID_INDEX_NUM, bArray, colsToUpdate, tc);
    }
}
Also used : ExecIndexRow(org.apache.derby.iapi.sql.execute.ExecIndexRow) SQLLongint(org.apache.derby.iapi.types.SQLLongint) ColPermsDescriptor(org.apache.derby.iapi.sql.dictionary.ColPermsDescriptor) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) UserType(org.apache.derby.iapi.types.UserType)

Example 23 with ExecRow

use of org.apache.derby.iapi.sql.execute.ExecRow in project derby by apache.

the class DataDictionaryImpl method updateConstraintDescriptor.

/**
 * Update the constraint descriptor in question.  Updates
 * every row in the base conglomerate.
 *
 * @param cd                    The Constraint descriptor
 * @param formerUUID			The UUID for this column in SYSCONSTRAINTS,
 *                              may differ from what is in {@code cd} if this
 *								is the column that is being set.
 * @param colsToSet             Array of integers of columns to be modified,
 *                              1 based.  May be null (all columns).
 * @param tc					The TransactionController to use
 *
 * @exception StandardException		Thrown on failure
 */
public void updateConstraintDescriptor(ConstraintDescriptor cd, UUID formerUUID, int[] colsToSet, TransactionController tc) throws StandardException {
    ExecIndexRow keyRow1 = null;
    ExecRow row;
    DataValueDescriptor IDOrderable;
    TabInfoImpl ti = getNonCoreTI(SYSCONSTRAINTS_CATALOG_NUM);
    SYSCONSTRAINTSRowFactory rf = (SYSCONSTRAINTSRowFactory) ti.getCatalogRowFactory();
    /* Use objectID/columnName in both start 
		 * and stop position for index 1 scan. 
		 */
    IDOrderable = getIDValueAsCHAR(formerUUID);
    /* Set up the start/stop position for the scan */
    keyRow1 = (ExecIndexRow) exFactory.getIndexableRow(1);
    keyRow1.setColumn(1, IDOrderable);
    // build the row to be stuffed into SYSCONSTRAINTS.
    row = rf.makeRow(cd, null);
    /*
		** Figure out if the index in sysconstraints needs 
		** to be updated. 
		*/
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(rf.getNumIndexes() == 3, "There are more indexes on sysconstraints than expected, the code herein needs to change");
    }
    boolean[] bArray = new boolean[3];
    /*
		** Do we need to update indexes?
		*/
    if (colsToSet == null) {
        bArray[0] = true;
        bArray[1] = true;
        bArray[2] = true;
    } else {
        /*
			** Check the specific columns for indexed
			** columns.
			*/
        for (int i = 0; i < colsToSet.length; i++) {
            switch(colsToSet[i]) {
                case SYSCONSTRAINTSRowFactory.SYSCONSTRAINTS_CONSTRAINTID:
                    bArray[0] = true;
                    break;
                case SYSCONSTRAINTSRowFactory.SYSCONSTRAINTS_CONSTRAINTNAME:
                case SYSCONSTRAINTSRowFactory.SYSCONSTRAINTS_SCHEMAID:
                    bArray[1] = true;
                    break;
                case SYSCONSTRAINTSRowFactory.SYSCONSTRAINTS_TABLEID:
                    bArray[2] = true;
                    break;
            }
        }
    }
    ti.updateRow(keyRow1, row, SYSCONSTRAINTSRowFactory.SYSCONSTRAINTS_INDEX1_ID, bArray, colsToSet, tc);
}
Also used : ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) ExecIndexRow(org.apache.derby.iapi.sql.execute.ExecIndexRow) SQLLongint(org.apache.derby.iapi.types.SQLLongint)

Example 24 with ExecRow

use of org.apache.derby.iapi.sql.execute.ExecRow in project derby by apache.

the class DataDictionaryImpl method getSetAutoincrementValue.

/**
 * @see DataDictionary#getSetAutoincrementValue
 */
public NumberDataValue getSetAutoincrementValue(RowLocation rl, TransactionController tc, boolean doUpdate, NumberDataValue newValue, boolean wait) throws StandardException {
    int columnNum = SYSCOLUMNSRowFactory.SYSCOLUMNS_AUTOINCREMENTVALUE;
    TabInfoImpl ti = coreInfo[SYSCOLUMNS_CORE_NUM];
    ConglomerateController heapCC = null;
    SYSCOLUMNSRowFactory rf = (SYSCOLUMNSRowFactory) ti.getCatalogRowFactory();
    ExecRow row = rf.makeEmptyRow();
    FormatableBitSet columnToRead = new FormatableBitSet(SYSCOLUMNSRowFactory.SYSCOLUMNS_COLUMN_COUNT);
    // FormatableBitSet is 0 based.
    // current value.
    columnToRead.set(columnNum - 1);
    // start value.
    columnToRead.set(columnNum);
    // increment value.
    columnToRead.set(columnNum + 1);
    try {
        /* if wait is true then we need to do a wait while trying to
			   open/fetch from the conglomerate. note we use wait both to
			   open as well as fetch from the conglomerate.
			*/
        heapCC = tc.openConglomerate(ti.getHeapConglomerate(), false, (TransactionController.OPENMODE_FORUPDATE | ((wait) ? 0 : TransactionController.OPENMODE_LOCK_NOWAIT)), TransactionController.MODE_RECORD, TransactionController.ISOLATION_REPEATABLE_READ);
        // fetch the current value
        boolean baseRowExists = heapCC.fetch(rl, row.getRowArray(), columnToRead, wait);
        if (SanityManager.DEBUG) {
            // We're not prepared for a non-existing base row.
            SanityManager.ASSERT(baseRowExists, "base row not found");
        }
        // while the Row interface is 1 based.
        NumberDataValue currentAI = (NumberDataValue) row.getColumn(columnNum);
        long currentAIValue = currentAI.getLong();
        if (doUpdate) {
            // increment the value
            NumberDataValue increment = (NumberDataValue) row.getColumn(columnNum + 2);
            currentAI = currentAI.plus(currentAI, increment, currentAI);
            row.setColumn(columnNum, currentAI);
            // store the new value in SYSCOLUMNS
            FormatableBitSet columnToUpdate = new FormatableBitSet(SYSCOLUMNSRowFactory.SYSCOLUMNS_COLUMN_COUNT);
            // current value.
            columnToUpdate.set(columnNum - 1);
            heapCC.replace(rl, row.getRowArray(), columnToUpdate);
        }
        // incrementing it.
        if (newValue != null) {
            // user has passed in an object; set the current value in there and
            // return it.
            newValue.setValue(currentAIValue);
            return newValue;
        } else {
            // reuse the object read from row.
            currentAI.setValue(currentAIValue);
            return currentAI;
        }
    } finally {
        if (heapCC != null)
            heapCC.close();
    }
}
Also used : ConglomerateController(org.apache.derby.iapi.store.access.ConglomerateController) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) NumberDataValue(org.apache.derby.iapi.types.NumberDataValue) SQLLongint(org.apache.derby.iapi.types.SQLLongint)

Example 25 with ExecRow

use of org.apache.derby.iapi.sql.execute.ExecRow in project derby by apache.

the class DataDictionaryImpl method getConstraintDescriptorViaIndex.

/**
 * Return a (single or list of) ConstraintDescriptor(s) from
 * SYSCONSTRAINTS where the access is from the index to the heap.
 *
 * @param indexId	The id of the index (0 to # of indexes on table) to use
 * @param keyRow	The supplied ExecIndexRow for search
 * @param ti		The TabInfoImpl to use
 * @param td		The TableDescriptor, if supplied.
 * @param dList		The list to build, if supplied.  If null, then caller expects
 *					a single descriptor
 * @param forUpdate			Whether or not to open scan for update
 *
 * @return	The last matching descriptor
 *
 * @exception StandardException		Thrown on error
 */
protected ConstraintDescriptor getConstraintDescriptorViaIndex(int indexId, ExecIndexRow keyRow, TabInfoImpl ti, TableDescriptor td, ConstraintDescriptorList dList, boolean forUpdate) throws StandardException {
    SYSCONSTRAINTSRowFactory rf = (SYSCONSTRAINTSRowFactory) ti.getCatalogRowFactory();
    ConglomerateController heapCC;
    ConstraintDescriptor cd = null;
    ExecIndexRow indexRow1;
    ExecRow outRow;
    RowLocation baseRowLocation;
    ScanController scanController;
    TransactionController tc;
    // Get the current transaction controller
    tc = getTransactionCompile();
    outRow = rf.makeEmptyRow();
    heapCC = tc.openConglomerate(ti.getHeapConglomerate(), false, 0, TransactionController.MODE_RECORD, TransactionController.ISOLATION_REPEATABLE_READ);
    /* Scan the index and go to the data pages for qualifying rows to
		 * build the column descriptor.
		 */
    scanController = tc.openScan(// conglomerate to open
    ti.getIndexConglomerate(indexId), // don't hold open across commit
    false, (forUpdate) ? TransactionController.OPENMODE_FORUPDATE : 0, TransactionController.MODE_RECORD, TransactionController.ISOLATION_REPEATABLE_READ, // all fields as objects
    (FormatableBitSet) null, // start position - exact key match.
    keyRow.getRowArray(), // startSearchOperation
    ScanController.GE, // scanQualifier,
    null, // stop position - exact key match.
    keyRow.getRowArray(), // stopSearchOperation
    ScanController.GT);
    while (scanController.next()) {
        SubConstraintDescriptor subCD = null;
        // create an index row template
        indexRow1 = getIndexRowFromHeapRow(ti.getIndexRowGenerator(indexId), heapCC.newRowLocationTemplate(), outRow);
        scanController.fetch(indexRow1.getRowArray());
        baseRowLocation = (RowLocation) indexRow1.getColumn(indexRow1.nColumns());
        boolean base_row_exists = heapCC.fetch(baseRowLocation, outRow.getRowArray(), (FormatableBitSet) null);
        if (SanityManager.DEBUG) {
            // it can not be possible for heap row to disappear while
            // holding scan cursor on index at ISOLATION_REPEATABLE_READ.
            SanityManager.ASSERT(base_row_exists, "base row doesn't exist");
        }
        switch(rf.getConstraintType(outRow)) {
            case DataDictionary.PRIMARYKEY_CONSTRAINT:
            case DataDictionary.FOREIGNKEY_CONSTRAINT:
            case DataDictionary.UNIQUE_CONSTRAINT:
                subCD = getSubKeyConstraint(rf.getConstraintId(outRow), rf.getConstraintType(outRow));
                break;
            case DataDictionary.CHECK_CONSTRAINT:
                subCD = getSubCheckConstraint(rf.getConstraintId(outRow));
                break;
            default:
                if (SanityManager.DEBUG) {
                    SanityManager.THROWASSERT("unexpected value " + "from rf.getConstraintType(outRow)" + rf.getConstraintType(outRow));
                }
        }
        if (SanityManager.DEBUG) {
            SanityManager.ASSERT(subCD != null, "subCD is expected to be non-null");
        }
        /* Cache the TD in the SCD so that
			 * the row factory doesn't need to go
			 * out to disk to get it.
			 */
        subCD.setTableDescriptor(td);
        cd = (ConstraintDescriptor) rf.buildDescriptor(outRow, subCD, this);
        /* If dList is null, then caller only wants a single descriptor - we're done
			 * else just add the current descriptor to the list.
			 */
        if (dList == null) {
            break;
        } else {
            dList.add(cd);
        }
    }
    scanController.close();
    heapCC.close();
    return cd;
}
Also used : ScanController(org.apache.derby.iapi.store.access.ScanController) ConglomerateController(org.apache.derby.iapi.store.access.ConglomerateController) SubConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.SubConstraintDescriptor) ConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) KeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.KeyConstraintDescriptor) SubKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.SubKeyConstraintDescriptor) CheckConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.CheckConstraintDescriptor) ForeignKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor) SubCheckConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.SubCheckConstraintDescriptor) SubConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.SubConstraintDescriptor) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) TransactionController(org.apache.derby.iapi.store.access.TransactionController) ExecIndexRow(org.apache.derby.iapi.sql.execute.ExecIndexRow) RowLocation(org.apache.derby.iapi.types.RowLocation)

Aggregations

ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)155 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)62 ExecIndexRow (org.apache.derby.iapi.sql.execute.ExecIndexRow)34 RowLocation (org.apache.derby.iapi.types.RowLocation)27 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)23 ConglomerateController (org.apache.derby.iapi.store.access.ConglomerateController)22 ScanController (org.apache.derby.iapi.store.access.ScanController)22 SQLVarchar (org.apache.derby.iapi.types.SQLVarchar)22 SQLChar (org.apache.derby.iapi.types.SQLChar)21 SQLLongint (org.apache.derby.iapi.types.SQLLongint)21 UUID (org.apache.derby.catalog.UUID)19 Properties (java.util.Properties)12 TransactionController (org.apache.derby.iapi.store.access.TransactionController)12 CursorResultSet (org.apache.derby.iapi.sql.execute.CursorResultSet)11 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)10 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)10 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)10 UserType (org.apache.derby.iapi.types.UserType)9 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)8 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)7