use of org.apache.derby.iapi.types.NumberDataValue in project derby by apache.
the class UpdateResultSet method getSetAutoincrementValue.
/**
* getSetAutoincrementValue will get the autoincrement value of the
* columnPosition specified for the target table. If increment is
* non-zero we will also update the autoincrement value.
*
* @param columnPosition position of the column in the table (1-based)
* @param increment amount of increment.
*
* @exception StandardException if anything goes wrong.
*/
public NumberDataValue getSetAutoincrementValue(int columnPosition, long increment) throws StandardException {
autoincrementGenerated = true;
// all our indices are 0 based.
int index = columnPosition - 1;
NumberDataValue newValue;
newValue = activation.getCurrentValueAndAdvance(identitySequenceUUIDString, aiCache[index].getTypeFormatId());
aiCache[index] = newValue;
// Save the generated auto increment value for use by JDBC api and
// IDENTITY_LOCAL_VAL function
identityVal = newValue.getLong();
return (NumberDataValue) aiCache[index];
}
use of org.apache.derby.iapi.types.NumberDataValue in project derby by apache.
the class SumAggregator method accumulate.
/**
* Accumulate
*
* @param addend value to be added in
*
* @exception StandardException on error
*
* @see ExecAggregator#accumulate
*/
protected void accumulate(DataValueDescriptor addend) throws StandardException {
/*
** If we don't have any value yet, just clone
** the addend.
*/
if (value == null) {
/* NOTE: We need to call cloneValue since value gets
* reused underneath us
*/
value = addend.cloneValue(false);
} else {
NumberDataValue input = (NumberDataValue) addend;
NumberDataValue nv = (NumberDataValue) value;
value = nv.plus(// addend 1
input, // addend 2
nv, // result
nv);
}
}
use of org.apache.derby.iapi.types.NumberDataValue in project derby by apache.
the class AvgAggregator method getResult.
/**
* Return the result of the aggregation. If the count
* is zero, then we haven't averaged anything yet, so
* we return null. Otherwise, return the running
* average as a double.
*
* @return null or the average as Double
*/
public DataValueDescriptor getResult() throws StandardException {
if (count == 0) {
return null;
}
NumberDataValue sum = (NumberDataValue) value;
NumberDataValue avg = (NumberDataValue) value.getNewNull();
if (count > (long) Integer.MAX_VALUE) {
// TINYINT, SMALLINT, INTEGER implement arithmetic using integers
// If the sum is still represented as a TINYINT, SMALLINT or INTEGER
// we cannot let their int based arithmetic handle it, since they
// will perform a getInt() on the long value which will truncate the long value.
// One solution would be to promote the sum to a SQLLongint, but its value
// will be less than or equal to Integer.MAX_VALUE, so the average will be 0.
String typeName = sum.getTypeName();
if (typeName.equals(TypeId.INTEGER_NAME) || typeName.equals(TypeId.TINYINT_NAME) || typeName.equals(TypeId.SMALLINT_NAME)) {
avg.setValue(0);
return avg;
}
}
NumberDataValue countv = new org.apache.derby.iapi.types.SQLLongint(count);
sum.divide(sum, countv, avg, scale);
return avg;
}
use of org.apache.derby.iapi.types.NumberDataValue in project derby by apache.
the class ConstraintConstantAction method validateConstraint.
/**
* Evaluate a check constraint or not null column constraint.
* Generate a query of the
* form SELECT COUNT(*) FROM t where NOT(<check constraint>)
* and run it by compiling and executing it. Will
* work ok if the table is empty and query returns null.
*
* @param constraintName constraint name
* @param constraintText constraint text
* @param constraintId constraint id
* @param td referenced table
* @param lcc the language connection context
* @param isCheckConstraint the constraint is a check constraint
* @param isInitiallyDeferred {@code true} if the constraint is
* initially deferred
*
* @return true if null constraint passes, false otherwise
*
* @exception StandardException if check constraint fails
*/
static boolean validateConstraint(String constraintName, String constraintText, UUID constraintId, TableDescriptor td, LanguageConnectionContext lcc, boolean isCheckConstraint, boolean isInitiallyDeferred) throws StandardException {
StringBuilder checkStmt = new StringBuilder();
/* should not use select sum(not(<check-predicate>) ? 1: 0) because
* that would generate much more complicated code and may exceed Java
* limits if we have a large number of check constraints, beetle 4347
*/
checkStmt.append("SELECT COUNT(*) FROM ");
checkStmt.append(td.getQualifiedName());
checkStmt.append(" WHERE NOT(");
checkStmt.append(constraintText);
checkStmt.append(")");
ResultSet rs = null;
try {
PreparedStatement ps = lcc.prepareInternalStatement(checkStmt.toString());
// This is a substatement; for now, we do not set any timeout
// for it. We might change this behaviour later, by linking
// timeout to its parent statement's timeout settings.
rs = ps.executeSubStatement(lcc, false, 0L);
ExecRow row = rs.getNextRow();
if (SanityManager.DEBUG) {
if (row == null) {
SanityManager.THROWASSERT("did not get any rows back from query: " + checkStmt.toString());
}
}
Number value = ((Number) ((NumberDataValue) row.getRowArray()[0]).getObject());
/*
** Value may be null if there are no rows in the
** table.
*/
if ((value != null) && (value.longValue() != 0)) {
// check constraint is violated.
if (isCheckConstraint) {
if (isInitiallyDeferred) {
// Remember the violation
List<UUID> violatingConstraints = new ArrayList<UUID>();
violatingConstraints.add(constraintId);
// FIXME: We don't know the row locations of the
// violating rows, so for now, just pretend we know one,
// then invalidate the row location information forcing
// full table check at validation time
CheckInfo[] newCi = new CheckInfo[1];
DeferredConstraintsMemory.rememberCheckViolations(lcc, td.getObjectID(), td.getSchemaName(), td.getName(), null, violatingConstraints, new HeapRowLocation(), /* dummy */
newCi);
newCi[0].setInvalidatedRowLocations();
} else {
throw StandardException.newException(SQLState.LANG_ADD_CHECK_CONSTRAINT_FAILED, constraintName, td.getQualifiedName(), value.toString());
}
}
/*
* for not null constraint violations exception will be thrown in caller
* check constraint will not get here since exception is thrown
* above
*/
return false;
}
} finally {
if (rs != null) {
rs.close();
}
}
return true;
}
use of org.apache.derby.iapi.types.NumberDataValue in project derby by apache.
the class InsertResultSet method processMergeRow.
/**
* <p>
* Special handling if this is an INSERT action of a MERGE statement.
* </p>
*/
private ExecRow processMergeRow(NoPutResultSet sourceRS, ExecRow row) throws StandardException {
//
if (constants.hasAutoincrement()) {
int columnPosition = constants.getAutoGenColumn();
long increment = constants.getAutoincIncrement(columnPosition);
DataValueDescriptor dvd = row.getColumn(columnPosition + 1);
//
// If the identity column was declared BY DEFAULT, then it could be
// overridden by the WHEN NOT MATCHED clause. In that case, the
// row will contain a non-null value which we do not want to clobber.
//
// boolean needToGenerateValue = ( dvd == null ) || ( dvd.isNullOp().getBoolean() );
boolean needToGenerateValue = (dvd == null);
if (needToGenerateValue) {
NumberDataValue newIdentityValue = getSetAutoincrementValue(columnPosition + 1, increment);
row.setColumn(columnPosition + 1, newIdentityValue);
}
}
return normalizeRow(sourceRS, row);
}
Aggregations