Search in sources :

Example 6 with UserDefinedTypeIdImpl

use of org.apache.derby.catalog.types.UserDefinedTypeIdImpl in project derby by apache.

the class BaseExpressionActivation method minValue.

/**
 * <p>
 * Get the minimum 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 minimum 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
 * @param judgePrecision		precision of the judge
 * @param judgeScale		    scale of the judge
 * @param judgeIsNullable		nullability of the judge
 * @param judgeMaximumWidth		maximum width of the judge
 * @param judgeCollationType	collation type of the judge
 * @param judgeCollationDerivation		collation derivation of the judge
 *
 * @return	The minimum value of the 4.
 */
public static DataValueDescriptor minValue(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 minVal = v1;
    if (v2 != null && (minVal.isNull() || judge.lessThan(v2, minVal).equals(true)))
        minVal = v2;
    if (v3 != null && (minVal.isNull() || judge.lessThan(v3, minVal).equals(true)))
        minVal = v3;
    if (v4 != null && (minVal.isNull() || judge.lessThan(v4, minVal).equals(true)))
        minVal = v4;
    return minVal;
}
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 7 with UserDefinedTypeIdImpl

use of org.apache.derby.catalog.types.UserDefinedTypeIdImpl in project derby by apache.

the class QueryTreeNode method getUDTDesc.

/**
 * Get the AliasDescriptor of a UDT
 */
public AliasDescriptor getUDTDesc(DataTypeDescriptor dtd) throws StandardException {
    UserDefinedTypeIdImpl userTypeID = (UserDefinedTypeIdImpl) dtd.getTypeId().getBaseTypeId();
    DataDictionary dd = getDataDictionary();
    SchemaDescriptor typeSchema = getSchemaDescriptor(userTypeID.getSchemaName());
    char udtNameSpace = AliasInfo.ALIAS_NAME_SPACE_UDT_AS_CHAR;
    String unqualifiedTypeName = userTypeID.getUnqualifiedName();
    AliasDescriptor ad = dd.getAliasDescriptor(typeSchema.getUUID().toString(), unqualifiedTypeName, udtNameSpace);
    return ad;
}
Also used : SchemaDescriptor(org.apache.derby.iapi.sql.dictionary.SchemaDescriptor) UserDefinedTypeIdImpl(org.apache.derby.catalog.types.UserDefinedTypeIdImpl) AliasDescriptor(org.apache.derby.iapi.sql.dictionary.AliasDescriptor) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary)

Example 8 with UserDefinedTypeIdImpl

use of org.apache.derby.catalog.types.UserDefinedTypeIdImpl in project derby by apache.

the class DataTypeDescriptor method readExternal.

// Formatable methods
/**
 * Read this object from a stream of stored objects.
 *
 * @param in read this.
 *
 * @exception IOException					thrown on error
 * @exception ClassNotFoundException		thrown on error
 */
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    typeDescriptor = (TypeDescriptorImpl) in.readObject();
    // built-in types have different methods for getting the type id.
    if (typeDescriptor.isUserDefinedType()) {
        try {
            typeId = TypeId.getUserDefinedTypeId(((UserDefinedTypeIdImpl) typeDescriptor.getTypeId()).getClassName());
        } catch (StandardException se) {
            throw (IOException) new IOException(se.getMessage()).initCause(se);
        }
    } else {
        typeId = TypeId.getBuiltInTypeId(this.getTypeName());
    }
    collationDerivation = in.readInt();
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) UserDefinedTypeIdImpl(org.apache.derby.catalog.types.UserDefinedTypeIdImpl) IOException(java.io.IOException)

Example 9 with UserDefinedTypeIdImpl

use of org.apache.derby.catalog.types.UserDefinedTypeIdImpl 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

UserDefinedTypeIdImpl (org.apache.derby.catalog.types.UserDefinedTypeIdImpl)9 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)4 TypeId (org.apache.derby.iapi.types.TypeId)3 AliasDescriptor (org.apache.derby.iapi.sql.dictionary.AliasDescriptor)2 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)2 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)2 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)2 IOException (java.io.IOException)1 ResultSet (java.sql.ResultSet)1 TypeDescriptorImpl (org.apache.derby.catalog.types.TypeDescriptorImpl)1 ClassInspector (org.apache.derby.iapi.services.loader.ClassInspector)1 TypeCompiler (org.apache.derby.iapi.sql.compile.TypeCompiler)1 StandardException (org.apache.derby.shared.common.error.StandardException)1