canned query that modifies parameter

22 views
Skip to first unread message

Matthew Pocock

unread,
Apr 23, 2015, 8:51:18 AM4/23/15
to scala...@googlegroups.com
Hi,

I'm writing a bunch of canned queries. Most of these I can wrap up with Compiled and a def with Rep[T] arguments. I'm stuck with this one:

def chebiByAccession(accNum: Rep[Int]) = for {
  compound <- chebi.Tables.Compounds
  if(compound.chebiAccession === s"chebi:$accNum")
} yield compound.id

val chebiByAccession = Compiled(chebiByAccession _)

val someChebi = db run chebiByAccession(15635).result

I'm not convinced that it's doing what I intended - I think the s"chebi:$accNum" expression is evaluating using Rep.toString. Is there a way to handle this?

Thanks,
Matthew

--
Dr Matthew Pocock
Turing ate my hamster LTD

Integrative Bioinformatics Group, School of Computing Science, Newcastle University

skype: matthew.pocock
tel: (0191) 2566550

virtualeyes

unread,
Apr 23, 2015, 12:53:42 PM4/23/15
to scala...@googlegroups.com
Compiled is just binding placeholders for the prepared statement; when you string interpolate you're forcing `accNum` to be evaluated at compile time, not as its runtime value.

Why not just pass in a Rep[String]?
def chebiByAccession(accNum: Rep[String]) =
  chebi
.Tables.Compounds.filter(_.chebiAccession === accNum).map(_.id)

Bit more boilerplate at call site, but the "chebi:" prefix requirement prevents the concise solution you're looking for (unless someone else has a better idea, that is...)

Naftoli Gugenheim

unread,
Apr 23, 2015, 3:49:41 PM4/23/15
to scala...@googlegroups.com

Perhaps something like LiteralColumn("chebi:") + accNum. That should force it to use the lifted + rather than string +.


--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/1362432a-9510-49ff-88d8-c6e0943a617c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages