Clean up wasNullFlag usage

From: Mikko Tiihonen <mikko(dot)tiihonen(at)iki(dot)fi>
To: pgsql-jdbc <pgsql-jdbc(at)postgresql(dot)org>
Subject: Clean up wasNullFlag usage
Date: 2007-07-21 11:31:58
Message-ID: 1185017518.1632.61.camel@dual.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hi,

The wasNullFlag must be evaluated for each getXXX method invocation.
Currently it is done all over the ResultSet classes. The patch moves
the evaluation inside checkResultSet method that is already invoked at
the beginning of each getXXX method. Also updates the javadoc to be
explicit about the functionality.

diff -ur --exclude=CVS --exclude=build --exclude='*.jar' pgjdbc.alreadysent/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
--- pgjdbc.alreadysent/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 2007-07-21 02:08:54.000000000 +0300
+++ pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 2007-07-21 13:54:15.000000000 +0300
@@ -65,7 +65,12 @@
protected int row_offset; // Offset of row 0 in the actual resultset
protected byte[][] this_row; // copy of the current result row
protected SQLWarning warnings = null; // The warning chain
- protected boolean wasNullFlag = false; // the flag for wasNull()
+ /**
+ * True if the last obtained column value was SQL NULL as specified by
+ * {(at)link #wasNull}. The value is always updated by the
+ * {(at)link #checkResultSet} method.
+ */
+ protected boolean wasNullFlag = false;
protected boolean onInsertRow = false; // are we on the insert row (for JDBC2 updatable resultsets)?

