更新时间:2023-12-03 19:47:22
TRIM(LEADING) 的稍微奇怪的语法意味着您必须放弃使用 SimpleExpression 并使用可让您访问的 QueryBuilder.
The slightly odd syntax of TRIM(LEADING means you have to drop to using a SimpleExpression and use the QueryBuilder that gives you access to.
val trimLeading = SimpleExpression.binary[String, String, String] {
(trimChar, str, queryBuilder) =>
import slick.util.MacroSupport._
import queryBuilder._
b"TRIM(LEADING $trimChar FROM $str)"
}
这是一个练习它的例子
import com.typesafe.config.ConfigFactory
import slick.backend.DatabaseConfig
import slick.driver.JdbcProfile
import scala.concurrent.duration.Duration
import scala.concurrent.Await
object TrimLeading extends App {
def trimLeadingExample(dbConfig: DatabaseConfig[JdbcProfile]): Unit = {
import dbConfig.driver.api._
val trimLeading = SimpleExpression.binary[String, String, String] {
(trimChar, str, queryBuilder) =>
import slick.util.MacroSupport._
import queryBuilder._
b"TRIM(LEADING $trimChar FROM $str)"
}
class ZeroTable(tag: Tag) extends Table[String](tag, "ZeroTable") {
def zeros = column[String]("zeros")
def * = zeros
}
val zeroTable = TableQuery[ZeroTable]
exec(zeroTable.schema.create)
exec(zeroTable ++= Seq("000000x", "00x", "000000000x", "00000x", "00xx"))
exec(zeroTable.
filter(s => trimLeading("0", s.zeros) === "x").
map(s => trimLeading("0", s.zeros)).result).foreach(println)
exec(zeroTable.schema.drop)
def exec[T](action: DBIO[T]): T = Await.result(dbConfig.db.run(action), Duration.Inf)
}
val configStr =
"""
| driver = "freeslick.OracleProfile$"
| db {
| driver = oracle.jdbc.OracleDriver
| url="jdbc:oracle:thin:@//localhost:49161/xe"
| properties = {
| databaseName = "freeslicktest"
| user = "system"
| password = "oracle"
| }
| }
""".stripMargin
trimLeadingExample(DatabaseConfig.forConfig[JdbcProfile]("", ConfigFactory.parseString(configStr)))
}
从日志中,语句是
*** (s.jdbc.JdbcBackend.statement) 准备语句: select TRIM(LEADING '0' FROM "zeros") from "ZeroTable" where TRIM(LEADING '0' FROM "zeros") ='x'