简体   繁体   中英

Cannot reuse a function for quill SQL filtering

I have a case class Employee

 case class Employee(id: Int, 
                     name: String, 
                     age: Int, is_permanent: Boolean, 
                     company_name: String)

I am filtering on this Employee case class using Quill SQL like this:

quote {
  query[Employee]
    .filter(e => e.age > 100)
    .filter(e => liftQuery(List("Robin, Peter")).contains(e.name))
}

It compiles fine.

Now I want to put the second filter into a function and reuse it. Like

  val employeeQueryFunc: (Employee => Boolean) = e => {
    liftQuery(List("Robin, Peter")).contains(e.name)
  }

and then apply this function at multiple locations wherever I need. If I put this to the employee query like this

 quote {
  query[Employee]
    .filter(e => e.age > 100)
    .filter(employeeQueryFunc)
}

It does not compile. I get the error

  this.Employee]).filter(((e: FindAllIssuesRepoQuery.this.Employee) => e.age.> . 
  (100))).filter(employeeQueryFunc)' can't be parsed to 'Ast'

Ideally it should have compiled as the filter block also contains a function that returns a boolean and employeeQueryFunc also returns a boolean. Does anyone know how it can be reused as a function ??

had a similar issue - have experimented with Daniel's suggestion and in my case it was enough to rewrite

quote {
  query[Employee]
    .filter(e => e.age > 100)
    .filter(employeeQueryFunc)
}

into:

quote {
  query[Employee]
    .filter(e => e.age > 100)
    .filter(e => employeeQueryFunc(e))
}

(no idea why)

I had a similar problem when trying to extract a common logic to a private method. update the employeeQueryFunc to:

private def employeeQueryFunc = quote {
    (e: Employee) => liftQuery(List("Robin, Peter")).contains(e.Name)
}

and call this method the following way:

quote {
  query[Employee]
    .filter(e => e.age > 100)
    .filter(e => employeeQueryFunc(e))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM