Package org.apache.derby.iapi.services.compiler

Examples of org.apache.derby.iapi.services.compiler.LocalField


    if(cascadeDelete)
    {
      parentResultSetId = targetTableDescriptor.getSchemaName() +
                             "." + targetTableDescriptor.getName();
      // Generate the code to build the array
      LocalField arrayField =
        acb.newFieldDeclaration(Modifier.PRIVATE, resultSetArrayType);
      mb.pushNewArray(ClassName.ResultSet, dependentNodes.length)// new ResultSet[size]
      mb.setField(arrayField);
      for(int index=0 ; index <  dependentNodes.length ; index++)
      {
View Full Code Here


  {
    int nargs = 0;
    String receiverType = null;

    /* Allocate an object for re-use to hold the result of the operator */
    LocalField field = acb.newFieldDeclaration(Modifier.PRIVATE, resultInterfaceType);

    receiver.generateExpression(acb, mb);
    if (operatorType == TRIM)
    {
      mb.push(trimType);
View Full Code Here

    /*
    ** Generate the code for a Boolean false constant value.
    */
    String interfaceName = getTypeCompiler().interfaceName();
    LocalField field = acb.newFieldDeclaration(Modifier.PRIVATE, interfaceName);
    /*
    ** Generate the call to the equals method.
    ** equals is only on Orderable, not any subinterfaces.
    */

 
View Full Code Here

      String typeName = getTypeId().getCorrespondingJavaTypeName();

      mb.push(value.toString());
      mb.callMethod(VMOpcode.INVOKESTATIC, typeName, "valueOf", typeName, 1);

      LocalField field = acb.newFieldDeclaration(Modifier.PRIVATE, fieldType);

      acb.generateDataValue(mb, tc, field);
    }
  }
View Full Code Here

    String subqueryTypeString =
              getTypeCompiler().interfaceName();
    MethodBuilder  mb = acb.newGeneratedFun(subqueryTypeString, Modifier.PROTECTED);

    /* Declare the field to hold the suquery's ResultSet tree */
    LocalField rsFieldLF = acb.newFieldDeclaration(Modifier.PRIVATE, ClassName.NoPutResultSet);

    ResultSetNode subNode = null;

    if (!isMaterializable())
    {
            MethodBuilder executeMB = acb.getExecuteMethod();
      if (pushedNewPredicate && (! hasCorrelatedCRs()))
      {
        /* We try to materialize the subquery if it can fit in the memory.  We
         * evaluate the subquery first.  If the result set fits in the memory,
         * we replace the resultset with in-memory unions of row result sets.
         * We do this trick by replacing the child result with a new node --
         * MaterializeSubqueryNode, which essentially generates the suitable
         * code to materialize the subquery if possible.  This may have big
         * performance improvement.  See beetle 4373.
         */
        if (SanityManager.DEBUG)
        {
          SanityManager.ASSERT(resultSet instanceof ProjectRestrictNode,
            "resultSet expected to be a ProjectRestrictNode!");
        }
        subNode = ((ProjectRestrictNode) resultSet).getChildResult();
        LocalField subRS = acb.newFieldDeclaration(Modifier.PRIVATE, ClassName.NoPutResultSet);
        mb.getField(subRS);
        mb.conditionalIfNull();

        ResultSetNode materialSubNode = new MaterializeSubqueryNode(subRS);

        // Propagate the resultSet's cost estimate to the new node.
        materialSubNode.costEstimate = resultSet.getFinalCostEstimate();

        ((ProjectRestrictNode) resultSet).setChildResult(materialSubNode);

        /* Evaluate subquery resultset here first.  Next time when we come to
         * this subquery it may be replaced by a bunch of unions of rows.
         */
        subNode.generate(acb, mb);
        mb.startElseCode();
        mb.getField(subRS);
        mb.completeConditional();
   
        mb.setField(subRS);

                executeMB.pushNull( ClassName.NoPutResultSet);
                executeMB.setField(subRS);
      }

            executeMB.pushNull( ClassName.NoPutResultSet);
            executeMB.setField(rsFieldLF);
      // now we fill in the body of the conditional
      mb.getField(rsFieldLF);
      mb.conditionalIfNull();
    }

    acb.pushGetResultSetFactoryExpression(mb);

    // start of args
    int nargs;

    /* Inside here is where subquery could already have been materialized. 4373
     */
    resultSet.generate(acb, mb);

    /* Get the next ResultSet #, so that we can number the subquery's
     * empty row ResultColumnList and Once/Any ResultSet.
     */
    int subqResultSetNumber = cc.getNextResultSetNumber();

    /* We will be reusing the RCL from the subquery's ResultSet for the
     * empty row function.  We need to reset the resultSetNumber in the
     * RCL, before we generate that function.  Now that we've called
     * generate() on the subquery's ResultSet, we can reset that
     * resultSetNumber.
     */
    resultSet.getResultColumns().setResultSetNumber(subqResultSetNumber);

    /* Generate code for empty row */
    resultSet.getResultColumns().generateNulls(acb, mb);

    /*
     *  arg1: suqueryExpress - Expression for subquery's
     *      ResultSet
     *  arg2: Activation
     *  arg3: Method to generate Row with null(s) if subquery
     *      Result Set is empty
     */
    if (subqueryType == EXPRESSION_SUBQUERY)
    {
      int cardinalityCheck;

      /* No need to do sort if subquery began life as a distinct expression subquery.
       * (We simply check for a single unique value at execution time.)
       * No need for cardinality check if we know that underlying
       * ResultSet can contain at most 1 row.
       * RESOLVE - Not necessary if we know we
       * are getting a single row because of a unique index.
       */
      if (distinctExpression)
      {
        cardinalityCheck = OnceResultSet.UNIQUE_CARDINALITY_CHECK;
      }
      else if (resultSet.returnsAtMostOneRow())
      {
        cardinalityCheck = OnceResultSet.NO_CARDINALITY_CHECK;
      }
      else
      {
        cardinalityCheck = OnceResultSet.DO_CARDINALITY_CHECK;
      }

      /*  arg4: int - whether or not cardinality check is required
       *        DO_CARDINALITY_CHECK - required
       *        NO_CARDINALITY_CHECK - not required
       *        UNIQUE_CARDINALITY_CHECK - verify single
       *                      unique value
       */
      mb.push(cardinalityCheck);
      nargs = 8;

    } else {
      nargs = 7;
    }

    mb.push(subqResultSetNumber);
    mb.push(subqueryNumber);
    mb.push(pointOfAttachment);
    mb.push(costEstimate.rowCount());
    mb.push(costEstimate.getEstimatedCost());

    mb.callMethod(VMOpcode.INVOKEINTERFACE, (String) null, resultSetString, ClassName.NoPutResultSet, nargs);



    /* Fill in the body of the method
     * generates the following.
     * (NOTE: the close() method only generated for
     * materialized subqueries.  All other subqueries
     * closed by top result set in the query.):
     *
     *  NoPutResultSet  rsFieldX;
     *  {
     *    <Datatype interface> col;
     *    ExecRow r;
     *    rsFieldX = (rsFieldX == null) ? outerRSCall() : rsFieldX; // <== NONmaterialized specific
     *    rsFieldX.openCore();
     *    r = rsFieldX.getNextRowCore();
     *    col = (<Datatype interface>) r.getColumn(1);
     *    return col;
     *  }
     *
     * MATERIALIZED:
     *  NoPutResultSet  rsFieldX;
     *  {
     *    <Datatype interface> col;
     *    ExecRow r;
     *    rsFieldX = outerRSCall();
     *    rsFieldX.openCore();
     *    r = rsFieldX.getNextRowCore();
     *    col = (<Datatype interface>) r.getColumn(1);
     *    rsFieldX.close();                // <== materialized specific
     *    return col;
     *  }
     * and adds it to exprFun
     */

    /* Generate the declarations */ // PUSHCOMPILE
    //VariableDeclaration colVar = mb.addVariableDeclaration(subqueryTypeString);
    //VariableDeclaration rVar   = mb.addVariableDeclaration(ClassName.ExecRow);

    if (!isMaterializable())
    {
      /* put it back
       */
      if (pushedNewPredicate && (! hasCorrelatedCRs()))
        ((ProjectRestrictNode) resultSet).setChildResult(subNode);

      // now we fill in the body of the conditional
      mb.startElseCode();
        mb.getField(rsFieldLF);
      mb.completeConditional();
    }
   
    mb.setField(rsFieldLF);

    /* rs.openCore() */
    mb.getField(rsFieldLF);
    mb.callMethod(VMOpcode.INVOKEINTERFACE, (String) null, "openCore", "void", 0);

    /* r = rs.next() */
    mb.getField(rsFieldLF);
    mb.callMethod(VMOpcode.INVOKEINTERFACE, (String) null, "getNextRowCore", ClassName.ExecRow, 0);
    //mb.putVariable(rVar);
    //mb.endStatement();

    /* col = (<Datatype interface>) r.getColumn(1) */
    //mb.getVariable(rVar);
    mb.push(1); // both the Row interface and columnId are 1-based
    mb.callMethod(VMOpcode.INVOKEINTERFACE, ClassName.Row, "getColumn", ClassName.DataValueDescriptor, 1);
    mb.cast(subqueryTypeString);
    //mb.putVariable(colVar);
    //mb.endStatement();

    /* Only generate the close() method for materialized
     * subqueries.  All others will be closed when the
     * close() method is called on the top ResultSet.
     */
    if (isMaterializable())
    {
      /* rs.close() */
      mb.getField(rsFieldLF);
      mb.callMethod(VMOpcode.INVOKEINTERFACE, ClassName.ResultSet, "close", "void", 0);
    }

    /* return col */
    //mb.getVariable(colVar);
    mb.methodReturn();
    mb.complete();

    /*
    ** If we have an expression subquery, then we
    ** can materialize it if it has no correlated
    ** column references and is invariant.
    */
    if (isMaterializable())
    {
      LocalField lf = generateMaterialization(acb, mb, subqueryTypeString);
      mbex.getField(lf);

    } else {
      /* Generate the call to the new method */
      mbex.pushThis();
 
View Full Code Here

      String       type)
  {
    MethodBuilder mb = acb.executeMethod;

    // declare field
    LocalField field = acb.newFieldDeclaration(Modifier.PRIVATE, type);

    /* Generate the call to the new method */
    mb.pushThis();
    mb.callMethod(VMOpcode.INVOKEVIRTUAL, (String) null, mbsq.getName(), type, 0);

View Full Code Here

    mb.cast(receiverType);

    if (needField) {

      /* Allocate an object for re-use to hold the result of the operator */
      LocalField field = acb.newFieldDeclaration(Modifier.PRIVATE, resultTypeName);
      mb.getField(field);

      /* If we're calling a method on a class (SqlXmlExecutor) instead
       * of calling a method on the operand interface, then we invoke
       * VIRTUAL; we then have 2 args (the operand and the local field)
View Full Code Here

      ** 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 */
 
View Full Code Here

      // application could retain a reference to it and corrupt
      // future calls with the same CallableStatement object.

      String methodParameterType = methodParameterTypes[parameterNumber];
      String arrayType = methodParameterType.substring(0, methodParameterType.length() - 2);
      LocalField lf = acb.newFieldDeclaration(Modifier.PRIVATE, methodParameterType);

      if (outParamArrays == null)
        outParamArrays = new LocalField[methodParms.length];

      outParamArrays[parameterNumber] = lf;
View Full Code Here

      mb.pushThis();
    }

    int nargs = generateParameters(acb, mb);

    LocalField functionEntrySQLAllowed = null;

    if (routineInfo != null) {

      short sqlAllowed = routineInfo.getSQLAllowed();

      // Before we set up our authorization level, add a check to see if this
      // method can be called. If the routine is NO SQL or CONTAINS SQL
      // then there is no need for a check. As follows:
      //
      // Current Level = NO_SQL - CALL will be rejected when getting CALL result set
      // Current Level = anything else - calls to procedures defined as NO_SQL and CONTAINS SQL both allowed.


      if (sqlAllowed != RoutineAliasInfo.NO_SQL)
      {
       
        int sqlOperation;
       
        if (sqlAllowed == RoutineAliasInfo.READS_SQL_DATA)
          sqlOperation = Authorizer.SQL_SELECT_OP;
        else if (sqlAllowed == RoutineAliasInfo.MODIFIES_SQL_DATA)
          sqlOperation = Authorizer.SQL_WRITE_OP;
        else
          sqlOperation = Authorizer.SQL_ARBITARY_OP;
       
        generateAuthorizeCheck((ActivationClassBuilder) acb, mb, sqlOperation);
      }

      int statmentContextReferences = isSystemCode ? 2 : 1;
     
      boolean isFunction = routineInfo.getReturnType() != null;

      if (isFunction)
        statmentContextReferences++;


      if (statmentContextReferences != 0) {
        acb.pushThisAsActivation(mb);
        mb.callMethod(VMOpcode.INVOKEINTERFACE, null,
                  "getLanguageConnectionContext", ClassName.LanguageConnectionContext, 0);
        mb.callMethod(VMOpcode.INVOKEINTERFACE, null,
                  "getStatementContext", "org.apache.derby.iapi.sql.conn.StatementContext", 0);

        for (int scc = 1; scc < statmentContextReferences; scc++)
          mb.dup();
      }

      /**
        Set the statement context to reflect we are running
        System procedures, so that we can execute non-standard SQL.
      */
      if (isSystemCode) {
        mb.callMethod(VMOpcode.INVOKEINTERFACE, null,
                  "setSystemCode", "void", 0);
      }

      // for a function we need to fetch the current SQL control
      // so that we can reset it once the function is complete.
      //
      if (isFunction)
      {
        functionEntrySQLAllowed = acb.newFieldDeclaration(Modifier.PRIVATE, "short");
        mb.callMethod(VMOpcode.INVOKEINTERFACE, null,
                  "getSQLAllowed", "short", 0);
        mb.setField(functionEntrySQLAllowed);

      }
     
     
      // Set up the statement context to reflect the
      // restricted SQL execution allowed by this routine.

      mb.push(sqlAllowed);
      mb.push(false);
      mb.callMethod(VMOpcode.INVOKEINTERFACE, null,
                "setSQLAllowed", "void", 2);

    }

    // add in the ResultSet arrays.
    if (routineInfo != null) {

      int compiledResultSets = methodParameterTypes.length - methodParms.length;

      if (compiledResultSets != 0) {

        // Add a method that indicates the maxium number of dynamic result sets.
        int maxDynamicResults = routineInfo.getMaxDynamicResultSets();
        if (maxDynamicResults > 0) {
          MethodBuilder gdr = acb.getClassBuilder().newMethodBuilder(Modifier.PUBLIC, "int", "getMaxDynamicResults");
          gdr.push(maxDynamicResults);
          gdr.methodReturn();
          gdr.complete();
        }

        // add a method to return all the dynamic result sets (unordered)
        MethodBuilder gdr = acb.getClassBuilder().newMethodBuilder(Modifier.PUBLIC, "java.sql.ResultSet[][]", "getDynamicResults");

        MethodBuilder cons = acb.getConstructor();
        // if (procDef.getParameterStyle() == RoutineAliasInfo.PS_JAVA)
        {
          // PARAMETER STYLE JAVA

          LocalField procedureResultSetsHolder = acb.newFieldDeclaration(Modifier.PRIVATE, "java.sql.ResultSet[][]");

          // getDynamicResults body
          gdr.getField(procedureResultSetsHolder);

          // create the holder of all the ResultSet arrays, new java.sql.ResultSet[][compiledResultSets]
          cons.pushNewArray("java.sql.ResultSet[]", compiledResultSets);
          cons.setField(procedureResultSetsHolder);


          // arguments for the dynamic result sets
          for (int i = 0; i < compiledResultSets; i++) {

            mb.pushNewArray("java.sql.ResultSet", 1);
            mb.dup();

            mb.getField(procedureResultSetsHolder);
            mb.swap();

            mb.setArrayElement(i);
          }
        }

        // complete the method that returns the ResultSet[][] to the
        gdr.methodReturn();
        gdr.complete();

        nargs += compiledResultSets;
      }

    }

    String javaReturnType = getJavaTypeName();

    MethodBuilder mbnc = null;
    MethodBuilder mbcm = mb;


    // If any of the parameters are null then
    // do not call the method, just return null.
    if (returnsNullOnNullState != null)
    {
      mbnc = acb.newGeneratedFun(javaReturnType, Modifier.PRIVATE, methodParameterTypes);

      // add the throws clause for the public static method we are going to call.
      Class[] throwsSet = ((java.lang.reflect.Method) method).getExceptionTypes();
      for (int te = 0; te < throwsSet.length; te++)
      {
        mbnc.addThrownException(throwsSet[te].getName());
      }

      mbnc.getField(returnsNullOnNullState);
      mbnc.conditionalIf();

      // set up for a null!!
      // for objects is easy.
      mbnc.pushNull(javaReturnType);

      mbnc.startElseCode()

      if (!actualMethodReturnType.equals(javaReturnType))
        mbnc.pushNewStart(javaReturnType);

      // fetch all the arguments
      for (int pa = 0; pa < nargs; pa++)
      {
        mbnc.getParameter(pa);
      }

      mbcm = mbnc;
    }

    mbcm.callMethod(VMOpcode.INVOKESTATIC, method.getDeclaringClass().getName(), methodName,
          actualMethodReturnType, nargs);


    if (returnsNullOnNullState != null)
    {
      if (!actualMethodReturnType.equals(javaReturnType))
        mbnc.pushNewComplete(1);

      mbnc.completeConditional();

      mbnc.methodReturn();
      mbnc.complete();

      // now call the wrapper method
      mb.callMethod(VMOpcode.INVOKEVIRTUAL, acb.getClassBuilder().getFullName(), mbnc.getName(),
          javaReturnType, nargs);
      mbnc = null;
    }


    if (routineInfo != null) {

      // reset the SQL allowed setting that we set upon
      // entry to the method.
      if (functionEntrySQLAllowed != null) {
        acb.pushThisAsActivation(mb);
        mb.callMethod(VMOpcode.INVOKEINTERFACE, null,
                  "getLanguageConnectionContext", ClassName.LanguageConnectionContext, 0);
        mb.callMethod(VMOpcode.INVOKEINTERFACE, null,
                  "getStatementContext", "org.apache.derby.iapi.sql.conn.StatementContext", 0);
        mb.getField(functionEntrySQLAllowed);
        mb.push(true); // override as we are ending the control set by this function all.
        mb.callMethod(VMOpcode.INVOKEINTERFACE, null,
                  "setSQLAllowed", "void", 2);

      }

      if (outParamArrays != null) {

        MethodBuilder constructor = acb.getConstructor();

        // constructor  - setting up correct paramter type info
        acb.pushThisAsActivation(constructor);
        constructor.callMethod(VMOpcode.INVOKEINTERFACE, null,
                  "getParameterValueSet", ClassName.ParameterValueSet, 0);

        // execute  - passing out parameters back.
        acb.pushThisAsActivation(mb);
        mb.callMethod(VMOpcode.INVOKEINTERFACE, null,
                  "getParameterValueSet", ClassName.ParameterValueSet, 0);

        int[] parameterModes = routineInfo.getParameterModes();
        for (int i = 0; i < outParamArrays.length; i++) {

          int parameterMode = parameterModes[i];
          if (parameterMode != JDBC30Translation.PARAMETER_MODE_IN) {

            // must be a parameter if it is INOUT or OUT.
            ValueNode sqlParamNode = ((SQLToJavaValueNode) methodParms[i]).getSQLValueNode();


            int applicationParameterNumber = applicationParameterNumbers[i];

            // Set the correct parameter nodes in the ParameterValueSet at constructor time.
            constructor.dup();
            constructor.push(applicationParameterNumber);
            constructor.push(parameterMode);
            constructor.callMethod(VMOpcode.INVOKEINTERFACE, null,
                    "setParameterMode", "void", 2);

            // Pass the value of the outparameters back to the calling code
            LocalField lf = outParamArrays[i];

            mb.dup();
            mb.push(applicationParameterNumber);
            mb.callMethod(VMOpcode.INVOKEINTERFACE, null,
                  "getParameter", ClassName.DataValueDescriptor, 1);
View Full Code Here

TOP

Related Classes of org.apache.derby.iapi.services.compiler.LocalField

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.