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

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


  public void generate(ActivationClassBuilder acb,
                MethodBuilder mb) throws StandardException
  {
    if (indexOfSessionTableNamesInSavedObjects != -1 ) //if this cursor references session schema tables, do following
    {
      MethodBuilder constructor = acb.getConstructor();
      constructor.pushThis();
      constructor.push(indexOfSessionTableNamesInSavedObjects);
      constructor.putField(org.apache.derby.iapi.reference.ClassName.BaseActivation, "indexOfSessionTableNamesInSavedObjects", "int");
      constructor.endStatement();
    }

    // generate the parameters
    generateParameterValueSet(acb);
View Full Code Here


                   Vector  parameterList)
    throws StandardException
  {
    if (numberOfParameters > 0)
    {
      MethodBuilder  constructor = acb.getConstructor();

      /*
      ** Check the first parameter to see if it is a return
      ** parameter.
      */
      boolean hasReturnParam = ((ParameterNode)parameterList.elementAt(0)).isReturnOutputParam();

      /*
      ** Generate the following:
      **
      ** pvs =
      **    getLanguageConnectionContext()
      **      .getLanguageFactory()
      **          .getParameterValueSet(numberOfParameters);
      **
      ** pvs is a ParameterValueSet that lives in the superclass of
      ** the activation being generated.
      */

      constructor.pushThis(); // for the put field down below

      /* Generate the call to getContext */
      //?X constructor.pushThis();
      //?Xconstructor.callMethod(VMOpcode.INVOKEINTERFACE, ClassName.Activation, "getLanguageConnectionContext",
      //?X          ClassName.LanguageConnectionContext, 0);
      /*
      ** Call getLanguageFactory()
      */
      //?Xconstructor.callMethod(VMOpcode.INVOKEINTERFACE, (String) null, "getLanguageFactory",
      //?X          ClassName.LanguageFactory, 0);

      /*
      ** Call getParameterValueSet(<number of parameters>, <hasReturnParam>)
      */

      constructor.push(numberOfParameters); // first arg
      constructor.push(hasReturnParam); // second arg

      constructor.callMethod(VMOpcode.INVOKEVIRTUAL, ClassName.BaseActivation,
                  "setParameterValueSet", "void", 2);

      //?Xconstructor.callMethod(VMOpcode.INVOKEINTERFACE, (String) null, "getParameterValueSet",
      //?X          ClassName.ParameterValueSet, 2);

      /* Assign the return from getParameterValueSet() to the field */
      //?Xconstructor.putField(ClassName.BaseActivation, "pvs", ClassName.ParameterValueSet);
      //?Xconstructor.endStatement();

      /*
      ** Add a call to the execute() method to check
      ** for missing parameters
      */
      MethodBuilder  executeMethod = acb.getExecuteMethod();

      executeMethod.pushThis();
      executeMethod.callMethod(VMOpcode.INVOKEVIRTUAL, ClassName.BaseActivation, "throwIfMissingParms", "void", 0);
    }
  }
View Full Code Here

     * we just make it protected.  This generated class won't
     * have any subclasses, certainly! (nat 12/97)
     */
    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();
    }
View Full Code Here

  private LocalField generateMaterialization(
      ActivationClassBuilder  acb,
      MethodBuilder mbsq,
      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);

    // generate: field = value (value is on stack)
    mb.setField(field);

    return field;
  }
View Full Code Here

          String parameterType = methodParameterTypes[parameterNumber];

          if (parameterType.endsWith("[]")) {

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

            constructor.push(applicationParameterNumber);
            constructor.push(JDBC30Translation.PARAMETER_MODE_UNKNOWN);
            constructor.callMethod(VMOpcode.INVOKEINTERFACE, null,
                      "setParameterMode", "void", 2);
            constructor.endStatement();
          }
        }
      }
    }
View Full Code Here

      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);

            // see if we need to set the desired length/scale/precision of the type
            DataTypeDescriptor paramdtd = sqlParamNode.getTypeServices();

            boolean isNumericType = paramdtd.getTypeId().isNumericTypeId();

            // is the underlying type for the OUT/INOUT parameter primitive.
            boolean isPrimitive = ((java.lang.reflect.Method) method).getParameterTypes()[i].getComponentType().isPrimitive();

            if (isNumericType) {
              // need to up-cast as the setValue(Number) method only exists on NumberDataValue

              if (!isPrimitive)
                mb.cast(ClassName.NumberDataValue);
            }
            else if (paramdtd.getTypeId().isBooleanTypeId())
            {
              // need to cast as the setValue(Boolean) method only exists on BooleanDataValue
              if (!isPrimitive)
                mb.cast(ClassName.BooleanDataValue);
            }

            if (paramdtd.getTypeId().variableLength()) {
              // need another DVD reference for the set width below.
              mb.dup();
            }


            mb.getField(lf); // pvs, dvd, array
            mb.getArrayElement(0); // pvs, dvd, value

            // The value needs to be set thorugh the setValue(Number) method.
            if (isNumericType && !isPrimitive)
            {
              mb.upCast("java.lang.Number");
            }

            mb.callMethod(VMOpcode.INVOKEINTERFACE, null, "setValue", "void", 1);

            if (paramdtd.getTypeId().variableLength()) {
              mb.push(isNumericType ? paramdtd.getPrecision() : paramdtd.getMaximumWidth());
              mb.push(paramdtd.getScale());
              mb.push(isNumericType);
              mb.callMethod(VMOpcode.INVOKEINTERFACE, ClassName.VariableSizeDataValue, "setWidth", "void", 3);
              // mb.endStatement();
            }
          }
        }
        constructor.endStatement();
        mb.endStatement();
      }

    }
  }
