scala - Squeryl:如何比较 where 子句中的 Option[T] 对象?

标签 scala squeryl

我遇到了以下问题。

我有一个类,比如 Post,它看起来像:

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/

相关文章:

scala - 如何使用 VS Code 和 Metals 运行现有的 Scala 项目?

inheritance - 实现不安全的 Java 接口(interface)

java - Spark 关闭问题中 Jackson ObjectNode 的 NotSerializableException

scala - Squeryl的where "java.util.Date does not take parameters"中出现错误 "clause"

mysql - DDL等价

使用 Squeryl 的持久存储中的 Scala 不变性

database - 在 Squeryl 中设置事务隔离级别

xml - 如何使用 scala Elem 生成 jersey text/xml?

scala - 使用返回 future 的函数遍历列表和流