我陷入了以下问题。

我说一堂课,看起来像:

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]


我的问题是,按发布字段排序时,从数据库中获取上一个和下一个帖子。我遇到的问题是发布的字段是Option [Timestamp]。我创建了一个Squeryl查询,如下所示:

val nextPost = from(postTable)( p =>
      where((p.published > post.published) and p.isVisible === true)
      select(p)
      orderBy(p.published asc)
    ).page(0, 1)


当我查看生成的sql时,我看到的是这样的内容:“ ... WHERE post.published> Some(“ ....”)...“,这当然会导致SQL查询中出现语法错误。

我浏览了文档,但找不到答案。我已经在考虑切换到Slick ...

更新

squeryl mysql查询构造中存在一个确定的错误。我最终以

val x : Timestamp =  post.published.getOrElse(new Timestamp(0))
val nextPost = from(postTable)( p =>
  where((p.published.getOrElse(new Timestamp(0)) > x) and p.isVisible === true)
    select(p)
    orderBy(p.published asc)
).page(0, 1)


产生查询:

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 > 2013-08-01 14:21:25.0) and (Post9.isVisible = true))
Order By
  Post9.published Asc
limit 1 offset 0


看,查询构造函数如何格式化日期...

我正在切换到SLICK。

最佳答案

我认为这是因为您比较了时间戳记,而不是数据库对象。了解使用squeryl的区别至关重要。

因此,您应该改用:

p.published gte post.published
p.published.~ > post.published
p.published === post.published
p.published gt post.published


参考:

http://squeryl.org/schema-definition.html

http://squeryl.org/inserts-updates-delete.html

实际上是所有需要“少” /“大”的示例。

关于scala - Squeryl:如何比较where子句中的Option [T]对象?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18053393/

10-13 07:32