Search in sources :

Example 41 with DataTypeDescriptor

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

the class BaseActivation method setParameters.

// how do we do/do we want any sanity checking for
// the number of parameters expected?
public void setParameters(ParameterValueSet parameterValues, DataTypeDescriptor[] parameterTypes) throws StandardException {
    if (!isClosed()) {
        if (this.pvs == null || parameterTypes == null) {
            pvs = parameterValues;
            return;
        }
        DataTypeDescriptor[] newParamTypes = preStmt.getParameterTypes();
        /*
			** If there are old parameters but not new ones,
			** they aren't compatible.
			*/
        boolean match = false;
        if (newParamTypes != null) {
            if (newParamTypes.length == parameterTypes.length) {
                /* Check each parameter */
                match = true;
                for (int i = 0; i < parameterTypes.length; i++) {
                    DataTypeDescriptor oldType = parameterTypes[i];
                    DataTypeDescriptor newType = newParamTypes[i];
                    if (!oldType.isExactTypeAndLengthMatch(newType)) {
                        match = false;
                        break;
                    }
                    /*
						** We could probably get away without checking nullability,
						** since parameters are always nullable.
						*/
                    if (oldType.isNullable() != newType.isNullable()) {
                        match = false;
                        break;
                    }
                }
            }
        }
        if (!match)
            throw StandardException.newException(SQLState.LANG_OBSOLETE_PARAMETERS);
        parameterValues.transferDataValues(pvs);
    } else if (SanityManager.DEBUG) {
        SanityManager.THROWASSERT("isClosed() is expected to return false");
    }
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Example 42 with DataTypeDescriptor

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

the class BaseExpressionActivation method maxValue.

/**
 * <p>
 * Get the maximum value of 4 input values.  If less than 4 values, input
 * {@code null} for the unused parameters and place them at the end.
 * If more than 4 input values, call this multiple times to
 * accumulate results.  Also have judge's type as parameter to have a base
 * upon which the comparison is based.  An example use is for code
 * generation in bug 3858.
 * </p>
 *
 * <p>
 * If all the input values are SQL NULL, return SQL NULL. Otherwise, return
 * the maximum value of the non-NULL inputs.
 * </p>
 *
 * @param v1		1st value
 * @param v2		2nd value
 * @param v3		3rd value
 * @param v4		4th value
 * @param judgeTypeFormatId		type format id of the judge
 * @param judgeUserJDBCTypeId	JDBC type id if judge is user type;
 *								-1 if not user type
 *
 * @return	The maximum value of the 4.
 */
public static DataValueDescriptor maxValue(DataValueDescriptor v1, DataValueDescriptor v2, DataValueDescriptor v3, DataValueDescriptor v4, int judgeTypeFormatId, int judgeUserJDBCTypeId, int judgePrecision, int judgeScale, boolean judgeIsNullable, int judgeMaximumWidth, int judgeCollationType, int judgeCollationDerivation) throws StandardException {
    DataValueDescriptor judge;
    if (judgeUserJDBCTypeId == -1) {
        judge = new DataTypeDescriptor(new TypeId(judgeTypeFormatId, null), judgePrecision, judgeScale, judgeIsNullable, judgeMaximumWidth, judgeCollationType, judgeCollationDerivation).getNull();
    } else
        judge = new TypeId(judgeTypeFormatId, new UserDefinedTypeIdImpl()).getNull();
    DataValueDescriptor maxVal = v1;
    if (v2 != null && (maxVal.isNull() || judge.greaterThan(v2, maxVal).equals(true)))
        maxVal = v2;
    if (v3 != null && (maxVal.isNull() || judge.greaterThan(v3, maxVal).equals(true)))
        maxVal = v3;
    if (v4 != null && (maxVal.isNull() || judge.greaterThan(v4, maxVal).equals(true)))
        maxVal = v4;
    return maxVal;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) UserDefinedTypeIdImpl(org.apache.derby.catalog.types.UserDefinedTypeIdImpl) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor)

Example 43 with DataTypeDescriptor

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

the class ValueNode method genSQLJavaSQLTree.

/**
 * Generate a SQL-&gt;Java-&gt;SQL conversion tree above the current node
 * and bind the new nodes individually.
 * This is useful when doing comparisons, built-in functions, etc. on
 * java types which have a direct mapping to system built-in types.
 *
 * @return ValueNode	The new tree.
 *
 * @exception StandardException	Thrown on error
 */
