-
Notifications
You must be signed in to change notification settings - Fork 348
/
update.scala
293 lines (250 loc) · 10.5 KB
/
update.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
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright (c) 2013-2018 Rob Norris and Contributors
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package doobie.util
import cats._
import cats.implicits._
import doobie._
import doobie.implicits._
import doobie.util.analysis.Analysis
import doobie.util.log.{ Success, ExecFailure, LogEvent }
import doobie.util.pos.Pos
import fs2.Stream
import scala.Predef.longWrapper
import scala.concurrent.duration.{ FiniteDuration, NANOSECONDS }
/** Module defining updates parameterized by input type. */
object update {
val DefaultChunkSize = query.DefaultChunkSize
/**
* Partial application hack to allow calling updateManyWithGeneratedKeys without passing the
* F[_] type argument explicitly.
*/
trait UpdateManyWithGeneratedKeysPartiallyApplied[A, K] {
def apply[F[_]](as: F[A])(implicit F: Foldable[F], K: Read[K]): Stream[ConnectionIO, K] =
withChunkSize(as, DefaultChunkSize)
def withChunkSize[F[_]](as: F[A], chunkSize: Int)(implicit F: Foldable[F], K: Read[K]): Stream[ConnectionIO, K]
}
/**
* An update parameterized by some input type `A`. This is the type constructed by the `sql`
* interpolator.
*/
trait Update[A] { u =>
// Contravariant coyoneda trick for A
protected implicit val write: Write[A]
// LogHandler is protected for now.
protected val logHandler: LogHandler
private val now: PreparedStatementIO[Long] =
FPS.delay(System.nanoTime)
// Equivalent to HPS.executeUpdate(k) but with logging if logHandler is defined
private def executeUpdate[T](a: A): PreparedStatementIO[Int] = {
val args = write.toList(a)
def diff(a: Long, b: Long) = FiniteDuration((a - b).abs, NANOSECONDS)
def log(e: LogEvent) = FPS.delay(logHandler.unsafeRun(e))
for {
t0 <- now
en <- FPS.executeUpdate.attempt
t1 <- now
n <- en.liftTo[PreparedStatementIO].onError { case e => log(ExecFailure(sql, args, diff(t1, t0), e)) }
_ <- log(Success(sql, args, diff(t1, t0), FiniteDuration(0L, NANOSECONDS)))
} yield n
}
/**
* The SQL string.
* @group Diagnostics
*/
val sql: String
/**
* An optional `[[Pos]]` indicating the source location where this `[[Update]]` was
* constructed. This is used only for diagnostic purposes.
* @group Diagnostics
*/
val pos: Option[Pos]
/** Convert this Update to a `Fragment`. */
def toFragment(a: A): Fragment =
write.toFragment(a, sql)
/**
* Program to construct an analysis of this query's SQL statement and asserted parameter types.
* @group Diagnostics
*/
def analysis: ConnectionIO[Analysis] =
HC.prepareUpdateAnalysis[A](sql)
/**
* Program to construct an inspection of the query. Given arguments `a`, calls `f` with the SQL
* representation of the query and a statement with all arguments set. Returns the result
* of the `ConnectionIO` program constructed.
*
* @group Diagnostics
*/
def inspect[R](a: A)(f: (String, PreparedStatementIO[Unit]) => ConnectionIO[R]): ConnectionIO[R] =
f(sql, HPS.set(a))
/**
* Construct a program to execute the update and yield a count of affected rows, given the
* writable argument `a`.
* @group Execution
*/
def run(a: A): ConnectionIO[Int] =
HC.prepareStatement(sql)(HPS.set(a) *> executeUpdate(a))
/**
* Program to execute a batch update and yield a count of affected rows. Note that failed
* updates are not reported (see https://github.com/tpolecat/doobie/issues/706). This API is
* likely to change.
* @group Execution
*/
def updateMany[F[_]: Foldable](fa: F[A]): ConnectionIO[Int] =
HC.prepareStatement(sql)(HPS.addBatchesAndExecute(fa))
/**
* Construct a stream that performs a batch update as with `updateMany`, yielding generated
* keys of readable type `K`, identified by the specified columns. Note that not all drivers
* support generated keys, and some support only a single key column.
* @group Execution
*/
def updateManyWithGeneratedKeys[K](columns: String*): UpdateManyWithGeneratedKeysPartiallyApplied[A, K] =
new UpdateManyWithGeneratedKeysPartiallyApplied[A, K] {
def withChunkSize[F[_]](as: F[A], chunkSize: Int)(implicit F: Foldable[F], K: Read[K]): Stream[ConnectionIO, K] =
HC.updateManyWithGeneratedKeys[List,A,K](columns.toList)(sql, FPS.unit, as.toList, chunkSize)
}
/**
* Construct a stream that performs the update, yielding generated keys of readable type `K`,
* identified by the specified columns, given a writable argument `a`. Note that not all
* drivers support generated keys, and some support only a single key column.
* @group Execution
*/
def withGeneratedKeys[K: Read](columns: String*)(a: A): Stream[ConnectionIO, K] =
withGeneratedKeysWithChunkSize[K](columns: _*)(a, DefaultChunkSize)
/**
* Construct a stream that performs the update, yielding generated keys of readable type `K`,
* identified by the specified columns, given a writable argument `a` and `chunkSize`. Note
* that not all drivers support generated keys, and some support only a single key column.
* @group Execution
*/
def withGeneratedKeysWithChunkSize[K: Read](columns: String*)(a: A, chunkSize: Int): Stream[ConnectionIO, K] =
HC.updateWithGeneratedKeys[K](columns.toList)(sql, HPS.set(a), chunkSize)
/**
* Construct a program that performs the update, yielding a single set of generated keys of
* readable type `K`, identified by the specified columns, given a writable argument `a`.
* Note that not all drivers support generated keys, and some support only a single key column.
* @group Execution
*/
def withUniqueGeneratedKeys[K: Read](columns: String*)(a: A): ConnectionIO[K] =
HC.prepareStatementS(sql, columns.toList)(HPS.set(a) *> HPS.executeUpdateWithUniqueGeneratedKeys)
/**
* Update is a contravariant functor.
* @group Transformations
*/
def contramap[C](f: C => A): Update[C] =
new Update[C] {
val write = u.write.contramap(f)
val sql = u.sql
val pos = u.pos
val logHandler = u.logHandler
}
/**
* Apply an argument, yielding a residual `[[Update0]]`.
* @group Transformations
*/
def toUpdate0(a: A): Update0 =
new Update0 {
val sql = u.sql
val pos = u.pos
def toFragment: Fragment = u.toFragment(a)
def analysis = u.analysis
def run = u.run(a)
def withGeneratedKeysWithChunkSize[K: Read](columns: String*)(chunkSize: Int) =
u.withGeneratedKeysWithChunkSize[K](columns: _*)(a, chunkSize)
def withUniqueGeneratedKeys[K: Read](columns: String*) =
u.withUniqueGeneratedKeys(columns: _*)(a)
def inspect[R](f: (String, PreparedStatementIO[Unit]) => ConnectionIO[R]) = u.inspect(a)(f)
}
}
object Update {
/**
* Construct an `Update` for some writable parameter type `A` with the given SQL string, and
* optionally a `Pos` and/or `LogHandler` for diagnostics. The normal mechanism
* for construction is the `sql/fr/fr0` interpolators.
* @group Constructors
*/
@SuppressWarnings(Array("org.wartremover.warts.DefaultArguments"))
def apply[A](sql0: String, pos0: Option[Pos] = None, logHandler0: LogHandler = LogHandler.nop)(
implicit W: Write[A]
): Update[A] =
new Update[A] {
val write = W
val sql = sql0
val logHandler = logHandler0
val pos = pos0
}
/**
* Update is a contravariant functor.
* @group Typeclass Instances
*/
implicit val updateContravariant: Contravariant[Update] =
new Contravariant[Update] {
def contramap[A, B](fa: Update[A])(f: B => A) = fa contramap f
}
}
trait Update0 {
/**
* The SQL string.
* @group Diagnostics
*/
val sql: String
/**
* An optional `[[Pos]]` indicating the source location where this `[[Query]]` was
* constructed. This is used only for diagnostic purposes.
* @group Diagnostics
*/
val pos: Option[Pos]
/** Convert this Update0 to a `Fragment`. */
def toFragment: Fragment
/**
* Program to construct an analysis of this query's SQL statement and asserted parameter types.
* @group Diagnostics
*/
def analysis: ConnectionIO[Analysis]
/**
* Program to construct an inspection of the query. Calls `f` with the SQL
* representation of the query and a statement with all statement arguments set. Returns the result
* of the `ConnectionIO` program constructed.
*
* @group Diagnostics
*/
def inspect[R](f: (String, PreparedStatementIO[Unit]) => ConnectionIO[R]): ConnectionIO[R]
/**
* Program to execute the update and yield a count of affected rows.
* @group Execution
*/
def run: ConnectionIO[Int]
/**
* Construct a stream that performs the update, yielding generated keys of readable type `K`,
* identified by the specified columns. Note that not all drivers support generated keys, and
* some support only a single key column.
* @group Execution
*/
def withGeneratedKeys[K: Read](columns: String*): Stream[ConnectionIO, K] =
withGeneratedKeysWithChunkSize(columns: _*)(DefaultChunkSize)
/**
* Construct a stream that performs the update, yielding generated keys of readable type `K`,
* identified by the specified columns, given a `chunkSize`. Note that not all drivers support
* generated keys, and some support only a single key column.
* @group Execution
*/
def withGeneratedKeysWithChunkSize[K: Read](columns: String*)(chunkSize:Int): Stream[ConnectionIO, K]
/**
* Construct a program that performs the update, yielding a single set of generated keys of
* readable type `K`, identified by the specified columns. Note that not all drivers support
* generated keys, and some support only a single key column.
* @group Execution
*/
def withUniqueGeneratedKeys[K: Read](columns: String*): ConnectionIO[K]
}
object Update0 {
/**
* Construct an `Update0` with the given SQL string, and optionally a `Pos`
* and/or `LogHandler` for diagnostics. The normal mechanism for construction is the
* `sql/fr/fr0` interpolators.
* @group Constructors
*/
def apply(sql0: String, pos0: Option[Pos]): Update0 =
Update[Unit](sql0, pos0).toUpdate0(())
}
}