【问题标题】:case class filter criteria in Slick 3.0.3Slick 3.0.3 中的案例类过滤条件
【发布时间】:2015-10-08 15:50:54
【问题描述】:

我有一个模型、相应的表和一个存储库。在我的存储库中,使用 TableQuery 我想根据一些标准(从模型到布尔值的函数)查找模型对象,存储库无法控制它,它作为参数注入。例如

case class JournalEntryModel(id: Option[Long] = None, isDebit: Boolean, principal: Double)

class JournalEntryTable(tag: Tag) extends Table[JournalEntryModel](tag, "journal_entries") {

   def id = column[Long]("id", O.PrimaryKey, O.AutoInc)

   def isDebit = column[Boolean]("is_debit")  

   def principal = column[Double]("principal")

   def * = (id.?, isDebit, principal) <>
      (JournalEntryModel.tupled, JournalEntryModel.unapply)
}

object journalEntries extends TableQuery(new JournalEntryTable(_))

object Repository {

    def find(criteria: JournalEntryModel => Boolean): List[JournalEntryModel] = db.run {
         journalEntries.filter(je => criteria(je)).result            
    } toList
}

val credits = Repository.find(!_.isDebit && _.principal > 500.0)

这样的过滤函数怎么写?

【问题讨论】:

  • 您需要提供更多信息,说明您的问题中的解决方案为何不起作用。它没有编译还是根本没有返回正确的结果?

标签: scala design-patterns repository-pattern slick slick-3.0


【解决方案1】:

我认为您的问题是“我如何为 JournalEntryModel =&gt; Boolean 编写函数文字”?

如果你想用像你一样的文字来做,你需要定义你的参数:

// Parameter list ("model" here) is required. You also need the argument type(s).
// Here, they're inferred from the argument to "find".
val credits = Repository.find(model => !model.isDebit && model.principal > 500.0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    相关资源
    最近更新 更多