Search in sources :

Example 31 with LocalField

use of org.apache.derby.iapi.services.compiler.LocalField in project derby by apache.

the class OperatorNode method pushSqlXmlUtil.

/**
 * <p>
 * Generate code that pushes an SqlXmlUtil instance onto the stack. The
 * instance will be created and cached in the activation's constructor, so
 * that we don't need to create a new instance for every row.
 * </p>
 *
 * <p>
 * If the {@code xmlQuery} parameter is non-null, there will also be code
 * that compiles the query when the SqlXmlUtil instance is created.
 * </p>
 *
 * @param acb builder for the class in which the generated code lives
 * @param mb builder for the method that implements this operator
 * @param xmlQuery the XML query to be executed by the operator, or
 * {@code null} if this isn't an XMLEXISTS or XMLQUERY operator
 * @param xmlOpName the name of the operator (ignored if {@code xmlQuery}
 * is {@code null})
 */
static void pushSqlXmlUtil(ExpressionClassBuilder acb, MethodBuilder mb, String xmlQuery, String xmlOpName) {
    // Create a field in which the instance can be cached.
    LocalField sqlXmlUtil = acb.newFieldDeclaration(Modifier.PRIVATE | Modifier.FINAL, SqlXmlUtil.class.getName());
    // Add code that creates the SqlXmlUtil instance in the constructor.
    MethodBuilder constructor = acb.getConstructor();
    constructor.pushNewStart(SqlXmlUtil.class.getName());
    constructor.pushNewComplete(0);
    constructor.putField(sqlXmlUtil);
    // Compile the query, if one is specified.
    if (xmlQuery == null) {
        // No query. The SqlXmlUtil instance is still on the stack. Pop it
        // to restore the initial state of the stack.
        constructor.pop();
    } else {
        // Compile the query. This will consume the SqlXmlUtil instance
        // and leave the stack in its initial state.
        constructor.push(xmlQuery);
        constructor.push(xmlOpName);
        constructor.callMethod(VMOpcode.INVOKEVIRTUAL, SqlXmlUtil.class.getName(), "compileXQExpr", "void", 2);
    }
    // Read the cached value and push it onto the stack in the method
    // generated for the operator.
    mb.getField(sqlXmlUtil);
}
Also used : SqlXmlUtil(org.apache.derby.iapi.types.SqlXmlUtil) LocalField(org.apache.derby.iapi.services.compiler.LocalField) MethodBuilder(org.apache.derby.iapi.services.compiler.MethodBuilder)

Example 32 with LocalField

use of org.apache.derby.iapi.services.compiler.LocalField in project derby by apache.

the class JavaToSQLValueNode method generateExpression.

/**
 * Do code generation for this conversion of a value from the Java to
 * the SQL domain.
 *
 * @param acb	The ExpressionClassBuilder for the class we're generating
 * @param mb the method  the expression will go into
 *
 * @exception StandardException		Thrown on error
 */
@Override
void generateExpression(ExpressionClassBuilder acb, MethodBuilder mb) throws StandardException {
    TypeId resultType;
    String resultTypeName;
    /*
		** Tell the Java node that it's value is being returned to the
		** SQL domain.  This way, it knows whether the checking for a null
		** receiver is to be done at the Java level or the SQL level.
		*/
    javaNode.returnValueToSQLDomain();
    /* Generate the receiver, if any. */
    boolean hasReceiver = javaNode.generateReceiver(acb, mb);
    /*
		** If the java expression has a receiver, we want to check whether
		** it's null before evaluating the whole expression (to avoid
		** a NullPointerException.
		*/
    if (hasReceiver) {
        /*
			** There is a receiver.  Generate a null SQL value to return
			** in case the receiver is null.  First, create a field to hold
			** the null SQL value.
			*/
        String nullValueClass = getTypeCompiler().interfaceName();
        LocalField nullValueField = acb.newFieldDeclaration(Modifier.PRIVATE, nullValueClass);
        /*
			** There is a receiver.  Generate the following to test
			** for null:
			**
			**		(receiverExpression == null) ? 
			*/
        mb.conditionalIfNull();
        mb.getField(nullValueField);
        acb.generateNullWithExpress(mb, getTypeCompiler(), getTypeServices().getCollationType());
        /*
			** We have now generated the expression to test, and the
			** "true" side of the ?: operator.  Finish the "true" side
			** so we can generate the "false" side.
			*/
        mb.startElseCode();
    }
    resultType = getTypeId();
    TypeCompiler tc = getTypeCompiler();
    resultTypeName = tc.interfaceName();
    /* Allocate an object for re-use to hold the result of the conversion */
    LocalField field = acb.newFieldDeclaration(Modifier.PRIVATE, resultTypeName);
    /* Generate the expression for the Java value under us */
    javaNode.generateExpression(acb, mb);
    /* Generate the SQL value, which is always nullable */
    acb.generateDataValue(mb, tc, getTypeServices().getCollationType(), field);
    /*
		** If there was a receiver, the return value will be the result
		** of the ?: operator.
		*/
    if (hasReceiver) {
        mb.completeConditional();
    }
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) TypeCompiler(org.apache.derby.iapi.sql.compile.TypeCompiler) LocalField(org.apache.derby.iapi.services.compiler.LocalField)

Aggregations

LocalField (org.apache.derby.iapi.services.compiler.LocalField)32 MethodBuilder (org.apache.derby.iapi.services.compiler.MethodBuilder)18 OptimizablePredicate (org.apache.derby.iapi.sql.compile.OptimizablePredicate)3 TypeCompiler (org.apache.derby.iapi.sql.compile.TypeCompiler)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)1 CostEstimate (org.apache.derby.iapi.sql.compile.CostEstimate)1 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)1 SqlXmlUtil (org.apache.derby.iapi.types.SqlXmlUtil)1 TypeId (org.apache.derby.iapi.types.TypeId)1