Created
July 24, 2013 11:50
-
-
Save pedrofurla/6069867 to your computer and use it in GitHub Desktop.
Extending query builder with extract... it's currently H2Driver only.
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 slickdemo.experiments | |
| import scala.slick.driver.{BasicStatementBuilderComponent, H2Driver} | |
| import scala.language.implicitConversions | |
| /** | |
| * Created with IntelliJ IDEA. | |
| * User: pedrofurla | |
| * Date: 18/07/13 | |
| * Time: 19:00 | |
| */ | |
| trait H2ExtraStatements extends H2Driver { self => | |
| // Oracle style row nums brakes extending BasicStatementBuilderComponent directly, it's a direct extension of H2Driver | |
| import scala.slick.ast._ | |
| import scala.slick.driver.QueryBuilderInput | |
| override def createQueryBuilder(input: QueryBuilderInput) = new QueryBuilder(input) | |
| class QueryBuilder(input: QueryBuilderInput) extends super.QueryBuilder(input) { | |
| import scala.slick.util.MacroSupport.macroSupportInterpolation | |
| override def expr(n: Node, skipParens: Boolean = false) = { | |
| n match { | |
| case Extract(n @ _* ) => | |
| val datePart = n(1).asInstanceOf[LiteralNode].value.asInstanceOf[String] | |
| b"""extract(`${datePart} from ${n(0)})""" | |
| //b"""extract(`${datePart} from ${date}""" | |
| case _ => super.expr(n, skipParens) | |
| }} | |
| } | |
| import scala.slick.ast.FunctionSymbol | |
| val Extract = new FunctionSymbol("Extract") | |
| } | |
| object H2ExtraDriver extends H2Driver with H2ExtraStatements { | |
| import org.joda.time.DateTime | |
| import scala.slick.lifted.{BaseTypeMapper, ExtensionMethods,Column, ConstColumn} | |
| import scala.slick.ast.{Node, LiteralNode} | |
| //type DATE = java.sql.Date | |
| type DATE = DateTime | |
| object DateTimeConstants { | |
| sealed trait DateTimeConstant | |
| val year = ConstColumn("YEAR") | |
| val month = ConstColumn("month") | |
| val day = ConstColumn("day") | |
| val hour = ConstColumn("hour") | |
| val minutes = ConstColumn("minutes") | |
| val seconds = ConstColumn("seconds") | |
| } | |
| class DateColumnExtensionMethods2[B1, P1](val c: Column[P1]) extends AnyVal with ExtensionMethods[B1, P1] { | |
| def year[R](implicit om: o#to[Int, R]) = extract(DateTimeConstants.year)//om(Extract.column[Int](n, LiteralNode("year"))) | |
| def month[R](implicit om: o#to[Int, R]) = om(Extract.column[Int](n, LiteralNode("month"))) | |
| def day[R](implicit om: o#to[Int, R]) = om(Extract.column[Int](n, LiteralNode("day"))) | |
| def extract[R](e: Column[String])(implicit om: o#to[Int, R]) = | |
| om(Extract.column(n, Node(e))) | |
| /*def extract[P2](e: Column[P2])(implicit om: o#arg[JDATE, P2]#to[JDATE, Long]) = | |
| om(Library.-.column[JDATE](n, Node(e)))*/ | |
| } | |
| implicit def dateColumnExtensionMethods2(c: Column[DATE])(implicit tm: BaseTypeMapper[DATE]) = | |
| new DateColumnExtensionMethods2[DATE,DATE](c) | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment