-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
059fec4
commit 48ca204
Showing
10 changed files
with
170 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
quill-engine/src/main/scala/io/getquill/sql/norm/SheathIdentContexts.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.getquill.sql.norm | ||
|
||
import io.getquill.NamingStrategy | ||
import io.getquill.ast.Ast.LeafQuat | ||
import io.getquill.ast.{Ident, Property, Renameable} | ||
import io.getquill.context.sql._ | ||
import io.getquill.quat.Quat | ||
|
||
object SheathIdentContexts extends StatelessQueryTransformer { | ||
|
||
override protected def expandNested(q: FlattenSqlQuery, level: QueryLevel): FlattenSqlQuery = { | ||
val from = q.from.map(expandContext(_)) | ||
val select = q.select.map { selectValue => | ||
selectValue.ast match { | ||
case LeafQuat(origId: Ident) => | ||
val selects = findSelectsOfAlias(from, selectValue.alias.getOrElse(origId.name)) | ||
selects.map(_.alias).distinct match { | ||
// if it's a single value of a single subselect eg: | ||
// SELECT z FROM (SELECT x.i FROM foo) AS z) | ||
// or | ||
// SELECT z FROM (SELECT x.i FROM foo) UNION (SELECT y.i FROM bar) AS z) | ||
case List(Some(value)) => | ||
// Then make it into: | ||
// SELECT z.i FROM (SELECT x.i FROM foo) AS z.i) | ||
// or | ||
// SELECT z.i FROM (SELECT x.i FROM foo) UNION (SELECT y.i FROM bar) AS z.i) | ||
// this z.i will be: | ||
// Property(Ident(z, CC(i -> zOrig.quat), "i") | ||
val newId = Ident(origId.name, Quat.Product("<single-prop-gen>", value -> origId.quat)) | ||
SelectValue(Property(newId, value)) | ||
case _ => | ||
selectValue | ||
} | ||
|
||
case _ => selectValue | ||
} | ||
} | ||
q.copy(select = select, from = from)(q.quat) | ||
} | ||
|
||
// find selects of a query aliased as X | ||
protected def findSelectsOfAlias(contexts: List[FromContext], alias: String): List[SelectValue] = { | ||
def handleQuery(q: SqlQuery, queryAlias: String): List[SelectValue] = | ||
q match { | ||
case SetOperationSqlQuery(a, _, b) => handleQuery(a, queryAlias) ++ handleQuery(b, queryAlias) | ||
case UnaryOperationSqlQuery(_, q) => handleQuery(q, queryAlias) | ||
case flatten: FlattenSqlQuery => flatten.select | ||
} | ||
|
||
contexts.flatMap { | ||
case QueryContext(q, queryAlias) => | ||
if (queryAlias == alias) | ||
handleQuery(q, queryAlias) | ||
else | ||
List() | ||
|
||
case JoinContext(_, a, b, _) => | ||
findSelectsOfAlias(List(a), alias) ++ findSelectsOfAlias(List(b), alias) | ||
|
||
case FlatJoinContext(_, a, _) => | ||
findSelectsOfAlias(List(a), alias) | ||
|
||
// table or infix would be a single variable, not sure can't do anything in that case | ||
case _: TableContext | _: InfixContext => List() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
quill-sql/src/test/scala/io/getquill/context/sql/idiom/SqlIdiomTestSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.getquill.context.sql.idiom | ||
|
||
import io.getquill.ReturnAction.ReturnColumns | ||
import io.getquill.{Action, Literal, MirrorSqlDialectWithReturnMulti, Ord, PostgresDialect, Query, SqlMirrorContext} | ||
import io.getquill.context.mirror.Row | ||
|
||
object SqlIdiomTestSpec { | ||
|
||
case class TestEntity(s: String, i: Int, l: Long, o: Option[Int], b: Boolean) | ||
case class TestEntity2(s: String, i: Int, l: Long, o: Option[Int]) | ||
|
||
val ctx = new SqlMirrorContext(PostgresDialect, Literal) | ||
import ctx._ | ||
|
||
val qr1 = quote { | ||
query[TestEntity] | ||
} | ||
val qr2 = quote { | ||
query[TestEntity2] | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
// System.setProperty("quill.trace.enabled", "true") | ||
// System.setProperty("quill.trace.color", "true") | ||
// System.setProperty("quill.trace.quat", "full") | ||
// System.setProperty("quill.trace.types", "all") | ||
// io.getquill.util.Messages.resetCache() // | ||
|
||
// val q = quote { | ||
// for { | ||
// v1 <- qr1.map(x => x.i).distinct | ||
// v2 <- qr2.filter(_.i == v1) | ||
// } yield (v1, v2) | ||
// }.dynamic | ||
// println(ctx.run(q).string) | ||
// "SELECT i._1, x1.s, x1.i, x1.l, x1.o FROM (SELECT DISTINCT i.i AS _1 FROM TestEntity i) AS i, TestEntity2 x1 WHERE x1.i = i._1" | ||
|
||
val q = quote { | ||
qr1.map(x => x.i).nested | ||
}.dynamic | ||
|
||
println(run(q).string) // | ||
} | ||
|
||
} |