abstractjdbc1resultset.java
来自「PostgreSQL7.4.6 for Linux」· Java 代码 · 共 1,260 行 · 第 1/3 页
JAVA
1,260 行
if (connection.haveMinimumCompatibleVersion("7.2")) { //Version 7.2 supports AsciiStream for all the PG text types //As the spec/javadoc for this method indicate this is to be used for //large text values (i.e. LONGVARCHAR) PG doesn't have a separate //long string datatype, but with toast the text datatype is capable of //handling very large values. Thus the implementation ends up calling //getString() since there is no current way to stream the value from the server try { return new ByteArrayInputStream(getString(columnIndex).getBytes("UTF-8")); } catch (UnsupportedEncodingException l_uee) { throw new PSQLException("postgresql.unusual", PSQLState.UNEXPECTED_ERROR, l_uee); } } else { // In 7.1 Handle as BLOBS so return the LargeObject input stream return getBinaryStream(columnIndex); } } public InputStream getBinaryStream(int columnIndex) throws SQLException { checkResultSet( columnIndex ); wasNullFlag = (this_row[columnIndex - 1] == null); if (wasNullFlag) return null; if (connection.haveMinimumCompatibleVersion("7.2")) { //Version 7.2 supports BinaryStream for all PG bytea type //As the spec/javadoc for this method indicate this is to be used for //large binary values (i.e. LONGVARBINARY) PG doesn't have a separate //long binary datatype, but with toast the bytea datatype is capable of //handling very large values. Thus the implementation ends up calling //getBytes() since there is no current way to stream the value from the server byte b[] = getBytes(columnIndex); if (b != null) return new ByteArrayInputStream(b); } else { // In 7.1 Handle as BLOBS so return the LargeObject input stream if ( fields[columnIndex - 1].getOID() == 26) { LargeObjectManager lom = connection.getLargeObjectAPI(); LargeObject lob = lom.open(getInt(columnIndex)); return lob.getInputStream(); } } return null; } public String getString(String columnName) throws SQLException { return getString(findColumn(columnName)); } public boolean getBoolean(String columnName) throws SQLException { return getBoolean(findColumn(columnName)); } public byte getByte(String columnName) throws SQLException { return getByte(findColumn(columnName)); } public short getShort(String columnName) throws SQLException { return getShort(findColumn(columnName)); } public int getInt(String columnName) throws SQLException { return getInt(findColumn(columnName)); } public long getLong(String columnName) throws SQLException { return getLong(findColumn(columnName)); } public float getFloat(String columnName) throws SQLException { return getFloat(findColumn(columnName)); } public double getDouble(String columnName) throws SQLException { return getDouble(findColumn(columnName)); } public BigDecimal getBigDecimal(String columnName, int scale) throws SQLException { return getBigDecimal(findColumn(columnName), scale); } public byte[] getBytes(String columnName) throws SQLException { return getBytes(findColumn(columnName)); } public java.sql.Date getDate(String columnName) throws SQLException { return getDate(findColumn(columnName)); } public Time getTime(String columnName) throws SQLException { return getTime(findColumn(columnName)); } public Timestamp getTimestamp(String columnName) throws SQLException { return getTimestamp(findColumn(columnName)); } public InputStream getAsciiStream(String columnName) throws SQLException { return getAsciiStream(findColumn(columnName)); } public InputStream getUnicodeStream(String columnName) throws SQLException { return getUnicodeStream(findColumn(columnName)); } public InputStream getBinaryStream(String columnName) throws SQLException { return getBinaryStream(findColumn(columnName)); } public SQLWarning getWarnings() throws SQLException { return warnings; } public void clearWarnings() throws SQLException { warnings = null; } public void addWarnings(SQLWarning warnings) { if ( this.warnings != null ) this.warnings.setNextWarning(warnings); else this.warnings = warnings; } public String getCursorName() throws SQLException { return (connection.getCursorName()); } /* * Get the value of a column in the current row as a Java object * * <p>This method will return the value of the given column as a * Java object. The type of the Java object will be the default * Java Object type corresponding to the column's SQL type, following * the mapping specified in the JDBC specification. * * <p>This method may also be used to read database specific abstract * data types. * * @param columnIndex the first column is 1, the second is 2... * @return a Object holding the column value * @exception SQLException if a database access error occurs */ public Object getObject(int columnIndex) throws SQLException { Field field; if (columnIndex < 1 || columnIndex > fields.length) throw new PSQLException("postgresql.res.colrange", PSQLState.INVALID_PARAMETER_VALUE); field = fields[columnIndex - 1]; // some fields can be null, mainly from those returned by MetaData methods if (field == null) { wasNullFlag = true; return null; } switch (field.getSQLType()) { case Types.BIT: return getBoolean(columnIndex) ? Boolean.TRUE : Boolean.FALSE; case Types.SMALLINT: return new Short(getShort(columnIndex)); case Types.INTEGER: return new Integer(getInt(columnIndex)); case Types.BIGINT: return new Long(getLong(columnIndex)); case Types.NUMERIC: return getBigDecimal (columnIndex, (field.getMod() == -1) ? -1 : ((field.getMod() - 4) & 0xffff)); case Types.REAL: return new Float(getFloat(columnIndex)); case Types.DOUBLE: return new Double(getDouble(columnIndex)); case Types.CHAR: case Types.VARCHAR: return getString(columnIndex); case Types.DATE: return getDate(columnIndex); case Types.TIME: return getTime(columnIndex); case Types.TIMESTAMP: return getTimestamp(columnIndex); case Types.BINARY: case Types.VARBINARY: return getBytes(columnIndex); default: String type = field.getPGType(); // if the backend doesn't know the type then coerce to String if (type.equals("unknown")) { return getString(columnIndex); } // Specialized support for ref cursors is neater. else if (type.equals("refcursor")) { String cursorName = getString(columnIndex); return statement.createRefCursorResultSet(cursorName); } else { return connection.getObject(field.getPGType(), getString(columnIndex)); } } } public Object getObject(String columnName) throws SQLException { return getObject(findColumn(columnName)); } /* * Map a ResultSet column name to a ResultSet column index */ public int findColumn(String columnName) throws SQLException { int i; final int flen = fields.length; for (i = 0 ; i < flen; ++i) if (fields[i].getName().equalsIgnoreCase(columnName)) return (i + 1); throw new PSQLException ("postgresql.res.colname", columnName); } /* * We at times need to know if the resultSet we are working * with is the result of an UPDATE, DELETE or INSERT (in which * case, we only have a row count), or of a SELECT operation * (in which case, we have multiple fields) - this routine * tells us. */ public boolean reallyResultSet() { return (fields != null); } /* * Since ResultSets can be chained, we need some method of * finding the next one in the chain. The method getNext() * returns the next one in the chain. * * @return the next ResultSet, or null if there are none */ public ResultSet getNext() { return (ResultSet)next; } /* * This following method allows us to add a ResultSet object * to the end of the current chain. */ public void append(BaseResultSet r) { if (next == null) next = r; else next.append(r); } /* * If we are just a place holder for results, we still need * to get an updateCount. This method returns it. */ public int getResultCount() { return updateCount; } /* * We also need to provide a couple of auxiliary functions for * the implementation of the ResultMetaData functions. In * particular, we need to know the number of rows and the * number of columns. Rows are also known as Tuples */ public int getTupleCount() { return rows.size(); } /* * getColumnCount returns the number of columns */ public int getColumnCount() { return fields.length; } /* * Returns the status message from the backend.<p> * It is used internally by the driver. */ public String getStatusString() { return status; } /* * returns the OID of a field.<p> * It is used internally by the driver. */ public int getColumnOID(int field) { return fields[field -1].getOID(); } /* * returns the OID of the last inserted row. Deprecated in 7.2 because * range for OID values is greater than java signed int. * @deprecated Replaced by getLastOID() in 7.2 */ public int getInsertedOID() { return (int) getLastOID(); } /* * returns the OID of the last inserted row * @since 7.2 */ public long getLastOID() { return insertOID; } /* * This is used to fix get*() methods on Money fields. It should only be * used by those methods! * * It converts ($##.##) to -##.## and $##.## to ##.## */ public String getFixedString(int col) throws SQLException { String s = getString(col); // Handle SQL Null wasNullFlag = (this_row[col - 1] == null); if (wasNullFlag) return null; // if we don't have at least 2 characters it can't be money. if (s.length() < 2) return s; // Handle Money if (s.charAt(0) == '(') { s = "-" + PGtokenizer.removePara(s).substring(1); } if (s.charAt(0) == '$') { s = s.substring(1); } else if (s.charAt(0) == '-' && s.charAt(1) == '$') { s = "-" + s.substring(2); } return s; } protected void checkResultSet( int column ) throws SQLException { if ( this_row == null ) throw new PSQLException("postgresql.res.nextrequired"); if ( column < 1 || column > fields.length ) throw new PSQLException("postgresql.res.colrange", PSQLState.INVALID_PARAMETER_VALUE ); } //----------------- Formatting Methods ------------------- public static boolean toBoolean(String s) { if (s != null) { s = s.trim(); if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("t")) return true; try {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?