diff options
Diffstat (limited to 'src/interfaces/jdbc/org/postgresql/jdbc1')
-rw-r--r-- | src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java | 59 | ||||
-rw-r--r-- | src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java | 25 |
2 files changed, 70 insertions, 14 deletions
diff --git a/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java b/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java index c314b77d65..5a8994f7a4 100644 --- a/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java +++ b/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java @@ -9,7 +9,7 @@ * Copyright (c) 2003, PostgreSQL Global Development Group * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.14 2003/08/06 05:53:13 barry Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.15 2003/08/24 22:10:09 barry Exp $ * *------------------------------------------------------------------------- */ @@ -176,7 +176,7 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet return null; Encoding encoding = connection.getEncoding(); - return encoding.decode(this_row[columnIndex - 1]); + return trimString(columnIndex, encoding.decode(this_row[columnIndex-1])); } public boolean getBoolean(int columnIndex) throws SQLException @@ -303,11 +303,11 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet //Version 7.2 supports the bytea datatype for byte arrays if (fields[columnIndex - 1].getPGType().equals("bytea")) { - return PGbytea.toBytes(this_row[columnIndex - 1]); + return trimBytes(columnIndex, PGbytea.toBytes(this_row[columnIndex - 1])); } else { - return this_row[columnIndex - 1]; + return trimBytes(columnIndex, this_row[columnIndex - 1]); } } else @@ -320,11 +320,11 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet LargeObject lob = lom.open(getInt(columnIndex)); byte buf[] = lob.read(lob.size()); lob.close(); - return buf; + return trimBytes(columnIndex, buf); } else { - return this_row[columnIndex - 1]; + return trimBytes(columnIndex, this_row[columnIndex - 1]); } } } @@ -1143,7 +1143,54 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet } } } + + private boolean isColumnTrimmable(int columnIndex) throws SQLException + { + switch (fields[columnIndex-1].getSQLType()) + { + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + case Types.BINARY: + case Types.VARBINARY: + case Types.LONGVARBINARY: + return true; + } + return false; + } + private byte[] trimBytes(int p_columnIndex, byte[] p_bytes) throws SQLException + { + int l_maxSize = statement.getMaxFieldSize(); + //we need to trim if maxsize is set and the length is greater than maxsize and the + //type of this column is a candidate for trimming + if (l_maxSize > 0 && p_bytes.length > l_maxSize && isColumnTrimmable(p_columnIndex)) + { + byte[] l_bytes = new byte[l_maxSize]; + System.arraycopy (p_bytes, 0, l_bytes, 0, l_maxSize); + return l_bytes; + } + else + { + return p_bytes; + } + } + + private String trimString(int p_columnIndex, String p_string) throws SQLException + { + int l_maxSize = statement.getMaxFieldSize(); + //we need to trim if maxsize is set and the length is greater than maxsize and the + //type of this column is a candidate for trimming + if (l_maxSize > 0 && p_string.length() > l_maxSize && isColumnTrimmable(p_columnIndex)) + { + return p_string.substring(0,l_maxSize); + } + else + { + return p_string; + } + } + public SimpleDateFormat getTimestampTZFormat() { if (m_tstzFormat == null) { m_tstzFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); diff --git a/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java b/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java index e4ab3f7cf2..60b5708364 100644 --- a/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java +++ b/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java @@ -25,7 +25,7 @@ import java.sql.Timestamp; import java.sql.Types; import java.util.Vector; -/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.31 2003/08/11 21:12:00 barry Exp $ +/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.32 2003/08/24 22:10:09 barry Exp $ * This class defines methods of the jdbc1 specification. This class is * extended by org.postgresql.jdbc2.AbstractJdbc2Statement which adds the jdbc2 * methods. The real Statement class (for jdbc1) is org.postgresql.jdbc1.Jdbc1Statement @@ -87,7 +87,7 @@ public abstract class AbstractJdbc1Statement implements BaseStatement // returnTypeSet is true when a proper call to registerOutParameter has been made private boolean returnTypeSet; protected Object callResult; - + protected static int maxfieldSize = 0; public abstract BaseResultSet createResultSet(Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor) throws SQLException; @@ -640,19 +640,19 @@ public abstract class AbstractJdbc1Statement implements BaseStatement */ public int getMaxFieldSize() throws SQLException { - return 8192; // We cannot change this + return maxfieldSize; } /* - * Sets the maxFieldSize - NOT! - We throw an SQLException just - * to inform them to stop doing this. + * Sets the maxFieldSize * * @param max the new max column size limit; zero means unlimited * @exception SQLException if a database access error occurs */ public void setMaxFieldSize(int max) throws SQLException { - throw new PSQLException("postgresql.stat.maxfieldsize"); + if (max < 0) throw new PSQLException("postgresql.input.field.gt0"); + maxfieldSize = max; } /* @@ -721,6 +721,15 @@ public abstract class AbstractJdbc1Statement implements BaseStatement result = null; } + /** + * This finalizer ensures that statements that have allocated server-side + * resources free them when they become unreferenced. + */ + protected void finalize() { + try { close(); } + catch (SQLException e) {} + } + /* * Filter the SQL string of Java SQL Escape clauses. * @@ -1088,7 +1097,7 @@ public abstract class AbstractJdbc1Statement implements BaseStatement } else { - setString(parameterIndex, PGbytea.toPGString(x), PG_TEXT); + setString(parameterIndex, PGbytea.toPGString(x), PG_BYTEA); } } else @@ -2055,7 +2064,7 @@ public abstract class AbstractJdbc1Statement implements BaseStatement if (connection.haveMinimumServerVersion("7.3")) { //If turning server prepared statements off deallocate statement //and reset statement name - if (m_useServerPrepare != flag && !flag) + if (m_useServerPrepare != flag && !flag && m_statementName != null) connection.execSQL("DEALLOCATE " + m_statementName); m_statementName = null; m_useServerPrepare = flag; |