jdbcstatementresource.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 754 行 · 第 1/2 页

JAVA
754
字号
   * Returns the number of rows in the result set.   *   * @return the number of rows in the result set   */  public int getNumRows()    throws SQLException  {    if (_rs != null)      return JdbcResultResource.getNumRows(_rs);    else      return 0;  }  /**   * Returns the internal prepared statement.   *   * @return the internal prepared statement   */  protected PreparedStatement getPreparedStatement()  {    return _stmt;  }  /**   * Resets _fieldOffset in _resultResource   *   * @return null if _resultResource == null, otherwise _resultResource   */  public JdbcResultResource getResultMetadata()  {    if (_resultResource != null) {      _resultResource.setFieldOffset(0);      return _resultResource;    }    if (_stmt == null || _rs == null)      return null;    _resultResource      = new JdbcResultResource(_conn.getEnv(), _stmt, _rs, _conn);    return _resultResource;  }  /**   * Returns the internal result set.   *   * @return the internal result set   */  protected ResultSet getResultSet()  {    return _rs;  }  /**   * Returns the underlying SQL connection   * associated to this statement.   */  protected Connection getJavaConnection()    throws SQLException  {    return validateConnection().getJavaConnection();  }  /**   * Returns this statement type.   *   * @return this statement type:   * SELECT, UPDATE, DELETE, INSERT, CREATE, DROP, ALTER, BEGIN, DECLARE, or UNKNOWN.   */  public String getStatementType()  {    // Oracle Statement type    // Also used internally in Postgres (see PostgresModule)    // (SELECT, UPDATE, DELETE, INSERT, CREATE, DROP, ALTER, BEGIN, DECLARE, UNKNOWN)    _stmtType = _query;    _stmtType = _stmtType.replaceAll("\\s+.*", "");        if (_stmtType.length() == 0)      _stmtType = "UNKNOWN";    else {      _stmtType = _stmtType.toUpperCase();      String s = _stmtType.replaceAll("(SELECT|UPDATE|DELETE|INSERT|CREATE|DROP|ALTER|BEGIN|DECLARE)", "");      if (! s.equals(""))        _stmtType = "UNKNOWN";    }    return _stmtType;  }  /**   * Counts the number of parameter markers in the query string.   *   * @return the number of parameter markers in the query string   */  public int paramCount()  {    if (_query == null)      return -1;    int count = 0;    int length = _query.length();    boolean inQuotes = false;    char c;    for (int i = 0; i < length; i++) {      c = _query.charAt(i);      if (c == '\\') {        if (i < length - 1)          i++;        continue;      }      if (inQuotes) {        if (c == '\'')          inQuotes = false;        continue;      }      if (c == '\'') {        inQuotes = true;        continue;      }      if (c == '?') {        count++;      }    }    return count;  }  /**   * Prepares this statement with the given query.   *   * @param query SQL query   * @return true on success or false on failure   */  public boolean prepare(Env env, StringValue query)  {    try {      if (_stmt != null)        _stmt.close();      _query = query.toString();      if (_query.length() == 0)        return false;      Connection conn = _conn.getConnection(env);            if (conn == null)        return false;            if (this instanceof OracleStatement) {        _stmt = conn.prepareCall(_query,                                 ResultSet.TYPE_SCROLL_INSENSITIVE,                                 ResultSet.CONCUR_READ_ONLY);      } else {        _stmt = conn.prepareStatement(_query,                                      ResultSet.TYPE_SCROLL_INSENSITIVE,                                      ResultSet.CONCUR_READ_ONLY);      }      return true;    } catch (SQLException e) {      log.log(Level.FINE, e.toString(), e);      _errorMessage = e.getMessage();      _errorCode = e.getErrorCode();      return false;    }  }  /**   * Prepares statement with the given query.   *   * @param query SQL query   * @return true on success or false on failure   */  public boolean prepareStatement(Env env, String query)  {    try {      if (_stmt != null)        _stmt.close();      _query = query;      Connection conn = _conn.getConnection(env);            if (conn == null)        return false;            if (this instanceof OracleStatement) {        _stmt = conn.prepareCall(query,                                 ResultSet.TYPE_SCROLL_INSENSITIVE,                                 ResultSet.CONCUR_READ_ONLY);      } else {        _stmt = conn.prepareStatement(query,                                      ResultSet.TYPE_SCROLL_INSENSITIVE,                                      ResultSet.CONCUR_READ_ONLY);      }      return true;    } catch (SQLException e) {      log.log(Level.FINE, e.toString(), e);      _errorMessage = e.getMessage();      _errorCode = e.getErrorCode();      return false;    }  }  /**   * Returns a parameter value   * Known subclasses: see PostgresStatement.execute   */  protected Value getParam(int i)  {    if (i >= _params.length)      return UnsetValue.UNSET;    return _params[i];  }  /**   * Returns the number of parameters available to binding   * Known subclasses: see PostgresStatement.execute   */  protected int getParamLength()  {    return _params.length;  }  /**   * Changes the internal statement.   */  protected void setPreparedStatement(PreparedStatement stmt)  {    _stmt = stmt;  }  /**   * Changes the internal result set.   */  protected void setResultSet(ResultSet rs)  {    _rs = rs;  }  /**   * Returns the number of fields in the result set.   *   * @param env the PHP executing environment   * @return the number of fields in the result set   */  public int getFieldCount()  {    if (_resultResource == null)      return 0;    return _resultResource.getFieldCount();  }  /**   * Sets the given parameter   * Known subclasses: see PostgresStatement.execute   */  protected void setObject(int i, Object param)    throws Exception  {    try {      // See php/4358, php/43b8, php/43d8, and php/43p8.      java.sql.ParameterMetaData pmd = _stmt.getParameterMetaData();      int type = pmd.getParameterType(i);      switch (type) {      case Types.OTHER:        {          // See php/43b8          String typeName = pmd.getParameterTypeName(i);          if (typeName.equals("interval")) {            _stmt.setObject(i, param);          } else {            Class cl = Class.forName("org.postgresql.util.PGobject");            Constructor constructor = cl.getDeclaredConstructor(null);            Object object = constructor.newInstance();            Method method = cl.getDeclaredMethod("setType", new Class[] {String.class});            method.invoke(object, new Object[] {typeName});            method = cl.getDeclaredMethod("setValue", new Class[] {String.class});            method.invoke(object, new Object[] {param});            _stmt.setObject(i, object, type);          }          break;        }      case Types.DOUBLE:        {          // See php/43p8.          String typeName = pmd.getParameterTypeName(i);          if (typeName.equals("money")) {            String s = param.toString();            if (s.length() == 0) {              throw new IllegalArgumentException(L.l("argument `{0}' cannot be empty", param));            } else {              String money = s;              if (s.charAt(0) == '$')                s = s.substring(1);              else                money = "$" + money;              try {                // This will throw an exception if not double while                // trying to setObject() would not. The error would                // come late, otherwise. See php/43p8.                Double.parseDouble(s);              } catch (Exception ex) {                throw new IllegalArgumentException(L.l("cannot convert argument `{0}' to money", param));              }              Class cl = Class.forName("org.postgresql.util.PGmoney");              Constructor constructor = cl.getDeclaredConstructor(new Class[] {String.class});              Object object = constructor.newInstance(new Object[] {money});              _stmt.setObject(i, object, Types.OTHER);              break;            }          }          // else falls to default case        }      default:        _stmt.setObject(i, param, type);      }    }    catch (SQLException e) {      _errorMessage = e.getMessage();      _errorCode = e.getErrorCode();      throw e;    }    catch (Exception e) {      _stmt.clearParameters();      throw e;    }  }  /**   * Returns a string representation for this object.   *   * @return the string representation for this object   */  public String toString()  {    return getClass().getName() + "[" + _conn + "]";  }  /**   * Validates the connection resource.   *   * @return the validated connection resource   */  public JdbcConnectionResource validateConnection()  {    return _conn;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?