Search in sources :

Example 11 with NumberDataValue

use of org.apache.derby.iapi.types.NumberDataValue in project derby by apache.

the class DataDictionaryImpl method setAutoincrementValue.

/**
 * sets a new value in SYSCOLUMNS for a particular
 * autoincrement column.
 *
 * @param tc		 Transaction Controller to use.
 * @param columnName Name of the column.
 * @param aiValue	 Value to write to SYSCOLUMNS.
 * @param incrementNeeded whether to increment the value passed in by the
 * user (aiValue) or not before writing it to SYSCOLUMNS.
 */
public void setAutoincrementValue(TransactionController tc, UUID tableUUID, String columnName, long aiValue, boolean incrementNeeded) throws StandardException {
    TabInfoImpl ti = coreInfo[SYSCOLUMNS_CORE_NUM];
    ExecIndexRow keyRow = null;
    keyRow = (ExecIndexRow) exFactory.getIndexableRow(2);
    keyRow.setColumn(1, getIDValueAsCHAR(tableUUID));
    keyRow.setColumn(2, new SQLChar(columnName));
    SYSCOLUMNSRowFactory rf = (SYSCOLUMNSRowFactory) ti.getCatalogRowFactory();
    ExecRow row = rf.makeEmptyRow();
    boolean[] bArray = new boolean[2];
    for (int index = 0; index < 2; index++) {
        bArray[index] = false;
    }
    int[] colsToUpdate = new int[1];
    colsToUpdate[0] = SYSCOLUMNSRowFactory.SYSCOLUMNS_AUTOINCREMENTVALUE;
    if (incrementNeeded) {
        ExecRow readRow = ti.getRow(tc, keyRow, SYSCOLUMNSRowFactory.SYSCOLUMNS_INDEX1_ID);
        NumberDataValue increment = (NumberDataValue) readRow.getColumn(SYSCOLUMNSRowFactory.SYSCOLUMNS_AUTOINCREMENTINC);
        aiValue += increment.getLong();
    }
    row.setColumn(SYSCOLUMNSRowFactory.SYSCOLUMNS_AUTOINCREMENTVALUE, new SQLLongint(aiValue));
    ti.updateRow(keyRow, row, SYSCOLUMNSRowFactory.SYSCOLUMNS_INDEX1_ID, bArray, colsToUpdate, tc);
}
Also used : SQLLongint(org.apache.derby.iapi.types.SQLLongint) SQLChar(org.apache.derby.iapi.types.SQLChar) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) ExecIndexRow(org.apache.derby.iapi.sql.execute.ExecIndexRow) NumberDataValue(org.apache.derby.iapi.types.NumberDataValue) SQLLongint(org.apache.derby.iapi.types.SQLLongint)

Example 12 with NumberDataValue

use of org.apache.derby.iapi.types.NumberDataValue in project derby by apache.

the class DataDictionaryImpl method updateCurrentSequenceValue.

/**
 * Set the current value of an ANSI/ISO sequence. This method does not perform
 * any sanity checking but assumes that the caller knows what they are doing. If the
 * old value on disk is not what we expect it to be, then we are in a race with another
 * session. They won and we don't update the value on disk. However, if the old value
 * is null, that is a signal to us that we should update the value on disk anyway.
 *
 * @param tc			Transaction Controller to use.
 * @param rowLocation Row in SYSSEQUENCES to update.
 * @param wait True if we should wait for locks
 * @param oldValue What we expect to find in the CURRENTVALUE column.
 * @param newValue What to stuff into the CURRENTVALUE column.
 *
 * @return Returns true if the value was successfully updated, false if we lost a race with another session.
 *
 * @exception StandardException thrown on failure.
 */
public boolean updateCurrentSequenceValue(TransactionController tc, RowLocation rowLocation, boolean wait, Long oldValue, Long newValue) throws StandardException {
    int columnNum = SYSSEQUENCESRowFactory.SYSSEQUENCES_CURRENT_VALUE;
    FormatableBitSet columnToUpdate = new FormatableBitSet(SYSSEQUENCESRowFactory.SYSSEQUENCES_COLUMN_COUNT);
    TabInfoImpl ti = getNonCoreTI(SYSSEQUENCES_CATALOG_NUM);
    ConglomerateController heapCC = null;
    SYSSEQUENCESRowFactory rf = (SYSSEQUENCESRowFactory) ti.getCatalogRowFactory();
    ExecRow row = rf.makeEmptyRow();
    // FormatableBitSet is 0 based.
    // current value.
    columnToUpdate.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);
        boolean baseRowExists = heapCC.fetch(rowLocation, row.getRowArray(), columnToUpdate, wait);
        // 
        if (!baseRowExists) {
            return false;
        }
        NumberDataValue oldValueOnDisk = (NumberDataValue) row.getColumn(columnNum);
        SQLLongint expectedOldValue;
        if (oldValue == null) {
            expectedOldValue = new SQLLongint();
        } else {
            expectedOldValue = new SQLLongint(oldValue.longValue());
        }
        // only update value if what's on disk is what we expected
        if ((oldValue == null) || (expectedOldValue.compare(oldValueOnDisk) == 0)) {
            SQLLongint newValueOnDisk;
            if (newValue == null) {
                newValueOnDisk = new SQLLongint();
            } else {
                newValueOnDisk = new SQLLongint(newValue.longValue());
            }
            row.setColumn(columnNum, newValueOnDisk);
            heapCC.replace(rowLocation, row.getRowArray(), columnToUpdate);
            return true;
        } else {
            return false;
        }
    } finally {
        if (heapCC != null) {
            heapCC.close();
        }
    }
}
Also used : ConglomerateController(org.apache.derby.iapi.store.access.ConglomerateController) SQLLongint(org.apache.derby.iapi.types.SQLLongint) 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)

Aggregations

NumberDataValue (org.apache.derby.iapi.types.NumberDataValue)12 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)4 SQLLongint (org.apache.derby.iapi.types.SQLLongint)3 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)2 ConglomerateController (org.apache.derby.iapi.store.access.ConglomerateController)2 ArrayList (java.util.ArrayList)1 UUID (org.apache.derby.catalog.UUID)1 PreparedStatement (org.apache.derby.iapi.sql.PreparedStatement)1 ResultColumnDescriptor (org.apache.derby.iapi.sql.ResultColumnDescriptor)1 ResultSet (org.apache.derby.iapi.sql.ResultSet)1 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)1 ExecIndexRow (org.apache.derby.iapi.sql.execute.ExecIndexRow)1 TransactionController (org.apache.derby.iapi.store.access.TransactionController)1 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)1 SQLChar (org.apache.derby.iapi.types.SQLChar)1 CheckInfo (org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.CheckInfo)1 HeapRowLocation (org.apache.derby.impl.store.access.heap.HeapRowLocation)1 StandardException (org.apache.derby.shared.common.error.StandardException)1