Search in sources :

Example 6 with NumberDataValue

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];
}
Also used : NumberDataValue(org.apache.derby.iapi.types.NumberDataValue)

Example 7 with NumberDataValue

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);
    }
}
Also used : NumberDataValue(org.apache.derby.iapi.types.NumberDataValue)

Example 8 with NumberDataValue

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;
}
Also used : NumberDataValue(org.apache.derby.iapi.types.NumberDataValue)

Example 9 with NumberDataValue

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;
}
Also used : HeapRowLocation(org.apache.derby.impl.store.access.heap.HeapRowLocation) ResultSet(org.apache.derby.iapi.sql.ResultSet) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) ArrayList(java.util.ArrayList) CheckInfo(org.apache.derby.impl.sql.execute.DeferredConstraintsMemory.CheckInfo) PreparedStatement(org.apache.derby.iapi.sql.PreparedStatement) UUID(org.apache.derby.catalog.UUID) NumberDataValue(org.apache.derby.iapi.types.NumberDataValue)

Example 10 with NumberDataValue

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);
}
Also used : DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) NumberDataValue(org.apache.derby.iapi.types.NumberDataValue)

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