예제 #1
0
  /**
   * Execute a SQL statement that retruns a single ResultSet
   *
   * @param Sql typically a static SQL SELECT statement
   * @return a ResulSet that contains the data produced by the query
   * @exception java.sql.SQLException if a database access error occurs
   */
  public java.sql.ResultSet executeQuery(String Sql) throws java.sql.SQLException {
    if (Driver.trace) {
      Object[] Args = {Sql};
      Debug.methodCall(this, "executeQuery", Args);
    }

    if (_escapeProcessing) {
      Sql = _Escaper.escapeSQL(Sql);
    }

    if (Sql.indexOf("||") != -1) {
      Sql = _Escaper.doConcat(Sql);
    }

    if (_Results != null) {
      _Results.close();
    }

    // If there isn't a limit clause in the SQL
    // then limit the number of rows to return in
    // an efficient manner. Only do this if
    // setMaxRows() hasn't been used on any Statements
    // generated from the current Connection (saves
    // a query, and network traffic).

    synchronized (_Conn.getMutex()) {
      String OldCatalog = null;

      if (!_Conn.getCatalog().equals(_Catalog)) {
        OldCatalog = _Conn.getCatalog();
        _Conn.setCatalog(_Catalog);
      }

      if (_Conn.useMaxRows()) {

        // We need to execute this all together
        // So synchronize on the Connection's mutex (because
        // even queries going through there synchronize
        // on the connection

        if (Sql.toUpperCase().indexOf("LIMIT") != -1) {
          _Results = _Conn.execSQL(Sql, _max_rows);
        } else {
          if (_max_rows <= 0) {
            _Conn.execSQL("SET OPTION SQL_SELECT_LIMIT=" + MysqlDefs.MAX_ROWS, -1);
          } else {
            _Conn.execSQL("SET OPTION SQL_SELECT_LIMIT=" + _max_rows, -1);
          }

          _Results = _Conn.execSQL(Sql, -1);

          if (OldCatalog != null) {
            _Conn.setCatalog(OldCatalog);
          }
        }
      } else {
        _Results = _Conn.execSQL(Sql, -1);
      }

      if (OldCatalog != null) {
        _Conn.setCatalog(OldCatalog);
      }
    }

    _last_insert_id = _Results.getUpdateID();
    _NextResults = _Results;
    _Results.setConnection(_Conn);
    return _Results;
  }
예제 #2
0
  /**
   * Execute a SQL statement that may return multiple results. We don't have to worry about this
   * since we do not support multiple ResultSets. You can use getResultSet or getUpdateCount to
   * retrieve the result.
   *
   * @param sql any SQL statement
   * @return true if the next result is a ResulSet, false if it is an update count or there are no
   *     more results
   * @exception java.sql.SQLException if a database access error occurs
   */
  public boolean execute(String Sql) throws java.sql.SQLException {
    if (Driver.trace) {
      Object[] Args = {Sql};
      Debug.methodCall(this, "execute", Args);
    }

    if (_escapeProcessing) {
      Sql = _Escaper.escapeSQL(Sql);
    }

    if (Sql.indexOf("||") != -1) {
      Sql = _Escaper.doConcat(Sql);
    }

    if (_Results != null) {
      _Results.close();
    }

    ResultSet RS = null;

    // If there isn't a limit clause in the SQL
    // then limit the number of rows to return in
    // an efficient manner. Only do this if
    // setMaxRows() hasn't been used on any Statements
    // generated from the current Connection (saves
    // a query, and network traffic).

    synchronized (_Conn.getMutex()) {
      String OldCatalog = null;

      if (!_Conn.getCatalog().equals(_Catalog)) {
        OldCatalog = _Conn.getCatalog();
        _Conn.setCatalog(_Catalog);
      }

      if (_Conn.useMaxRows()) {
        if (Sql.toUpperCase().indexOf("LIMIT") != -1) {
          RS = _Conn.execSQL(Sql, _max_rows);
        } else {
          if (_max_rows <= 0) {
            _Conn.execSQL("SET OPTION SQL_SELECT_LIMIT=" + MysqlDefs.MAX_ROWS, -1);
          } else {
            _Conn.execSQL("SET OPTION SQL_SELECT_LIMIT=" + _max_rows, -1);
          }
          RS = _Conn.execSQL(Sql, -1);
        }
      } else {
        RS = _Conn.execSQL(Sql, -1);
      }

      if (OldCatalog != null) {
        _Conn.setCatalog(OldCatalog);
      }
    }

    _last_insert_id = RS.getUpdateID();

    if (RS != null) {
      _Results = RS;
    }

    RS.setConnection(_Conn);

    return (RS != null && RS.reallyResult());
  }