From cf315fa1172cd8ffc11a5a3f3f833303d79db25b Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 22 Apr 2015 17:26:41 -0400 Subject: Gracefully handle SQLite's pre-closed ResultSets for queries with 0 returned rows --- src/main/java/com/iciql/Db.java | 13 ++++++++----- src/main/java/com/iciql/Query.java | 35 ++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 18 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/iciql/Db.java b/src/main/java/com/iciql/Db.java index 7413c8f..4b55150 100644 --- a/src/main/java/com/iciql/Db.java +++ b/src/main/java/com/iciql/Db.java @@ -414,11 +414,14 @@ public class Db implements AutoCloseable { List result = new ArrayList(); TableDefinition def = (TableDefinition) define(modelClass); try { - int[] columns = def.mapColumns(wildcardSelect, rs); - while (rs.next()) { - T item = Utils.newObject(modelClass); - def.readRow(dialect, item, rs, columns); - result.add(item); + // SQLite returns pre-closed ResultSets for query results with 0 rows + if (!rs.isClosed()) { + int[] columns = def.mapColumns(wildcardSelect, rs); + while (rs.next()) { + T item = Utils.newObject(modelClass); + def.readRow(dialect, item, rs, columns); + result.add(item); + } } } catch (SQLException e) { throw new IciqlException(e); diff --git a/src/main/java/com/iciql/Query.java b/src/main/java/com/iciql/Query.java index 946a460..1a8cbc7 100644 --- a/src/main/java/com/iciql/Query.java +++ b/src/main/java/com/iciql/Query.java @@ -248,11 +248,14 @@ public class Query { appendFromWhere(stat); ResultSet rs = stat.executeQuery(); try { - int[] columns = def.mapColumns(false, rs); - while (rs.next()) { - T item = from.newObject(); - def.readRow(db.getDialect(), item, rs, columns); - result.add(item); + // SQLite returns pre-closed ResultSets for query results with 0 rows + if (!rs.isClosed()) { + int[] columns = def.mapColumns(false, rs); + while (rs.next()) { + T item = from.newObject(); + def.readRow(db.getDialect(), item, rs, columns); + result.add(item); + } } } catch (SQLException e) { throw IciqlException.fromSQL(stat.getSQL(), e); @@ -401,11 +404,14 @@ public class Query { appendFromWhere(stat); ResultSet rs = stat.executeQuery(); try { - int[] columns = def.mapColumns(false, rs); - while (rs.next()) { - X row = Utils.newObject(clazz); - def.readRow(db.getDialect(), row, rs, columns); - result.add(row); + // SQLite returns pre-closed ResultSets for query results with 0 rows + if (!rs.isClosed()) { + int[] columns = def.mapColumns(false, rs); + while (rs.next()) { + X row = Utils.newObject(clazz); + def.readRow(db.getDialect(), row, rs, columns); + result.add(row); + } } } catch (SQLException e) { throw IciqlException.fromSQL(stat.getSQL(), e); @@ -428,9 +434,12 @@ public class Query { db.getDialect().registerAdapter(dta); } try { - while (rs.next()) { - X value = (X) db.getDialect().deserialize(rs, 1, x.getClass()); - result.add(value); + // SQLite returns pre-closed ResultSets for query results with 0 rows + if (!rs.isClosed()) { + while (rs.next()) { + X value = (X) db.getDialect().deserialize(rs, 1, x.getClass()); + result.add(value); + } } } catch (Exception e) { throw IciqlException.fromSQL(stat.getSQL(), e); -- cgit v1.2.3