ValueNode genSQLJavaSQLTree() throws StandardException {
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(getTypeId() != null, "genSQLJavaSQLTree() only expected to be called on a bound node");
        SanityManager.ASSERT(getTypeId().userType(), "genSQLJavaSQLTree() only expected to be called on user types");
    }
    final ContextManager cm = getContextManager();
    JavaValueNode stjvn = new SQLToJavaValueNode(this, cm);
    ValueNode jtsvn = new JavaToSQLValueNode(stjvn, cm);
    DataTypeDescriptor resultType;
    if ((getTypeServices() != null) && getTypeId().userType()) {
        resultType = getTypeServices();
    } else {
        resultType = DataTypeDescriptor.getSQLDataTypeDescriptor(stjvn.getJavaTypeName());
    }
    jtsvn.setType(resultType);
    return jtsvn;
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) ContextManager(org.apache.derby.iapi.services.context.ContextManager)

Example 44 with DataTypeDescriptor

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

the class ValueNodeList method getDominantTypeServices.

/**
 * Get the dominant DataTypeServices from the elements in the list. This
 * method will also set the correct collation information on the dominant
 * DataTypeService if we are dealing with character string datatypes.
 *
 * Algorithm for determining collation information
 * This method will check if it is dealing with character string datatypes.
 * If yes, then it will check if all the character string datatypes have
 * the same collation derivation and collation type associated with them.
 * If not, then the resultant DTD from this method will have collation
 * derivation of NONE. If yes, then the resultant DTD from this method will
 * have the same collation derivation and collation type as all the
 * character string datatypes.
 *
 * Note that this method calls DTD.getDominantType and that method returns
 * the dominant type of the 2 DTDs involved in this method. That method
 * sets the collation info on the dominant type following the algorithm
 * mentioned in the comments of
 * @see DataTypeDescriptor#getDominantType(DataTypeDescriptor, ClassFactory)
 * With that algorithm, if one DTD has collation derivation of NONE and the
 * other DTD has collation derivation of IMPLICIT, then the return DTD from
 * DTD.getDominantType will have collation derivation of IMPLICIT. That is
 * not the correct algorithm for aggregate operators. SQL standards says
 * that if EVERY type has implicit derivation AND is of the same type, then
 * the collation of the resultant will be of that type with derivation
 * IMPLICIT. To provide this behavior for aggregate operator, we basically
 * ignore the collation type and derivation picked by
 * DataTypeDescriptor.getDominantType. Instead we let
 * getDominantTypeServices use the simple algorithm listed at the top of
 * this method's comments to determine the collation type and derivation
 * for this ValueNodeList object.
 *
 * @return DataTypeServices		The dominant DataTypeServices.
 *
 * @exception StandardException		Thrown on error
 */
