Search in sources :

Example 6 with ClassInspector

use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.

the class ConditionalNode method bindExpression.

/**
 * Bind this expression.  This means binding the sub-expressions,
 * as well as figuring out what the return type is for this expression.
 *
 * @param fromList		The FROM list for the query this
 *				expression is in, for binding columns.
 * @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 {
    CompilerContext cc = getCompilerContext();
    int previousReliability = orReliability(CompilerContext.CONDITIONAL_RESTRICTION);
    ValueNodeList caseOperandParameters = bindCaseOperand(cc, fromList, subqueryList, aggregates);
    testConditions.bindExpression(fromList, subqueryList, aggregates);
    // parameter), find out which type best describes it.
    if (caseOperandParameters != null) {
        // when testConditions was bound.
        for (ValueNode vn : caseOperandParameters) {
            // Check that this parameter is comparable to all the other
            // parameters in the list. This indirectly checks whether
            // all when operands have compatible types.
            caseOperandParameters.comparable(vn);
            // Replace the dummy parameter node with the actual case
            // operand.
            testConditions.accept(new ReplaceNodeVisitor(vn, caseOperand));
        }
        // Finally, after we have determined that all the when operands
        // are compatible, and we have reinserted the case operand into
        // the tree, set the type of the case operand to the dominant
        // type of all the when operands.
        caseOperand.setType(caseOperandParameters.getDominantTypeServices());
    }
    thenElseList.bindExpression(fromList, subqueryList, aggregates);
    // Find the type of the first typed value in thenElseList and cast
    // all untyped NULL values to that type. We don't need to find the
    // dominant type here, since a top-level cast to that type will be
    // added later, if necessary.
    DataTypeDescriptor nullType = thenElseList.getTypeServices();
    if (nullType == null) {
        // an error.
        throw StandardException.newException(SQLState.LANG_ALL_RESULT_EXPRESSIONS_UNTYPED);
    } else {
        recastNullNodes(nullType, fromList, subqueryList, aggregates);
    }
    // Set the result type of this conditional to be the dominant type
    // of the result expressions.
    setType(thenElseList.getDominantTypeServices());
    /* testCondition must be a boolean expression.
		 * If it is a ? parameter on the left, then set type to boolean,
		 * otherwise verify that the result type is boolean.
		 */
    testConditions.setParameterDescriptor(new DataTypeDescriptor(TypeId.BOOLEAN_ID, true));
    for (ValueNode testCondition : testConditions) {
        if (!testCondition.getTypeServices().getTypeId().equals(TypeId.BOOLEAN_ID)) {
            throw StandardException.newException(SQLState.LANG_CONDITIONAL_NON_BOOLEAN);
        }
    }
    // Set the type of the parameters.
    thenElseList.setParameterDescriptor(getTypeServices());
    /* The then and else expressions must be type compatible */
    ClassInspector cu = getClassFactory().getClassInspector();
    /*
		** If it is comparable, then we are ok.  Note that we
		** could in fact allow any expressions that are convertible()
		** since we are going to generate a cast node, but that might
		** be confusing to users...
		*/
    for (ValueNode expr : thenElseList) {
        DataTypeDescriptor dtd = expr.getTypeServices();
        String javaTypeName = dtd.getTypeId().getCorrespondingJavaTypeName();
        String resultJavaTypeName = getTypeId().getCorrespondingJavaTypeName();
        if (!dtd.comparable(getTypeServices(), false, getClassFactory()) && !cu.assignableTo(javaTypeName, resultJavaTypeName) && !cu.assignableTo(resultJavaTypeName, javaTypeName)) {
            throw StandardException.newException(SQLState.LANG_NOT_TYPE_COMPATIBLE, dtd.getTypeId().getSQLTypeName(), getTypeId().getSQLTypeName());
        }
    }
    // The result is nullable if and only if at least one of the result
    // expressions is nullable (DERBY-6567).
    setNullability(thenElseList.isNullable());
    /*
		** Generate a CastNode if necessary and
		** stick it over the original expression
		*/
    TypeId condTypeId = getTypeId();
    for (int i = 0; i < thenElseList.size(); i++) {
        ValueNode expr = thenElseList.elementAt(i);
        if (expr.getTypeId().typePrecedence() != condTypeId.typePrecedence()) {
            // Cast to dominant type.
            ValueNode cast = new CastNode(expr, getTypeServices(), getContextManager());
            cast = cast.bindExpression(fromList, subqueryList, aggregates);
            thenElseList.setElementAt(cast, i);
        }
    }
    cc.setReliability(previousReliability);
    return this;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) ClassInspector(org.apache.derby.iapi.services.loader.ClassInspector) CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext)

Example 7 with ClassInspector

use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.

the class LuceneSupport method getIndexDescriptorNoPrivs.

/**
 * Invoke a static method (possibly supplied by the user) to instantiate an index descriptor.
 * The method has no arguments.
 */
static LuceneIndexDescriptor getIndexDescriptorNoPrivs(String indexDescriptorMaker) throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException {
    int lastDotIdx = indexDescriptorMaker.lastIndexOf(".");
    String className = indexDescriptorMaker.substring(0, lastDotIdx);
    ClassInspector ci = getClassFactory().getClassInspector();
    Class<? extends Object> klass = ci.getClass(className);
    String methodName = indexDescriptorMaker.substring(lastDotIdx + 1, indexDescriptorMaker.length());
    Method method = klass.getDeclaredMethod(methodName);
    return (LuceneIndexDescriptor) method.invoke(null);
}
Also used : ClassInspector(org.apache.derby.iapi.services.loader.ClassInspector) LuceneIndexDescriptor(org.apache.derby.optional.api.LuceneIndexDescriptor) Method(java.lang.reflect.Method)

