Search in sources :

Example 46 with TypeId

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

the class BinaryOperatorNode method genSQLJavaSQLTree.

/**
 * generate a SQL->Java->SQL conversion tree above the left and right
 * operand of this Binary Operator Node if needed. Subclasses can override
 * the default behavior.
 */
@Override
ValueNode genSQLJavaSQLTree() throws StandardException {
    TypeId leftTypeId = leftOperand.getTypeId();
    if (leftTypeId.userType())
        leftOperand = leftOperand.genSQLJavaSQLTree();
    TypeId rightTypeId = rightOperand.getTypeId();
    if (rightTypeId.userType())
        rightOperand = rightOperand.genSQLJavaSQLTree();
    return this;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId)

Example 47 with TypeId

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

the class VTIResultSet method cast.

/**
 * <p>
 * Cast the value coming out of the user-coded ResultSet. The
 * rules are described in CastNode.getDataValueConversion().
 * </p>
 */
private void cast(DataTypeDescriptor dtd, DataValueDescriptor dvd) throws StandardException {
    TypeId typeID = dtd.getTypeId();
    if (!typeID.isBlobTypeId() && !typeID.isClobTypeId()) {
        if (typeID.isLongVarcharTypeId()) {
            castLongvarchar(dtd, dvd);
        } else if (typeID.isLongVarbinaryTypeId()) {
            castLongvarbinary(dtd, dvd);
        } else if (typeID.isDecimalTypeId()) {
            castDecimal(dtd, dvd);
        } else {
            Object o = dvd.getObject();
            dvd.setObjectForCast(o, true, typeID.getCorrespondingJavaTypeName());
            if (typeID.variableLength()) {
                VariableSizeDataValue vsdv = (VariableSizeDataValue) dvd;
                int width;
                if (typeID.isNumericTypeId()) {
                    width = dtd.getPrecision();
                } else {
                    width = dtd.getMaximumWidth();
                }
                vsdv.setWidth(width, dtd.getScale(), false);
            }
        }
    }
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) VariableSizeDataValue(org.apache.derby.iapi.types.VariableSizeDataValue)

Example 48 with TypeId

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

the class LengthOperatorNode method bindExpression.

/**
 * Bind this operator
 *
 * @param fromList			The query's FROM list
 * @param subqueryList		The subquery list being built as we find SubqueryNodes
 * @param aggregates        The aggregate list being built as we find AggregateNodes
 *
 * @return	The new top of the expression tree.
 *
 * @exception StandardException		Thrown on error
 */
