-
Notifications
You must be signed in to change notification settings - Fork 348
/
connection.scala
247 lines (193 loc) · 9.72 KB
/
connection.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
// 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.hi
import doobie.enum.Holdability
import doobie.enum.ResultSetType
import doobie.enum.ResultSetConcurrency
import doobie.enum.TransactionIsolation
import doobie.enum.AutoGeneratedKeys
import doobie.enum.JdbcType
import doobie.syntax.monaderror._
import doobie.util.analysis.Analysis
import doobie.util.composite.Composite
import doobie.util.stream.repeatEvalChunks
import java.sql.{ Savepoint, PreparedStatement, ResultSet }
import scala.collection.immutable.Map
import scala.collection.JavaConverters._
import cats.Foldable
import cats.implicits._
import fs2.Stream
import fs2.Stream.{ eval, bracket }
/**
* Module of high-level constructors for `ConnectionIO` actions.
* @group Modules
*/
@SuppressWarnings(Array("org.wartremover.warts.Overloading"))
object connection {
import implicits._
/** @group Lifting */
def delay[A](a: => A): ConnectionIO[A] =
FC.delay(a)
private def liftStream[A: Composite](
chunkSize: Int,
create: ConnectionIO[PreparedStatement],
prep: PreparedStatementIO[Unit],
exec: PreparedStatementIO[ResultSet]): Stream[ConnectionIO, A] = {
def prepared(ps: PreparedStatement): Stream[ConnectionIO, PreparedStatement] =
eval[ConnectionIO, PreparedStatement] {
val fs = FPS.setFetchSize(chunkSize)
FC.embed(ps, fs *> prep).map(_ => ps)
}
def unrolled(rs: ResultSet): Stream[ConnectionIO, A] =
repeatEvalChunks(FC.embed(rs, resultset.getNextChunk[A](chunkSize)))
val preparedStatement: Stream[ConnectionIO, PreparedStatement] =
bracket(create)(prepared, FC.embed(_, FPS.close))
def results(ps: PreparedStatement): Stream[ConnectionIO, A] =
bracket(FC.embed(ps, exec))(unrolled, FC.embed(_, FRS.close))
preparedStatement.flatMap(results)
}
/**
* Construct a prepared statement from the given `sql`, configure it with the given `PreparedStatementIO`
* action, and return results via a `Stream`.
* @group Prepared Statements
*/
def stream[A: Composite](sql: String, prep: PreparedStatementIO[Unit], chunkSize: Int): Stream[ConnectionIO, A] =
liftStream(chunkSize, FC.prepareStatement(sql), prep, FPS.executeQuery)
/**
* Construct a prepared update statement with the given return columns (and composite destination
* type `A`) and sql source, configure it with the given `PreparedStatementIO` action, and return
* the generated key results via a
* `Stream`.
* @group Prepared Statements
*/
def updateWithGeneratedKeys[A: Composite](cols: List[String])(sql: String, prep: PreparedStatementIO[Unit], chunkSize: Int): Stream[ConnectionIO, A] =
liftStream(chunkSize, FC.prepareStatement(sql, cols.toArray), prep, FPS.executeUpdate *> FPS.getGeneratedKeys)
/** @group Prepared Statements */
def updateManyWithGeneratedKeys[F[_]: Foldable, A: Composite, B: Composite](cols: List[String])(sql: String, prep: PreparedStatementIO[Unit], fa: F[A], chunkSize: Int): Stream[ConnectionIO, B] =
liftStream[B](chunkSize, FC.prepareStatement(sql, cols.toArray), prep, HPS.addBatchesAndExecute(fa) *> FPS.getGeneratedKeys)
/** @group Transaction Control */
val commit: ConnectionIO[Unit] =
FC.commit
/**
* Construct an analysis for the provided `sql` query, given parameter composite type `A` and
* resultset row composite `B`.
*/
def prepareQueryAnalysis[A: Composite, B: Composite](sql: String): ConnectionIO[Analysis] =
nativeTypeMap flatMap (m => prepareStatement(sql) {
(HPS.getParameterMappings[A], HPS.getColumnMappings[B]) mapN (Analysis(sql, m, _, _))
})
def prepareQueryAnalysis0[B: Composite](sql: String): ConnectionIO[Analysis] =
nativeTypeMap flatMap (m => prepareStatement(sql) {
HPS.getColumnMappings[B] map (cm => Analysis(sql, m, Nil, cm))
})
def prepareUpdateAnalysis[A: Composite](sql: String): ConnectionIO[Analysis] =
nativeTypeMap flatMap (m => prepareStatement(sql) {
HPS.getParameterMappings[A] map (pm => Analysis(sql, m, pm, Nil))
})
def prepareUpdateAnalysis0(sql: String): ConnectionIO[Analysis] =
nativeTypeMap flatMap (m => prepareStatement(sql) {
Analysis(sql, m, Nil, Nil).pure[PreparedStatementIO]
})
/** @group Statements */
def createStatement[A](k: StatementIO[A]): ConnectionIO[A] =
FC.createStatement.flatMap(s => FC.embed(s, k guarantee FS.close))
/** @group Statements */
def createStatement[A](rst: ResultSetType, rsc: ResultSetConcurrency)(k: StatementIO[A]): ConnectionIO[A] =
FC.createStatement(rst.toInt, rsc.toInt).flatMap(s => FC.embed(s, k guarantee FS.close))
/** @group Statements */
def createStatement[A](rst: ResultSetType, rsc: ResultSetConcurrency, rsh: Holdability)(k: StatementIO[A]): ConnectionIO[A] =
FC.createStatement(rst.toInt, rsc.toInt, rsh.toInt).flatMap(s => FC.embed(s, k guarantee FS.close))
/** @group Connection Properties */
val getCatalog: ConnectionIO[String] =
FC.getCatalog
/** @group Connection Properties */
def getClientInfo(key: String): ConnectionIO[Option[String]] =
FC.getClientInfo(key).map(Option(_))
/** @group Connection Properties */
val getClientInfo: ConnectionIO[Map[String, String]] =
FC.getClientInfo.map(_.asScala.toMap)
/** @group Connection Properties */
val getHoldability: ConnectionIO[Holdability] =
FC.getHoldability.map(Holdability.unsafeFromInt)
/** @group Connection Properties */
def getMetaData[A](k: DatabaseMetaDataIO[A]): ConnectionIO[A] =
FC.getMetaData.flatMap(s => FC.embed(s, k))
/** @group Transaction Control */
val getTransactionIsolation: ConnectionIO[TransactionIsolation] =
FC.getTransactionIsolation.map(TransactionIsolation.unsafeFromInt)
/** @group Connection Properties */
val isReadOnly: ConnectionIO[Boolean] =
FC.isReadOnly
/** @group Callable Statements */
def prepareCall[A](sql: String, rst: ResultSetType, rsc: ResultSetConcurrency)(k: CallableStatementIO[A]): ConnectionIO[A] =
FC.prepareCall(sql, rst.toInt, rsc.toInt).flatMap(s => FC.embed(s, k guarantee FCS.close))
/** @group Callable Statements */
def prepareCall[A](sql: String)(k: CallableStatementIO[A]): ConnectionIO[A] =
FC.prepareCall(sql).flatMap(s => FC.embed(s, k guarantee FCS.close))
/** @group Callable Statements */
def prepareCall[A](sql: String, rst: ResultSetType, rsc: ResultSetConcurrency, rsh: Holdability)(k: CallableStatementIO[A]): ConnectionIO[A] =
FC.prepareCall(sql, rst.toInt, rsc.toInt, rsh.toInt).flatMap(s => FC.embed(s, k guarantee FCS.close))
/** @group Prepared Statements */
def prepareStatement[A](sql: String, rst: ResultSetType, rsc: ResultSetConcurrency)(k: PreparedStatementIO[A]): ConnectionIO[A] =
FC.prepareStatement(sql, rst.toInt, rsc.toInt).flatMap(s => FC.embed(s, k guarantee FPS.close))
/** @group Prepared Statements */
def prepareStatement[A](sql: String)(k: PreparedStatementIO[A]): ConnectionIO[A] =
FC.prepareStatement(sql).flatMap(s => FC.embed(s, k guarantee FPS.close))
/** @group Prepared Statements */
def prepareStatement[A](sql: String, rst: ResultSetType, rsc: ResultSetConcurrency, rsh: Holdability)(k: PreparedStatementIO[A]): ConnectionIO[A] =
FC.prepareStatement(sql, rst.toInt, rsc.toInt, rsh.toInt).flatMap(s => FC.embed(s, k guarantee FPS.close))
/** @group Prepared Statements */
def prepareStatement[A](sql: String, agk: AutoGeneratedKeys)(k: PreparedStatementIO[A]): ConnectionIO[A] =
FC.prepareStatement(sql, agk.toInt).flatMap(s => FC.embed(s, k guarantee FPS.close))
/** @group Prepared Statements */
def prepareStatementI[A](sql: String, columnIndexes: List[Int])(k: PreparedStatementIO[A]): ConnectionIO[A] =
FC.prepareStatement(sql, columnIndexes.toArray).flatMap(s => FC.embed(s, k guarantee FPS.close))
/** @group Prepared Statements */
def prepareStatementS[A](sql: String, columnNames: List[String])(k: PreparedStatementIO[A]): ConnectionIO[A] =
FC.prepareStatement(sql, columnNames.toArray).flatMap(s => FC.embed(s, k guarantee FPS.close))
/** @group Transaction Control */
def releaseSavepoint(sp: Savepoint): ConnectionIO[Unit] =
FC.releaseSavepoint(sp)
/** @group Transaction Control */
def rollback(sp: Savepoint): ConnectionIO[Unit] =
FC.rollback(sp)
/** @group Transaction Control */
val rollback: ConnectionIO[Unit] =
FC.rollback
/** @group Connection Properties */
def setCatalog(catalog: String): ConnectionIO[Unit] =
FC.setCatalog(catalog)
/** @group Connection Properties */
def setClientInfo(key: String, value: String): ConnectionIO[Unit] =
FC.setClientInfo(key, value)
/** @group Connection Properties */
def setClientInfo(info: Map[String, String]): ConnectionIO[Unit] =
FC.setClientInfo {
val ps = new java.util.Properties
ps.putAll(info.asJava)
ps
}
/** @group Connection Properties */
def setHoldability(h: Holdability): ConnectionIO[Unit] =
FC.setHoldability(h.toInt)
/** @group Connection Properties */
def setReadOnly(readOnly: Boolean): ConnectionIO[Unit] =
FC.setReadOnly(readOnly)
/** @group Transaction Control */
val setSavepoint: ConnectionIO[Savepoint] =
FC.setSavepoint
/** @group Transaction Control */
def setSavepoint(name: String): ConnectionIO[Savepoint] =
FC.setSavepoint(name)
/** @group Transaction Control */
def setTransactionIsolation(ti: TransactionIsolation): ConnectionIO[Unit] =
FC.setTransactionIsolation(ti.toInt)
/**
* Compute a map from native type to closest-matching JDBC type.
* @group MetaData
*/
val nativeTypeMap: ConnectionIO[Map[String, JdbcType]] = {
getMetaData(FDMD.getTypeInfo.flatMap(FDMD.embed(_, HRS.list[(String, JdbcType)].map(_.toMap))))
}
}