Skip to content

Commit

Permalink
Make all SqlResult methods close the ResultSet when finished
Browse files Browse the repository at this point in the history
Before the previous commit, it perhaps made sense for these methods to
not close the ResultSet—the SqlResult exposed methods to iterate through
the results one-by-one, and these methods only deal with the current
row.
  • Loading branch information
coreywoodfield committed Oct 31, 2024
1 parent 1d881a6 commit c7ed6ba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
8 changes: 2 additions & 6 deletions relate/src/main/scala/com/lucidchart/relate/SqlResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ class SqlResult(private[relate] val resultSet: java.sql.ResultSet) extends Colle
}

def asScalar[A](): A = asScalarOption.get
def asScalarOption[A](): Option[A] = {
if (resultSet.next()) {
Some(resultSet.getObject(1).asInstanceOf[A])
} else {
None
}
def asScalarOption[A](): Option[A] = withResultSet { resultSet =>
Option.when(resultSet.next())(resultSet.getObject(1).asInstanceOf[A])
}

/**
Expand Down
21 changes: 21 additions & 0 deletions relate/src/test/scala/SqlResultSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,27 @@ class SqlResultSpec extends Specification with Mockito {

result.asScalarOption[Long] must_== None
}

"close the ResultSet" in {
val (rs, _, result) = getMocks

rs.next returns true
rs.getObject(1) returns (2: java.lang.Long)

result.asScalar[Long] mustEqual 2L

there was one(rs).close()
}

"close the ResultSet if it was empty" in {
val (rs, _, result) = getMocks

rs.next returns false

result.asScalarOption[Long] must beNone

there was one(rs).close()
}
}

"extractOption" should {
Expand Down

0 comments on commit c7ed6ba

Please sign in to comment.