@@ -27,6 +27,7 @@ |
import java.util.ArrayList; |
import java.util.Calendar; |
import java.util.HashMap; |
+import java.util.Iterator; |
import java.util.Map; |
import java.util.logging.Level; |
import java.util.logging.Logger; |
@@ -46,6 +47,7 @@ |
int rowId; |
int maxRows; |
SDMStatement statement; |
+ boolean lastReadNull = false; |
|
public SDMResultSet(SDMStatement statement, JSONObject result) throws SQLException { |
results = result; |
@@ -81,7 +83,10 @@ |
} |
|
} else { |
- throw new SQLException("Invalid results from database. No Table object found"); |
+ // The USDA REST service for SDM returns an empty JSONObject when the result |
+ //of the query had no return values. Assume here that the query suceeded but had no results. |
+ |
+ //throw new SQLException("Invalid results from database. No Table object found"); |
} |
} |
|
@@ -118,7 +123,14 @@ |
@Override |
public void close() throws SQLException { |
//TODO: clean arraylists here. |
- |
+ rows.clear(); |
+ columns.clear(); |
+ rowId = -1; |
+ maxRows = 0; |
+ Iterator<String> itr = results.keys(); |
+ while (itr.hasNext()) { |
+ results.remove(itr.next()); |
+ } |
} |
|
@Override |
@@ -137,7 +149,7 @@ |
throw new SQLException("ResultSet is empty, no column lables are found."); |
} |
|
- if ( ret_val == -1 ){ |
+ if (ret_val == -1) { |
throw new SQLException("Column was not found in the ResultSet: " + columnLabel); |
} |
return ret_val; |
@@ -295,9 +307,11 @@ |
@Override |
public double getDouble(int columnIndex) throws SQLException { |
String value = rows.get(rowId).getValue(columnIndex); |
- |
+ this.lastReadNull = false; |
if ((null != value) && !value.equalsIgnoreCase("null")) { |
return Double.parseDouble(value); |
+ } else { |
+ this.lastReadNull = true; |
} |
return Double.NaN; |
} |
@@ -306,9 +320,11 @@ |
public double getDouble(String columnLabel) throws SQLException { |
int index = findColumn(columnLabel); |
String value = rows.get(rowId).getValue(index); |
- |
+ this.lastReadNull = false; |
if ((null != value) && !value.equalsIgnoreCase("null")) { |
return Double.parseDouble(value); |
+ } else { |
+ this.lastReadNull = true; |
} |
return Double.NaN; |
} |
@@ -336,9 +352,11 @@ |
@Override |
public float getFloat(int columnIndex) throws SQLException { |
String value = rows.get(rowId).getValue(columnIndex); |
- |
+ this.lastReadNull = false; |
if ((null != value) && !value.equalsIgnoreCase("null")) { |
return Float.parseFloat(value); |
+ } else { |
+ this.lastReadNull = true; |
} |
return Float.NaN; |
} |
@@ -347,9 +365,11 @@ |
public float getFloat(String columnLabel) throws SQLException { |
int index = findColumn(columnLabel); |
String value = rows.get(rowId).getValue(index); |
- |
+ this.lastReadNull = false; |
if ((null != value) && !value.equalsIgnoreCase("null")) { |
return Float.parseFloat(value); |
+ } else { |
+ this.lastReadNull = true; |
} |
return Float.NaN; |
} |
@@ -362,9 +382,11 @@ |
@Override |
public int getInt(int columnIndex) throws SQLException { |
String value = rows.get(rowId).getValue(columnIndex); |
- |
+ this.lastReadNull = false; |
if ((null != value) && !value.equalsIgnoreCase("null")) { |
return Integer.parseInt(value); |
+ } else { |
+ this.lastReadNull = true; |
} |
return Integer.MAX_VALUE; |
} |
@@ -373,9 +395,11 @@ |
public int getInt(String columnLabel) throws SQLException { |
int index = findColumn(columnLabel); |
String value = rows.get(rowId).getValue(index); |
- |
+ this.lastReadNull = false; |
if ((null != value) && !value.equalsIgnoreCase("null")) { |
return Integer.parseInt(value); |
+ } else { |
+ this.lastReadNull = true; |
} |
return Integer.MAX_VALUE; |
} |
@@ -383,9 +407,11 @@ |
@Override |
public long getLong(int columnIndex) throws SQLException { |
String value = rows.get(rowId).getValue(columnIndex); |
- |
+ this.lastReadNull = false; |
if ((null != value) && !value.equalsIgnoreCase("null")) { |
return Long.parseLong(value); |
+ } else { |
+ this.lastReadNull = true; |
} |
return Long.MAX_VALUE; |
} |
@@ -394,9 +420,11 @@ |
public long getLong(String columnLabel) throws SQLException { |
int index = findColumn(columnLabel); |
String value = rows.get(rowId).getValue(index); |
- |
+ this.lastReadNull = false; |
if ((null != value) && !value.equalsIgnoreCase("null")) { |
return Long.parseLong(value); |
+ } else { |
+ this.lastReadNull = true; |
} |
return Long.MAX_VALUE; |
} |
@@ -518,13 +546,28 @@ |
|
@Override |
public String getString(int columnIndex) throws SQLException { |
- return rows.get(rowId).getValue(columnIndex); |
+ String ret_val; |
+ this.lastReadNull = false; |
+ |
+ ret_val = rows.get(rowId).getValue(columnIndex); |
+ if ((null == ret_val) || (ret_val.equalsIgnoreCase("null"))) { |
+ this.lastReadNull = true; |
+ } |
+ |
+ return ret_val; |
} |
|
@Override |
public String getString(String columnLabel) throws SQLException { |
int index = findColumn(columnLabel); |
- return rows.get(rowId).getValue(index); |
+ String ret_val; |
+ this.lastReadNull = false; |
+ |
+ ret_val = rows.get(rowId).getValue(index); |
+ if ((null == ret_val) || (ret_val.equalsIgnoreCase("null"))) { |
+ this.lastReadNull = true; |
+ } |
+ return ret_val; |
} |
|
@Override |
@@ -649,7 +692,7 @@ |
|
@Override |
public boolean next() throws SQLException { |
- if ((maxRows > 0) && (rowId < (maxRows-1))) { |
+ if ((maxRows > 0) && (rowId < (maxRows - 1))) { |
rowId++; |
return true; |
} |
@@ -1114,7 +1157,7 @@ |
|
@Override |
public boolean wasNull() throws SQLException { |
- throw new SQLFeatureNotSupportedException("Operation not supported."); //To change body of generated methods, choose Tools | Templates. |
+ return lastReadNull; |
} |
|
private class ResultRow { |
@@ -1135,5 +1178,5 @@ |
throw new SQLException("Invalid index used in row operation: getValue"); |
} |
} |
- } |
+ } |
} |