From c04c180feb1fc24fd9a11865b0b80eb34c7d6a9c Mon Sep 17 00:00:00 2001 From: Jacob Wang Date: Sun, 21 Apr 2024 20:27:50 +0100 Subject: [PATCH] Improve analysis message + Small optimizations --- .../src/main/scala/doobie/util/analysis.scala | 35 ++++++++++--------- .../core/src/main/scala/doobie/util/get.scala | 2 ++ 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/modules/core/src/main/scala/doobie/util/analysis.scala b/modules/core/src/main/scala/doobie/util/analysis.scala index 4dd0f7dd4..de07c8b07 100644 --- a/modules/core/src/main/scala/doobie/util/analysis.scala +++ b/modules/core/src/main/scala/doobie/util/analysis.scala @@ -59,7 +59,7 @@ object analysis { override val tag = "P" override def msg = s"""|${typeName(put.typeStack.last, n)} is not coercible to ${jdbcType.show.toUpperCase} - |(${vendorTypeName}) + |(vendor's name: $vendorTypeName) |according to the JDBC specification. |Expected schema type was ${put.jdbcTargets.head.show.toUpperCase}.""".stripMargin.linesIterator.mkString(" ") } @@ -100,16 +100,19 @@ object analysis { final case class ColumnTypeError(index: Int, get: Get[?], n: NullabilityKnown, schema: ColumnMeta) extends AlignmentError { override val tag = "C" - override def msg = - s"""|${schema.jdbcType.show.toUpperCase} (${schema.vendorTypeName}) is not - |coercible to ${typeName(get.typeStack.last, n)} (${get.vendorTypeNames.mkString( - ",")}) according to the JDBC specification or any defined - |mapping. - |Fix this by changing the schema type to + override def msg = { + val vendorNamePartFromGet = + if (get.vendorTypeNames.isEmpty) "" + else s" (vendor's name: ${get.vendorTypeNames.mkString(",")})" + + s"""|${schema.jdbcType.show.toUpperCase} (vendor name: ${schema.vendorTypeName}) is not + |coercible to ${typeName(get.typeStack.last, n)}$vendorNamePartFromGet based on defined mapping. + |Fix this by changing the column type to |${get.jdbcSources.toList.map(_.show.toUpperCase).mkString(" or ")}; or the |Scala type to an appropriate ${if (schema.jdbcType === JdbcType.Array) "array" else "object"} |type. |""".stripMargin.linesIterator.mkString(" ") + } } final case class ColumnTypeWarning(index: Int, get: Get[?], n: NullabilityKnown, schema: ColumnMeta) @@ -139,10 +142,9 @@ object analysis { } private def hasParameterTypeErrors[A](put: Put[A], paramMeta: ParameterMeta): Boolean = { - val jdbcTypeMatches = put.jdbcTargets.contains_(paramMeta.jdbcType) - val vendorTypeMatches = put.vendorTypeNames.isEmpty || put.vendorTypeNames.contains_(paramMeta.vendorTypeName) - - !jdbcTypeMatches || !vendorTypeMatches + !put.jdbcTargets.contains_(paramMeta.jdbcType) || + !(put.vendorTypeNames.isEmpty || + put.vendorTypeNames.contains_(paramMeta.vendorTypeName)) } def parameterTypeErrors: List[ParameterTypeError] = @@ -158,10 +160,11 @@ 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.vendorTypeNames.isEmpty || get.vendorTypeNames.contains_(columnMeta.vendorTypeName) - !jdbcTypeMatches || !vendorTypeMatches + !get.allJdbcSources.contains_(columnMeta.jdbcType) || + !(get.vendorTypeNames.isEmpty || + get.vendorTypeNames.contains_(columnMeta.vendorTypeName)) } + def columnTypeErrors: List[ColumnTypeError] = columnAlignment.zipWithIndex.collect { case (Ior.Both((get, n1), p), n) if hasColumnTypeError(get, p) => @@ -190,8 +193,8 @@ object analysis { columnMisalignments ++ columnTypeErrors ++ columnTypeWarnings ++ nullabilityMisalignments lazy val alignmentErrors = - (parameterAlignmentErrors).sortBy(m => (m.index, m.msg)) ++ - (columnAlignmentErrors).sortBy(m => (m.index, m.msg)) + parameterAlignmentErrors.sortBy(m => (m.index, m.msg)) ++ + columnAlignmentErrors.sortBy(m => (m.index, m.msg)) /** Description of each parameter, paired with its errors. */ lazy val paramDescriptions: List[(String, List[AlignmentError])] = { diff --git a/modules/core/src/main/scala/doobie/util/get.scala b/modules/core/src/main/scala/doobie/util/get.scala index 9c714be50..bbfeb45b9 100644 --- a/modules/core/src/main/scala/doobie/util/get.scala +++ b/modules/core/src/main/scala/doobie/util/get.scala @@ -32,6 +32,8 @@ sealed abstract class Get[A]( val get: Coyoneda[(ResultSet, Int) => *, A] ) { + val allJdbcSources: NonEmptyList[JdbcType] = this.jdbcSources ++ this.jdbcSourceSecondary + final def unsafeGetNonNullable(rs: ResultSet, n: Int): A = { val i = get.fi(rs, n) if (rs.wasNull)