- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个源自another question的问题.我需要能够动态传递要在具有左连接的 Slick 查询中进行排序的列。这种特殊情况下的问题是左连接表变得可选,我不知道如何处理。如果我使表 Company
不是可选的,我将得到 SlickException: Read NULL value for ResultSet column Path
例子:
def list(filter: String, orderBy: Int) = {
DB.withDynSession {
val data = for {
(computer, company) <- Computer.where(_.name like filter) leftJoin
Company on (_.companyId === _.id)
} yield (computer, company.?)
val sortedData = orderBy match {
case 2 => data.sortBy(_._1.name) //Works ok, column from a primary table
case 3 => data.sortBy(_._2.name) //Error "Cannot resolve symbol name", because table is optional
}
}
}
上面示例中使用的 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))
}
最佳答案
.?
使用 <>
实现这会阻止您以后访问成员。因此,您需要在执行 .?
之前应用排序
val data = for {
(computer, company) <- Computer.where(_.name like filter) leftJoin
Company on (_.companyId === _.id)
} yield (computer, company) // <- no .?
val sortedData = orderBy match {
case 2 => data.sortBy(_._1.name) //Works ok, column from a primary table
case 3 => data.sortBy(_._2.name) //Error "Cannot resolve symbol name", because table is optional
}
val optionalJoinData = sortedData.map{
case (computer, company) => (computer, company.?)
} // <- do .? last
关于scala - Slick:带左连接的查询中的动态 sortBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24190955/
我有一个向量列表。但现在我想使用 sortBy 函数按长度对这个向量列表进行排序。我已经拥有的是: import Data.List vectorLength::(Int,Int)->Float ve
我想按名称列对集合进行排序。使用 sortBy() 方法不起作用。 $collection = \App\Http\Resources\MyResource::collection($test
我有这个代码 $list = Elements::where('list_id', $id)->with('visitors')->get()->sortBy(function
我想知道为什么 Scala Set 没有 sortBy 方法,而 Seq 或 List 因为它也扩展了 Iterable... 最佳答案 Set 是一种有点有序(这就是它可迭代的原因)但未排序的元素集
它基本上显示来自mysql数据库的数据并使用 $sortby = $_GET['sort']; 我得到的错误是 Notice: Undefined index: sort in /home/45074
我有一个带有这个 ng-repeat 的表 ticket in filteredTickets = (vm.tickets | filter : vm.search | orderBy : vm.pr
HTML: Date Progress adminUsername 指令: .directive('reportrowgroup', function () { r
我正在尝试对对象进行排序,但似乎无法在排序后保留每个对象的键名称。 这是我正在排序的示例 json series_data: { embeded: { objectNameA: {
这是一个源自another question的问题.我需要能够动态传递要在具有左连接的 Slick 查询中进行排序的列。这种特殊情况下的问题是左连接表变得可选,我不知道如何处理。如果我使表 Compa
假设函数的输入是: [(6,-275),(3,123),(7,1000),(9,-572),(3,333),(1,-23)] 输出应该是: [(9,-572),(6,-275),(1,-23),(3,
好的,我有一个 Controller ,其模型设置如下: App.MyModel = DS.Model.extend({ myOtherProperty: attr(), mySort
我对 F# 还很陌生,使用了 Seq.sortBy 函数,但是它按升序对我的列表进行排序。如何使用 Seq.sort 按降序对其进行排序? 例如,示例代码将是... let DisplayList =
我使用 underscore 的 sortBy() 对字符串的 array 进行排序。但我想比其他字符串更早地对几个(硬编码的)字符串进行排序 - 我将向用户显示输出,并且将相关字符串放在前面会更自然
Haskell 的 sortBy 相当于什么?在 Coq 中? 总的来说,我发现 Coq 标准库的排序令人困惑。 我希望对排序列表进行一些“公理化”,以及不同排序的可用性,我可以为其提供排序功能。 然
我有一个带有子集合的集合,例如: + +--- name: 1a8 Lorem Ipsum + +--- name: 1a2 Lorem Ipsum + +--- name: 1a10 Lorem I
我正在使用 Primefaces + JSF 开发一个应用程序。我的数据表可以工作,但在排序 sortBy= 时出现问题,我尝试了 sortBy="#{pc.rota}" 但它也不起作用: 数据表显示
我想将 List.sortBy 接受的参数传递给另一个函数。我的代码看起来像这样: //Scala def sortAndMore(list: List[(String, _)], sortFn: O
我正在尝试根据每个元组中的第 4 个元素对我的元组列表进行排序。第四个元素包含一个人名字符串。我想将包含相同名称的元组放在一起。排序前的元组列表示例如下: [("A",100,"Q",3,"Todd"
我有一个带有子集合的集合,例如: + +--- name: 1a8 Lorem Ipsum + +--- name: 1a2 Lorem Ipsum + +--- name: 1a10 Lorem I
也许我需要将数组转换为真实日期才能使其工作?如果使用 moment 性能会成为问题,因为我还需要对数组进行排序。 const arr = [{ "created_at": "2017-07-19
我是一名优秀的程序员,十分优秀!