我目前正在编写Slick代码来定位一个带有两个表的旧模式> 22列。我如何use the new HList code?在Scala 2.10.3下,我在其他方面有2.0-M3正常工作。 这是我目前使用的案例类/元组的语法。我该怎么办use the new HLists mentioned in the docs?
case class Joiner(
id: Int,
name: Option[String],
contact: Option[String]
)
class Joiners(tag: Tag) extends Table[Joiner](tag, "joiner") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc, O.DBType("int(11)"))
def name = column[Option[String]]("name", O.DBType("varchar(255)"))
def contact = column[Option[String]]("contact", O.DBType("text"))
def * = (id, name.?, contact.?) <> (Joiner.tupled, Joiner.unapply)
}
val joiners = TableQuery[Joiners]
我在示例中没有看到任何内容,只是在最新更新的文档中提及了一下。我是Scala和Slick的新手。
答案 0 :(得分:10)
<强>定义强>
使用Scala&gt; = 2.10.4-RC2(也由Slick 2.0.0代码生成器发出):
import scala.slick.collection.heterogenous._
import syntax._
class Joiners(tag: Tag) extends Table[
Int :: Option[String] :: Option[String] :: HNil
](tag, "joiner") {
...
def * = id :: name :: contact :: HNil
}
以上导致Scala 2.10.3 / 2.10.4-RC1中的指数编译时间。由于编译时间过长,超过26列不可行。
Scala的解决方法&lt; = 2.10.3 / 2.10.4-RC1(也由Slick 2.0.1代码生成器发出)
import scala.slick.collection.heterogenous._
import syntax._
class Joiners(tag: Tag) extends Table[
HCons[Int, HCons[Option[String], HCons[Option[String], HNil]]]
](tag, "joiner") {
...
def * = id :: name :: contact :: HNil
}
我们用30-40列测试没有问题。
目前在Scala 2.10.4-RC2中偶尔出现零星的编译错误似乎仍然存在问题,看起来它将在即将发布的2.10.4-RC3中修复。见https://issues.scala-lang.org/browse/SI-8146
使用示例
Joiners.run.map( r => r(2) ) // Gets column contact. It's typesafe. .apply is a macro. Only works for literals not for variables as positions.
使用元组&lt; 22能够将它们映射到案例类。将HLists用于&gt; 22没有映射到案例类(Scala 2.10中的最大字段限制为22)。
另外:不要使用O.Nullable。请改用column[Option[String]]
。它推断出可空性。
答案 1 :(得分:1)
此代码用于演示当列数超过26时Scala v2.10.4_RC1中仍然影响编译器(它只是卡住)的性能问题
import java.sql.Timestamp
import scala.slick.driver.MySQLDriver.simple._
import scala.slick.collection.heterogenous._
// **** Uncomment this ****
//import scala.slick.collection.heterogenous.syntax._
object DealSlick {
class Deals(tag: Tag) extends Table[
Long :: String :: String :: Option[String] :: Option[String] :: Option[String] ::
// urlKeywords
Option[String] :: Option[String] :: Option[String] :: Option[String] :: Option[String] ::
// extTags
Option[String] :: Option[String] :: Option[String] :: Option[String] :: Option[String] ::
// currency
Option[String] :: Option[String] ::
// price
Option[Double] :: Option[Double] :: Option[Double] :: Option[Double] ::
// extStatus
Option[String] :: Option[String] :: Option[Int] :: Option[Int] ::
/* If you add more columns the compiler get stuck in a never-ending
* compilation possibly related to
* https://github.com/slick/slick/issues/577
*/
// endAt
Option[Timestamp] :: /*Option[Timestamp] :: Option[Timestamp] :: Option[Timestamp] ::
// timeZoneOffset
Option[Int] :: Option[String] :: Option[Timestamp] :: Option[String] ::
// locationName
Option[String] :: Option[String] :: Option[String] :: Option[String] ::
// city
Option[String] :: Option[String] :: Option[String] :: Option[String] :: Option[String] ::
// latitude
Option[Double] :: Option[Double] ::
// merchantTitle
Option[String] :: */
// End of list
HNil
](tag, "deal") {
def id = column[Long]("id", O.PrimaryKey)
def siteName = column[String]("partner_site_name", O.NotNull)
def siteDomain = column[String]("partner_site_domain", O.NotNull)
def localeLanguage = column[Option[String]]("deal_language")
def localeCountry = column[Option[String]]("deal_country")
def extId = column[Option[String]]("deal_ext_id")
def urlKeywords = column[Option[String]]("deal_url_keywords")
def keywords = column[Option[String]]("deal_keywords")
def extCategories = column[Option[String]]("deal_ext_categories")
def categoryText = column[Option[String]]("deal_category_text")
def coverage = column[Option[String]]("deal_coverage")
def extTags = column[Option[String]]("deal_ext_tags")
def title = column[Option[String]]("deal_title")
def description = column[Option[String]]("deal_description")
def extImage = column[Option[String]]("deal_ext_image")
def url = column[Option[String]]("deal_url")
def currency = column[Option[String]]("deal_currency")
def currencySym = column[Option[String]]("deal_currency_sym")
def price = column[Option[Double]]("deal_price")
def saving = column[Option[Double]]("deal_saving")
def discount = column[Option[Double]]("deal_discount")
def dvalue = column[Option[Double]]("deal_value")
def extStatus = column[Option[String]]("deal_ext_status")
def status = column[Option[String]]("deal_status")
def soldQty = column[Option[Int]]("deal_sold_qty")
def leftQty = column[Option[Int]]("deal_left_qty")
def endAt = column[Option[Timestamp]]("deal_end_at")
/* def endAtUtc = column[Option[Timestamp]]("deal_end_at_utc")
def expiresAt = column[Option[Timestamp]]("deal_expires_at")
def expiresAtUtc = column[Option[Timestamp]]("deal_expires_at_utc")
def timeZoneOffset = column[Option[Int]]("time_zone_offset")
def timeZoneName = column[Option[String]]("time_zone_name")
def timeGrabbed = column[Option[Timestamp]]("time_grabbed")
def timeRemainingStr = column[Option[String]]("time_remaining_str")
def locationName = column[Option[String]]("location_name")
def address = column[Option[String]]("location_address")
def street = column[Option[String]]("location_street")
def postalCode = column[Option[String]]("location_postalcode")
def city = column[Option[String]]("location_city")
def province = column[Option[String]]("location_province")
def region = column[Option[String]]("location_region")
def state = column[Option[String]]("location_state")
def country = column[Option[String]]("location_country")
def latitude = column[Option[Double]]("location_latitude")
def longitude = column[Option[Double]]("location_longitude")
def merchantTitle = column[Option[String]]("merchant_title")
*/
def * = (id :: siteName :: siteDomain :: localeLanguage :: localeCountry :: extId ::
urlKeywords :: keywords :: extCategories :: categoryText :: coverage ::
extTags :: title :: description :: extImage :: url ::
currency :: currencySym :: price :: saving :: discount :: dvalue ::
extStatus :: status :: soldQty :: leftQty ::
endAt :: /*endAtUtc :: expiresAt :: expiresAtUtc ::
timeZoneOffset :: timeZoneName :: timeGrabbed :: timeRemainingStr ::
locationName :: address :: street :: postalCode ::
city :: province :: region :: state :: country ::
latitude :: longitude ::
merchantTitle :: */
HNil )
}
}
在更新到Scala 2.10.4-RC2之后,编译器在编译过程中进一步发展,但它再次陷入困境:
这里是编译器输出,只定义了几个表列
[info] [加载类文件/Users/max/.ivy2/cache/com.typesafe.slick/slick_2.10/jars/slick_2.10-2.0.0.jar(scala/slick/backend/DatabaseComponent。 (1ms]
[info] [加载类文件/Users/max/.ivy2/cache/com.typesafe.slick/slick_2.10/jars/slick_2.10-2.0.0.jar(scala/slick/lifted/ShapedValue。在2ms]
[info] [2ms内加载的包装载工具] 当使用超过26列时,此输出永远不会在屏幕上打印