Permalink
Browse files

[core] Supports max rows limit on statement (and row list):

```java
java.sql.PreparedStatement stmt = conn.prepareStatement("SQL");
stmt.setMaxRows(2);

stmt.execute();

// Will go through RowList.resultSet(2)
java.sql.ResultSet rs = stmt.getResultSet();
```
  • Loading branch information...
cchantep
cchantep committed Nov 27, 2013
1 parent 44f9c1e commit 9d01cedde46f13cb1d9a2224732b2e96ba9314ad
@@ -10,7 +10,6 @@
import java.sql.Connection;
import java.sql.ResultSet;
import acolyte.StatementHandler.Parameter;
/**
@@ -69,6 +68,11 @@
*/
protected ResultSet result = null;
/**
* Max rows
*/
protected int maxRows = 0;
/**
* Last update count
*/
@@ -134,8 +138,8 @@ public ResultSet executeQuery(final String sql) throws SQLException {
this.warning = res.getWarning();
return (this.result =
res.getRowList().resultSet().withStatement(this));
return (this.result = res.getRowList().
resultSet(this.maxRows).withStatement(this));
} // end of executeQuery
@@ -185,7 +189,7 @@ public void setMaxFieldSize(final int max) throws SQLException {
public int getMaxRows() throws SQLException {
checkClosed();
return 0;
return this.maxRows;
} // end of getMaxRows
/**
@@ -194,7 +198,11 @@ public int getMaxRows() throws SQLException {
public void setMaxRows(final int max) throws SQLException {
checkClosed();
throw new UnsupportedOperationException();
if (max < 0) {
throw new SQLException("Negative max rows");
} // end of if
this.maxRows = max;
} // end of setMaxRows
/**
@@ -48,9 +48,26 @@
/**
* Returns result set from these rows.
*
* @param maxRows Limit for the maximum number of rows.
* If <= 0 no limit will be set.
* If the limit is set and exceeded, the excess rows are silently dropped.
*/
public RowResultSet<R> resultSet(int maxRows) {
if (maxRows <= 0) {
return new RowResultSet<R>(getRows());
} // end of if
return new RowResultSet<R>(getRows().subList(0, maxRows));
} // end of resultSet
/**
* Returns result set from these rows (without row limit).
*
* @see #resultSet(int)
*/
public RowResultSet<R> resultSet() {
return new RowResultSet<R>(getRows());
return resultSet(0);
} // end of resultSet
/**
@@ -196,12 +196,6 @@ object AbstractStatementSpec extends Specification {
aka("setter") must throwA[UnsupportedOperationException])
}
"have no max row count" in {
(statement().getMaxRows aka "max count" mustEqual 0).
and(statement().setMaxRows(1).
aka("setter") must throwA[UnsupportedOperationException])
}
"have not query timeout" in {
(statement().getQueryTimeout aka "timeout" mustEqual 0).
and(statement().setQueryTimeout(1).
@@ -259,6 +253,47 @@ object AbstractStatementSpec extends Specification {
}
}
"Max row count" should {
"initially be zero" in {
statement().getMaxRows aka "initial count" mustEqual 0
}
"not be accessible on a closed statement" in {
lazy val s = statement()
s.close()
(s.getMaxRows aka "getter" must throwA[SQLException](
message = "Statement is closed")).
and(s.setMaxRows(1) aka "setter" must throwA[SQLException](
message = "Statement is closed"))
}
"not be set negative" in {
statement().setMaxRows(-1) aka "setter" must throwA[SQLException](
message = "Negative max rows")
}
"skip row #3 with max count 2" in {
lazy val h = new StatementHandler {
def getGeneratedKeys = null
def isQuery(s: String) = true
def whenSQLUpdate(s: String, p: Params) = UpdateResult.Nothing
def whenSQLQuery(s: String, p: Params) = {
RowLists.stringList.append("A").append("B").append("C").asResult
}
}
lazy val s = statement(h = h)
s.setMaxRows(2)
(s.execute("QUERY") aka "flag" must beTrue).
and(s.getResultSet aka "resultset" mustEqual {
RowLists.stringList.append("A").append("B").resultSet
})
}
}
"Batch" should {
"not be added" in {
statement().addBatch("BATCH") aka "add" must throwA[SQLException](
@@ -353,6 +353,32 @@ object RowListSpec extends Specification with RowListTest {
}
}
"Max rows limit" should {
lazy val rows = RowLists.intList(10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
"drop half of rows" in {
rows.resultSet(5) aka "resultset" mustEqual {
RowLists.intList(10, 9, 8, 7, 6).resultSet
}
}
"drop 2 rows at end (max = 8)" in {
rows.resultSet(8) aka "resultset" mustEqual {
RowLists.intList(10, 9, 8, 7, 6, 5, 4, 3).resultSet
}
}
"extract only 3 rows" in {
rows.resultSet(3) aka "resultset" mustEqual {
RowLists.intList(10, 9, 8).resultSet
}
}
"get all rows" in {
rows.resultSet(11) aka "resultset" mustEqual rows.resultSet
}
}
"Object column by index" should {
"not be read when not on a row" in {
lazy val typemap = null.asInstanceOf[java.util.Map[String, Class[_]]]
View
@@ -159,6 +159,7 @@ public final class Rows {""")
val rlf: java.io.File = {
val f = managedSources / "acolyte" / "RowLists.java"
// @todo medium Move as template file
IO.writer[java.io.File](f, "", IO.defaultCharset, false) { w =>
w.append("""package acolyte;
@@ -202,6 +203,20 @@ public final class RowLists {
return rowList1(Integer.TYPE);
}
/**
* Convinience alias for row list of 1 int column.
* @param values Initial values
*/
public static RowList1<Integer> intList(final Integer... values) {
RowList1<Integer> list = intList();
for (Integer v : values) {
list = list.append(v);
}
return list;
}
/**
* Convinience alias for row list of 1 long column.
*/

0 comments on commit 9d01ced

Please sign in to comment.