Logo Search packages:      
Sourcecode: henplus version File versions  Download package

int henplus::commands::SQLCommand::execute ( SQLSession  session,
String  cmd,
String  param 
) [inline]

execute the command given.

Implements henplus::Command.

Definition at line 166 of file SQLCommand.java.

References henplus::SQLSession::connect(), henplus::SQLSession::createStatement(), henplus::Command::EXEC_FAILED, henplus::commands::ResultSetRenderer::execute(), henplus::SQLSession::getConnection(), henplus::commands::ResultSetRenderer::getFirstRowTime(), henplus::commands::ResultSetRenderer::limitReached(), henplus::SQLSession::print(), henplus::SQLSession::println(), henplus::SQLSession::printMessages(), henplus::Command::SUCCESS, and henplus::commands::ListUserObjectsCommand::unhash().

                                                                     {
      Statement stmt = null;
      ResultSet rset = null;
      String command = cmd + " " + param;
        boolean background = false;

      if (command.endsWith("/")) {
          command = command.substring(0, command.length()-1);
      }

        if (command.endsWith("&")) {
            command = command.substring(0, command.length()-1);
            HenPlus.msg().println("## executing command in the background not yet supported");
            background = true;
        }

      long startTime = System.currentTimeMillis();
      long lapTime  = -1;
      long execTime = -1;
      try {
//        SigIntHandler.getInstance()
//          .registerInterrupt(Thread.currentThread());
          if (command.startsWith("commit")) {
            session.print("commit..");
            session.getConnection().commit();
            session.println(".done.");
          }
          else if (command.startsWith("rollback")) {
            session.print("rollback..");
            session.getConnection().rollback();
            session.println(".done.");
          }
          else {
            boolean hasResultSet = false;
            boolean hasSingleResult = false;

            /* this is basically a hack around SAP-DB's tendency to
             * throw NullPointerExceptions, if the session timed out.
             */
            for (int retry=2; retry > 0; --retry) {
                try {
                        if ("call-procedure".equals(cmd)) {
                            CallableStatement pstmt;
                            command = "{? = call " + param + "}";
                            pstmt = session.getConnection()
                                .prepareCall(command);
                            //pstmt.registerOutParameter(1, Types.REF);
                            pstmt.registerOutParameter(1, Types.VARCHAR);
                            pstmt.execute();
                            //HenPlus.msg().println("call this .." + command);
                            //pstmt.setR
                            hasResultSet = false;
                            hasSingleResult = true;
                            stmt = pstmt;
                        }
                        else {
                            stmt = session.createStatement();
                            hasResultSet = stmt.execute(command);
                        }
                  break;
                }
                catch (SQLException e) { throw e; }
                catch (Throwable e) {
                  if (retry == 1) {
                      return EXEC_FAILED;
                  }
                  HenPlus.msg().println("Problem: " + e.getMessage()
                                 + "; trying reconnect...");
                  session.connect();
                }
            }
            
            if (hasResultSet) {
                rset = stmt.getResultSet();
                ResultSetRenderer renderer;
                renderer = new ResultSetRenderer(rset, 
                                                     getColumnDelimiter(),
                                                     isShowHeader(),
                                                     isShowFooter(),
                                                     getRowLimit(),
                                                     HenPlus.out());
                SigIntHandler.getInstance().pushInterruptable(renderer);
                int rows = renderer.execute();
                if (renderer.limitReached()) {
                  session.println("limit of " + getRowLimit() + 
                                        " rows reached ..");
                  session.print("> ");
                }
                session.print(rows + " row" + 
                             ((rows == 1)? "" : "s")
                             + " in result");
                lapTime = renderer.getFirstRowTime() - startTime;
            }
                else if (hasSingleResult) {
                    HenPlus.msg().println(((CallableStatement)stmt).getString(1));
                }
            else {
                int updateCount = stmt.getUpdateCount();
                if (updateCount >= 0) {
                  session.print("affected "+updateCount+" rows");
                }
                else {
                  session.print("ok.");
                }
            }
            execTime = System.currentTimeMillis() - startTime;
            session.print(" (");
            if (lapTime > 0) {
                session.print("first row: ");
                if (session.printMessages()) {
                  TimeRenderer.printTime(lapTime, HenPlus.msg());
                }
                session.print("; total: ");
            }
            if (session.printMessages()) {
                TimeRenderer.printTime(execTime, HenPlus.msg());
            }
            session.println(")");
          }

          // be smart and retrigger hashing of the tablenames.
          if ("drop".equals(cmd) || "create".equals(cmd)) {
            tableCompleter.unhash(session);
          }

          return SUCCESS;
      }
      /*
      catch (InterruptedException ie) {
          session.print("interrupted after ");
          execTime = System.currentTimeMillis() - startTime;
          TimeRenderer.printTime(execTime, HenPlus.msg());
          session.println(".");
          return SUCCESS;
      }
      */
      catch (Exception e) {
          String msg = e.getMessage();
          if (msg != null) {
            // oracle appends a newline to the message for some reason.
            HenPlus.msg().println("FAILURE: " + msg.trim());
          }
          if (verbose) e.printStackTrace();
          return EXEC_FAILED;
      }
      finally {
          try { if (rset != null) rset.close(); } catch (Exception e) {}
          try { if (stmt != null) stmt.close(); } catch (Exception e) {}
      }
    }


Generated by  Doxygen 1.6.0   Back to index