Permalink
Browse files

[jdbc-driver] Better fetch-size

  • Loading branch information...
cchantep
cchantep committed Dec 13, 2015
1 parent 57e8e55 commit d60d07e1afb887e055c46f5d49ace4842a93946f
@@ -246,7 +246,7 @@ public NilRowList withNullable(final int columnIndex,
public final class RowResultSet<R extends Row> extends AbstractResultSet {
final List<Class<?>> columnClasses;
final Map<String,Integer> columnLabels;
final List<R> rows;
List<R> rows;
final AbstractStatement statement;
final SQLWarning warning;
private Object last;
@@ -387,8 +387,16 @@ public AbstractStatement getStatement() {
/**
* {@inheritDoc}
*/
public void setFetchSize(final int rows) throws SQLException {
throw new UnsupportedOperationException();
public void setFetchSize(final int maxRows) throws SQLException {
checkClosed();
synchronized(this) {
if (maxRows > this.rows.size()) {
return;
}
this.rows = this.rows.subList(0, maxRows);
}
} // end of setFetchSize
/**
@@ -50,6 +50,8 @@ public final class Row#N#<#CP#> implements Row {
#SP#
#AO#
// --- Object support ---
/**
@@ -81,7 +81,8 @@ object AbstractStatementSpec extends Specification {
lazy val s = statement(h = h)
(s.execute("QUERY") aka "flag" must beTrue).
and(s.getResultSet aka "resultset" mustEqual RowLists.stringList.resultSet).
and(s.getResultSet.
aka("resultset") mustEqual RowLists.stringList.resultSet).
and(s.getUpdateCount aka "update count" mustEqual -1).
and(sql aka "executed SQL" mustEqual "QUERY")
@@ -286,6 +287,14 @@ object AbstractStatementSpec extends Specification {
}
"Max row count" should {
lazy val h = new StatementHandler {
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
}
}
"initially be zero" in {
statement().getMaxRows aka "initial count" mustEqual 0
}
@@ -308,20 +317,14 @@ object AbstractStatementSpec extends Specification {
}
"skip row #3 with max count 2" in {
lazy val h = new StatementHandler {
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)
lazy val rs = s.getResultSet
(s.execute("QUERY") aka "flag" must beTrue).
and(s.getResultSet aka "resultset" mustEqual {
RowLists.stringList.append("A").append("B").resultSet
})
and(rs.getFetchSize must_== 2).
and(rs aka "resultset" mustEqual(
RowLists.stringList("A", "B").resultSet))
}
}
@@ -664,12 +664,6 @@ object RowListSpec extends Specification with RowListTest {
}
"Result set fetch size" should {
"be immutable" in {
new RowList1.Impl(classOf[String]).resultSet.setFetchSize(1).
aka("setter") must throwA[UnsupportedOperationException]
}
"be 1" in {
(new RowList1.Impl(classOf[String]).append(row1("str")).
resultSet.getFetchSize aka "size" must_== 1).
@@ -684,6 +678,21 @@ object RowListSpec extends Specification with RowListTest {
resultSet.getFetchSize aka "size" mustEqual 2)
}
"be updated to 4" in {
lazy val rows = RowLists.intList(10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
val rs = rows.resultSet()
rs.setFetchSize(4)
rs aka "resultset" mustEqual(RowLists.intList(10, 9, 8, 7).resultSet)
}
"not be updated on closed result set" in {
val rs = RowLists.intList(1).resultSet
rs.close()
rs.setFetchSize(1) must throwA[SQLException]("Result set is closed")
}
}
"Max rows limit" should {
@@ -696,19 +705,19 @@ object RowListSpec extends Specification with RowListTest {
}
"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
}
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
}
rows.resultSet(3) aka "resultset" mustEqual(
RowLists.intList(10, 9, 8).resultSet)
}
"get all rows" in {
rows.resultSet(11) aka "resultset" mustEqual rows.resultSet
rows.resultSet(11) aka "resultset" mustEqual rows.resultSet and (
rows.resultSet(0) mustEqual rows.resultSet) and (
rows.resultSet() mustEqual rows.resultSet)
}
}
View
@@ -16,7 +16,7 @@ object Acolyte extends Build with Dependencies
else "1.6"
lazy val root = Project(id = "acolyte", base = file(".")).
aggregate(scalacPlugin, reactiveMongo,
aggregate(scalacPlugin/*, reactiveMongo*/,
jdbcDriver, jdbcScala, jdbcClojure, studio).
settings(
organization in ThisBuild := "org.eu.acolyte",
View
@@ -17,8 +17,8 @@ trait JdbcDriver { deps: Dependencies ⇒
sourceGenerators in Compile <+= (baseDirectory in Compile) zip (sourceManaged in Compile) map (dirs {
val (base, managed) = dirs
generateCallableStatement(base, managed / "acolyte" / "jdbc") +:
generateRowClasses(base, managed / "acolyte" / "jdbc",
"acolyte.jdbc", false)
generateRowClasses(base, managed / "acolyte" / "jdbc",
"acolyte.jdbc", false)
}))
// Source generators
@@ -96,6 +96,16 @@ trait JdbcDriver { deps: Dependencies ⇒
return new Row%s<%s>(%s);
}""".format(i + 1, n, cp.mkString(","), i + 1, letter(i), n, cp.mkString(","), (for (j 0 until n) yield { if (j == i) "value" else s"this._$j" }).mkString(", "))
val ao = for (i (n + 1) to (lim - n)) yield {
val ol = for (j 0 until i) yield letter(n + j - 1)
s"""/**
* Appends the values of the rows.
*/
public <${ol mkString ", "}> void append(Row$i<${ol mkString ", "}> other) {
}"""
}
val hc = for (i 0 until n) yield s"append(this._$i)"
val eq = for (i 0 until n) yield s"append(this._$i, other._$i)"
@@ -112,6 +122,7 @@ trait JdbcDriver { deps: Dependencies ⇒
replace("#RP#", rp.mkString("\n ")).
replace("#NA#", na.mkString(", ")).
replace("#SP#", sp.mkString("\n\n ")).
replace("#AO#", ao.mkString("\n\n ")).
replace("#HC#", hc.mkString(".\n ")).
replace("#EQ#", eq.mkString(".\n "))).
append("\n")
@@ -133,8 +144,8 @@ trait JdbcDriver { deps: Dependencies ⇒
* @deprecated Rows are created by append operation on row lists.
*/
%s
public final class Rows {""".format(pkg,
{ if (deprecated) "@Deprecated" else "" }))
public final class Rows {""".format(pkg,
{ if (deprecated) "@Deprecated" else "" }))
for (n 1 to lim) yield {
val g = for (i 0 until n) yield letter(i)
@@ -236,9 +247,9 @@ public final class Rows {""".format(pkg,
val ps = for (i 0 until n) yield s"final Class<${letter(i)}> _c$i"
val cs = for (i 0 until n) yield s"final Column<${letter(i)}> _c$i"
val as = for (i 0 until n) yield s"_c$i"
val ls = for (i 0 until n) yield s"withLabel(${i+1}, _c$i.name)"
val ls = for (i 0 until n) yield s"withLabel(${i + 1}, _c$i.name)"
val ns = for (i 0 until n) yield (
s"withNullable(${i+1}, _c$i.nullable)")
s"withNullable(${i + 1}, _c$i.nullable)")
val gp = g.mkString(",")
l :+ """
View
@@ -10,6 +10,11 @@ trait JdbcScala { deps: Dependencies ⇒
Project(id = "jdbc-scala", base = file("jdbc-scala")).settings(
name := "jdbc-scala",
javacOptions in Test ++= Seq("-Xlint:unchecked", "-Xlint:deprecation"),
scalacOptions in Compile ++= Seq("-unchecked", "-deprecation"),
scalacOptions in Compile <++= (scalaVersion in ThisBuild).map { v =>
if (v startsWith "2.11") Seq("-Ywarn-unused-import")
else Nil
},
scalacOptions in Test <++= (version in ThisBuild).
zip(scalaVersion in ThisBuild).
zip(baseDirectory in (scalacPlugin, Compile)).
@@ -30,11 +35,12 @@ trait JdbcScala { deps: Dependencies ⇒
libraryDependencies ++= Seq(
"org.eu.acolyte" % "jdbc-driver" % (version in ThisBuild).value,
specs2Test),
sourceGenerators in Compile <+= (baseDirectory in Compile) zip (sourceManaged in Compile) map (dirs {
val (base, managed) = dirs
generateRowClasses(base, managed / "acolyte" / "jdbc",
"acolyte.jdbc", false)
})).dependsOn(scalacPlugin, jdbcDriver)
sourceGenerators in Compile <+= (baseDirectory in Compile).
zip(sourceManaged in Compile) map (dirs {
val (base, managed) = dirs
generateRowClasses(base, managed / "acolyte" / "jdbc",
"acolyte.jdbc", false)
})).dependsOn(scalacPlugin, jdbcDriver)
// Source generator
private def generateRowClasses(base: File, outdir: File, pkg: String, deprecated: Boolean): Seq[File] = {
View
@@ -33,7 +33,7 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
<version>3.4</version>
</dependency>
</dependencies>
@@ -165,7 +165,7 @@
<dependency>
<groupId>lt.velykis.maven.skins</groupId>
<artifactId>reflow-velocity-tools</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
</dependency>
<!-- Reflow skin requires Velocity >= 1.7 -->
View
@@ -43,6 +43,13 @@
<menu ref="reports" />
</body>
<custom>
<reflowSkin>
<theme>bootswatch-flatly</theme>
<highlightJs>true</highlightJs>
</reflowSkin>
</custom>
<skin>
<groupId>lt.velykis.maven.skins</groupId>
<artifactId>reflow-maven-skin</artifactId>

0 comments on commit d60d07e

Please sign in to comment.