/
PreparedStatement.scala
280 lines (263 loc) · 9.76 KB
/
PreparedStatement.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package com.twitter.finagle.mysql
import com.twitter.function.JavaFunction
import com.twitter.util.Future
import java.{util => ju}
import scala.annotation.varargs
import scala.collection.JavaConverters._
/**
* A `PreparedStatement` represents a parameterized SQL statement which may be
* applied concurrently with varying parameters.
*
* These are SQL statements with `?`'s used for the parameters which are
* "filled in" per usage by `read`, `select`, and `modify`.
*
* @see [[Client.prepare(String)]]
* @see [[CursoredStatement]] for a lazy stream of [[Row]]s.
*/
trait PreparedStatement {
/**
* Executes the prepared statement with the given `params`.
*
* '''Note:''' this is a lower-level API. For SELECT queries, prefer using
* [[select]] or [[read]], and use [[modify]] for DML (INSERT/UPDATE/DELETE)
* and DDL.
*
* For Scala users, you can use the implicit conversions to [[Parameter]]
* by importing `Parameter._`. For example:
* {{{
* import com.twitter.finagle.mysql.{Client, PreparedStatement, Result}
* import com.twitter.finagle.mysql.Parameter._
* import com.twitter.util.Future
*
* val client: Client = ???
* val preparedStatement: PreparedStatement =
* client.prepare("INSERT INTO a_table (column1, column2) VALUES (?, ?)")
*
* // note the implicit conversions of the String and Int to Parameters
* val result: Future[Result] = preparedStatement("value1", 1234)
* }}}
*
* Java users, see [[asJava]] and use [[PreparedStatement.AsJava.execute]].
*
* @return a [[Result]] `Future`. A successful SELECT query will satisfy the
* `Future` with a [[ResultSet]] while DML and DDL will be an [[OK]].
* If there is a server error the `Future` will be failed with a [[ServerError]]
* exception, '''not''' an [[Error]] from the [[Result]] ADT.
*/
def apply(params: Parameter*): Future[Result]
/**
* Executes the prepared statement SELECT query with the given `params`.
*
* For Scala users, you can use the implicit conversions to [[Parameter]]
* by importing `Parameter._`. For example:
* {{{
* import com.twitter.finagle.mysql.{Client, PreparedStatement, ResultSet}
* import com.twitter.finagle.mysql.Parameter._
* import com.twitter.util.Future
*
* val client: Client = ???
* val preparedStatement: PreparedStatement =
* client.prepare("SELECT column1 FROM a_table WHERE column2 = ? AND column3 = ?)")
*
* // note the implicit conversions of the String and Int to Parameters
* val resultSet: Future[ResultSet] = preparedStatement.read("value1", 1234)
* }}}
*
* Java users, see [[asJava]] and use [[PreparedStatement.AsJava.read]].
*
* @see [[select]]
*/
def read(params: Parameter*): Future[ResultSet] =
apply(params: _*).flatMap {
case rs: ResultSet => Future.value(rs)
case r => Future.exception(new IllegalStateException(s"Unsupported response to a read='$r'"))
}
/**
* Executes the prepared statement DML (e.g. INSERT/UPDATE/DELETE) or DDL
* (e.g. CREATE TABLE, DROP TABLE, COMMIT, START TRANSACTION, etc) with the
* given `params`.
*
* For Scala users, you can use the implicit conversions to [[Parameter]]
* by importing `Parameter._`. For example:
* {{{
* import com.twitter.finagle.mysql.{Client, OK, PreparedStatement}
* import com.twitter.finagle.mysql.Parameter._
* import com.twitter.util.Future
*
* val client: Client = ???
* val preparedStatement: PreparedStatement =
* client.prepare("INSERT INTO a_table (column1, column2) VALUES (?, ?)")
*
* // note the implicit conversions of the String and Int to Parameters
* val ok: Future[OK] = preparedStatement.modify("value1", 1234)
* }}}
*
* Java users, see [[asJava]] and use [[PreparedStatement.AsJava.modify]].
*/
def modify(params: Parameter*): Future[OK] =
apply(params: _*).flatMap {
case ok: OK => Future.value(ok)
case r =>
Future.exception(new IllegalStateException(s"Unsupported response to a modify='$r'"))
}
/**
* Executes the prepared statement with the given `params` and maps `f` to the
* rows of the returned [[ResultSet]]. If no [[ResultSet]] is returned, the function
* returns an empty `Seq`.
*
* For Scala users, you can use the implicit conversions to [[Parameter]]
* by importing `Parameter._`. For example:
* {{{
* import com.twitter.finagle.mysql.{Client, PreparedStatement, StringValue}
* import com.twitter.finagle.mysql.Parameter._
* import com.twitter.util.Future
*
* val client: Client = ???
* val preparedStatement: PreparedStatement =
* client.prepare("SELECT column1 FROM a_table WHERE column2 = ?")
*
* // note the implicit conversion of the Int, 1234, into a Parameter
* val result: Future[Seq[String] = preparedStatement.select(1234) { row =>
* row.stringOrNull("column1")
* }
* }}}
*
* Java users, see [[asJava]] and use [[PreparedStatement.AsJava.select]].
*
* @see [[read]]
*/
def select[T](params: Parameter*)(f: Row => T): Future[Seq[T]] =
apply(params: _*).map {
case rs: ResultSet => rs.rows.map(f)
case _ => Nil
}
/**
* Provides a Java-friendly API for this [[PreparedStatement]].
*/
final def asJava: PreparedStatement.AsJava =
new PreparedStatement.AsJava(this)
}
object PreparedStatement {
private[this] val ScalaSeqToFutureJavaList: Seq[Any] => Future[ju.List[Any]] =
seq => Future.value(seq.asJava)
private[this] def scalaSeqToFutureJavaList[T]: Seq[T] => Future[ju.List[T]] =
ScalaSeqToFutureJavaList.asInstanceOf[Seq[T] => Future[ju.List[T]]]
/**
* A Java-friendly API for [[PreparedStatement]]s.
*
* These should be constructed via [[PreparedStatement.asJava]] but is package
* exposed for testing.
*/
final class AsJava private[mysql] (underlying: PreparedStatement) {
/**
* Executes the prepared statement with the given `params`.
*
* Use [[Parameters.of]] for converting the inputs into [[Parameter]]s.
*
* {{{
* import com.twitter.finagle.mysql.Client;
* import com.twitter.finagle.mysql.PreparedStatement.AsJava;
* import com.twitter.finagle.mysql.Result;
* import com.twitter.util.Future;
* import static com.twitter.finagle.mysql.Parameters.of;
*
* Client client = ...
* PreparedStatement.AsJava preparedStatement = client
* .prepare("SELECT column1 FROM a_table WHERE column2 = ?")
* .asJava();
* Future<Result> result = preparedStatement.execute(of("value1"), of(1234));
* }}}
*
* @see [[PreparedStatement.apply]]
*/
@varargs
def execute(params: Parameter*): Future[Result] =
underlying(params: _*)
/**
* Executes the prepared statement SELECT query with the given `params`.
*
* Use [[Parameters.of]] for converting the inputs into [[Parameter]]s.
*
* {{{
* import com.twitter.finagle.mysql.Client;
* import com.twitter.finagle.mysql.PreparedStatement.AsJava;
* import com.twitter.finagle.mysql.ResultSet;
* import com.twitter.util.Future;
* import static com.twitter.finagle.mysql.Parameters.of;
*
* Client client = ...
* PreparedStatement.AsJava preparedStatement = client
* .prepare("SELECT column1 FROM a_table WHERE column2 = ?")
* .asJava();
* Future<ResultSet> resultSet = preparedStatement.read(of("value1"), of(1234));
* }}}
*
* @see [[select]]
* @see [[PreparedStatement.read]]
*/
@varargs
def read(params: Parameter*): Future[ResultSet] =
underlying.read(params: _*)
/**
* Executes the prepared statement DML (e.g. INSERT/UPDATE/DELETE) or DDL
* (e.g. CREATE TABLE, DROP TABLE, COMMIT, START TRANSACTION, etc) with the
* given `params`.
*
* Use [[Parameters.of]] for converting the inputs into [[Parameter]]s.
*
* {{{
* import com.twitter.finagle.mysql.Client;
* import com.twitter.finagle.mysql.OK;
* import com.twitter.finagle.mysql.PreparedStatement.AsJava;
* import com.twitter.util.Future;
* import static com.twitter.finagle.mysql.Parameters.of;
*
* Client client = ...
* PreparedStatement.AsJava preparedStatement = client
* .prepare("INSERT INTO a_table (column1, column2) VALUES (?, ?)")
* .asJava();
* Future<OK> ok = preparedStatement.modify(of("value1"), of(1234));
* }}}
*
* @see [[PreparedStatement.modify]]
*/
@varargs
def modify(params: Parameter*): Future[OK] =
underlying.modify(params: _*)
/**
* Executes the prepared statement with the given `params` and maps `f` to the
* rows of the returned [[ResultSet]]. If no [[ResultSet]] is returned, the function
* returns an empty `List`.
*
* Use [[Parameters.of]] for converting the inputs into [[Parameter]]s.
*
* {{{
* import com.twitter.finagle.mysql.Client;
* import com.twitter.finagle.mysql.PreparedStatement.AsJava;
* import com.twitter.finagle.mysql.Row;
* import com.twitter.finagle.mysql.StringValue;
* import com.twitter.util.Future;
* import java.util.List
* import static com.twitter.finagle.mysql.Parameters.of;
*
* Client client = ...
* PreparedStatement.AsJava preparedStatement = client
* .prepare("SELECT column1 FROM a_table WHERE column2 = ?")
* .asJava();
* Future<List<String>> result = preparedStatement.select((Row row) -> {
* return row.stringOrNull();
* },
* of(1234)
* );
* }}}
*
* @see [[read]]
* @see [[PreparedStatement.select]]
*/
@varargs
def select[T](f: JavaFunction[Row, T], params: Parameter*): Future[ju.List[T]] = {
val asSeq = underlying.select(params: _*)(f(_))
asSeq.flatMap(scalaSeqToFutureJavaList[T])
}
}
}