Note that there are some explanatory texts on larger screens.

plurals
  1. POSqueryl: how to compare Option[T] objects in where clause?
    primarykey
    data
    text
    <p>I got stuck with the following problem.</p> <p>I have a class, say Post, that looks like:</p> <pre><code>case class Post ( id: Int, slug: String, title: String, @Column("postText") text: String, isVisible: Boolean, created: Timestamp, lastUpdated: Timestamp, published: Option[Timestamp] ) extends KeyedEntity[Int] </code></pre> <p>My problem is to get previous and next posts from the database, when the posts in ordered by <em>published</em> field. The problem I stuck with is that published field is Option[Timestamp]. I created a Squeryl query like this:</p> <pre><code>val nextPost = from(postTable)( p =&gt; where((p.published &gt; post.published) and p.isVisible === true) select(p) orderBy(p.published asc) ).page(0, 1) </code></pre> <p>And when I looked at the resulted sql I saw something like this: <em>"... WHERE post.published > Some("....") ..."</em> and of course this resulted in a syntax error in SQL query.</p> <p>I looked throgh the documentation, but cannot find the answer. I am already thinking of switching to Slick...</p> <p><strong>UPDATE</strong></p> <p>There is a definite bug in squeryl mysql query construction. I ended up with </p> <pre><code>val x : Timestamp = post.published.getOrElse(new Timestamp(0)) val nextPost = from(postTable)( p =&gt; where((p.published.getOrElse(new Timestamp(0)) &gt; x) and p.isVisible === true) select(p) orderBy(p.published asc) ).page(0, 1) </code></pre> <p>which produces query:</p> <pre><code>Select Post9.lastUpdated as Post9_lastUpdated, Post9.published as Post9_published, Post9.postText as Post9_postText, Post9.slug as Post9_slug, Post9.id as Post9_id, Post9.isVisible as Post9_isVisible, Post9.title as Post9_title, Post9.created as Post9_created From Post Post9 Where ((Post9.published &gt; 2013-08-01 14:21:25.0) and (Post9.isVisible = true)) Order By Post9.published Asc limit 1 offset 0 </code></pre> <p>see, how the query constructor formatted the date...</p> <p>I am switching to SLICK.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload