- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将异常查询转换为流畅的 one of Play 2.3 samples , 但我不确定如何实现动态排序。
这是原来的方法:
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
我如何在 slick 中执行“order by”部分,记住它是作为参数动态传递给方法的列名?
Scala 2.10.4/Play 2.3/Slick 2.0.2
下面由 Slick 代码生成器生成的表类:
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))
}
更新 - 解决方案 最终解决方案在 this question 中.
最佳答案
我的第一个答案是在正确的位置插入排序功能,但由于 Slick 的复杂输入而很快变得复杂。您可以使用 Slick 的查询组合根据所需的顺序直接修改查询,从而避免这些输入问题。
def list(page: Int = 0, pageSize: Int = 10, orderBy: Int = 1, filter: String = "%") = {
//..
val q = for {
(computer, company) <- Computer.where(_.name like filter) leftJoin
Company on (_.companyId === _.id)
} yield (computer, company.?)
val sortedQ = orderBy match {
case 1 => q.sortBy(_._1.id)
case 2 => q.sortBy(_._1.description)
// Others
}
val pagedQ = sortedQ.drop(page * pageSize).take(pageSize)
pagedQ.list
}
关于scala - Slick 中的动态查询参数(排序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24180017/
这是非常非常令人沮丧的。一段时间以来,我一直在尝试使用 Slick,但障碍不断涌现。 Slick 的概念真的很棒,但是很难学,而且不像Scala,它没有“初学者”、“中级”和“高级”风格,各个阶段的人
我在 Event 上定义了一个可选的外键,它转到 EventType。我想查询所有事件,即使是那些具有 None (null) 事件类型的事件。这是在 Event 上定义的外键。 def eventT
Another question通过在“一”案例类 (Directorate) 中定义一个返回“多”类的 Seq 的方法来回答如何定义一对多关联(服务区)。但它没有解决问题的“双向”部分。 使用该示例
我有一个向表中添加日期列的要求,默认值是由 oracle 的 sysdate 设置的,如下所示 例子{...CREATED_ON DATE 默认系统日期 我想使用 slick table ddl 创建
当我设置 speed: 1000 , pauseOnHover工作正常,但一旦我将其设置为 speed: 10000 , pauseOnHover不能立即工作 - 它在几秒钟后工作。 代码: $('#
我正在 try catch Slick 3.x 中的 SQL 错误。下面的代码不打印任何内容,但如果在调试下跟踪,它工作正常(它打印失败)。这段代码有什么问题? object TestSlick ex
我正在 try catch Slick 3.x 中的 SQL 错误。下面的代码不打印任何内容,但如果在调试下跟踪,它工作正常(它打印失败)。这段代码有什么问题? object TestSlick ex
我一直在使用 Slick's insertOrUpdate since its release in 2.1版本没有任何问题。 现在我正在尝试提高我的代码性能,而 insertOrUpdate 是瓶颈
我有一个列类型为日期的表。此列接受空值,因此,我将其声明为一个选项(请参阅下面的字段 perDate)。问题显然是从/到 java.time.LocalDate 的隐式转换/java.sql.Date
我试图在拖动幻灯片时向 a 标签添加“slick active”类。该类在单击时应用,但无法弄清楚如何将其应用到其他类! 如果能帮助破译我的代码,我们将不胜感激! JS $(document).rea
我有透明背景的 slider ,我想在悬停中心 div 时为 slider 背景制作动画,但在 slider 移动后,悬停效果会卡在所有以前具有“光滑中心”类的幻灯片上。任何建议如何解决它? Sp 到
我使用 Slick 3.1 代码生成器来创建默认对象和特征 Tables.scala 下面的方法有效,但我想隐式或显式地将 UserRow 和 PasswordsRow 转换为 User 和 User
我正在使用 Slick Slider 和 Slider Syncing 选项。是否可以滚动“缩略图”(示例中为 .slider-nav)而不更改主图像(示例中为 .slider-for)? 我只想在您
似乎 vue-slick 还没有提供任何事件(afterChange、beforeChange、Swipe...)。就像 jQuery 版本一样。 我应该使用 MutationObserver 来监听
问题 我正在 WordPress 网站上设置一个灵活的 slider /轮播。一切工作/显示都很完美,但是 slider 已停止正确拖动。 我仍然可以用鼠标物理拖动 slider ,它会按应有的方式进
我正在尝试使用 slick 库实现 slider 同步。我有一组来自后端的名为 pictures 的图像。我遍历这些图像,将它们填充到我的 slider-for 和 slider-nav div 下。
不知何故,我无法正确使用 slick carousel ( http://kenwheeler.github.io/slick/ )。 我收到以下错误: Uncaught TypeError: $(.
我正在使用 AOS 在滚动条上显示 html 元素。它单独运行良好,但当我在包含 Slick slider 的页面上使用它时,应用 AOS 的元素不显示。元素被隐藏,如果有很多滚动,看起来浏览器向 A
我正在使用 AOS 在滚动条上显示 html 元素。它单独运行良好,但当我在包含 Slick slider 的页面上使用它时,应用 AOS 的元素不显示。元素被隐藏,如果有很多滚动,看起来浏览器向 A
在我测试slick.js 前端 slider 插件时,我发现在Chrome 浏览器中,有时导航点在class ".slick-dots li button:before" 突然变了 来自 内容:'•'
我是一名优秀的程序员,十分优秀!