Skip to content

Commit

Permalink
compile fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jatcwang committed Apr 1, 2024
1 parent b29f2cf commit 5418d58
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 49 deletions.
4 changes: 2 additions & 2 deletions modules/core/src/main/scala/doobie/util/analysis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ object analysis {

private def hasParameterTypeErrors[A](put: Put[A], paramMeta: ParameterMeta): Boolean = {
val jdbcTypeMatches = put.jdbcTargets.contains_(paramMeta.jdbcType)
val vendorTypeMatches = put.schemaTypes.isEmpty || put.schemaTypes.contains_(paramMeta.vendorTypeName)
val vendorTypeMatches = put.vendorTypeNames.isEmpty || put.vendorTypeNames.contains_(paramMeta.vendorTypeName)

!jdbcTypeMatches || !vendorTypeMatches
}
Expand All @@ -136,7 +136,7 @@ object analysis {

private def hasColumnTypeError[A](get: Get[A], columnMeta: ColumnMeta): Boolean = {
val jdbcTypeMatches = (get.jdbcSources.toList ++ get.jdbcSourceSecondary).contains_(columnMeta.jdbcType)
val vendorTypeMatches = get.schemaTypes.isEmpty || get.schemaTypes.contains_(columnMeta.vendorTypeName)
val vendorTypeMatches = get.vendorTypeNames.isEmpty || get.vendorTypeNames.contains_(columnMeta.vendorTypeName)
!jdbcTypeMatches || !vendorTypeMatches
}
def columnTypeErrors: List[ColumnTypeError] =
Expand Down
41 changes: 25 additions & 16 deletions modules/core/src/main/scala/doobie/util/get.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ import scala.reflect.ClassTag
import org.tpolecat.typename._
import doobie.util.meta.Meta

/**
*
* @param typeStack
* @param jdbcSources
* @param jdbcSourceSecondary
* @param vendorTypeNames If non-empty, the column/parameter type reported
* by the database will be checked to match this list
* during typechecking against the database.
*/
sealed abstract class Get[A](
val typeStack: NonEmptyList[Option[String]],
val jdbcSources: NonEmptyList[JdbcType],
val jdbcSourceSecondary: List[JdbcType],
val schemaTypes: List[String],
val vendorTypeNames: List[String],
val get: Coyoneda[(ResultSet, Int) => *, A],
) {

Expand Down Expand Up @@ -55,7 +64,7 @@ sealed abstract class Get[A](
typeStack = typ :: typeStack,
jdbcSources = jdbcSources,
jdbcSourceSecondary = jdbcSourceSecondary,
schemaTypes = schemaTypes,
vendorTypeNames = vendorTypeNames,
get = get.map(f),
) {}

Expand Down Expand Up @@ -96,7 +105,7 @@ object Get extends GetInstances {
typeStack,
jdbcSources = jdbcSources,
jdbcSourceSecondary = jdbcSourceSecondary,
schemaTypes = checkedVendorType.toList,
vendorTypeNames = checkedVendorType.toList,
get = get,
) {}

Expand Down Expand Up @@ -124,45 +133,45 @@ object Get extends GetInstances {
def apply[A](
typeStack: NonEmptyList[Option[String]],
jdbcSources: NonEmptyList[JdbcType],
schemaTypes: NonEmptyList[String],
vendorTypeNames: NonEmptyList[String],
get: Coyoneda[(ResultSet, Int) => *, A],
): Get[A] = new Get[A](
typeStack,
jdbcSources = jdbcSources,
jdbcSourceSecondary = Nil,
schemaTypes = schemaTypes.toList,
vendorTypeNames = vendorTypeNames.toList,
get = get,
) {}

def many[A](
jdbcSources: NonEmptyList[JdbcType],
schemaTypes: NonEmptyList[String],
vendorTypeNames: NonEmptyList[String],
get: (ResultSet, Int) => A,
)(implicit ev: TypeName[A]): Get[A] =
Advanced(NonEmptyList.of(Some(ev.value)), jdbcSources, schemaTypes, Coyoneda.lift(get))
Advanced(NonEmptyList.of(Some(ev.value)), jdbcSources, vendorTypeNames, Coyoneda.lift(get))

def one[A](
jdbcSource: JdbcType,
schemaTypes: NonEmptyList[String],
vendorTypeNames: NonEmptyList[String],
get: (ResultSet, Int) => A,
)(implicit ev: TypeName[A]): Get[A] =
Advanced(NonEmptyList.of(Some(ev.value)), NonEmptyList.of(jdbcSource), schemaTypes, Coyoneda.lift(get))
Advanced(NonEmptyList.of(Some(ev.value)), NonEmptyList.of(jdbcSource), vendorTypeNames, Coyoneda.lift(get))

@SuppressWarnings(Array("org.wartremover.warts.Equals", "org.wartremover.warts.AsInstanceOf"))
def array[A >: Null <: AnyRef](schemaTypes: NonEmptyList[String]): Get[Array[A]] =
one(JdbcType.Array, schemaTypes, (r, n) => {
def array[A >: Null <: AnyRef](vendorTypeNames: NonEmptyList[String]): Get[Array[A]] =
one(JdbcType.Array, vendorTypeNames, (r, n) => {
val a = r.getArray(n)
(if (a == null) null else a.getArray).asInstanceOf[Array[A]]
}
)

@SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf", "org.wartremover.warts.Throw"))
def other[A >: Null <: AnyRef: TypeName](schemaTypes: NonEmptyList[String])(
def other[A >: Null <: AnyRef: TypeName](vendorTypeNames: NonEmptyList[String])(
implicit A: ClassTag[A]
): Get[A] =
many(
NonEmptyList.of(JdbcType.Other, JdbcType.JavaObject),
schemaTypes,
vendorTypeNames,
(rs, n) => {
rs.getObject(n) match {
case null => null
Expand Down Expand Up @@ -210,17 +219,17 @@ sealed abstract class MkGet[A](
override val typeStack: NonEmptyList[Option[String]],
override val jdbcSources: NonEmptyList[JdbcType],
override val jdbcSourceSecondary: List[JdbcType],
override val schemaTypes: List[String],
override val vendorTypeNames: List[String],
override val get: Coyoneda[(ResultSet, Int) => *, A],
) extends Get[A](typeStack, jdbcSources, jdbcSourceSecondary, schemaTypes, get)
) extends Get[A](typeStack, jdbcSources, jdbcSourceSecondary, vendorTypeNames, get)
object MkGet extends GetPlatform {

def lift[A](g: Get[A]): MkGet[A] =
new MkGet[A](
typeStack = g.typeStack,
jdbcSources = g.jdbcSources,
jdbcSourceSecondary = g.jdbcSourceSecondary,
schemaTypes = g.schemaTypes,
vendorTypeNames = g.vendorTypeNames,
get = g.get,
) {}
}
12 changes: 6 additions & 6 deletions modules/core/src/main/scala/doobie/util/meta/meta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,26 @@ trait MetaConstructors {

def many[A: TypeName](
jdbcTypes: NonEmptyList[JdbcType],
schemaTypes: NonEmptyList[String],
vendorTypeNames: NonEmptyList[String],
get: (ResultSet, Int) => A,
put: (PreparedStatement, Int, A) => Unit,
update: (ResultSet, Int, A) => Unit,
): Meta[A] =
new Meta(
Get.Advanced.many(jdbcTypes, schemaTypes, get),
Put.Advanced.many(jdbcTypes, schemaTypes, put, update)
Get.Advanced.many(jdbcTypes, vendorTypeNames, get),
Put.Advanced.many(jdbcTypes, vendorTypeNames, put, update)
)

def one[A: TypeName](
jdbcTypes: JdbcType,
schemaTypes: NonEmptyList[String],
vendorTypeNames: NonEmptyList[String],
get: (ResultSet, Int) => A,
put: (PreparedStatement, Int, A) => Unit,
update: (ResultSet, Int, A) => Unit,
): Meta[A] =
new Meta(
Get.Advanced.one(jdbcTypes, schemaTypes, get),
Put.Advanced.one(jdbcTypes, schemaTypes, put, update)
Get.Advanced.one(jdbcTypes, vendorTypeNames, get),
Put.Advanced.one(jdbcTypes, vendorTypeNames, put, update)
)

def array[A >: Null <: AnyRef](
Expand Down
36 changes: 18 additions & 18 deletions modules/core/src/main/scala/doobie/util/put.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import scala.reflect.ClassTag
sealed abstract class Put[A](
val typeStack: NonEmptyList[Option[String]],
val jdbcTargets: NonEmptyList[JdbcType],
val schemaTypes: List[String],
val vendorTypeNames: List[String],
val put: ContravariantCoyoneda[(PreparedStatement, Int, *) => Unit, A],
val update: ContravariantCoyoneda[(ResultSet, Int, *) => Unit, A],
) {

def unsafeSetNull(ps: PreparedStatement, n: Int): Unit = {
val sqlType = jdbcTargets.head.toInt

schemaTypes.headOption match {
vendorTypeNames.headOption match {
case None => ps.setNull(n, sqlType)
case Some(schemaType) => ps.setNull(n, sqlType, schemaType)
}
Expand All @@ -42,7 +42,7 @@ sealed abstract class Put[A](
new Put[B](
typeStack = typ :: typeStack,
jdbcTargets = jdbcTargets,
schemaTypes = schemaTypes,
vendorTypeNames = vendorTypeNames,
put = put.contramap(f),
update = update.contramap(f),
) {}
Expand Down Expand Up @@ -70,7 +70,7 @@ sealed abstract class Put[A](
}

override def toString(): String = {
s"Put(typeStack=${typeStack.mkString_(",")}, jdbcTargets=${jdbcTargets.mkString_(",")}, schemaTypes=${schemaTypes.mkString_(",")})"
s"Put(typeStack=${typeStack.mkString_(",")}, jdbcTargets=${jdbcTargets.mkString_(",")}, vendorTypeNames=${vendorTypeNames.mkString_(",")})"
}

}
Expand All @@ -96,7 +96,7 @@ object Put extends PutInstances {
): Put[A] = new Put[A](
typeStack = typeStack,
jdbcTargets = jdbcTargets,
schemaTypes = checkedVendorType.toList,
vendorTypeNames = checkedVendorType.toList,
put = put,
update = update,
) {}
Expand Down Expand Up @@ -130,47 +130,47 @@ object Put extends PutInstances {
def apply[A](
typeStack: NonEmptyList[Option[String]],
jdbcTargets: NonEmptyList[JdbcType],
schemaTypes: NonEmptyList[String],
vendorTypeNames: NonEmptyList[String],
put: ContravariantCoyoneda[(PreparedStatement, Int, *) => Unit, A],
update: ContravariantCoyoneda[(ResultSet, Int, *) => Unit, A],
): Put[A] = new Put[A](
typeStack = typeStack,
jdbcTargets = jdbcTargets,
schemaTypes = schemaTypes.toList,
vendorTypeNames = vendorTypeNames.toList,
put = put,
update = update,
) {}

def many[A](
jdbcTargets: NonEmptyList[JdbcType],
schemaTypes: NonEmptyList[String],
vendorTypeNames: NonEmptyList[String],
put: (PreparedStatement, Int, A) => Unit,
update: (ResultSet, Int, A) => Unit,
)(implicit ev: TypeName[A]): Put[A] =
Advanced(
NonEmptyList.of(Some(ev.value)),
jdbcTargets,
schemaTypes,
vendorTypeNames,
ContravariantCoyoneda.lift[(PreparedStatement, Int, *) => Unit, A](put),
ContravariantCoyoneda.lift[(ResultSet, Int, *) => Unit, A](update),
)

def one[A: TypeName](
jdbcTarget: JdbcType,
schemaTypes: NonEmptyList[String],
vendorTypeNames: NonEmptyList[String],
put: (PreparedStatement, Int, A) => Unit,
update: (ResultSet, Int, A) => Unit,
): Put[A] =
many(NonEmptyList.of(jdbcTarget), schemaTypes, put, update)
many(NonEmptyList.of(jdbcTarget), vendorTypeNames, put, update)

@SuppressWarnings(Array("org.wartremover.warts.Equals", "org.wartremover.warts.AsInstanceOf"))
def array[A >: Null <: AnyRef](
schemaTypes: NonEmptyList[String],
vendorTypeNames: NonEmptyList[String],
elementType: String
): Put[Array[A]] =
one(
JdbcType.Array,
schemaTypes,
vendorTypeNames,
(ps, n, a) => {
val conn = ps.getConnection
val arr = conn.createArrayOf(elementType, a.asInstanceOf[Array[AnyRef]])
Expand All @@ -184,10 +184,10 @@ object Put extends PutInstances {
}
)

def other[A >: Null <: AnyRef: TypeName](schemaTypes: NonEmptyList[String]): Put[A] =
def other[A >: Null <: AnyRef: TypeName](vendorTypeNames: NonEmptyList[String]): Put[A] =
many(
NonEmptyList.of(JdbcType.Other, JdbcType.JavaObject),
schemaTypes,
vendorTypeNames,
(ps, n, a) => ps.setObject(n, a),
(rs, n, a) => rs.updateObject(n, a)
)
Expand Down Expand Up @@ -224,17 +224,17 @@ trait PutInstances {
sealed abstract class MkPut[A](
override val typeStack: NonEmptyList[Option[String]],
override val jdbcTargets: NonEmptyList[JdbcType],
override val schemaTypes: List[String],
override val vendorTypeNames: List[String],
override val put: ContravariantCoyoneda[(PreparedStatement, Int, *) => Unit, A],
override val update: ContravariantCoyoneda[(ResultSet, Int, *) => Unit, A],
) extends Put[A](typeStack, jdbcTargets, schemaTypes, put, update)
) extends Put[A](typeStack, jdbcTargets, vendorTypeNames, put, update)
object MkPut extends PutPlatform {

def lift[A](g: Put[A]): MkPut[A] =
new MkPut[A](
typeStack = g.typeStack,
jdbcTargets = g.jdbcTargets,
schemaTypes = g.schemaTypes,
vendorTypeNames = g.vendorTypeNames,
put = g.put,
update = g.update,
) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package doobie.h2.circe

import cats.Show
import cats.data.NonEmptyList
import cats.syntax.all._
import doobie.enumerated.JdbcType
import io.circe._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

package doobie.h2

import cats.data.NonEmptyList
import doobie.enumerated.JdbcType.*
import doobie.enumerated.JdbcType._
import doobie.util.meta.Meta
import doobie.util.meta.MetaConstructors.Basic

Expand Down
2 changes: 1 addition & 1 deletion modules/h2/src/test/scala/doobie/h2/h2types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import cats.effect.IO
import doobie._
import doobie.implicits._
import doobie.h2.implicits._
import doobie.util.analysis.{Analysis, ColumnTypeError, ColumnTypeWarning}
import doobie.util.analysis.{Analysis, ColumnTypeError}
import doobie.util.arbitraries.SQLArbitraries._
import doobie.util.arbitraries.StringArbitraries._
import org.scalacheck.Prop.forAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package doobie.mysql

import cats.data.NonEmptyList
import doobie.Meta
import doobie.enumerated.JdbcType
import doobie.util.meta.MetaConstructors.Basic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

package doobie.postgres

import cats.data.NonEmptyList
import doobie.enumerated.JdbcType.*
import doobie.enumerated.JdbcType._
import doobie.util.meta.Meta
import doobie.util.meta.MetaConstructors.Basic

Expand Down

0 comments on commit 5418d58

Please sign in to comment.