-
Notifications
You must be signed in to change notification settings - Fork 157
/
Command.scala
74 lines (63 loc) · 2.31 KB
/
Command.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
// Copyright (c) 2018-2021 by Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package skunk
import cats.Contravariant
import org.typelevel.twiddles.Iso
import skunk.util.Origin
import skunk.util.Twiddler
/**
* SQL and parameter encoder for a statement that returns no rows. We assume that `sql` has the
* same number of placeholders of the form `$1`, `$2`, etc., as the number of slots encoded by
* `encoder`, and that the parameter types specified by `encoder` are consistent with the schema.
* The `check` methods on [[skunk.Session Session]] provide a means to verify this assumption.
*
* You can construct a `Command` directly, although it is more typical to use the `sql`
* interpolator.
*
* {{{
* sql"INSERT INTO foo VALUES ($int2, $varchar)".command // Command[(Short, String)]
* }}}
*
* @param sql A SQL statement returning no rows.
* @param encoder An encoder for all parameters `$1`, `$2`, etc., in `sql`.
*
* @see [[skunk.syntax.StringContextOps StringContextOps]] for information on the `sql`
* interpolator.
* @see [[skunk.Session Session]] for information on executing a `Command`.
*
* @group Statements
*/
final case class Command[A](
override val sql: String,
override val origin: Origin,
override val encoder: Encoder[A]
) extends Statement[A] {
/**
* Command is a [[https://typelevel.org/cats/typeclasses/contravariant.html contravariant
* functor]].
* @group Transformations
*/
def contramap[B](f: B => A): Command[B] =
Command(sql, origin, encoder.contramap(f))
@deprecated("Use .to[CaseClass] instead of .gcontramap[CaseClass]", "0.6")
def gcontramap[B](implicit ev: Twiddler.Aux[B, A]): Command[B] =
contramap(ev.to)
def to[B](implicit ev: Iso[A, B]): Command[B] =
contramap(ev.from)
def cacheKey: Statement.CacheKey =
Statement.CacheKey(sql, encoder.types, Nil)
}
/** @group Companions */
object Command {
/**
* Command is a [[https://typelevel.org/cats/typeclasses/contravariant.html contravariant
* functor]].
* @group Typeclass Instances
*/
implicit val CommandContravariant: Contravariant[Command] =
new Contravariant[Command] {
override def contramap[A, B](fa: Command[A])(f: B => A): Command[B] =
fa.contramap(f)
}
}