Permalink
Browse files

[jdbc-driver] Support for ResultSet type/scrollability

  • Loading branch information...
cchantep
cchantep committed Aug 22, 2014
1 parent f8f7cbe commit 1d8160dd6b26fcf964fce51096f3043994069fab
@@ -54,6 +54,15 @@
*/
protected int row = 0;
/**
* Result set type
*
* @see java.sql.ResultSet#TYPE_FORWARD_ONLY
* @see java.sql.ResultSet#TYPE_SCROLL_INSENSITIVE
* @see java.sql.ResultSet#TYPE_SCROLL_SENSITIVE
*/
protected final int type;
/**
* Cursor name
*/
@@ -68,6 +77,7 @@ protected AbstractResultSet() {
this.cursorName =
String.format("cursor-%d", System.identityHashCode(this));
this.type = TYPE_FORWARD_ONLY;
} // end of <init>
/**
@@ -79,6 +89,19 @@ protected AbstractResultSet(final String cursor) {
} // end of if
this.cursorName = cursor;
this.type = TYPE_FORWARD_ONLY;
} // end of <init>
/**
* Bulk constructor.
*/
protected AbstractResultSet(final String cursor, final int type) {
if (cursor == null) {
throw new IllegalArgumentException();
} // end of if
this.cursorName = cursor;
this.type = type;
} // end of <init>
// ---
@@ -458,15 +481,15 @@ public boolean isLast() throws SQLException {
* {@inheritDoc}
*/
public void beforeFirst() throws SQLException {
if (this.row > 0) {
throw new SQLException("Backward move");
} // end of if
checkNotForwardOnly();
} // end of beforeFirst
/**
* {@inheritDoc}
*/
public void afterLast() throws SQLException {
checkNotForwardOnly();
this.row = this.fetchSize + 1;
} // end of afterLast
@@ -526,6 +549,8 @@ public boolean relative(final int rows) throws SQLException {
* {@inheritDoc}
*/
public boolean previous() throws SQLException {
checkNotForwardOnly();
return relative(-1);
} // end of previous
@@ -540,6 +565,12 @@ public boolean next() throws SQLException {
* {@inheritDoc}
*/
public void setFetchDirection(final int direction) throws SQLException {
if (direction == FETCH_REVERSE || direction == FETCH_UNKNOWN) {
checkNotForwardOnly();
} // end of if
// ---
this.fetchDirection = direction;
} // end of setFetchDirection
@@ -568,7 +599,7 @@ public int getFetchSize() throws SQLException {
* {@inheritDoc}
*/
public int getType() throws SQLException {
return TYPE_FORWARD_ONLY;
return this.type;
} // end of getType
/**
@@ -1703,4 +1734,14 @@ protected void checkClosed() throws SQLException {
throw new SQLException("Result set is closed");
} // end of if
} // end of checkClosed
/**
* Throws a SQLException("Type of result set is forward only")
* if {java.sql.ResultSet#TYPE_FORWARD_ONLY}.
*/
protected void checkNotForwardOnly() throws SQLException {
if (this.type == TYPE_FORWARD_ONLY) {
throw new SQLException("Type of result set is forward only");
} // end of if
} // end of checkClosed
} // end of class AbstractResultSet
@@ -31,7 +31,7 @@ object AbstractResultSetSpec extends Specification {
"New resultset" should {
"refuse invalid cursor name" in {
lazy val rs = new AbstractResultSet(null) { }
lazy val rs = new AbstractResultSet(null) {}
rs aka "ctor" must throwA[IllegalArgumentException]
}
@@ -74,36 +74,55 @@ object AbstractResultSetSpec extends Specification {
"Fetch size" should {
"initially be zero" in {
defaultSet.getFetchSize aka "size" mustEqual 0
defaultSet.getFetchSize aka "size" must_== 0
}
"be properly set" in {
lazy val rs = defaultSet
rs.setFetchSize(2)
rs.getFetchSize aka "size" mustEqual 2
rs.getFetchSize aka "size" must_== 2
}
}
"Fetch directory" should {
"Fetch direction" should {
"initially be FETCH_FORWARD" in {
defaultSet.getFetchDirection.
aka("direction") mustEqual ResultSet.FETCH_FORWARD
}
"be property set" in {
lazy val rs = defaultSet
val rdir = ResultSet.FETCH_REVERSE
rs.setFetchDirection(rdir)
"fail to be set when not scrollable" in {
defaultSet.setFetchDirection(ResultSet.FETCH_REVERSE).
aka("setting fetch direction") must throwA[SQLException](
"Type of result set is forward only") and (
defaultSet.setFetchDirection(ResultSet.FETCH_UNKNOWN).
aka("setting fetch direction") must throwA[SQLException](
"Type of result set is forward only"))
}
"be property set on scrollable set" >> {
"reverse" in {
lazy val rs = scrollInsensitiveSet
rs.setFetchDirection(ResultSet.FETCH_REVERSE)
rs.getFetchDirection aka "direction" mustEqual ResultSet.FETCH_REVERSE
}
"unknown" in {
lazy val rs = scrollInsensitiveSet
rs.setFetchDirection(ResultSet.FETCH_UNKNOWN)
rs.getFetchDirection aka "directory" mustEqual rdir
rs.getFetchDirection aka "direction" mustEqual ResultSet.FETCH_UNKNOWN
}
}
}
"Row" should {
"initially be zero" in {
defaultSet.getRow aka "row" mustEqual 0
defaultSet.getRow aka "row" must_== 0
}
"not be moved backward (forward only)" in {
@@ -206,17 +225,31 @@ object AbstractResultSetSpec extends Specification {
}
}
"be moved to last" >> {
"without change" in {
"be moved before first" >> {
"throwing exception" in {
lazy val rs = defaultSet
(rs.last aka "last" must beTrue) and (rs.getRow aka "row" mustEqual 0)
rs.beforeFirst aka "moving before first" must throwA[SQLException](
message = "Type of result set is forward only")
}
"at 1" in {
"without change if scrollable" in {
lazy val rs = scrollInsensitiveSet
rs.beforeFirst
rs.getRow aka "row" must_== 0 and (
rs.isBeforeFirst aka "before first" must beTrue)
}
"with failure when backward and not scrollable" in {
lazy val rs = defaultSet
rs.setFetchSize(1)
(rs.last aka "last" must beTrue) and (rs.getRow aka "row" mustEqual 1)
(rs.first aka "move first" must beTrue).
and(rs.getRow aka "row" mustEqual 1).
and(rs.beforeFirst aka "before first" must throwA[SQLException](
message = "Type of result set is forward only"))
}
}
@@ -249,25 +282,32 @@ object AbstractResultSetSpec extends Specification {
}
}
"be moved before first" >> {
"be moved to last" >> {
"without change" in {
lazy val rs = defaultSet
rs.beforeFirst
(rs.getRow aka "row" mustEqual 0).
and(rs.isBeforeFirst aka "before first" must beTrue)
rs.last aka "last" must beTrue and (rs.getRow aka "row" must_== 0)
}
"with failure when backward" in {
"at 1" in {
lazy val rs = defaultSet
rs.setFetchSize(1)
(rs.first aka "move first" must beTrue).
and(rs.getRow aka "row" mustEqual 1).
and(rs.beforeFirst aka "before first" must throwA[SQLException](
message = "Backward move"))
rs.last aka "last" must beTrue and (rs.getRow aka "row" must_== 1)
}
}
"be moved to after last" >> {
"with failure when not scrollable" in {
defaultSet.afterLast aka "moving after last" must throwA[SQLException](
"Type of result set is forward only")
}
"at 2" in {
lazy val rs = scrollInsensitiveSet
rs.setFetchSize(1)
rs.afterLast()
rs.getRow aka "row" must_== 2
}
}
}
@@ -479,4 +519,6 @@ object AbstractResultSetSpec extends Specification {
}
def defaultSet = new AbstractResultSet {}
def scrollInsensitiveSet = new AbstractResultSet(
"si", ResultSet.TYPE_SCROLL_INSENSITIVE) {}
}

0 comments on commit 1d8160d

Please sign in to comment.