public byte[][] rowBuffer = null; // updateable rowbuffer
@@ -308,8 +313,6 @@
public java.sql.Array getArray(int i) throws SQLException
{
checkResultSet( i );
-
- wasNullFlag = (this_row[i - 1] == null);
if (wasNullFlag)
return null;

@@ -347,7 +350,6 @@
public java.io.Reader getCharacterStream(int i) throws SQLException
{
checkResultSet( i );
- wasNullFlag = (this_row[i - 1] == null);
if (wasNullFlag)
return null;

@@ -398,6 +400,8 @@
public java.sql.Date getDate(int i, java.util.Calendar cal) throws SQLException
{
this.checkResultSet(i);
+ if (wasNullFlag)
+ return null;

if (cal != null)
cal = (Calendar)cal.clone();
@@ -409,6 +413,8 @@
public Time getTime(int i, java.util.Calendar cal) throws SQLException
{
this.checkResultSet(i);
+ if (wasNullFlag)
+ return null;

if (cal != null)
cal = (Calendar)cal.clone();
@@ -420,6 +426,8 @@
public Timestamp getTimestamp(int i, java.util.Calendar cal) throws SQLException
{
this.checkResultSet(i);
+ if (wasNullFlag)
+ return null;

if (cal != null)
cal = (Calendar)cal.clone();
@@ -1866,7 +1874,6 @@
public String getString(int columnIndex) throws SQLException
{
checkResultSet( columnIndex );
- wasNullFlag = (this_row[columnIndex - 1] == null);
if (wasNullFlag)
return null;

@@ -1883,6 +1890,10 @@

public boolean getBoolean(int columnIndex) throws SQLException
{
+ checkResultSet(columnIndex);
+ if (wasNullFlag)
+ return false; // SQL NULL
+
return toBoolean( getString(columnIndex) );
}

@@ -1891,6 +1902,10 @@

public byte getByte(int columnIndex) throws SQLException
{
+ checkResultSet(columnIndex);
+ if (wasNullFlag)
+ return 0; // SQL NULL
+
String s = getString(columnIndex);

if (s != null )
@@ -1936,6 +1951,10 @@

public short getShort(int columnIndex) throws SQLException
{
+ checkResultSet(columnIndex);
+ if (wasNullFlag)
+ return 0; // SQL NULL
+
String s = getFixedString(columnIndex);

if (s != null)
@@ -1974,6 +1993,10 @@

public int getInt(int columnIndex) throws SQLException
{
+ checkResultSet(columnIndex);
+ if (wasNullFlag)
+ return 0; // SQL NULL
+
Encoding encoding = connection.getEncoding();
if (encoding.hasAsciiNumbers()) {
try {
@@ -1981,11 +2004,18 @@
} catch (NumberFormatException ex) {
}
}
+ if (fields[columnIndex - 1].getOID() == Oid.BOOL) {
+ return toBoolean(getFixedString(columnIndex)) ? 1 : 0;
+ }
return toInt( getFixedString(columnIndex) );
}

public long getLong(int columnIndex) throws SQLException
{
+ checkResultSet(columnIndex);
+ if (wasNullFlag)
+ return 0; // SQL NULL
+
Encoding encoding = connection.getEncoding();
if (encoding.hasAsciiNumbers()) {
try {
@@ -1996,6 +2026,11 @@
return toLong( getFixedString(columnIndex) );
}

+ /**
+ * A dummy exception thrown when fast byte[] to number parsing fails and
+ * no value can be returned. The exact stack trace does not matter because
+ * the exception is always caught and is not visible to users.
+ */
private static final NumberFormatException FAST_NUMBER_FAILED =
new NumberFormatException();

@@ -2011,14 +2046,8 @@
*/
private long getFastLong(int columnIndex) throws SQLException,
NumberFormatException {
-
- checkResultSet( columnIndex );

columnIndex--;
- wasNullFlag = (this_row[columnIndex] == null);
- if (wasNullFlag) {
- return 0; // SQL NULL
- }

byte[] bytes = this_row[columnIndex];

@@ -2071,13 +2100,7 @@
* {(at)link #toLong(String)}.
*/
private int getFastInt(int columnIndex) throws SQLException {
- checkResultSet( columnIndex );
-
columnIndex--;
- wasNullFlag = (this_row[columnIndex] == null);
- if (wasNullFlag) {
- return 0; // SQL NULL
- }

byte[] bytes = this_row[columnIndex];

@@ -2121,16 +2144,28 @@

public float getFloat(int columnIndex) throws SQLException
{
+ checkResultSet(columnIndex);
+ if (wasNullFlag)
+ return 0; // SQL NULL
+
return toFloat( getFixedString(columnIndex) );
}

public double getDouble(int columnIndex) throws SQLException
{
+ checkResultSet(columnIndex);
+ if (wasNullFlag)
+ return 0; // SQL NULL
+
return toDouble( getFixedString(columnIndex) );
}

public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException
{
+ checkResultSet(columnIndex);
+ if (wasNullFlag)
+ return null;
+
return toBigDecimal( getFixedString(columnIndex), scale );
}

@@ -2152,9 +2187,9 @@
public byte[] getBytes(int columnIndex) throws SQLException
{
checkResultSet( columnIndex );
- wasNullFlag = (this_row[columnIndex - 1] == null);
- if (!wasNullFlag)
- {
+ if (wasNullFlag)
+ return null;
+
if (fields[columnIndex - 1].getFormat() == Field.BINARY_FORMAT)
{
//If the data is already binary then just return it
@@ -2189,8 +2224,6 @@
return trimBytes(columnIndex, this_row[columnIndex - 1]);
}
}
- }
- return null;
}

public java.sql.Date getDate(int columnIndex) throws SQLException
@@ -2211,7 +2244,6 @@
public InputStream getAsciiStream(int columnIndex) throws SQLException
{
checkResultSet( columnIndex );
- wasNullFlag = (this_row[columnIndex - 1] == null);
if (wasNullFlag)
return null;

@@ -2242,7 +2274,6 @@
public InputStream getUnicodeStream(int columnIndex) throws SQLException
{
checkResultSet( columnIndex );
- wasNullFlag = (this_row[columnIndex - 1] == null);
if (wasNullFlag)
return null;

@@ -2273,7 +2304,6 @@
public InputStream getBinaryStream(int columnIndex) throws SQLException
{
checkResultSet( columnIndex );
- wasNullFlag = (this_row[columnIndex - 1] == null);
if (wasNullFlag)
return null;

@@ -2428,8 +2458,6 @@
Field field;

checkResultSet(columnIndex);
-
- wasNullFlag = (this_row[columnIndex - 1] == null);
if (wasNullFlag)
return null;

@@ -2512,12 +2540,9 @@
*/
public String getFixedString(int col) throws SQLException
{
- // Handle SQL Null
- wasNullFlag = (this_row[col - 1] == null);
- if (wasNullFlag)
- return null;
-
String s = getString(col);
+ if (s == null)
+ return null;

// if we don't have at least 2 characters it can't be money.
if (s.length() < 2)
@@ -2584,6 +2609,18 @@
throw new PSQLException(GT.tr("The column index is out of range: {0}, number of columns: {1}.", new Object[]{new Integer(column), new Integer(fields.length)}), PSQLState.INVALID_PARAMETER_VALUE );
}

+ private void updateWasNullFlag(int column) {
+ wasNullFlag = (this_row[column - 1] == null);
+ }
+
+ /**
+ * Checks that the result set is not closed, it's positioned on a
+ * valid row and that the given column number is valid. Also
+ * updates the {(at)link #wasNullFlag} to correct value.
+ *
+ * @param column The column number to check. Range starts from 1.
+ * @throws SQLException If state or column is invalid.
+ */
protected void checkResultSet( int column ) throws SQLException
{
checkClosed();
@@ -2591,6 +2628,7 @@
throw new PSQLException(GT.tr("ResultSet not positioned properly, perhaps you need to call next."),
PSQLState.INVALID_CURSOR_STATE);
checkColumnIndex(column);
+ updateWasNullFlag(column);
}

//----------------- Formatting Methods -------------------
diff -ur --exclude=CVS --exclude=build --exclude='*.jar' pgjdbc.alreadysent/org/postgresql/jdbc3/Jdbc3ResultSet.java pgjdbc/org/postgresql/jdbc3/Jdbc3ResultSet.java
--- pgjdbc.alreadysent/org/postgresql/jdbc3/Jdbc3ResultSet.java 2007-02-19 08:00:25.000000000 +0200
+++ pgjdbc/org/postgresql/jdbc3/Jdbc3ResultSet.java 2007-07-21 13:13:00.000000000 +0300
@@ -37,7 +37,6 @@
public java.sql.Clob getClob(int i) throws SQLException
{
checkResultSet(i);
- wasNullFlag = (this_row[i - 1] == null);
if (wasNullFlag)
return null;

@@ -47,7 +46,6 @@
public java.sql.Blob getBlob(int i) throws SQLException
{
checkResultSet(i);
- wasNullFlag = (this_row[i - 1] == null);
if (wasNullFlag)
return null;

diff -ur --exclude=CVS --exclude=build --exclude='*.jar' pgjdbc.alreadysent/org/postgresql/jdbc3g/Jdbc3gResultSet.java pgjdbc/org/postgresql/jdbc3g/Jdbc3gResultSet.java
--- pgjdbc.alreadysent/org/postgresql/jdbc3g/Jdbc3gResultSet.java 2007-02-19 08:00:25.000000000 +0200
+++ pgjdbc/org/postgresql/jdbc3g/Jdbc3gResultSet.java 2007-07-21 13:13:00.000000000 +0300
@@ -37,7 +37,6 @@
public java.sql.Clob getClob(int i) throws SQLException
{
checkResultSet(i);
- wasNullFlag = (this_row[i - 1] == null);
if (wasNullFlag)
return null;

@@ -47,7 +46,6 @@
public java.sql.Blob getBlob(int i) throws SQLException
{
checkResultSet(i);
- wasNullFlag = (this_row[i - 1] == null);
if (wasNullFlag)
return null;

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Kris Jurka 2007-07-21 15:40:41 Re: Fix resultset results after updateBinaryStream
Previous Message Mikko Tiihonen 2007-07-20 23:02:24 Fix resultset results after updateBinaryStream