Question

I'm using Squeryl with custom functions to do geo-spatial lookups, and the SQL produced is incorrect. I'm pulling my hair out trying to figure out what I did wrong. Here's a super simplified example of what I have:

I created the following custom function for the sine equation:

class SIN(e: NumericalExpression[Double], m:OutMapper[Double])
    extends FunctionNode[Double]("sin", Some(m), Seq(e)) with NumericalExpression[Double]

def sin(e: NumericalExpression[Double])(implicit m:OutMapper[Double]) = new SIN(e,m)   

Then I wrote a query that looks like this (obviously simplified for the sake of this test):

val query = from(StoreTable)(s =>
    select(s)
    orderBy(sin(s.latitude * Math.PI / 180))
)

Which produces the following SQL

Select ...
From
  store store14
Order By
  sin(store14.latitude)

What happened to the "Math.PI / 180" part in the order by? Upon digging a little further, I discovered the & function to evaluate the expression on the database side, which makes sense in this context, but using an & expression actually results in a NullPointerException. Here's what I did specifically:

val query = from(this.table)(s =>
    select(s)
    orderBy(sin(&(s.latitude * Math.PI / 180.0)))
)

And that results in the following exception:

[error]     NullPointerException: null (QueryDsl.scala:159)
[error] org.squeryl.internals.FieldReferenceLinker$.pushExpressionOrCollectValue(FieldReferenceLinker.scala:36)
[error] org.squeryl.dsl.QueryDsl$class.$amp(QueryDsl.scala:159)
[error] org.squeryl.PrimitiveTypeMode$.$amp(PrimitiveTypeMode.scala:40)

Can anyone tell me what I'm doing wrong?

Thanks in advance! Joe

Was it helpful?

Solution

Have you tried this :

val query = from(StoreTable)(s =>
    select(s)
    orderBy(sin((s.latitude times Math.PI) div 180))
)

?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top