22 程序的列限制 [英] 22 Column limit for procedures

查看:19
本文介绍了22 程序的列限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Slick 调用过程时,我们如何克服 22 的限制?

How can we overcome the 22 limit when calling procedures with Slick?

我们目前有:

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

问题是我们必须返回超过 22 列,而 Transaction case 类不能超过 22 列,因为当我们执行 JSONFormat 时,我们会得到一个错误:

The problem is that we have to return more than 22 columns and Transaction case class cannot have more than 22 columns since when we do JSONFormat we get an error:

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

有什么建议吗?

推荐答案

好吧 - 所以如果你真的可以修改你的 Transaction 案例类,那么有一个比 HList 更好的解决方案>(说实话,以后操作起来可能有点麻烦).

Alright - so if you can actually modify your Transaction case class than there is a better solution than HList (which to be honest may be a little cumbersome to operate with later on).

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

So here is the thing: let's imagine you have User table with following attributes:

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

以上列可能没有意义,但让我们以它们为例.上面最直接的处理方式就是创建一个case类:

Above columns may not make sense but let's use them as example. The most straightforward way to deal with above is to create a case class:

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

这将从应用程序端的表映射.

which would be mapped from table on the application side.

现在你也可以这样做:

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)

这种组合将帮助您避免列过多的问题(这基本上是案例类/元组中属性过多的问题).除此之外 - 我会争辩说,如果您有很多列,这样做总是(经常?)有益的 - 如果只是为了可读性目的.

This composition will help you to avoid problem with too many columns (which is basically problem with too many attributes in your case class/tuple). Apart from that - I would argue that it is always (very often?) beneficial to do this if you have many columns - if for nothing else than simply for readability purposes.

如何进行映射

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 映射也可以做到这一点.

The same can be done with custom SQL mapping.

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))
)         

<小时>

所以简单地说 - 尝试将您的字段分成多个嵌套的案例类,您的问题应该会消失(增加可读性的好处).如果你这样做接近元组/案例类限制应该几乎永远不会成为问题(你甚至不需要使用 HList).

这篇关于22 程序的列限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