|
创建表查询时,我想通过映射默认表查询来修改我的select语句。但是,我找不到映射列值,仍然映射到我的案例类方法 B' \0 f1 D8 R) P5 k# r$ J4 g( M# y. Z+ ^
case class MyRecord(id: Int,name: String,value: Int)class MyTable(tag: Tag) extends Table[MyRecord](tag,"MYTABLE") def id = column[Int]("id") def name = column[String]("name") def value = column[Int]("value") def * = (id,name,value) (MyRecord.tupled,MyRecord.unapply) }lazy val tableQuery = TableQuery[MyTable]我想name用此函数调整值:. ?* L0 [: m/ Z# W
def trimLeading0: (Rep[String]) => Rep[String] = SimpleExpression.unary[String,String] (str,queryBuilder) => import slick.util.MacroSupport._ import queryBuilder._ b"TRIM(LEADING 0 FROM $str)" }现在,我对这里的操作一无所知:, h. o) ?) y2 f+ d i* _; b9 r
val trimmedTableQuery: Query[MyTable,MyRecord,Seq] = tableQuery.map(s => ???)我试过映射,Rep就像处理case类一样:
+ @4 Q9 A6 P* B6 x& u4 K9 y1 h" dval trimmedTableQuery = tableQuery.map(s => s.copy(name = trimLeading0(s.name)))这拒绝与 value copy is not a member of MyTable
2 r* J9 A P2 l4 [" Q8 W: f2 c我目前的解决方案是使用自定义函数,而不是使用MyRecord.tupled默认投影:
* k; K, \6 p2 p5 ~- S- \def trimming(t: (Int,String,Int)) = MyRecord(t._1,t._2.dropWhile(_ == "0"),t._3)def * = (id,name,value) (trimming,MyRecord.unapply)另外,我可以回去DBIOAction元组的返回结果映射到case类,这不是很优雅:0 E5 l6 D0 h5 D' N' h9 w
val action = tableQuery.map{ s => (s.id,trimLeading0(s.name),s.value)}.resultval futureTuples: Future[Seq[(Int,String,Int)]] = db.run(action)val records = futureTuples map (s => s.map(MyRecord.tupled))但是map构建查询时如何在方法内部操作?还是更改?def name列表会更好吗?0 U- h) j2 V* K5 A
6 m! |) z; ` x3 N$ I
解决方案: |
|