View Full Code Here

    /* The array gets created in the constructor.
     * All constant elements in the array are initialized
     * in the constructor. 
     */
    /* Assign the initializer to the DataValueDescriptor[] field */
    MethodBuilder cb = acb.getConstructor();
    cb.pushNewArray(ClassName.DataValueDescriptor, argumentsListSize);
    cb.setField(arrayField);

    /* Set the array elements that are constant */
    int numConstants = 0;
    MethodBuilder nonConstantMethod = null;
    MethodBuilder currentConstMethod = cb;
    for (int index = 0; index < argumentsListSize; index++)
    {
      MethodBuilder setArrayMethod;
 
      if (argumentsList.elementAt(index) instanceof ConstantNode)
      {
        numConstants++;
   
        /*if too many statements are added  to a  method,
        *size of method can hit  65k limit, which will
        *lead to the class format errors at load time.
        *To avoid this problem, when number of statements added
        *to a method is > 2048, remaing statements are added to  a new function
        *and called from the function which created the function.
        *See Beetle 5135 or 4293 for further details on this type of problem.
        */
        if(currentConstMethod.statementNumHitLimit(1))
        {
          MethodBuilder genConstantMethod = acb.newGeneratedFun("void", Modifier.PRIVATE);
          currentConstMethod.pushThis();
          currentConstMethod.callMethod(VMOpcode.INVOKEVIRTUAL,
                          (String) null,
                          genConstantMethod.getName(),
                          "void", 0);
          //if it is a generate function, close the metod.
          if(currentConstMethod != cb){
            currentConstMethod.methodReturn();
            currentConstMethod.complete();
View Full Code Here

    */

    if (numberOfStopPredicates != 0)
    {
      /* This sets up the method and the static field */
      MethodBuilder exprFun = acb.newExprFun();

      /* Now we fill in the body of the method */
      LocalField rowField =
                generateIndexableRow(acb, numberOfStopPredicates);

View Full Code Here

        throws StandardException
  {
    ExpressionClassBuilder  acb         = (ExpressionClassBuilder) acbi;

    String                  retvalType  = ClassName.Qualifier + "[][]";
    MethodBuilder           consMB      = acb.getConstructor();
    MethodBuilder           executeMB   = acb.getExecuteMethod();

    /* Create and initialize the array of Qualifiers */
    LocalField qualField =
            acb.newFieldDeclaration(Modifier.PRIVATE, retvalType);


    /*
    ** Stick a reinitialize of the Qualifier array in execute().
    ** Done because although we call Exec/Qualifier.clearOrderableCache()
    ** before each query, we only clear the cache for VARIANT and
    ** SCAN_INVARIANT qualifiers.  However, each time the same
    ** statement is executed, even the QUERY_INVARIANT qualifiers
    ** need to be flushed.  For example:
    **  prepare select c1 from t where c1 = (select max(c1) from t) as p;
    **  execute p; -- we now have the materialized subquery result (1)
    **         -- in our predicate
    **  insert into t values 666;
    **  execute p; -- we need to clear out 1 and recache the subq result
    */

    // PUSHCOMPILER
//    if (mb == executeMB) {
//      System.out.println("adding code to method in two places");
//      new Throwable().printStackTrace();
//    }
//

        // generate code to reinitializeQualifiers(Qualifier[][] qualifiers)
    executeMB.getField(qualField); // first arg to reinitializeQualifiers()
    executeMB.callMethod(
            VMOpcode.INVOKESTATIC,
            acb.getBaseClassName(), "reinitializeQualifiers", "void", 1);

    /*
    ** Initialize the Qualifier array to a new Qualifier[][] if
View Full Code Here

    */

    if (numberOfStartPredicates != 0)
    {
      /* This sets up the method and the static field */
      MethodBuilder exprFun = acb.newExprFun();

      /* Now we fill in the body of the method */
      LocalField rowField = generateIndexableRow(acb, numberOfStartPredicates);

      int  colNum = 0;
View Full Code Here

TOP

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

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.