22 程序的列限制

22 Column limit for procedures

Slick 调用程序时如何突破 22 的限制?

我们目前有:

val q3 = sql"""call getStatements(${accountNumber})""".as[Transaction]

问题是我们必须 return 超过 22 列,而事务案例 class 不能超过 22 列,因为当我们执行 JSONFormat 时会出现错误:

[error] E:\IdeaProjects\admin\Transaction.scala:59: No unapply or unapplySeq function found
[error]   implicit val jsonFormat = Json.format[Transaction]

有什么建议吗?

好吧 - 所以如果你真的可以修改你的 Transaction 案例 class 比 HList 更好的解决方案(老实说操作起来可能有点麻烦稍后)。

事情是这样的:假设您有 User table 具有以下属性:

  • id
  • 名字
  • 姓氏
  • 教师
  • 最终成绩
  • 街道
  • 人数
  • 城市
  • 邮政编码

上面的列可能没有意义,但让我们以它们为例。处理上面最直接的方法是创建一个案例 class:

case class User(
   id: Long,
   name: String,
   ...  // rest of the attributes here
   postCode: String)

这将从应用程序端的 table 映射。

现在你还可以这样做:

case class Address(street: String, number: String, city: String, postCode: String)

case class UniversityInfo(faculty: String, finalGrade: Double)

case class User(id: Long, name: String, surname: String, uniInfo: UniversityInfo, address: Address)

此组合将帮助您避免列过多的问题(在您的案例中,这基本上是属性过多的问题 class/tuple)。 除此之外 - 我认为如果你有很多列,这样做总是(经常?)有益 - 如果只是为了提高可读性。

如何做映射

class User(tag: Tag) extends Table(tag, "User") {

  // cricoss info
  def id = column[Long]("id")
  def name = column[String]("name")

  // ... all the other fields
  def postCode = column[String]("postCode")

  def * = (id, name, surname, uniInfoProjection, addressProjection) <>((User.apply _).tupled, User.unapply)

  def uniInfoProjection = (faculty, finalGrade) <>((UniversityInfo.apply _).tupled, UniversityInfo.unapply)

  def addressProjection = (street, number, city, city) <>((Address.apply _).tupled, Address.unapply)
}

自定义 SQL 映射也可以做到这一点。

implicit val getUserResult = GetResult(r => 
    User(r.nextLong, r.nextString, r.nextString, 
         UniversityInfo(r.nextString, r.nextDouble),
         Adress(r.nextString, r.nextString, r.nextString, r.nextString))
)         

因此,简单来说 - 尝试将您的字段分成多个嵌套案例 classes,您的问题应该会消失(具有提高可读性的额外好处)。如果你这样做接近 tuple/case class 限制几乎永远不会成为问题(你甚至不需要使用 HList)。