Slick选择表的所有行而不进行过滤

时间:2017-04-07 15:19:00

标签: scala playframework slick

如何获取JurisdictionRow个对象的集合?我需要一个SELECT * FROM jurisdiction

object JurisdictionRepo extends {
  val profile = slick.driver.MySQLDriver
} with JurisdictionRepo

trait JurisdictionRepo {
  private val dbConfig: DatabaseConfig[MySQLDriver] = DatabaseConfig.forConfig("pnga-master-data")
  private val db = dbConfig.db

  val profile: slick.driver.JdbcProfile
  val tableName = "jurisdiction"

  def add(jurisdictionRow: JurisdictionRow): Future[Unit] = db.run(query += jurisdictionRow).map { _ => () }

  def delete(id: String): Future[Int] = db.run(query.filter(_.id === id).delete)

  def get(id: String): Future[Option[JurisdictionRow]] = db.run(query.filter(_.id === id).result.headOption)

def all() = ???

  import profile.api._

  lazy val schema: profile.SchemaDescription = query.schema

  case class JurisdictionRow(id: String,
                             parentId: String,
                             name: String,
                             code: String)

  class Jurisdiction(_tableTag: Tag) extends Table[JurisdictionRow](_tableTag, tableName) {
    val id: Rep[String] = column[String](s"${tableName}_id", O.PrimaryKey, O.Length(36, varying=true))
    val parentId: Rep[String] = column[String]("parent_id", O.Length(36, varying=true))
    val name: Rep[String] = column[String]("name", O.Length(255, varying=true))
    val code: Rep[String] = column[String]("code", O.Length(255, varying=true))

    def * = (id, parentId, name, code) <> (JurisdictionRow.tupled, JurisdictionRow.unapply _)
  }

  lazy val query = new TableQuery(tag => new Jurisdiction(tag))

}

我想实现all方法来返回表中所有可能的JurisdictionRow个对象。这似乎是一种常见的情况,但Slick文档没有帮助。我只需要一个简单的旧结果集,没有花哨的过滤等等。

1 个答案:

答案 0 :(得分:3)

只需复制您在其他查询中已有但没有过滤器部分的内容。

def all = db.run(query.result)

查看第一个示例: http://slick.lightbend.com/doc/3.2.0/gettingstarted.html#querying