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);
}
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();
}
}
}
Aggregations