forked from tpolecat/doobie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
columnnullable.scala
51 lines (38 loc) · 1.48 KB
/
columnnullable.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
// 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.enumerated
import cats.ApplicativeError
import cats.kernel.Eq
import doobie.util.invariant.*
import java.sql.ResultSetMetaData.*
/** @group Types */
sealed abstract class ColumnNullable(val toInt: Int) extends Product with Serializable {
def toNullability: Nullability =
Nullability.fromColumnNullable(this)
}
/** @group Modules */
object ColumnNullable {
/** @group Values */
case object NoNulls extends ColumnNullable(columnNoNulls)
/** @group Values */
case object Nullable extends ColumnNullable(columnNullable)
/** @group Values */
case object NullableUnknown extends ColumnNullable(columnNullableUnknown)
def fromInt(n: Int): Option[ColumnNullable] =
Some(n) collect {
case NoNulls.toInt => NoNulls
case Nullable.toInt => Nullable
case NullableUnknown.toInt => NullableUnknown
}
def fromNullability(n: Nullability): ColumnNullable =
n match {
case Nullability.NoNulls => NoNulls
case Nullability.Nullable => Nullable
case Nullability.NullableUnknown => NullableUnknown
}
def fromIntF[F[_]](n: Int)(implicit AE: ApplicativeError[F, Throwable]): F[ColumnNullable] =
ApplicativeError.liftFromOption(fromInt(n), InvalidOrdinal[ColumnNullable](n))
implicit val EqColumnNullable: Eq[ColumnNullable] =
Eq.by(_.toInt)
}