newbie question on sorting

251 views
Skip to first unread message

Adam Crain

unread,
Nov 13, 2012, 3:57:23 PM11/13/12
to scala...@googlegroups.com
Hi,

How would you all write this query? Bare with me here as I'm very new to SLICK. Here's a simplified representation of my schema:

object MetadataTable extends Table[(String, String, ...)]("METADATA") {
    def uuid = column[String]("uuid", O.PrimaryKey)
    def tag = column[String]("tag")
    ...
    def idxTag = index("idx_tag", tag, unique = true)
    def * = uuid ~ tag ~ ...
  }

I have obviously inserted ... where irrelevant non-keyed columns exist.

Now I want to retrieve all rows with a query generating function like the following:

def getAllMetadataByTagRange(start: Option[String], stop: Option[String], limit: Int, dir: ListDirection) = {
    val query = for { m <- MetadataTable } yield m.*
    val q1 = start.fold(query)(s => query.filter(_._2 >= s))
    val q2 = stop.fold(q1)(s => query.filter(_._2 < stop))
    val q3 = if(dir == ListDirection.LEFT) q2.sortBy(_._2) else q2.sortBy(_._2).desc
    q3.take(limit)
}

My first problem is that the 'desc' doesn't compile, it fails with:

error: could not find implicit value for evidence parameter of type scala.slick.lifted.TypeMapper[scala.slick.lifted.Query[scala.slick.lifted.Projection3[String,String,Option[String]],(String, String, Option[String])]]
if(dir == ListDirection.LEFT) q2.sortBy(_._2) else q2.sortBy(_._2).desc

How do I do the desc sort? How can I better write this parameterized query? 

To sum up the query in English:

Return all rows lexically between start (inclusive) and stop (exclusive)... If the start and stop are supplied. Sort and limit the returned rows.

thanks for your help!
Adam

Pradeep Kumar Mishra

unread,
Nov 13, 2012, 11:37:41 PM11/13/12
to scala...@googlegroups.com
Try this

val q3 = if(dir == ListDirection.LEFT) q2.sortBy(_._2) else q2.sortBy(_._2.desc)

Thanks,
Pradeep

Adam Crain

unread,
Nov 14, 2012, 9:05:11 AM11/14/12
to scala...@googlegroups.com
Perfect, thanks! I LOVE the succinctness and design of this library, but would also like to see the documentation and examples more fully flushed out. I'd volunteer, but my usage would probably be less than idiomatic.

Give that this is a product for Typesafe, what's the timeline like for investing a bit in the docs? Don't get me wrong. I'm 100% better off with Slick as it is than any of the Java ORMs I could be using.

-Adam

--
 
 
 

Reply all
Reply to author
Forward
0 new messages