aselab / scala-activerecord

ActiveRecord-like ORM library for Scala

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Map custom database functions

vnicolici opened this issue · comments

Squeryl supports defining custom functions: http://squeryl.org/custom-functions.html

I tried to map the random() postgresql function in activerecord, and after looking at the source code for a few hours, I can't figure it out.

The random() function has no arguments, returns a double, and I have no idea how to define it.

I got as far as this:

  def random = new FunctionNode("random", Seq())

But when attempting to use it in a query doesn't compile:

type mismatch; found : org.squeryl.dsl.ast.FunctionNode required: org.squeryl.dsl.TypedExpression[?,?]

I have no idea how convert that FunctionNode to the required TypedExpression

Finally, I found the code for the count(*) function and adapted it. Ended up with this:

  import com.github.aselab.activerecord._
  import com.github.aselab.activerecord.dsl._

  import org.squeryl.dsl.TDouble
  import org.squeryl.dsl.ast.FunctionNode

  def random = new FunctionNode("random", Seq()) with TypedExpression[Double,TDouble] {
    def mapper = doubleTEF.createOutMapper
  }

This compiles and runs properly.