/**
   * Some prepared statements return multiple results; the execute method handles these complex
   * statements as well as the simpler form of statements handled by executeQuery and executeUpdate.
   *
   * @exception SQLException if a database-access error occurs.
   * @see Statement#execute
   */
  public boolean execute(Tds tds) throws SQLException {
    //
    // TDS can handle prepared statements by creating a temporary
    // procedure.  Since procedure must have the datatype specified
    // in the procedure declaration we will have to defer creating
    // the actual procedure until the statement is executed.  By
    // that time we know all the types of all of the parameters.
    //

    Procedure procedure = null;
    boolean result = false;

    // SAfe No need for this either. We'll have to consume all input, nut
    //      just the last ResultSet (if one exists).
    //        closeResults(false);
    // SAfe No need for this. getMoreResults sets it to -1 at start, anyway
    //        updateCount = -2;

    // First make sure the caller has filled in all the parameters.
    ParameterUtils.verifyThatParametersAreSet(parameterList);

    // Find a stored procedure that is compatible with this set of parameters if one exists.
    procedure = findCompatibleStoredProcedure(tds, rawQueryString);
    // now look in tds

    // if we don't have a suitable match then create a new temporary stored procedure
    if (procedure == null) {
      // Create the stored procedure
      procedure = new Procedure(rawQueryString, tds.getUniqueProcedureName(), parameterList, tds);

      // SAfe Submit it to the SQL Server before adding it to the cache or procedures of transaction
      // list.
      //      Otherwise, if submitProcedure fails (e.g. because of a syntax error) it will be in our
      // procedure
      //      cache, but not on the server.
      submitProcedure(tds, procedure);

      // store it in the procedureCache
      tds.procedureCache.put(rawQueryString, procedure);

      // MJH Only record the proc name in proceduresOfTra if in manual commit mode
      if (!getConnection().getAutoCommit()) // MJH
      tds.proceduresOfTra.add(procedure);
    }

    result =
        internalExecuteCall(
            procedure.getProcedureName(),
            procedure.getParameterList(),
            parameterList,
            tds,
            warningChain);

    return result;
  }
  /**
   * Some prepared statements return multiple results; the execute method handles these complex
   * statements as well as the simpler form of statements handled by executeQuery and executeUpdate.
   *
   * @exception SQLException if a database-access error occurs.
   * @see Statement#execute
   */
  public boolean execute(Tds tds) throws SQLException {
    //
    // TDS can handle prepared statements by creating a temporary
    // procedure.  Since procedure must have the datatype specified
    // in the procedure declaration we will have to defer creating
    // the actual procedure until the statement is executed.  By
    // that time we know all the types of all of the parameters.
    //

    Procedure procedure = null;
    boolean result = false;

    closeResults();
    updateCount = -2;

    // First make sure the caller has filled in all the parameters.
    ParameterUtils.verifyThatParametersAreSet(parameterList);

    // Find a stored procedure that is compatible with this set of
    // parameters if one exists.
    procedure = findCompatibleStoredProcedure(tds, rawQueryString);
    // now look in tds

    // if we don't have a suitable match then create a new
    // temporary stored procedure
    if (procedure == null) {
      // create the stored procedure
      procedure = new Procedure(rawQueryString, tds.getUniqueProcedureName(), parameterList, tds);

      // store it in the procedureCache
      // procedureCache.addElement(procedure);
      // store it in the procedureCache
      tds.procedureCache.put(rawQueryString, procedure);
      tds.proceduresOfTra.add(procedure);

      // create it on the SQLServer.
      submitProcedure(tds, procedure);
    }
    result =
        executeCall(
            tds,
            procedure.getProcedureName(),
            procedure.getParameterList(),
            // formal params
            parameterList);
    // actual params

    return result;
  }
 private void submitProcedure(Tds tds, Procedure proc) throws SQLException {
   String sql = proc.getPreparedSqlString();
   tds.submitProcedure(sql, warningChain);
 }
  protected boolean executeCall(
      Tds tds,
      String name,
      ParameterListItem[] formalParameterList,
      ParameterListItem[] actualParameterList)
      throws SQLException {
    boolean result;
    boolean wasCanceled = false;

    try {
      SQLException exception = null;
      PacketResult tmp = null;

      // execute the stored procedure.
      tds.executeProcedure(name, formalParameterList, actualParameterList, this, timeout);

      result = getMoreResults(tds);
      /*
      while (tds.isErrorPacket() || tds.isMessagePacket())
      {
         tmp = tds.processSubPacket();
         exception = warningChain.addOrReturn((PacketMsgResult)tmp);
         if (exception != null)
         {
            throw exception;
         }
      }
      while(tds.isDoneInProc())
      {
         tmp = tds.processSubPacket();
      }
      if (tds.isProcId())
      {
         tmp = tds.processSubPacket();
      }
      if (tds.isResultSet())
      {
         result = true;
      }
      else
      {
         result = false;
         boolean done = false;
         do
         {
            tmp = tds.processSubPacket();
            if (tmp instanceof PacketEndTokenResult)
            {
               done = ! ((PacketEndTokenResult)tmp).moreResults();
               wasCanceled = wasCanceled
                  || ((PacketEndTokenResult)tmp).wasCanceled();
               updateCount = ((PacketEndTokenResult)tmp).getRowCount();
            }
            else if (tmp instanceof PacketOutputParamResult)
            {
               if (CallableStatement_base.class.isInstance(this))
               {
                  ((CallableStatement_base)this).addOutputParam(
                     ((PacketOutputParamResult)tmp).getValue());
               }
            }
            else if (tmp.getPacketType()
                     == TdsDefinitions.TDS_RET_STAT_TOKEN)
            {
               // nop
            }
            else if (tmp instanceof PacketMsgResult)
            {
               exception = warningChain.addOrReturn((PacketMsgResult)tmp);
               if (exception != null)
               {
                  throw exception;
               }
            }
            else
            {
               throw new SQLException("Protocol confusion"
                                      + "Found a "
                                      + tmp.getClass().getName()
                                      + " (packet type 0x"
                                      + Integer.toHexString(tmp.getPacketType()
                                                            & 0xff)
                                      + ")");
            }
         } while (!done);
      }
       */
    } catch (TdsException e) {
      e.printStackTrace();
      throw new SQLException(e.getMessage());
    }
    /*
    catch(java.io.IOException e)
    {
       e.printStackTrace();
       throw new SQLException(e.getMessage());
    }
     */
    finally {
      tds.comm.packetType = 0;
    }
    if (wasCanceled) {
      throw new SQLException("Query was canceled or timed out.");
    }
    return result;
  }