@Override
ValueNode bindExpression(FromList fromList, SubqueryList subqueryList, List<AggregateNode> aggregates) throws StandardException {
    TypeId operandType;
    bindOperand(fromList, subqueryList, aggregates);
    /*
		** Check the type of the operand - this function is allowed only on
		** string value types.  
		*/
    operandType = operand.getTypeId();
    switch(operandType.getJDBCTypeId()) {
        case Types.CHAR:
        case Types.VARCHAR:
        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
        case Types.LONGVARCHAR:
        case Types.BLOB:
        case Types.CLOB:
            break;
        default:
            throw StandardException.newException(SQLState.LANG_UNARY_FUNCTION_BAD_TYPE, getOperatorString(), operandType.getSQLTypeName());
    }
    /*
		** The result type of XXX_length is int.
		*/
    setType(new DataTypeDescriptor(TypeId.INTEGER_ID, operand.getTypeServices().isNullable()));
    return this;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Example 49 with TypeId

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

the class LikeEscapeOperatorNode method bindComparisonOperator.

/**
 * Bind this operator
 *
 * @exception StandardException  Thrown on error
 */
public void bindComparisonOperator() throws StandardException {
    TypeId receiverType = receiver.getTypeId();
    TypeId leftType = leftOperand.getTypeId();
    if (!receiverType.isStringTypeId()) {
        throw StandardException.newException(SQLState.LANG_LIKE_BAD_TYPE, receiverType.getSQLTypeName());
    }
    if (!leftType.isStringTypeId()) {
        throw StandardException.newException(SQLState.LANG_LIKE_BAD_TYPE, leftType.getSQLTypeName());
    }
    if (rightOperand != null && !rightOperand.getTypeId().isStringTypeId()) {
        throw StandardException.newException(SQLState.LANG_LIKE_BAD_TYPE, rightOperand.getTypeId().getSQLTypeName());
    }
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId)

Example 50 with TypeId

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

the class MethodCallNode method resolveMethodCall.

protected void resolveMethodCall(String javaClassName, boolean staticMethod) throws StandardException {
    // only allow direct method calls through routines and internal SQL.
    if (routineInfo == null && !internalCall) {
        // See if we are being executed in an internal context
        if ((getCompilerContext().getReliability() & CompilerContext.INTERNAL_SQL_ILLEGAL) != 0) {
            throw StandardException.newException(SQLState.LANG_SYNTAX_ERROR, javaClassName + (staticMethod ? "::" : ".") + methodName);
        }
    }
    int count = signature.length;
    ClassInspector classInspector = getClassFactory().getClassInspector();
    String[] parmTypeNames;
    String[] primParmTypeNames = null;
    boolean[] isParam = getIsParam();
    boolean hasDynamicResultSets = hasVarargs() ? false : (routineInfo != null) && (count != 0) && (count != methodParms.length);
    /*
        ** Find the matching method that is public.
        */
    int signatureOffset = methodName.indexOf('(');
    // support Java signatures by checking if the method name contains a '('
    if (signatureOffset != -1) {
        parmTypeNames = parseValidateSignature(methodName, signatureOffset, hasDynamicResultSets);
        methodName = methodName.substring(0, signatureOffset);
        // If the signature is specified then Derby resolves to exactly
        // that method. Setting this flag to false disables the method
        // resolution from automatically optionally repeating the last
        // parameter as needed.
        hasDynamicResultSets = false;
    } else {
        parmTypeNames = getObjectSignature();
    }
    // the actual type of the trailing Java varargs arg is an array
    if (hasVarargs()) {
        parmTypeNames[count - 1] = parmTypeNames[count - 1] + "[]";
    }
    try {
        method = classInspector.findPublicMethod(javaClassName, methodName, parmTypeNames, null, isParam, staticMethod, hasDynamicResultSets, hasVarargs());
        // Also if the DDL specified a signature, then no alternate resolution
        if (signatureOffset == -1 && routineInfo == null) {
            /* If no match, then retry with combinations of object and
                 * primitive types.
                 */
            if (method == null) {
                primParmTypeNames = getPrimitiveSignature(false);
                method = classInspector.findPublicMethod(javaClassName, methodName, parmTypeNames, primParmTypeNames, isParam, staticMethod, hasDynamicResultSets, hasVarargs());
            }
        }
    } catch (ClassNotFoundException e) {
        /*
            ** If one of the classes couldn't be found, just act like the
            ** method couldn't be found.  The error lists all the class names,
            ** which should give the user enough info to diagnose the problem.
            */
        method = null;
    }
    /* Throw exception if no matching signature found */
    if (method == null) {
        throwNoMethodFound(javaClassName, parmTypeNames, primParmTypeNames);
    }
    String typeName = classInspector.getType(method);
    actualMethodReturnType = typeName;
    if (routineInfo == null) {
        /* void methods are only okay for CALL Statements */
        if (typeName.equals("void")) {
            if (!forCallStatement)
                throw StandardException.newException(SQLState.LANG_VOID_METHOD_CALL);
        }
    } else {
        String promoteName = null;
        TypeDescriptorImpl returnType = (TypeDescriptorImpl) routineInfo.getReturnType();
        String requiredType;
        if (returnType == null) {
            // must have a void method for a procedure call.
            requiredType = "void";
        } else {
            TypeId returnTypeId = TypeId.getBuiltInTypeId(returnType.getJDBCTypeId());
            if (returnType.isRowMultiSet() && (routineInfo.getParameterStyle() == RoutineAliasInfo.PS_DERBY_JDBC_RESULT_SET)) {
                requiredType = ResultSet.class.getName();
            } else if (returnType.getTypeId().userType()) {
                requiredType = ((UserDefinedTypeIdImpl) returnType.getTypeId()).getClassName();
            } else {
                requiredType = returnTypeId.getCorrespondingJavaTypeName();
                if (!requiredType.equals(typeName)) {
                    switch(returnType.getJDBCTypeId()) {
                        case java.sql.Types.BOOLEAN:
                        case java.sql.Types.SMALLINT:
                        case java.sql.Types.INTEGER:
                        case java.sql.Types.BIGINT:
                        case java.sql.Types.REAL:
                        case java.sql.Types.DOUBLE:
                            TypeCompiler tc = getTypeCompiler(returnTypeId);
                            requiredType = tc.getCorrespondingPrimitiveTypeName();
                            if (!routineInfo.calledOnNullInput() && routineInfo.getParameterCount() != 0) {
                                promoteName = returnTypeId.getCorrespondingJavaTypeName();
                            }
                            break;
                    }
                }
            }
        }
        boolean foundCorrectType;
        if (ResultSet.class.getName().equals(requiredType)) {
            // allow subtypes of ResultSet too
            try {
                Class<?> actualType = classInspector.getClass(typeName);
                foundCorrectType = ResultSet.class.isAssignableFrom(actualType);
            } catch (ClassNotFoundException cnfe) {
                foundCorrectType = false;
            }
        } else {
            foundCorrectType = requiredType.equals(typeName);
        }
        if (!foundCorrectType) {
            throwNoMethodFound(requiredType + " " + javaClassName, parmTypeNames, primParmTypeNames);
        }
        // type we need to promote to an object so we can return null.
        if (promoteName != null)
            typeName = promoteName;
        // MethodCallNode DERBY-2972
        if (routineInfo.getReturnType() != null)
            setCollationType(routineInfo.getReturnType().getCollationType());
    }
    setJavaTypeName(typeName);
    methodParameterTypes = classInspector.getParameterTypes(method);
    String methodParameter = null;
    for (int i = 0; i < methodParameterTypes.length; i++) {
        methodParameter = methodParameterTypes[i];
        if (routineInfo != null) {
            if (i < routineInfo.getParameterCount()) {
                int parameterMode = routineInfo.getParameterModes()[getRoutineArgIdx(i)];
                switch(parameterMode) {
                    case (ParameterMetaData.parameterModeIn):
                        break;
                    case (ParameterMetaData.parameterModeInOut):
                        // we need to see if the type of the array is
                        // primitive, not the array itself.
                        methodParameter = stripOneArrayLevel(methodParameter);
                        break;
                    case (ParameterMetaData.parameterModeOut):
                        // value is not obtained *from* parameter.
                        continue;
                }
            }
        }
        // 
        if (hasVarargs() && (i >= getFirstVarargIdx())) {
            methodParameter = stripOneArrayLevel(methodParameter);
        }
        if (ClassInspector.primitiveType(methodParameter)) {
            // corresponding to the vararg
            if (i < methodParms.length) {
                methodParms[i].castToPrimitive(true);
            }
        }
    }
    // casting may be needed on the trailing varargs
    if (hasVarargs()) {
        int firstVarargIdx = getFirstVarargIdx();
        int trailingVarargCount = methodParms.length - firstVarargIdx;
        // the first vararg was handled in the preceding loop
        for (int i = 1; i < trailingVarargCount; i++) {
            if (ClassInspector.primitiveType(methodParameter)) {
                methodParms[i + firstVarargIdx].castToPrimitive(true);
            }
        }
    }
    /* Set type info for any null parameters */
    if (someParametersAreNull()) {
        setNullParameterInfo(methodParameterTypes);
    }
    /* bug 4450 - if the callable statement is ? = call form, generate the metadata
		infor for the return parameter. We don't really need that info in order to
		execute the callable statement. But with jdbc3.0, this information should be
		made available for return parameter through ParameterMetaData class.
		Parser sets a flag in compilercontext if ? = call. If the flag is set,
		we generate the metadata info for the return parameter and reset the flag
		in the compilercontext for future call statements*/
    DataTypeDescriptor dts = DataTypeDescriptor.getSQLDataTypeDescriptor(typeName);
    if (getCompilerContext().getReturnParameterFlag()) {
        getParameterTypes()[0] = dts;
    }
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) UserDefinedTypeIdImpl(org.apache.derby.catalog.types.UserDefinedTypeIdImpl) ClassInspector(org.apache.derby.iapi.services.loader.ClassInspector) ResultSet(java.sql.ResultSet) TypeDescriptorImpl(org.apache.derby.catalog.types.TypeDescriptorImpl) TypeCompiler(org.apache.derby.iapi.sql.compile.TypeCompiler)

Aggregations

TypeId (org.apache.derby.iapi.types.TypeId)53 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)32 TypeCompiler (org.apache.derby.iapi.sql.compile.TypeCompiler)8 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)6 ClassFactory (org.apache.derby.iapi.services.loader.ClassFactory)4 UserDefinedTypeIdImpl (org.apache.derby.catalog.types.UserDefinedTypeIdImpl)3 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)3 StandardException (org.apache.derby.shared.common.error.StandardException)3 TypeDescriptor (org.apache.derby.catalog.TypeDescriptor)2 DefaultInfoImpl (org.apache.derby.catalog.types.DefaultInfoImpl)2 RoutineAliasInfo (org.apache.derby.catalog.types.RoutineAliasInfo)2 ClassInspector (org.apache.derby.iapi.services.loader.ClassInspector)2 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)2 ProviderList (org.apache.derby.iapi.sql.depend.ProviderList)2 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)2 JSQLType (org.apache.derby.iapi.types.JSQLType)2 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Properties (java.util.Properties)1