-
Notifications
You must be signed in to change notification settings - Fork 348
/
nclob.scala
244 lines (223 loc) · 11.7 KB
/
nclob.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
// Copyright (c) 2013-2020 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.free
import cats.{~>, Applicative, Semigroup, Monoid}
import cats.effect.kernel.{ CancelScope, Poll, Sync }
import cats.free.{ Free => FF } // alias because some algebras have an op called Free
import doobie.util.log.LogEvent
import doobie.WeakAsync
import scala.concurrent.Future
import scala.concurrent.duration.FiniteDuration
import java.io.InputStream
import java.io.OutputStream
import java.io.Reader
import java.io.Writer
import java.lang.String
import java.sql.Clob
import java.sql.NClob
// This file is Auto-generated using FreeGen2.scala
object nclob { module =>
// Algebra of operations for NClob. Each accepts a visitor as an alternative to pattern-matching.
sealed trait NClobOp[A] {
def visit[F[_]](v: NClobOp.Visitor[F]): F[A]
}
// Free monad over NClobOp.
type NClobIO[A] = FF[NClobOp, A]
// Module of instances and constructors of NClobOp.
object NClobOp {
// Given a NClob we can embed a NClobIO program in any algebra that understands embedding.
implicit val NClobOpEmbeddable: Embeddable[NClobOp, NClob] =
new Embeddable[NClobOp, NClob] {
def embed[A](j: NClob, fa: FF[NClobOp, A]) = Embedded.NClob(j, fa)
}
// Interface for a natural transformation NClobOp ~> F encoded via the visitor pattern.
// This approach is much more efficient than pattern-matching for large algebras.
trait Visitor[F[_]] extends (NClobOp ~> F) {
final def apply[A](fa: NClobOp[A]): F[A] = fa.visit(this)
// Common
def raw[A](f: NClob => A): F[A]
def embed[A](e: Embedded[A]): F[A]
def raiseError[A](e: Throwable): F[A]
def handleErrorWith[A](fa: NClobIO[A])(f: Throwable => NClobIO[A]): F[A]
def monotonic: F[FiniteDuration]
def realTime: F[FiniteDuration]
def delay[A](thunk: => A): F[A]
def suspend[A](hint: Sync.Type)(thunk: => A): F[A]
def forceR[A, B](fa: NClobIO[A])(fb: NClobIO[B]): F[B]
def uncancelable[A](body: Poll[NClobIO] => NClobIO[A]): F[A]
def poll[A](poll: Any, fa: NClobIO[A]): F[A]
def canceled: F[Unit]
def onCancel[A](fa: NClobIO[A], fin: NClobIO[Unit]): F[A]
def fromFuture[A](fut: NClobIO[Future[A]]): F[A]
def fromFutureCancelable[A](fut: NClobIO[(Future[A], NClobIO[Unit])]): F[A]
def performLogging(event: LogEvent): F[Unit]
// NClob
def free: F[Unit]
def getAsciiStream: F[InputStream]
def getCharacterStream: F[Reader]
def getCharacterStream(a: Long, b: Long): F[Reader]
def getSubString(a: Long, b: Int): F[String]
def length: F[Long]
def position(a: Clob, b: Long): F[Long]
def position(a: String, b: Long): F[Long]
def setAsciiStream(a: Long): F[OutputStream]
def setCharacterStream(a: Long): F[Writer]
def setString(a: Long, b: String): F[Int]
def setString(a: Long, b: String, c: Int, d: Int): F[Int]
def truncate(a: Long): F[Unit]
}
// Common operations for all algebras.
final case class Raw[A](f: NClob => A) extends NClobOp[A] {
def visit[F[_]](v: Visitor[F]) = v.raw(f)
}
final case class Embed[A](e: Embedded[A]) extends NClobOp[A] {
def visit[F[_]](v: Visitor[F]) = v.embed(e)
}
final case class RaiseError[A](e: Throwable) extends NClobOp[A] {
def visit[F[_]](v: Visitor[F]) = v.raiseError(e)
}
final case class HandleErrorWith[A](fa: NClobIO[A], f: Throwable => NClobIO[A]) extends NClobOp[A] {
def visit[F[_]](v: Visitor[F]) = v.handleErrorWith(fa)(f)
}
case object Monotonic extends NClobOp[FiniteDuration] {
def visit[F[_]](v: Visitor[F]) = v.monotonic
}
case object Realtime extends NClobOp[FiniteDuration] {
def visit[F[_]](v: Visitor[F]) = v.realTime
}
case class Suspend[A](hint: Sync.Type, thunk: () => A) extends NClobOp[A] {
def visit[F[_]](v: Visitor[F]) = v.suspend(hint)(thunk())
}
case class ForceR[A, B](fa: NClobIO[A], fb: NClobIO[B]) extends NClobOp[B] {
def visit[F[_]](v: Visitor[F]) = v.forceR(fa)(fb)
}
case class Uncancelable[A](body: Poll[NClobIO] => NClobIO[A]) extends NClobOp[A] {
def visit[F[_]](v: Visitor[F]) = v.uncancelable(body)
}
case class Poll1[A](poll: Any, fa: NClobIO[A]) extends NClobOp[A] {
def visit[F[_]](v: Visitor[F]) = v.poll(poll, fa)
}
case object Canceled extends NClobOp[Unit] {
def visit[F[_]](v: Visitor[F]) = v.canceled
}
case class OnCancel[A](fa: NClobIO[A], fin: NClobIO[Unit]) extends NClobOp[A] {
def visit[F[_]](v: Visitor[F]) = v.onCancel(fa, fin)
}
case class FromFuture[A](fut: NClobIO[Future[A]]) extends NClobOp[A] {
def visit[F[_]](v: Visitor[F]) = v.fromFuture(fut)
}
case class FromFutureCancelable[A](fut: NClobIO[(Future[A], NClobIO[Unit])]) extends NClobOp[A] {
def visit[F[_]](v: Visitor[F]) = v.fromFutureCancelable(fut)
}
case class PerformLogging(event: LogEvent) extends NClobOp[Unit] {
def visit[F[_]](v: Visitor[F]) = v.performLogging(event)
}
// NClob-specific operations.
case object Free extends NClobOp[Unit] {
def visit[F[_]](v: Visitor[F]) = v.free
}
case object GetAsciiStream extends NClobOp[InputStream] {
def visit[F[_]](v: Visitor[F]) = v.getAsciiStream
}
case object GetCharacterStream extends NClobOp[Reader] {
def visit[F[_]](v: Visitor[F]) = v.getCharacterStream
}
final case class GetCharacterStream1(a: Long, b: Long) extends NClobOp[Reader] {
def visit[F[_]](v: Visitor[F]) = v.getCharacterStream(a, b)
}
final case class GetSubString(a: Long, b: Int) extends NClobOp[String] {
def visit[F[_]](v: Visitor[F]) = v.getSubString(a, b)
}
case object Length extends NClobOp[Long] {
def visit[F[_]](v: Visitor[F]) = v.length
}
final case class Position(a: Clob, b: Long) extends NClobOp[Long] {
def visit[F[_]](v: Visitor[F]) = v.position(a, b)
}
final case class Position1(a: String, b: Long) extends NClobOp[Long] {
def visit[F[_]](v: Visitor[F]) = v.position(a, b)
}
final case class SetAsciiStream(a: Long) extends NClobOp[OutputStream] {
def visit[F[_]](v: Visitor[F]) = v.setAsciiStream(a)
}
final case class SetCharacterStream(a: Long) extends NClobOp[Writer] {
def visit[F[_]](v: Visitor[F]) = v.setCharacterStream(a)
}
final case class SetString(a: Long, b: String) extends NClobOp[Int] {
def visit[F[_]](v: Visitor[F]) = v.setString(a, b)
}
final case class SetString1(a: Long, b: String, c: Int, d: Int) extends NClobOp[Int] {
def visit[F[_]](v: Visitor[F]) = v.setString(a, b, c, d)
}
final case class Truncate(a: Long) extends NClobOp[Unit] {
def visit[F[_]](v: Visitor[F]) = v.truncate(a)
}
}
import NClobOp._
// Smart constructors for operations common to all algebras.
val unit: NClobIO[Unit] = FF.pure[NClobOp, Unit](())
def pure[A](a: A): NClobIO[A] = FF.pure[NClobOp, A](a)
def raw[A](f: NClob => A): NClobIO[A] = FF.liftF(Raw(f))
def embed[F[_], J, A](j: J, fa: FF[F, A])(implicit ev: Embeddable[F, J]): FF[NClobOp, A] = FF.liftF(Embed(ev.embed(j, fa)))
def raiseError[A](err: Throwable): NClobIO[A] = FF.liftF[NClobOp, A](RaiseError(err))
def handleErrorWith[A](fa: NClobIO[A])(f: Throwable => NClobIO[A]): NClobIO[A] = FF.liftF[NClobOp, A](HandleErrorWith(fa, f))
val monotonic = FF.liftF[NClobOp, FiniteDuration](Monotonic)
val realtime = FF.liftF[NClobOp, FiniteDuration](Realtime)
def delay[A](thunk: => A) = FF.liftF[NClobOp, A](Suspend(Sync.Type.Delay, () => thunk))
def suspend[A](hint: Sync.Type)(thunk: => A) = FF.liftF[NClobOp, A](Suspend(hint, () => thunk))
def forceR[A, B](fa: NClobIO[A])(fb: NClobIO[B]) = FF.liftF[NClobOp, B](ForceR(fa, fb))
def uncancelable[A](body: Poll[NClobIO] => NClobIO[A]) = FF.liftF[NClobOp, A](Uncancelable(body))
def capturePoll[M[_]](mpoll: Poll[M]) = new Poll[NClobIO] {
def apply[A](fa: NClobIO[A]) = FF.liftF[NClobOp, A](Poll1(mpoll, fa))
}
val canceled = FF.liftF[NClobOp, Unit](Canceled)
def onCancel[A](fa: NClobIO[A], fin: NClobIO[Unit]) = FF.liftF[NClobOp, A](OnCancel(fa, fin))
def fromFuture[A](fut: NClobIO[Future[A]]) = FF.liftF[NClobOp, A](FromFuture(fut))
def fromFutureCancelable[A](fut: NClobIO[(Future[A], NClobIO[Unit])]) = FF.liftF[NClobOp, A](FromFutureCancelable(fut))
def performLogging(event: LogEvent) = FF.liftF[NClobOp, Unit](PerformLogging(event))
// Smart constructors for NClob-specific operations.
val free: NClobIO[Unit] = FF.liftF(Free)
val getAsciiStream: NClobIO[InputStream] = FF.liftF(GetAsciiStream)
val getCharacterStream: NClobIO[Reader] = FF.liftF(GetCharacterStream)
def getCharacterStream(a: Long, b: Long): NClobIO[Reader] = FF.liftF(GetCharacterStream1(a, b))
def getSubString(a: Long, b: Int): NClobIO[String] = FF.liftF(GetSubString(a, b))
val length: NClobIO[Long] = FF.liftF(Length)
def position(a: Clob, b: Long): NClobIO[Long] = FF.liftF(Position(a, b))
def position(a: String, b: Long): NClobIO[Long] = FF.liftF(Position1(a, b))
def setAsciiStream(a: Long): NClobIO[OutputStream] = FF.liftF(SetAsciiStream(a))
def setCharacterStream(a: Long): NClobIO[Writer] = FF.liftF(SetCharacterStream(a))
def setString(a: Long, b: String): NClobIO[Int] = FF.liftF(SetString(a, b))
def setString(a: Long, b: String, c: Int, d: Int): NClobIO[Int] = FF.liftF(SetString1(a, b, c, d))
def truncate(a: Long): NClobIO[Unit] = FF.liftF(Truncate(a))
// Typeclass instances for NClobIO
implicit val WeakAsyncNClobIO: WeakAsync[NClobIO] =
new WeakAsync[NClobIO] {
val monad = FF.catsFreeMonadForFree[NClobOp]
override val applicative = monad
override val rootCancelScope = CancelScope.Cancelable
override def pure[A](x: A): NClobIO[A] = monad.pure(x)
override def flatMap[A, B](fa: NClobIO[A])(f: A => NClobIO[B]): NClobIO[B] = monad.flatMap(fa)(f)
override def tailRecM[A, B](a: A)(f: A => NClobIO[Either[A, B]]): NClobIO[B] = monad.tailRecM(a)(f)
override def raiseError[A](e: Throwable): NClobIO[A] = module.raiseError(e)
override def handleErrorWith[A](fa: NClobIO[A])(f: Throwable => NClobIO[A]): NClobIO[A] = module.handleErrorWith(fa)(f)
override def monotonic: NClobIO[FiniteDuration] = module.monotonic
override def realTime: NClobIO[FiniteDuration] = module.realtime
override def suspend[A](hint: Sync.Type)(thunk: => A): NClobIO[A] = module.suspend(hint)(thunk)
override def forceR[A, B](fa: NClobIO[A])(fb: NClobIO[B]): NClobIO[B] = module.forceR(fa)(fb)
override def uncancelable[A](body: Poll[NClobIO] => NClobIO[A]): NClobIO[A] = module.uncancelable(body)
override def canceled: NClobIO[Unit] = module.canceled
override def onCancel[A](fa: NClobIO[A], fin: NClobIO[Unit]): NClobIO[A] = module.onCancel(fa, fin)
override def fromFuture[A](fut: NClobIO[Future[A]]): NClobIO[A] = module.fromFuture(fut)
override def fromFutureCancelable[A](fut: NClobIO[(Future[A], NClobIO[Unit])]): NClobIO[A] = module.fromFutureCancelable(fut)
}
implicit def MonoidNClobIO[A : Monoid]: Monoid[NClobIO[A]] = new Monoid[NClobIO[A]] {
override def empty: NClobIO[A] = Applicative[NClobIO].pure(Monoid[A].empty)
override def combine(x: NClobIO[A], y: NClobIO[A]): NClobIO[A] =
Applicative[NClobIO].product(x, y).map { case (x, y) => Monoid[A].combine(x, y) }
}
implicit def SemigroupNClobIO[A : Semigroup]: Semigroup[NClobIO[A]] = new Semigroup[NClobIO[A]] {
override def combine(x: NClobIO[A], y: NClobIO[A]): NClobIO[A] =
Applicative[NClobIO].product(x, y).map { case (x, y) => Semigroup[A].combine(x, y) }
}
}