-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathPekkoStream.scala
184 lines (153 loc) · 5.53 KB
/
PekkoStream.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/
package anorm
import java.sql.Connection
import scala.util.control.NonFatal
import scala.concurrent.{ Future, Promise }
import org.apache.pekko.stream.scaladsl.Source
/**
* Anorm companion for the Pekko Streams.
*
* @define materialization It materializes a [[scala.concurrent.Future]] of [[scala.Int]] containing the number of rows read from the source upon completion, and a possible exception if row parsing failed.
* @define sqlParam the SQL query
* @define connectionParam the JDBC connection, which must not be closed until the source is materialized.
* @define columnAliaserParam the column aliaser
*/
object PekkoStream {
/**
* Returns the rows parsed from the `sql` query as a reactive source.
*
* $materialization
*
* @tparam T the type of the result elements
* @param sql $sqlParam
* @param parser the result (row) parser
* @param as $columnAliaserParam
* @param connection $connectionParam
*
* {{{
* import java.sql.Connection
*
* import scala.concurrent.Future
*
* import org.apache.pekko.stream.scaladsl.Source
*
* import anorm._
*
* def resultSource(implicit con: Connection): Source[String, Future[Int]] = PekkoStream.source(SQL"SELECT * FROM Test", SqlParser.scalar[String], ColumnAliaser.empty)
* }}}
*/
@SuppressWarnings(Array("UnusedMethodParameter"))
def source[T](sql: => Sql, parser: RowParser[T], as: ColumnAliaser)(implicit
con: Connection
): Source[T, Future[Int]] = Source.fromGraph(new ResultSource[T](con, sql, as, parser))
/**
* Returns the rows parsed from the `sql` query as a reactive source.
*
* $materialization
*
* @tparam T the type of the result elements
* @param sql $sqlParam
* @param parser the result (row) parser
* @param connection $connectionParam
*/
@SuppressWarnings(Array("UnusedMethodParameter"))
def source[T](sql: => Sql, parser: RowParser[T])(implicit con: Connection): Source[T, Future[Int]] =
source[T](sql, parser, ColumnAliaser.empty)
/**
* Returns the result rows from the `sql` query as an enumerator.
* This is equivalent to `source[Row](sql, RowParser.successful, as)`.
*
* $materialization
*
* @param sql $sqlParam
* @param as $columnAliaserParam
* @param connection $connectionParam
*/
def source(sql: => Sql, as: ColumnAliaser)(implicit connection: Connection): Source[Row, Future[Int]] =
source(sql, RowParser.successful, as)
/**
* Returns the result rows from the `sql` query as an enumerator.
* This is equivalent to
* `source[Row](sql, RowParser.successful, ColumnAliaser.empty)`.
*
* $materialization
*
* @param sql $sqlParam
* @param connection $connectionParam
*/
def source(sql: => Sql)(implicit connnection: Connection): Source[Row, Future[Int]] =
source(sql, RowParser.successful, ColumnAliaser.empty)
// Internal stages
import org.apache.pekko.stream.stage.{ GraphStageLogic, GraphStageWithMaterializedValue, OutHandler }
import org.apache.pekko.stream.{ Attributes, Outlet, SourceShape }
import java.sql.ResultSet
import scala.util.{ Failure, Success }
private[anorm] class ResultSource[T](connection: Connection, sql: Sql, as: ColumnAliaser, parser: RowParser[T])
extends GraphStageWithMaterializedValue[SourceShape[T], Future[Int]] {
private[anorm] var resultSet: ResultSet = _
override val toString = "AnormQueryResult"
val out: Outlet[T] = Outlet(s"${toString}.out")
val shape: SourceShape[T] = SourceShape(out)
override def createLogicAndMaterializedValue(inheritedAttributes: Attributes): (GraphStageLogic, Future[Int]) = {
val result = Promise[Int]()
val logic = new GraphStageLogic(shape) with OutHandler {
private var cursor: Option[Cursor] = None
private var counter: Int = 0
private def failWith(cause: Throwable): Unit = {
result.failure(cause)
fail(out, cause)
()
}
override def preStart(): Unit = {
try {
resultSet = sql.unsafeResultSet(connection)
nextCursor()
} catch {
case NonFatal(cause) => failWith(cause)
}
}
override def postStop() = release()
private def release(): Unit = {
val stmt: Option[java.sql.Statement] = {
if (resultSet != null && !resultSet.isClosed) {
val s = resultSet.getStatement
resultSet.close()
Option(s)
} else None
}
stmt.foreach { s =>
if (!s.isClosed) s.close()
}
}
private def nextCursor(): Unit = {
cursor = Sql.unsafeCursor(resultSet, sql.resultSetOnFirstRow, as)
}
def onPull(): Unit = cursor match {
case Some(c) =>
c.row.as(parser) match {
case Success(parsed) => {
counter += 1
push(out, parsed)
nextCursor()
}
case Failure(cause) =>
failWith(cause)
}
case _ => {
result.success(counter)
complete(out)
}
}
override def onDownstreamFinish() = {
result.tryFailure(new InterruptedException("Downstream finished"))
release()
super.onDownstreamFinish()
}
setHandler(out, this)
}
logic -> result.future
}
}
}