-
Notifications
You must be signed in to change notification settings - Fork 348
/
put.scala
207 lines (168 loc) · 6.78 KB
/
put.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
// 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.Contravariant
import cats.free.ContravariantCoyoneda
import cats.data.NonEmptyList
import doobie.enum.JdbcType
import java.sql.{ PreparedStatement, ResultSet }
import scala.reflect.ClassTag
import scala.reflect.runtime.universe.{ Type, TypeTag }
import shapeless._
import shapeless.ops.hlist.IsHCons
sealed abstract class Put[A](
val typeStack: NonEmptyList[Option[Type]],
val jdbcTargets: NonEmptyList[JdbcType],
val put: ContravariantCoyoneda[(PreparedStatement, Int, ?) => Unit, A],
val update: ContravariantCoyoneda[(ResultSet, Int, ?) => Unit, A]
) {
protected def contramapImpl[B](f: B => A, typ: Option[Type]): Put[B]
def unsafeSetNull(ps: PreparedStatement, n: Int): Unit
final def contramap[B](f: B => A): Put[B] =
contramapImpl(f, None)
final def tcontramap[B](f: B => A)(implicit ev: TypeTag[B]): Put[B] =
contramapImpl(f, Some(ev.tpe))
@SuppressWarnings(Array("org.wartremover.warts.Equals"))
def unsafeSetNonNullable(ps: PreparedStatement, n: Int, a: A): Unit =
if (a == null) sys.error("oops, null")
else put.fi.apply(ps, n, (put.k(a)))
def unsafeSetNullable(ps: PreparedStatement, n: Int, oa: Option[A]): Unit =
oa match {
case Some(a) => unsafeSetNonNullable(ps, n, a)
case None => unsafeSetNull(ps, n)
}
@SuppressWarnings(Array("org.wartremover.warts.Equals"))
def unsafeUpdateNonNullable(rs: ResultSet, n: Int, a: A): Unit =
if (a == null) sys.error("oops, null")
else update.fi.apply(rs, n, (update.k(a)))
def unsafeUpdateNullable(rs: ResultSet, n: Int, oa: Option[A]): Unit =
oa match {
case Some(a) => unsafeUpdateNonNullable(rs, n, a)
case None => rs.updateNull(n)
}
}
object Put extends PutInstances {
def apply[A](implicit ev: Put[A]): ev.type = ev
final case class Basic[A](
override val typeStack: NonEmptyList[Option[Type]],
override val jdbcTargets: NonEmptyList[JdbcType],
override val put: ContravariantCoyoneda[(PreparedStatement, Int, ?) => Unit, A],
override val update: ContravariantCoyoneda[(ResultSet, Int, ?) => Unit, A]
) extends Put[A](typeStack, jdbcTargets, put, update) {
protected def contramapImpl[B](f: B => A, typ: Option[Type]): Put[B] =
copy(typeStack = typ :: typeStack, update = update.contramap(f), put = put.contramap(f))
def unsafeSetNull(ps: PreparedStatement, n: Int): Unit =
ps.setNull(n, jdbcTargets.head.toInt)
}
object Basic {
def many[A](
jdbcTargets: NonEmptyList[JdbcType],
put: (PreparedStatement, Int, A) => Unit,
update: (ResultSet, Int, A) => Unit
)(implicit ev: TypeTag[A]): Basic[A] =
Basic(
NonEmptyList.of(Some(ev.tpe)),
jdbcTargets,
ContravariantCoyoneda.lift[(PreparedStatement, Int, ?) => Unit, A](put),
ContravariantCoyoneda.lift[(ResultSet, Int, ?) => Unit, A](update)
)
def one[A](
jdbcTarget: JdbcType,
put: (PreparedStatement, Int, A) => Unit,
update: (ResultSet, Int, A) => Unit
)(implicit ev: TypeTag[A]): Basic[A] =
many(NonEmptyList.of(jdbcTarget), put, update)
}
final case class Advanced[A](
override val typeStack: NonEmptyList[Option[Type]],
override val jdbcTargets: NonEmptyList[JdbcType],
val schemaTypes: NonEmptyList[String],
override val put: ContravariantCoyoneda[(PreparedStatement, Int, ?) => Unit, A],
override val update: ContravariantCoyoneda[(ResultSet, Int, ?) => Unit, A]
) extends Put[A](typeStack, jdbcTargets, put, update) {
protected def contramapImpl[B](f: B => A, typ: Option[Type]): Put[B] =
copy(typeStack = typ :: typeStack, update = update.contramap(f), put = put.contramap(f))
def unsafeSetNull(ps: PreparedStatement, n: Int): Unit =
ps.setNull(n, jdbcTargets.head.toInt, schemaTypes.head)
}
object Advanced {
def many[A](
jdbcTargets: NonEmptyList[JdbcType],
schemaTypes: NonEmptyList[String],
put: (PreparedStatement, Int, A) => Unit,
update: (ResultSet, Int, A) => Unit
)(implicit ev: TypeTag[A]): Advanced[A] =
Advanced(
NonEmptyList.of(Some(ev.tpe)),
jdbcTargets,
schemaTypes,
ContravariantCoyoneda.lift[(PreparedStatement, Int, ?) => Unit, A](put),
ContravariantCoyoneda.lift[(ResultSet, Int, ?) => Unit, A](update)
)
def one[A: TypeTag](
jdbcTarget: JdbcType,
schemaTypes: NonEmptyList[String],
put: (PreparedStatement, Int, A) => Unit,
update: (ResultSet, Int, A) => Unit
): Advanced[A] =
many(NonEmptyList.of(jdbcTarget), schemaTypes, put, update)
@SuppressWarnings(Array("org.wartremover.warts.Equals", "org.wartremover.warts.AsInstanceOf"))
def array[A >: Null <: AnyRef: TypeTag](
schemaTypes: NonEmptyList[String],
elementType: String
): Advanced[Array[A]] =
one(
JdbcType.Array,
schemaTypes,
(ps, n, a) => {
val conn = ps.getConnection
val arr = conn.createArrayOf(elementType, a.asInstanceOf[Array[AnyRef]])
ps.setArray(n, arr)
},
(rs, n, a) => {
val stmt = rs.getStatement
val conn = stmt.getConnection
val arr = conn.createArrayOf(elementType, a.asInstanceOf[Array[AnyRef]])
rs.updateArray(n, arr)
}
)
def other[A >: Null <: AnyRef: TypeTag](schemaTypes: NonEmptyList[String]): Advanced[A] =
many(
NonEmptyList.of(JdbcType.Other, JdbcType.JavaObject),
schemaTypes,
(ps, n, a) => ps.setObject(n, a),
(rs, n, a) => rs.updateObject(n, a)
)
}
/** An implicit Meta[A] means we also have an implicit Put[A]. */
implicit def metaProjectionWrite[A](
implicit m: Meta[A]
): Put[A] =
m.put
}
trait PutInstances {
import Predef.=:=
/** @group Instances */
implicit val ContravariantPut: Contravariant[Put] =
new Contravariant[Put] {
def contramap[A, B](fa: Put[A])(f: B => A): Put[B] =
fa.contramap(f)
}
/** @group Instances */
implicit def ArrayTypeAsListGet[A: ClassTag: TypeTag](implicit ev: Put[Array[A]]): Put[List[A]] =
ev.tcontramap(_.toArray)
/** @group Instances */
implicit def ArrayTypeAsVectorGet[A: ClassTag: TypeTag](implicit ev: Put[Array[A]]): Put[Vector[A]] =
ev.tcontramap(_.toArray)
/** @group Instances */
implicit def unaryProductPut[A: TypeTag, L <: HList, H, T <: HList](
implicit G: Generic.Aux[A, L],
C: IsHCons.Aux[L, H, T],
H: Lazy[Put[H]],
E: (H :: HNil) =:= L
): Put[A] = {
void(E) // E is a necessary constraint but isn't used directly
H.value.contramap[A](a => G.to(a).head)
}
}