DataTypeDescriptor getDominantTypeServices() throws StandardException {
    DataTypeDescriptor dominantDTS = null;
    // Following 2 will hold the collation derivation and type of the first
    // string operand. This collation information will be checked against
    // the collation derivation and type of other string operands. If a
    // mismatch is found, foundCollationMisMatch will be set to true.
    int firstCollationDerivation = -1;
    int firstCollationType = -1;
    // As soon as we find 2 strings with different collations, we set the
    // following flag to true. At the end of the method, if this flag is set
    // to true then it means that we have operands with different collation
    // types and hence the resultant dominant type will have to have the
    // collation derivation of NONE.
    boolean foundCollationMisMatch = false;
    for (int index = 0; index < size(); index++) {
        ValueNode valueNode;
        valueNode = elementAt(index);
        // haven't already been bound to a type.
        if (valueNode.requiresTypeFromContext() && valueNode.getTypeServices() == null) {
            continue;
        }
        DataTypeDescriptor valueNodeDTS = valueNode.getTypeServices();
        if (valueNodeDTS.getTypeId().isStringTypeId()) {
            if (firstCollationDerivation == -1) {
                // found first string type. Initialize firstCollationDerivation
                // and firstCollationType with collation information from
                // that first string type operand.
                firstCollationDerivation = valueNodeDTS.getCollationDerivation();
                firstCollationType = valueNodeDTS.getCollationType();
            } else if (!foundCollationMisMatch) {
                if (firstCollationDerivation != valueNodeDTS.getCollationDerivation())
                    // collation derivations don't match
                    foundCollationMisMatch = true;
                else if (firstCollationType != valueNodeDTS.getCollationType())
                    // collation types don't match
                    foundCollationMisMatch = true;
            }
        }
        if (dominantDTS == null) {
            dominantDTS = valueNodeDTS;
        } else {
            dominantDTS = dominantDTS.getDominantType(valueNodeDTS, getClassFactory());
        }
    }
    // string operands.
    if (firstCollationDerivation != -1) {
        if (foundCollationMisMatch) {
            // if we come here that it means that alll the string operands
            // do not have matching collation information on them. Hence the
            // resultant dominant DTD should have collation derivation of
            // NONE.
            dominantDTS = dominantDTS.getCollatedType(dominantDTS.getCollationType(), StringDataValue.COLLATION_DERIVATION_NONE);
        }
    // if we didn't find any collation mismatch, then resultant dominant
    // DTD already has the correct collation information on it and hence
    // we don't need to do anything.
    }
    return dominantDTS;
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Example 45 with DataTypeDescriptor

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

the class TernaryOperatorNode method locateBind.

/**
 * Bind locate operator
 * The variable receiver is the string which will searched
 * The variable leftOperand is the search character that will looked in the
 *     receiver variable.
 *
 * @return	The new top of the expression tree.
 *
 * @exception StandardException		Thrown on error
 */
ValueNode locateBind() throws StandardException {
    TypeId firstOperandType, secondOperandType, offsetType;
    /*
		 * Is there a ? parameter for the first arg.  Copy the 
		 * left/firstOperand's.  If the left/firstOperand are both parameters,
		 * both will be max length.
		 */
    if (receiver.requiresTypeFromContext()) {
        if (leftOperand.requiresTypeFromContext()) {
            receiver.setType(getVarcharDescriptor());
            // Since both receiver and leftOperands are parameters, use the
            // collation of compilation schema for receiver.
            receiver.setCollationUsingCompilationSchema();
        } else {
            if (leftOperand.getTypeId().isStringTypeId()) {
                // Since the leftOperand is not a parameter, receiver will
                // get it's collation from leftOperand through following
                // setType method
                receiver.setType(leftOperand.getTypeServices());
            }
        }
    }
    /*
		 * Is there a ? parameter for the second arg.  Copy the receiver's.
		 * If the receiver are both parameters, both will be max length.
		 */
    if (leftOperand.requiresTypeFromContext()) {
        if (receiver.requiresTypeFromContext()) {
            leftOperand.setType(getVarcharDescriptor());
        } else {
            if (receiver.getTypeId().isStringTypeId()) {
                leftOperand.setType(receiver.getTypeServices());
            }
        }
        // collation of ? operand should be picked up from the context.
        // By the time we come here, receiver will have correct collation
        // set on it and hence we can rely on it to get correct collation
        // for this ?
        leftOperand.setCollationInfo(receiver.getTypeServices());
    }
    /*
		 * Is there a ? parameter for the third arg.  It will be an int.
		 */
    if (rightOperand.requiresTypeFromContext()) {
        rightOperand.setType(new DataTypeDescriptor(TypeId.INTEGER_ID, true));
    }
    bindToBuiltIn();
    /*
		** Check the type of the operand - this function is allowed only
		** for: receiver = CHAR
		**      firstOperand = CHAR
		**      secondOperand = INT
		*/
    secondOperandType = leftOperand.getTypeId();
    offsetType = rightOperand.getTypeId();
    firstOperandType = receiver.getTypeId();
    if (!firstOperandType.isStringTypeId() || !secondOperandType.isStringTypeId() || offsetType.getJDBCTypeId() != Types.INTEGER)
        throw StandardException.newException(SQLState.LANG_DB2_FUNCTION_INCOMPATIBLE, "LOCATE", "FUNCTION");
    /*
		** The result type of a LocateFunctionNode is an integer.
		*/
    setType(new DataTypeDescriptor(TypeId.INTEGER_ID, receiver.getTypeServices().isNullable()));
    return this;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Aggregations

DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)99 TypeId (org.apache.derby.iapi.types.TypeId)32 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)14 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)14 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)9 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)8 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)8 TypeDescriptor (org.apache.derby.catalog.TypeDescriptor)7 UUID (org.apache.derby.catalog.UUID)5 ClassFactory (org.apache.derby.iapi.services.loader.ClassFactory)5 ResultColumnDescriptor (org.apache.derby.iapi.sql.ResultColumnDescriptor)5 TypeCompiler (org.apache.derby.iapi.sql.compile.TypeCompiler)5 StandardException (org.apache.derby.shared.common.error.StandardException)5 Properties (java.util.Properties)4 UserDefinedTypeIdImpl (org.apache.derby.catalog.types.UserDefinedTypeIdImpl)4 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)4 AliasDescriptor (org.apache.derby.iapi.sql.dictionary.AliasDescriptor)3 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)3 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)3 ExecIndexRow (org.apache.derby.iapi.sql.execute.ExecIndexRow)3