Scala Slick 模式特征中的 'override def *' 是什么
What is 'override def *' in Scala Slick schema trait
case class Fruit(
apple: String,
banana: String
)
trait AppleComponent { self: HasDatabaseConfig[JdbcProfile] =>
import profile.api._
class Fruits(tag: Tag) extends Table[Fruit](tag, "fruits") {
def apple = column[String]("apple")
def banana = column[String]("banana")
// what is this line of code used for?
override def * = (apple,banana) <> (Fruit.tupled, Fruit.unapply)
def applePK = primaryKey("apple", apple)
}
protected lazy val Apples = TableQuery[Fruits]
}
我是Scala Slick的新手,所以我想知道override def * = (apple,banana) <> (Fruit.tupled, Fruit.unapply)
是什么意思?我真的找不到任何关于它的文件。
另外,为什么我们在这里需要一个特征?
Table
class 有 *
方法应该实现 SELECT * FROM ...
语义。由于 Slick 无法猜测您要如何提取列,因此您必须手动编写(按您想要的顺序使用所有列)。
<>
只是为了让 *
返回一个案例 class 而不是一个元组。
这里不需要trait
。这个:
// module name
trait AppleComponent {
// self-type for dependency injection
self: HasDatabaseConfig[JdbcProfile] =>
// dependencies injected by mixing-in this trait:
protected lazy val Apples = TableQuery[Fruits]
}
叫做蛋糕图案。一般来说,它是一种反模式(但 ZIO 最近提倡一种 特定的 使用它的方式)。
我会说您使用的任何文档或教程都已经过时了几年。
case class Fruit(
apple: String,
banana: String
)
trait AppleComponent { self: HasDatabaseConfig[JdbcProfile] =>
import profile.api._
class Fruits(tag: Tag) extends Table[Fruit](tag, "fruits") {
def apple = column[String]("apple")
def banana = column[String]("banana")
// what is this line of code used for?
override def * = (apple,banana) <> (Fruit.tupled, Fruit.unapply)
def applePK = primaryKey("apple", apple)
}
protected lazy val Apples = TableQuery[Fruits]
}
我是Scala Slick的新手,所以我想知道override def * = (apple,banana) <> (Fruit.tupled, Fruit.unapply)
是什么意思?我真的找不到任何关于它的文件。
另外,为什么我们在这里需要一个特征?
Table
class 有 *
方法应该实现 SELECT * FROM ...
语义。由于 Slick 无法猜测您要如何提取列,因此您必须手动编写(按您想要的顺序使用所有列)。
<>
只是为了让 *
返回一个案例 class 而不是一个元组。
这里不需要trait
。这个:
// module name
trait AppleComponent {
// self-type for dependency injection
self: HasDatabaseConfig[JdbcProfile] =>
// dependencies injected by mixing-in this trait:
protected lazy val Apples = TableQuery[Fruits]
}
叫做蛋糕图案。一般来说,它是一种反模式(但 ZIO 最近提倡一种 特定的 使用它的方式)。
我会说您使用的任何文档或教程都已经过时了几年。