Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make responsibilities more focused and clear #89

Merged
merged 2 commits into from
Oct 31, 2024

Conversation

coreywoodfield
Copy link
Contributor

No description provided.

class SqlResult(val resultSet: java.sql.ResultSet) extends ResultSetWrapper with CollectionsSqlResult {

def as[A: RowParser](): A = implicitly[RowParser[A]].parse(asRow)
class SqlResult(val resultSet: java.sql.ResultSet) extends CollectionsSqlResult {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we make resultSet private?

@@ -42,20 +42,16 @@ class SqlResult(val resultSet: java.sql.ResultSet) extends ResultSetWrapper with
val mm: mutable.MultiMap[U, V] = new mutable.HashMap[U, mutable.Set[V]] with mutable.MultiMap[U, V]
withResultSet { resultSet =>
while (resultSet.next()) {
val parsed = parser(asRow)
val parsed = parser(SqlRow(resultSet))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be worth replacing asRow with currentRow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I did, I'd want it to be private to relate, as it doesn't really align with the rest of the public-facing interface, which all involves processing the whole ResultSet in one go. But really SqlRow(resultSet) isn't that hard to type so I think I'll just leave it

if (_hasNext) {
_hasNext = result.next()
_hasNext = result.resultSet.next()
}

// if we've iterated through the whole thing, close resources
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something I noticed, when looking over this, is that if we stop iterating over the iterator, then we won't close the resultSet.

I wonder if we should also replace asIterator with withIterator that calls a function with the Iterator, and ensure that it is closed at the end.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:upvote:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think asIterator intentionally skips the auto-closing stuff to give the user more control. I looked into this but wasn't able to quickly decide a good way to structure things as there are a lot of different pieces involved (e.g. this is the only place that uses StreamedStatementPreparer). I think it could be good to take a more comprehensive look at cleaning up/improving relate, but my main goal here is just to try to track down some data service issues, so I'm going to say this is out of scope

if (_hasNext) {
_hasNext = result.next()
_hasNext = result.resultSet.next()
}

// if we've iterated through the whole thing, close resources

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:upvote:

@@ -13,7 +13,7 @@ object SqlRow {
def apply(rs: java.sql.ResultSet): SqlRow = new SqlRow(rs)
}

class SqlRow(val resultSet: java.sql.ResultSet) extends ResultSetWrapper {
class SqlRow(val resultSet: java.sql.ResultSet) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this:

That's not their purpose though—SqlRow represents a single row and shouldn't expose implementation details allowing it to access the whole result set

Should we make resultSet private?

} else {
None
}
def asScalarOption[A](): Option[A] = withResultSet { resultSet =>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this asCollection[A, Option](_.asInstanceOf[A])?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I'd rather go the opposite direction, and make asSingle and asSingleOption stop using asCollection

There was some ambiguity in how some things should work (specifically,
`asScalarOption` and `as` in SqlResult) due to the very general nature
of things extending SqlResultWrapper. SqlResultWrapper made all the
methods on ResultSet available, which resulted in SqlRow and SqlResult
both being usable like a ResultSet. That's not their purpose though—
SqlRow represents _a single row_ and shouldn't expose implementation
details allowing it to access the whole result set (similarly, RowParser
should parse _a single row_ and never parse or be able to parse or
access anything beyond that row) and SqlResult represents the result set
as a whole, abstracting away the underlying implementation details.
These two classes _should not_ be interchangeable.

This commit streamlines the interface of the two mentioned classes so
that their purpose is more clear, and so that their behavior can be more
clearly indicated by their purpose. Methods that did not align with the
ostensible purpose of each class were removed.
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.
@coreywoodfield coreywoodfield force-pushed the cwoodfield-SqlResult-asScalarOption branch from d75acd5 to c7ed6ba Compare October 31, 2024 18:46
@coreywoodfield coreywoodfield merged commit c998635 into master Oct 31, 2024
3 checks passed
@coreywoodfield coreywoodfield deleted the cwoodfield-SqlResult-asScalarOption branch October 31, 2024 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants