-
Notifications
You must be signed in to change notification settings - Fork 348
/
analysis.scala
211 lines (178 loc) · 10.3 KB
/
analysis.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
// Copyright (c) 2013-2018 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.util
import doobie.enum.Nullability
import doobie.enum.Nullability._
import doobie.enum.ParameterMode
import doobie.enum.JdbcType
import doobie.util.pretty._
import scala.Predef._ // TODO: minimize
import scala.reflect.runtime.universe.Type
import cats.implicits._
import cats.data.Ior
/** Module defining a type for analyzing the type alignment of prepared statements. */
object analysis {
/** Metadata for the JDBC end of a column/parameter mapping. */
final case class ColumnMeta(jdbcType: JdbcType, vendorTypeName: String, nullability: Nullability, name: String)
/** Metadata for the JDBC end of a column/parameter mapping. */
final case class ParameterMeta(jdbcType: JdbcType, vendorTypeName: String, nullability: Nullability, mode: ParameterMode)
sealed trait AlignmentError {
def tag: String
def index: Int
def msg: String
}
final case class ParameterMisalignment(index: Int, alignment: Option[ParameterMeta]) extends AlignmentError {
val tag = "P"
override def msg = this match {
case ParameterMisalignment(_, None) =>
s"""|Interpolated value has no corresponding SQL parameter and likely appears inside a
|comment or quoted string. Ior.Left will result in a runtime failure; fix this by removing
|the parameter.""".stripMargin.linesIterator.mkString(" ")
case ParameterMisalignment(_, Some(pm)) =>
s"""|${pm.jdbcType.show.toUpperCase} parameter is not set; this will result in a runtime
|failure. Perhaps you used a literal ? rather than an interpolated value.""".stripMargin.linesIterator.mkString(" ")
}
}
final case class ParameterTypeError(index: Int, put: Put[_], n: NullabilityKnown, jdbcType: JdbcType, vendorTypeName: String) extends AlignmentError {
override val tag = "P"
override def msg =
s"""|${typeName(put.typeStack.last, n)} is not coercible to ${jdbcType.show.toUpperCase}
|(${vendorTypeName})
|according to the JDBC specification.
|Expected schema type was ${put.jdbcTargets.head.show.toUpperCase}.""".stripMargin.linesIterator.mkString(" ")
}
final case class ColumnMisalignment(index: Int, alignment: Either[(Get[_], NullabilityKnown), ColumnMeta]) extends AlignmentError {
override val tag = "C"
override def msg = this match {
case ColumnMisalignment(_, Left((get, n))) =>
s"""|Too few columns are selected, which will result in a runtime failure. Add a column or
|remove mapped ${typeName(get.typeStack.last, n)} from the result type.""".stripMargin.linesIterator.mkString(" ")
case ColumnMisalignment(_, Right(_)) =>
s"""Column is unused. Remove it from the SELECT statement."""
}
}
final case class NullabilityMisalignment(index: Int, name: String, st: Option[Type], jdk: NullabilityKnown, jdbc: NullabilityKnown) extends AlignmentError {
override val tag = "C"
override def msg = this match {
// https://github.com/tpolecat/doobie/issues/164 ... NoNulls means "maybe no nulls" :-\
// case NullabilityMisalignment(i, name, st, NoNulls, Nullable) =>
// s"""Non-nullable column ${name.toUpperCase} is unnecessarily mapped to an Option type."""
case NullabilityMisalignment(_, _, st, Nullable, NoNulls) =>
s"""|Reading a NULL value into ${typeName(st, NoNulls)} will result in a runtime failure.
|Fix this by making the schema type ${formatNullability(NoNulls)} or by changing the
|Scala type to ${typeName(st, Nullable)}""".stripMargin.linesIterator.mkString(" ")
case _ => sys.error("unpossible, evidently")
}
}
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)} according to the JDBC specification or any defined
|mapping.
|Fix this by changing the schema type to
|${get.jdbcSources.toList.map(_.show.toUpperCase).toList.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) extends AlignmentError {
override val tag = "C"
override def msg =
s"""|${schema.jdbcType.show.toUpperCase} (${schema.vendorTypeName}) is ostensibly
|coercible to ${typeName(get.typeStack.last, n)}
|according to the JDBC specification but is not a recommended target type.
|Expected schema type was
|${get.jdbcSources.toList.map(_.show.toUpperCase).toList.mkString(" or ") }.
|""".stripMargin.linesIterator.mkString(" ")
}
/** Compatibility analysis for the given statement and aligned mappings. */
final case class Analysis(
sql: String,
parameterAlignment: List[(Put[_], NullabilityKnown) Ior ParameterMeta],
columnAlignment: List[(Get[_], NullabilityKnown) Ior ColumnMeta]) {
def parameterMisalignments: List[ParameterMisalignment] =
parameterAlignment.zipWithIndex.collect {
case (Ior.Left(_), n) => ParameterMisalignment(n + 1, None)
case (Ior.Right(p), n) => ParameterMisalignment(n + 1, Some(p))
}
def parameterTypeErrors: List[ParameterTypeError] =
parameterAlignment.zipWithIndex.collect {
case (Ior.Both((j, n1), p), n) if !j.jdbcTargets.contains_(p.jdbcType) =>
ParameterTypeError(n + 1, j, n1, p.jdbcType, p.vendorTypeName)
}
def columnMisalignments: List[ColumnMisalignment] =
columnAlignment.zipWithIndex.collect {
case (Ior.Left(j), n) => ColumnMisalignment(n + 1, Left(j))
case (Ior.Right(p), n) => ColumnMisalignment(n + 1, Right(p))
}
def columnTypeErrors: List[ColumnTypeError] =
columnAlignment.zipWithIndex.collect {
case (Ior.Both((j, n1), p), n) if !(j.jdbcSources.toList ++ j.fold(_.jdbcSourceSecondary.toList, _ => Nil)).contains_(p.jdbcType) =>
ColumnTypeError(n + 1, j, n1, p)
case (Ior.Both((j, n1), p), n) if (p.jdbcType === JdbcType.JavaObject || p.jdbcType === JdbcType.Other) && !j.fold(_ => None, a => Some(a.schemaTypes.head)).contains_(p.vendorTypeName) =>
ColumnTypeError(n + 1, j, n1, p)
}
def columnTypeWarnings: List[ColumnTypeWarning] =
columnAlignment.zipWithIndex.collect {
case (Ior.Both((j, n1), p), n) if j.fold(_.jdbcSourceSecondary.toList, _ => Nil).contains_(p.jdbcType) =>
ColumnTypeWarning(n + 1, j, n1, p)
}
def nullabilityMisalignments: List[NullabilityMisalignment] =
columnAlignment.zipWithIndex.collect {
// We can't do anything helpful with NoNulls .. it means "might not be nullable"
// case (Ior.Both((st, Nullable), ColumnMeta(_, _, NoNulls, col)), n) => NullabilityMisalignment(n + 1, col, st, NoNulls, Nullable)
case (Ior.Both((st, NoNulls), ColumnMeta(_, _, Nullable, col)), n) => NullabilityMisalignment(n + 1, col, st.typeStack.last, Nullable, NoNulls)
// N.B. if we had a warning mechanism we could issue a warning for NullableUnknown
}
lazy val parameterAlignmentErrors =
parameterMisalignments ++ parameterTypeErrors
lazy val columnAlignmentErrors =
columnMisalignments ++ columnTypeErrors ++ columnTypeWarnings ++ nullabilityMisalignments
lazy val alignmentErrors =
(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])] = {
val params: Block =
parameterAlignment.zipWithIndex.map {
case (Ior.Both((j1, n1), ParameterMeta(j2, s2, _, _)), i) => List(f"P${i+1}%02d", show"${typeName(j1.typeStack.last, n1)}", " → ", j2.show.toUpperCase, show"($s2)")
case (Ior.Left((j1, n1)), i) => List(f"P${i+1}%02d", show"${typeName(j1.typeStack.last, n1)}", " → ", "", "")
case (Ior.Right( ParameterMeta(j2, s2, _, _)), i) => List(f"P${i+1}%02d", "", " → ", j2.show.toUpperCase, show"($s2)")
} .transpose.map(Block(_)).foldLeft(Block(Nil))(_ leftOf1 _).trimLeft(1)
params.toString.linesIterator.toList.zipWithIndex.map { case (show, n) =>
(show, parameterAlignmentErrors.filter(_.index == n + 1))
}
}
/** Description of each parameter, paird with its errors. */
lazy val columnDescriptions: List[(String, List[AlignmentError])] = {
import pretty._
val cols: Block =
columnAlignment.zipWithIndex.map {
case (Ior.Both((j1, n1), ColumnMeta(j2, s2, n2, m)), i) => List(f"C${i+1}%02d", m, j2.show.toUpperCase, show"(${s2.toString})", formatNullability(n2), " → ", typeName(j1.typeStack.last, n1))
case (Ior.Left((j1, n1)), i) => List(f"C${i+1}%02d", "", "", "", "", " → ", typeName(j1.typeStack.last, n1))
case (Ior.Right( ColumnMeta(j2, s2, n2, m)), i) => List(f"C${i+1}%02d", m, j2.show.toUpperCase, show"(${s2.toString})", formatNullability(n2), " → ", "")
} .transpose.map(Block(_)).foldLeft(Block(Nil))(_ leftOf1 _).trimLeft(1)
cols.toString.linesIterator.toList.zipWithIndex.map { case (show, n) =>
(show, columnAlignmentErrors.filter(_.index == n + 1))
}
}
}
// Some stringy helpers
private val packagePrefix = "\\b[a-z]+\\.".r
@SuppressWarnings(Array("org.wartremover.warts.ToString"))
private def typeName(t: Option[Type], n: NullabilityKnown): String = {
val name = packagePrefix.replaceAllIn(t.fold("«erased»")(_.toString), "")
n match {
case NoNulls => name
case Nullable => show"Option[${name}]"
}
}
private def formatNullability(n: Nullability): String =
n match {
case NoNulls => "NOT NULL"
case Nullable => "NULL"
case NullableUnknown => "NULL?"
}
}