Created
August 5, 2013 16:33
-
-
Save Rogach/6157353 to your computer and use it in GitHub Desktop.
Phase for Slick, that unboxes wrapped tables
This file contains hidden or 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
package scala.slick.compiler | |
import scala.slick.ast._ | |
import scala.collection.mutable.HashMap | |
class Declutter extends Phase { | |
val name = "declutter" | |
def apply(tree: Node, state: CompilationState): Node = { | |
val replaced = new HashMap[Symbol, Symbol] | |
val unwrapTables = new Transformer { | |
def replace = { | |
case c @ Comprehension((_, from) :: Nil, Nil, None, Nil, Some(Pure(StructNode(select))), None, None) => | |
val unwrapped = select.forall { | |
case (s, Path(col :: _)) => true | |
case _ => false | |
} | |
if (unwrapped) { | |
select.foreach { | |
case (s, Path(col :: _)) => replaced += s -> col | |
} | |
from | |
} else c | |
} | |
} | |
val simplifyComprehensions = new Transformer { | |
def replace = { | |
case c @ Comprehension(_, _, _, _, Some(Pure(ProductNode(s))), _, _) => | |
val symbols = s.map(Path.unapply).flatten.map(_.head) | |
unwrapTables.once(c) | |
} | |
} | |
val replaceRefs = new Transformer { | |
def replace = { | |
case p @ Path(s :: rest) if replaced.isDefinedAt(s) => Path(replaced(s) :: rest) | |
} | |
} | |
replaceRefs.once(simplifyComprehensions.repeat(tree)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment