폭풍우 쿼리를 one of Play 2.3 samples에 매끄럽게 변환하려고하는데 동적 정렬을 구현하는 방법을 모르겠습니다.Slick (정렬)의 동적 쿼리 매개 변수
이 원래 방법 : 지금까지 내가 지금까지 첫 번째 쿼리와 함께이있어
def list(page: Int = 0, pageSize: Int = 10, orderBy: Int = 1, filter: String = "%"): Page[(Computer, Option[Company])] = {
val offest = pageSize * page
DB.withConnection { implicit connection =>
val computers = SQL(
"""
select * from computer
left join company on computer.company_id = company.id
where computer.name like {filter}
order by {orderBy} nulls last
limit {pageSize} offset {offset}
"""
).on(
'pageSize -> pageSize,
'offset -> offest,
'filter -> filter,
'orderBy -> orderBy
).as(Computer.withCompany *)
val totalRows = SQL(
"""
select count(*) from computer
left join company on computer.company_id = company.id
where computer.name like {filter}
"""
).on(
'filter -> filter
).as(scalar[Long].single)
Page(computers, page, offest, totalRows)
}
}
:
val computers_ = (for {
(computer, company) <- Computer.where(_.name like filter) leftJoin
Company on (_.companyId === _.id)
} yield (computer, company.?)).list
나는 매끄러운에 참여 "에 의해 순서"를해야합니까 방법 매개 변수로 메서드에 동적으로 전달되는 열 이름을 염두에 둡니까?
스칼라 2.10.4/2.3/슬릭 2.0.2
테이블 아래 슬릭 코드 생성기에 의해 생성 된 클래스 재생 :
package tables
// AUTO-GENERATED Slick data model
/** Stand-alone Slick data model for immediate use */
object Tables extends {
val profile = scala.slick.driver.H2Driver
} with Tables
/** Slick data model trait for extension, choice of backend or usage in the cake pattern. (Make sure to initialize this late.) */
trait Tables {
val profile: scala.slick.driver.JdbcProfile
import profile.simple._
import scala.slick.model.ForeignKeyAction
// NOTE: GetResult mappers for plain SQL are only generated for tables where Slick knows how to map the types of all columns.
import scala.slick.jdbc.{GetResult => GR}
/** DDL for all tables. Call .create to execute. */
lazy val ddl = Company.ddl ++ Computer.ddl
/** Entity class storing rows of table Company
* @param id Database column ID PrimaryKey
* @param name Database column NAME */
case class CompanyRow(id: Long, name: String)
/** GetResult implicit for fetching CompanyRow objects using plain SQL queries */
implicit def GetResultCompanyRow(implicit e0: GR[Long], e1: GR[String]): GR[CompanyRow] = GR{
prs => import prs._
CompanyRow.tupled((<<[Long], <<[String]))
}
/** Table description of table COMPANY. Objects of this class serve as prototypes for rows in queries. */
class Company(tag: Tag) extends Table[CompanyRow](tag, "COMPANY") {
def * = (id, name) <> (CompanyRow.tupled, CompanyRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = (id.?, name.?).shaped.<>({r=>import r._; _1.map(_=> CompanyRow.tupled((_1.get, _2.get)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
/** Database column ID PrimaryKey */
val id: Column[Long] = column[Long]("ID", O.PrimaryKey)
/** Database column NAME */
val name: Column[String] = column[String]("NAME")
}
/** Collection-like TableQuery object for table Company */
lazy val Company = new TableQuery(tag => new Company(tag))
/** Entity class storing rows of table Computer
* @param id Database column ID PrimaryKey
* @param name Database column NAME
* @param introduced Database column INTRODUCED
* @param discontinued Database column DISCONTINUED
* @param companyId Database column COMPANY_ID */
case class ComputerRow(id: Long, name: String, introduced: Option[java.sql.Timestamp], discontinued: Option[java.sql.Timestamp], companyId: Option[Long])
/** GetResult implicit for fetching ComputerRow objects using plain SQL queries */
implicit def GetResultComputerRow(implicit e0: GR[Long], e1: GR[String], e2: GR[Option[java.sql.Timestamp]], e3: GR[Option[Long]]): GR[ComputerRow] = GR{
prs => import prs._
ComputerRow.tupled((<<[Long], <<[String], <<?[java.sql.Timestamp], <<?[java.sql.Timestamp], <<?[Long]))
}
/** Table description of table COMPUTER. Objects of this class serve as prototypes for rows in queries. */
class Computer(tag: Tag) extends Table[ComputerRow](tag, "COMPUTER") {
def * = (id, name, introduced, discontinued, companyId) <> (ComputerRow.tupled, ComputerRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = (id.?, name.?, introduced, discontinued, companyId).shaped.<>({r=>import r._; _1.map(_=> ComputerRow.tupled((_1.get, _2.get, _3, _4, _5)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
/** Database column ID PrimaryKey */
val id: Column[Long] = column[Long]("ID", O.PrimaryKey)
/** Database column NAME */
val name: Column[String] = column[String]("NAME")
/** Database column INTRODUCED */
val introduced: Column[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("INTRODUCED")
/** Database column DISCONTINUED */
val discontinued: Column[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("DISCONTINUED")
/** Database column COMPANY_ID */
val companyId: Column[Option[Long]] = column[Option[Long]]("COMPANY_ID")
/** Foreign key referencing Company (database name FK_COMPUTER_COMPANY_1) */
lazy val companyFk = foreignKey("FK_COMPUTER_COMPANY_1", companyId, Company)(r => r.id, onUpdate=ForeignKeyAction.Restrict, onDelete=ForeignKeyAction.Restrict)
}
/** Collection-like TableQuery object for table Computer */
lazy val Computer = new TableQuery(tag => new Computer(tag))
}
UPDATE - 최종 용액 this question에 용액.
당신의 노력에 감사 드리며, 지금은 정말 가깝습니다. 그러나 만약'yield (computer, company)'옵션을 사용하지 않으면,'SlickException : ResultSet 컬럼 경로에 대한 NULL 값 읽기 '가 발생합니다. 왼쪽 가입 회사의 경우 때때로 null이됩니다. 회사의 칼럼을 참조하고있는''yield (computer, company?) ','case 3 => c.sortBy (_._ 2.name)'옵션 회사를 떠나면''심볼 이름을 해석 할 수 없습니다' 이 시점에서 회사가 선택 사항이기 때문에 추측하고 있습니다 ... – Caballero
@Caballero 그래, 어리석은 짓이야! 나는 그 최종 문제를 극복하는 방법을 실제로 모르지만 ... 아마도 그게 다른 SO 질문에 좋은 재료입니까? – DCKing
도와 주셔서 감사합니다. 예, 이건 성가신 일입니다. 나는 Slick 팀의 누군가가 결국에 들어올 것을 희망한다. – Caballero