abstractjdbc2resultset.java
来自「PostgreSQL7.4.6 for Linux」· Java 代码 · 共 1,625 行 · 第 1/3 页
JAVA
1,625 行
/*------------------------------------------------------------------------- * * AbstractJdbc2ResultSet.java * This class defines methods of the jdbc2 specification. This class * extends org.postgresql.jdbc1.AbstractJdbc1ResultSet which provides the * jdbc1 methods. The real Statement class (for jdbc2) is * org.postgresql.jdbc2.Jdbc2ResultSet * * Copyright (c) 2003, PostgreSQL Global Development Group * * IDENTIFICATION * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.25.2.9 2004/09/13 07:14:26 jurka Exp $ * *------------------------------------------------------------------------- */package org.postgresql.jdbc2;import java.io.CharArrayReader;import java.io.InputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.math.BigDecimal;import java.sql.*;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.StringTokenizer;import java.util.Vector;import org.postgresql.Driver;import org.postgresql.core.BaseStatement;import org.postgresql.core.Field;import org.postgresql.core.Encoding;import org.postgresql.util.PSQLException;import org.postgresql.util.PSQLState;public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.AbstractJdbc1ResultSet{ //needed for updateable result set support protected boolean updateable = false; protected boolean doingUpdates = false; protected boolean onInsertRow = false; protected Hashtable updateValues = new Hashtable(); private boolean usingOID = false; // are we using the OID for the primary key? private Vector primaryKeys; // list of primary keys private int numKeys = 0; private boolean singleTable = false; protected String tableName = null; protected PreparedStatement updateStatement = null; protected PreparedStatement insertStatement = null; protected PreparedStatement deleteStatement = null; private PreparedStatement selectStatement = null; public AbstractJdbc2ResultSet(BaseStatement statement, Field[] fields, Vector tuples, String status, int updateCount, long insertOID) { super (statement, fields, tuples, status, updateCount, insertOID); } public java.net.URL getURL(int columnIndex) throws SQLException { return null; } public java.net.URL getURL(String columnName) throws SQLException { return null; } /* * 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; checkResultSet( columnIndex ); wasNullFlag = (this_row[columnIndex - 1] == null); if (wasNullFlag) return null; 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); case Types.ARRAY: return getArray(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 boolean absolute(int index) throws SQLException { // index is 1-based, but internally we use 0-based indices int internalIndex; if (index == 0) throw new SQLException("Cannot move to index of 0"); final int rows_size = rows.size(); //if index<0, count from the end of the result set, but check //to be sure that it is not beyond the first index if (index < 0) { if (index >= -rows_size) internalIndex = rows_size + index; else { beforeFirst(); return false; } } else { //must be the case that index>0, //find the correct place, assuming that //the index is not too large if (index <= rows_size) internalIndex = index - 1; else { afterLast(); return false; } } current_row = internalIndex; this_row = (byte[][]) rows.elementAt(internalIndex); rowBuffer = new byte[this_row.length][]; System.arraycopy(this_row, 0, rowBuffer, 0, this_row.length); onInsertRow = false; return true; } public void afterLast() throws SQLException { final int rows_size = rows.size(); if (rows_size > 0) current_row = rows_size; onInsertRow = false; this_row = null; rowBuffer = null; } public void beforeFirst() throws SQLException { if (rows.size() > 0) current_row = -1; onInsertRow = false; this_row = null; rowBuffer = null; } public boolean first() throws SQLException { if (rows.size() <= 0) return false; current_row = 0; this_row = (byte[][]) rows.elementAt(current_row); rowBuffer = new byte[this_row.length][]; System.arraycopy(this_row, 0, rowBuffer, 0, this_row.length); onInsertRow = false; return true; } public java.sql.Array getArray(String colName) throws SQLException { return getArray(findColumn(colName)); } public java.sql.Array getArray(int i) throws SQLException { checkResultSet( i ); wasNullFlag = (this_row[i - 1] == null); if (wasNullFlag) return null; if (i < 1 || i > fields.length) throw new PSQLException("postgresql.res.colrange", PSQLState.INVALID_PARAMETER_VALUE); return (java.sql.Array) new org.postgresql.jdbc2.Array( connection, i, fields[i - 1], this ); } public java.math.BigDecimal getBigDecimal(int columnIndex) throws SQLException { return getBigDecimal(columnIndex, -1); } public java.math.BigDecimal getBigDecimal(String columnName) throws SQLException { return getBigDecimal(findColumn(columnName)); } public Blob getBlob(String columnName) throws SQLException { return getBlob(findColumn(columnName)); } public abstract Blob getBlob(int i) throws SQLException; public java.io.Reader getCharacterStream(String columnName) throws SQLException { return getCharacterStream(findColumn(columnName)); } public java.io.Reader getCharacterStream(int i) throws SQLException { checkResultSet( i ); wasNullFlag = (this_row[i - 1] == null); if (wasNullFlag) return null; if (((AbstractJdbc2Connection) 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 return new CharArrayReader(getString(i).toCharArray()); } else { // In 7.1 Handle as BLOBS so return the LargeObject input stream Encoding encoding = connection.getEncoding(); InputStream input = getBinaryStream(i); return encoding.getDecodingReader(input); } } public Clob getClob(String columnName) throws SQLException { return getClob(findColumn(columnName)); } public abstract Clob getClob(int i) throws SQLException; public int getConcurrency() throws SQLException { if (statement == null) return java.sql.ResultSet.CONCUR_READ_ONLY; return statement.getResultSetConcurrency(); } public java.sql.Date getDate(int i, java.util.Calendar cal) throws SQLException { // If I read the specs, this should use cal only if we don't // store the timezone, and if we do, then act just like getDate()? // for now... return getDate(i); } public Time getTime(int i, java.util.Calendar cal) throws SQLException { // If I read the specs, this should use cal only if we don't // store the timezone, and if we do, then act just like getTime()? // for now... return getTime(i); } public Timestamp getTimestamp(int i, java.util.Calendar cal) throws SQLException { // If I read the specs, this should use cal only if we don't // store the timezone, and if we do, then act just like getDate()? // for now... return getTimestamp(i); } public java.sql.Date getDate(String c, java.util.Calendar cal) throws SQLException { return getDate(findColumn(c), cal); } public Time getTime(String c, java.util.Calendar cal) throws SQLException { return getTime(findColumn(c), cal); } public Timestamp getTimestamp(String c, java.util.Calendar cal) throws SQLException { return getTimestamp(findColumn(c), cal); } public int getFetchDirection() throws SQLException { //PostgreSQL normally sends rows first->last return java.sql.ResultSet.FETCH_FORWARD; } public Object getObject(String columnName, java.util.Map map) throws SQLException { return getObject(findColumn(columnName), map); } /* * This checks against map for the type of column i, and if found returns * an object based on that mapping. The class must implement the SQLData * interface. */ public Object getObject(int i, java.util.Map map) throws SQLException { throw org.postgresql.Driver.notImplemented(); } public Ref getRef(String columnName) throws SQLException { return getRef(findColumn(columnName)); } public Ref getRef(int i) throws SQLException { //The backend doesn't yet have SQL3 REF types throw new PSQLException("postgresql.psqlnotimp", PSQLState.NOT_IMPLEMENTED); } public int getRow() throws SQLException { final int rows_size = rows.size(); if (current_row < 0 || current_row >= rows_size) return 0; return current_row + 1; } // This one needs some thought, as not all ResultSets come from a statement public Statement getStatement() throws SQLException { return (Statement) statement; } public int getType() throws SQLException { // This implementation allows scrolling but is not able to // see any changes. Sub-classes may overide this to return a more // meaningful result. return java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE; } public boolean isAfterLast() throws SQLException { final int rows_size = rows.size(); return (current_row >= rows_size && rows_size > 0); } public boolean isBeforeFirst() throws SQLException { return (current_row < 0 && rows.size() > 0); } public boolean isFirst() throws SQLException { return (current_row == 0 && rows.size() >= 0); } public boolean isLast() throws SQLException { final int rows_size = rows.size(); return (current_row == rows_size - 1 && rows_size > 0); } public boolean last() throws SQLException { final int rows_size = rows.size(); if (rows_size <= 0) return false; current_row = rows_size - 1; this_row = (byte[][]) rows.elementAt(current_row); rowBuffer = new byte[this_row.length][]; System.arraycopy(this_row, 0, rowBuffer, 0, this_row.length); onInsertRow = false; return true; } public boolean previous() throws SQLException { if (current_row-1 < 0) { current_row = -1; this_row = null; rowBuffer = null; return false; } else { current_row--; } this_row = (byte[][]) rows.elementAt(current_row); rowBuffer = new byte[this_row.length][]; System.arraycopy(this_row, 0, rowBuffer, 0, this_row.length); return true; } public boolean relative(int rows) throws SQLException { //have to add 1 since absolute expects a 1-based index return absolute(current_row + 1 + rows); } public void setFetchDirection(int direction) throws SQLException { throw new PSQLException("postgresql.psqlnotimp", PSQLState.NOT_IMPLEMENTED); } public synchronized void cancelRowUpdates() throws SQLException { if (doingUpdates) { doingUpdates = false; clearRowBuffer(true); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?