Example 8 with ClassInspector

use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.

the class QueryTreeNode method verifyClassExist.

/**
 * Verify that a java class exists, is accessible (public)
 * and not a class representing a primitive type.
 * @param javaClassName	The name of the java class to resolve.
 *
 * @exception StandardException		Thrown on error
 */
void verifyClassExist(String javaClassName) throws StandardException {
    ClassInspector classInspector = getClassFactory().getClassInspector();
    Throwable reason = null;
    boolean foundMatch = false;
    try {
        foundMatch = classInspector.accessible(javaClassName);
    } catch (ClassNotFoundException cnfe) {
        reason = cnfe;
    }
    if (!foundMatch)
        throw StandardException.newException(SQLState.LANG_TYPE_DOESNT_EXIST2, reason, javaClassName);
    if (ClassInspector.primitiveType(javaClassName))
        throw StandardException.newException(SQLState.LANG_TYPE_DOESNT_EXIST3, javaClassName);
}
Also used : ClassInspector(org.apache.derby.iapi.services.loader.ClassInspector)

Example 9 with ClassInspector

use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.

the class StaticClassFieldReferenceNode method bindExpression.

/**
 * Bind this expression.  This means binding the sub-expressions,
 * as well as figuring out what the return type is for this expression.
 *
 * @param fromList		The FROM list for the query this
 *                      expression is in, for binding columns.
 * @param subqueryList  The subquery list being built as we find SubqueryNodes
 * @param aggregates    The aggregate list being built as we find AggregateNodes
 *
 * @return	Nothing
 *
 * @exception StandardException		Thrown on error
 */
JavaValueNode bindExpression(FromList fromList, SubqueryList subqueryList, List<AggregateNode> aggregates) throws StandardException {
    ClassInspector classInspector = getClassFactory().getClassInspector();
    if (((getCompilerContext().getReliability() & CompilerContext.INTERNAL_SQL_ILLEGAL) != 0) || !javaClassName.startsWith("java.sql.")) {
        throw StandardException.newException(SQLState.LANG_SYNTAX_ERROR, javaClassName + "::" + fieldName);
    }
    verifyClassExist(javaClassName);
    /*
		** Find the field that is public.
		*/
    field = classInspector.findPublicField(javaClassName, fieldName, true);
    /* Get the field type */
    setJavaTypeName(classInspector.getType(field));
    return this;
}
Also used : ClassInspector(org.apache.derby.iapi.services.loader.ClassInspector)

Example 10 with ClassInspector

use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.

the class ColumnDefinitionNode method checkUserType.

/**
 * Check the validity of a user type.  Checks whether this column
 * definition describes a user type that either doesn't exist or is
 * inaccessible, or that doesn't implement Serializable.
 *
 * @exception StandardException		Thrown on error
 */
void checkUserType(TableDescriptor td) throws StandardException {
    String columnTypeName;
    // omitted. we can't check generation clauses until later on
    if (hasGenerationClause() && (getType() == null)) {
        return;
    }
    /* Built-in types need no checking */
    if (!getType().getTypeId().userType())
        return;
    // bind the UDT if necessary
    setType(bindUserType(getType()));
    ClassInspector classInspector = getClassFactory().getClassInspector();
    columnTypeName = getType().getTypeId().getCorrespondingJavaTypeName();
    /* User type - We first check for the columnTypeName as a java class.
		 * If that fails, then we treat it as a class alias.
		 */
    boolean foundMatch = false;
    Throwable reason = null;
    try {
        foundMatch = classInspector.accessible(columnTypeName);
    } catch (ClassNotFoundException cnfe) {
        reason = cnfe;
    }
    if (!foundMatch) {
        throw StandardException.newException(SQLState.LANG_TYPE_DOESNT_EXIST, reason, columnTypeName, name);
    }
    if (!classInspector.assignableTo(columnTypeName, "java.io.Serializable") && // Before Java2, SQLData is not defined, assignableTo call returns false
    !classInspector.assignableTo(columnTypeName, "java.sql.SQLData")) {
        getCompilerContext().addWarning(StandardException.newWarning(SQLState.LANG_TYPE_NOT_SERIALIZABLE, columnTypeName, name));
    }
}
Also used : ClassInspector(org.apache.derby.iapi.services.loader.ClassInspector)

Aggregations

ClassInspector (org.apache.derby.iapi.services.loader.ClassInspector)12 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)2 TypeId (org.apache.derby.iapi.types.TypeId)2 Member (java.lang.reflect.Member)1 Method (java.lang.reflect.Method)1 ResultSet (java.sql.ResultSet)1 TypeDescriptor (org.apache.derby.catalog.TypeDescriptor)1 TypeDescriptorImpl (org.apache.derby.catalog.types.TypeDescriptorImpl)1 UserDefinedTypeIdImpl (org.apache.derby.catalog.types.UserDefinedTypeIdImpl)1 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)1 TypeCompiler (org.apache.derby.iapi.sql.compile.TypeCompiler)1 LuceneIndexDescriptor (org.apache.derby.optional.api.LuceneIndexDescriptor)1