Skip to content

Commit

Permalink
remove subquery t.* support; yield table t instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
godenji committed Dec 21, 2022
1 parent 9fb3f83 commit d8bd865
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
12 changes: 4 additions & 8 deletions src/main/scala/slaq/scalaquery/ql/Subquery.scala
Expand Up @@ -11,10 +11,8 @@ case class Subquery
override def isNamedTable = true
}

case class SubqueryColumn(
pos: Int, subquery: Subquery, typeMapper: TypeMapper[_],
underlying: Option[MappedProjection[_, _]] = None
) extends Node {
case class SubqueryColumn(pos: Int, subquery: Subquery, typeMapper: TypeMapper[_])
extends Node {

def nodeChildren = subquery :: Nil
override def nodeNamedChildren = (subquery, "subquery") :: Nil
Expand Down Expand Up @@ -57,8 +55,6 @@ object Subquery {
unpackable.mapOp { n =>
pos += 1
n match
case t: MappedProjection[_, _] =>
t.mapOp(_ => SubqueryColumn(pos, p, null, Some(t)))
case t: Table[_] =>
val xs = t.*.nodeChildren.collect {
case pn: ProductNode =>
Expand All @@ -77,9 +73,9 @@ object Subquery {

private def mapper(n: Node) = n match {
case c: Column[_] => c.typeMapper
case SubqueryColumn(_, _, tm, _) => tm
case SubqueryColumn(_, _, tm) => tm
case x => Fail(s"""
Expected Column or SubqueryColumn but got $x, maybe you forgot t.* projection?
Expected Column, SubqueryColumn, or Table but got $x -- maybe you tried to yield `t.*`?
See UnionTest.scala for detailed example of union queries with table projections.
"""
)
Expand Down
45 changes: 19 additions & 26 deletions src/main/scala/slaq/scalaquery/ql/core/QueryBuilder.scala
Expand Up @@ -60,32 +60,25 @@ abstract class QueryBuilder(
node match {
case p: ProductNode =>
val xs = p.nodeChildren
val hasProjection = xs.collect {
case SubqueryColumn(_, q, _, Some(projection)) => q
}
hasProjection.headOption match
// hack in star projection for union that yields MappedProjections
case Some(q) => b += s"${quote(tableAlias(q))}.*"
case None =>
xs.zipWithIndex.foreach {
// following two cases yield full table from subquery
case (ta @ Table.Alias(t: Table[_]), _) if rename =>
show(ta, b, true)
case (j: Join, i) if rename =>
show(p.product.productElement(i).asInstanceOf[Table[_]], b, true)
case (_: Join, i) =>
delimit( // delegate is Join, show parent Table
show(p.product.productElement(i).asInstanceOf[Table[_]], b)
)
case (n, i) if rename && pos != 0 =>
delimit(expr(n, b, true))
case (n: ProductNode, _) =>
n.nodeChildren.foreach { x =>
delimit(expr(x, b))
}
case (n, _) =>
delimit(expr(n, b))
xs.zipWithIndex.foreach {
// following two cases yield full table from subquery
case (ta @ Table.Alias(t: Table[_]), _) if rename =>
show(ta, b, true)
case (j: Join, i) if rename =>
show(p.product.productElement(i).asInstanceOf[Table[_]], b, true)
case (_: Join, i) =>
delimit( // delegate is Join, show parent Table
show(p.product.productElement(i).asInstanceOf[Table[_]], b)
)
case (n, i) if rename && pos != 0 =>
delimit(expr(n, b, true))
case (n: ProductNode, _) =>
n.nodeChildren.foreach { x =>
delimit(expr(x, b))
}
case (n, _) =>
delimit(expr(n, b))
}
case j: Join => // query yields a single table (i.e. non-product/projection)
val t = query.unpackable.value.asInstanceOf[Table[_]]
show(
Expand Down Expand Up @@ -113,7 +106,7 @@ abstract class QueryBuilder(
show(x, b)
if i != xs.size -1 then b += ','
)
case SubqueryColumn(pos, q, _, _) => b += s"${quote(tableAlias(q))}.${quote(s"c$pos")}"
case SubqueryColumn(pos, q, _) => b += s"${quote(tableAlias(q))}.${quote(s"c$pos")}"
case SimpleLiteral(w) => b += w
case _ =>
Fail(s"Unmatched node `$c` in show block")
Expand Down

0 comments on commit d8bd865

Please sign in to comment.