Skip to content

Commit

Permalink
Merge pull request #89 from lucidsoftware/cwoodfield-SqlResult-asScal…
Browse files Browse the repository at this point in the history
…arOption

Make responsibilities more focused and clear
  • Loading branch information
coreywoodfield authored Oct 31, 2024
2 parents 6fd0fc1 + c7ed6ba commit c998635
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 275 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait CollectionsSqlResult { self: SqlResult =>

withResultSet { resultSet =>
while (resultSet.getRow < maxRows && resultSet.next()) {
builder += parser(asRow)
builder += parser(SqlRow(resultSet))
}
}

Expand All @@ -41,7 +41,7 @@ trait CollectionsSqlResult { self: SqlResult =>

withResultSet { resultSet =>
while (resultSet.getRow < maxRows && resultSet.next()) {
builder += parser(asRow)
builder += parser(SqlRow(resultSet))
}
}

Expand Down
43 changes: 0 additions & 43 deletions relate/src/main/scala/com/lucidchart/relate/ResultSetWrapper.scala

This file was deleted.

6 changes: 3 additions & 3 deletions relate/src/main/scala/com/lucidchart/relate/RowIterator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ private[relate] object RowIterator {
private[relate] class RowIterator[A](parser: SqlRow => A, stmt: PreparedStatement, result: SqlResult)
extends Iterator[A] {

private var _hasNext = result.next()
private var _hasNext = result.resultSet.next()

/**
* Make certain that all resources are closed
Expand All @@ -32,9 +32,9 @@ private[relate] class RowIterator[A](parser: SqlRow => A, stmt: PreparedStatemen
* the parsed record
*/
override def next(): A = {
val ret = parser(result.asRow)
val ret = parser(SqlRow(result.resultSet))
if (_hasNext) {
_hasNext = result.next()
_hasNext = result.resultSet.next()
}

// if we've iterated through the whole thing, close resources
Expand Down
22 changes: 1 addition & 21 deletions relate/src/main/scala/com/lucidchart/relate/RowParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait RowParser[A] extends (SqlRow => A) {
def apply(row: SqlRow) = parse(row)
}

object RowParser extends CollectionsParser {
object RowParser {
def apply[A](f: (SqlRow) => A) = new RowParser[A] {
def parse(row: SqlRow) = f(row)
}
Expand All @@ -25,24 +25,4 @@ object RowParser extends CollectionsParser {

private[relate] val insertInt = (row: SqlRow) => row.strictInt(1)
private[relate] val insertLong = (row: SqlRow) => row.strictLong(1)

implicit def multiMap[Key: RowParser, Value: RowParser] = RowParser[Map[Key, Set[Value]]] { result =>
val mm: mutable.Map[Key, Set[Value]] = new mutable.HashMap[Key, Set[Value]]

result.withResultSet { resultSet =>
while (resultSet.next()) {
val key = implicitly[RowParser[Key]].parse(result)
val value = implicitly[RowParser[Value]].parse(result)

mm.get(key)
.map { foundValue =>
mm += (key -> (foundValue + value))
}
.getOrElse {
mm += (key -> Set(value))
}
}
}
mm.toMap
}
}
2 changes: 0 additions & 2 deletions relate/src/main/scala/com/lucidchart/relate/SqlQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ trait Sql extends CollectionsSql {
def executeInsertSingle[U](parser: SqlRow => U)(implicit connection: Connection): U =
insertionStatement.execute(_.asSingle(parser))

def as[A: RowParser]()(implicit connection: Connection): A = normalStatement.execute(_.as[A])

/**
* Execute this query and get back the result as a single record
* @param parser
Expand Down
16 changes: 6 additions & 10 deletions relate/src/main/scala/com/lucidchart/relate/SqlResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.lucidchart.relate
import java.sql.ResultSetMetaData
import scala.collection.mutable
import scala.language.higherKinds
import scala.util.Using

object SqlResult {
def apply(resultSet: java.sql.ResultSet) = new SqlResult(resultSet)
Expand All @@ -19,9 +20,8 @@ object SqlResult {
* The extraction methods (int, string, long, etc.) also have "strict" counterparts. The "strict" methods are slightly
* faster, but do not do type checking or handle null values.
*/
class SqlResult(val resultSet: java.sql.ResultSet) extends ResultSetWrapper with CollectionsSqlResult {

def as[A: RowParser](): A = implicitly[RowParser[A]].parse(asRow)
class SqlResult(private[relate] val resultSet: java.sql.ResultSet) extends CollectionsSqlResult {
private[relate] def withResultSet[A](f: (java.sql.ResultSet) => A) = Using.resource(resultSet)(f)

def asSingle[A: RowParser](): A = asCollection[A, Seq](1).head
def asSingle[A](parser: SqlRow => A): A = asCollection[A, Seq](parser, 1).head
Expand All @@ -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))
mm.addBinding(parsed._1, parsed._2)
}
}
mm.toMap.map(x => x._1 -> x._2.toSet)
}

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
2 changes: 1 addition & 1 deletion relate/src/main/scala/com/lucidchart/relate/SqlRow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(resultSet: java.sql.ResultSet) {

/**
* Get the number of the row the SqlResult is currently on
Expand Down
142 changes: 0 additions & 142 deletions relate/src/test/scala/ImplicitParsingTest.scala

This file was deleted.

Loading

0 comments on commit c998635

Please sign in to comment.