I am just trying to add a row from slick to my postgreSQL Database.
Here what I am trying to do :
val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
import dbConfig.driver.api._
val query = Task += new TaskRow(5, "taskName", status = "other")
println(Task.insertStatement)
val resultingQuery = dbConfig.db.run(query).map(res => "Task successfully added").recover {
case ex: Exception => ex.getCause.getMessage
}
Here the result of println :
insert into "task" ("taskname","description","status","datetask","pediocitynumber","periodicitytype") values (?,?,?,?,?,?)
I don't have any result exception or success from the resulting query.
Code generate by slick-codegen 3.1.1 :
case class TaskRow(taskid: Int, taskname: String, description: Option[String] = None, status: String, datetask: Option[java.sql.Timestamp] = None, pediocitynumber: Option[Int] = None, periodicitytype: Option[String] = None)
/** GetResult implicit for fetching TaskRow objects using plain SQL queries */
implicit def GetResultTaskRow(implicit e0: GR[Int], e1: GR[String], e2: GR[Option[String]], e3: GR[Option[java.sql.Timestamp]], e4: GR[Option[Int]]): GR[TaskRow] = GR{
prs => import prs._
TaskRow.tupled((<<[Int], <<[String], <<?[String], <<[String], <<?[java.sql.Timestamp], <<?[Int], <<?[String]))
}
/** Table description of table task. Objects of this class serve as prototypes for rows in queries. */
class Task(_tableTag: Tag) extends Table[TaskRow](_tableTag, "task") {
def * = (taskid, taskname, description, status, datetask, pediocitynumber, periodicitytype) <> (TaskRow.tupled, TaskRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = (Rep.Some(taskid), Rep.Some(taskname), description, Rep.Some(status), datetask, pediocitynumber, periodicitytype).shaped.<>({r=>import r._; _1.map(_=> TaskRow.tupled((_1.get, _2.get, _3, _4.get, _5, _6, _7)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
/** Database column taskid SqlType(serial), AutoInc, PrimaryKey */
val taskid: Rep[Int] = column[Int]("taskid", O.AutoInc, O.PrimaryKey)
/** Database column taskname SqlType(text) */
val taskname: Rep[String] = column[String]("taskname")
/** Database column description SqlType(text), Default(None) */
val description: Rep[Option[String]] = column[Option[String]]("description", O.Default(None))
/** Database column status SqlType(statustask) */
val status: Rep[String] = column[String]("status")
/** Database column datetask SqlType(timestamp), Default(None) */
val datetask: Rep[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("datetask", O.Default(None))
/** Database column pediocitynumber SqlType(int4), Default(None) */
val pediocitynumber: Rep[Option[Int]] = column[Option[Int]]("pediocitynumber", O.Default(None))
/** Database column periodicitytype SqlType(periodicitytype), Default(None) */
val periodicitytype: Rep[Option[String]] = column[Option[String]]("periodicitytype", O.Default(None))
}
/** Collection-like TableQuery object for table Task */
lazy val Task = new TableQuery(tag => new Task(tag))
Could someone explain what I am doing wrong?
EDIT:
To clarify my question :
The row is not added to the table, it seems that nothing happen. I can't see any exception or error thrown.
I think it could come from the sql statement and the questions marks (values (?,?,?,?,?,?)). It should be the actual values of the fields, right?
More code:
class Application #Inject()(dbConfigProvider: DatabaseConfigProvider) extends Controller {
def index = Action {
Ok(views.html.main())
}
def taskSave = Action.async { implicit request =>
println(request.body.asJson)
val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
import dbConfig.driver.api._
val query = Task += new TaskRow(5, "taskName", status = "other")
println(Task.insertStatement)
println(query)
val resultingQuery = dbConfig.db.run(query).map(res => "TAsk successfully added").recover {
case ex: Exception => ex.getCause.getMessage
}
resultingQuery.map(r => println("result : " + r))
Future(Ok(""))
}
}
route :
PUT /task-save controllers.Application.taskSave
The statement is not wrong because of the values (?,?,? ...).
The lazy val Task is of type TableQuery[...] . When you call insertStatement on this object you get back a string that represents the template SQL string that runs in the background. That is expected.
So that is not the indicator of the problem.
Try something like this :
val db = Database.forConfig("h2mem1")
try {
Await.result(db.run(DBIO.seq(
// create the schema
Task.schema.create,
// insert two User instances
Task += (2, "abcd", "201-01-01"),
// print the users (select * from USERS)
Task.result.map(println))), Duration.Inf)
} finally db.close
Related
I'm trying to get a hang of Slick by doing a small test.
I'm trying to do an insert. The test runs, no errors, but when I check the db, no record has been inserted.
What am I doing wrong?
Here's my test code:
Note: I disabled the first 'flatMap' because when I wanted to test the second insert method, and that code was not executed when the first flatmap function was enabled.
Both insert methods do not insert a new record.
The first query for all items does work. The 'Test id:xx' lines are printed to console.
object TestSlick extends App {
import slick.driver.PostgresDriver.api._
import concurrent.ExecutionContext.Implicits.global
import concurrent.duration._
val config = ConfigFactory.load()
val username = config.getString("app.database.jdbc.username")
val password = config.getString("app.database.jdbc.password")
val url: String = config.getString("app.database.jdbc.url")
val db = Database.forURL(url, username, password)
try {
import Tables._
val res = db.run(headlines.result).map(_.foreach {
case HeadLineRow(id, _, _, _, _, companyId, text, from, days, end, user) =>
println(s"Test id:$id")
}).flatMap { _ =>
// println("Inserting....")
// val ts = Timestamp.valueOf(LocalDateTime.now())
// val insertAction: DBIO[Option[Int]] = (headlines returning headlines.map(_.id)) +=
// HeadLineRow(None, 100, 100, "tekst", ts, 5, ts, None, None, None, None)
//
// db.run(insertAction.transactionally.map(
// newId => println(s"New id: $newId"))
// )
// }.flatMap { _ =>
println("Inserting....(2)")
val ts = Timestamp.valueOf(LocalDateTime.now())
val insertAction = headlines.map(p => p) += HeadLineRow(None, 1921, 65, "tekst2", ts, 5, ts, None, None, None, None)
db.run(insertAction.transactionally.map(
r => println(s"Insert result: ${r}"))
)
}
Await.ready(res, 30 seconds);
} finally db.close()
}
And my table (generated using Slick's generator and then adjusted a bit (auto-inc id, swapped some properties around))
package com.wanneerwerkik.db.slick
// AUTO-GENERATED Slick data model
/** Stand-alone Slick data model for immediate use */
object Tables extends {
val profile = slick.driver.PostgresDriver
} 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: slick.driver.JdbcProfile
import profile.api._
import slick.model.ForeignKeyAction
import slick.collection.heterogeneous._
import slick.collection.heterogeneous.syntax._
// NOTE: GetResult mappers for plain SQL are only generated for tables where Slick knows how to map the types of all columns.
import slick.jdbc.{GetResult => GR}
/** DDL for all tables. Call .create to execute. */
lazy val schema = Array(headlines.schema).reduceLeft(_ ++ _)
#deprecated("Use .schema instead of .ddl", "3.0")
def ddl = schema
/**
* Entity class storing rows of table 'head_line_bar'
* #param id Database column id SqlType(int4), PrimaryKey
* #param createdBy Database column created_by SqlType(int4), Default(None)
* #param createdOn Database column created_on SqlType(timestamp), Default(None)
* #param updatedBy Database column updated_by SqlType(int4), Default(None)
* #param updatedOn Database column updated_on SqlType(timestamp), Default(None)
* #param companyId Database column company_id SqlType(int4), Default(None)
* #param contentType Database column content_type SqlType(varchar), Length(255,true), Default(None)
* #param fromDate Database column from_date SqlType(timestamp), Default(None)
* #param numberofdays Database column numberofdays SqlType(int4), Default(None)
* #param uptoEndDate Database column upto_end_date SqlType(timestamp), Default(None)
* #param userId Database column user_id SqlType(int4), Default(None)
*/
case class HeadLineRow(
id: Option[Int],
userId: Int,
companyId: Int,
contentType: String,
fromDate: java.sql.Timestamp,
numberofdays: Int,
uptoEndDate: java.sql.Timestamp,
createdBy: Option[Int] = None,
createdOn: Option[java.sql.Timestamp] = None,
updatedBy: Option[Int] = None,
updatedOn: Option[java.sql.Timestamp] = None
)
/** GetResult implicit for fetching HeadLineBarRow objects using plain SQL queries */
implicit def GetResultHeadLineRow(implicit e0: GR[Int], e1: GR[Option[Int]], e2: GR[Option[java.sql.Timestamp]], e3: GR[Option[String]]): GR[HeadLineRow] = GR{
prs => import prs._
HeadLineRow.tupled((<<?[Int], <<[Int], <<[Int], <<[String], <<[java.sql.Timestamp], <<[Int], <<[java.sql.Timestamp], <<?[Int], <<?[java.sql.Timestamp], <<?[Int], <<?[java.sql.Timestamp]))
}
/**
* Table description of table head_line_bar.
* Objects of this class serve as prototypes for rows in queries.
*/
class Headlines(_tableTag: Tag) extends Table[HeadLineRow](_tableTag, "head_line_bar") {
def * = (id, userId, companyId, contentType, fromDate, numberofdays, uptoEndDate, createdBy, createdOn, updatedBy, updatedOn) <> (HeadLineRow.tupled, HeadLineRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = (Rep.Some(id), userId, companyId, contentType, fromDate, numberofdays, uptoEndDate, createdBy, createdOn, updatedBy, updatedOn).shaped.<>({r=>import r._; _1.map(_=> HeadLineRow.tupled((_1.get, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
/** Database column id SqlType(int4), PrimaryKey */
val id: Rep[Option[Int]] = column[Option[Int]]("id", O.PrimaryKey, O.AutoInc)
/** Database column user_id SqlType(int4), Default(None) */
val userId: Rep[Int] = column[Int]("user_id")
/** Database column company_id SqlType(int4), Default(None) */
val companyId: Rep[Int] = column[Int]("company_id")
/** Database column content_type SqlType(varchar), Length(255,true), Default(None) */
val contentType: Rep[String] = column[String]("content_type", O.Length(255,varying=true))
/** Database column from_date SqlType(timestamp), Default(None) */
val fromDate: Rep[java.sql.Timestamp] = column[java.sql.Timestamp]("from_date")
/** Database column numberofdays SqlType(int4), Default(None) */
val numberofdays: Rep[Int] = column[Int]("numberofdays")
/** Database column upto_end_date SqlType(timestamp), Default(None) */
val uptoEndDate: Rep[java.sql.Timestamp] = column[java.sql.Timestamp]("upto_end_date")
/** Database column created_by SqlType(int4), Default(None) */
val createdBy: Rep[Option[Int]] = column[Option[Int]]("created_by", O.Default(None))
/** Database column created_on SqlType(timestamp), Default(None) */
val createdOn: Rep[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("created_on", O.Default(None))
/** Database column updated_by SqlType(int4), Default(None) */
val updatedBy: Rep[Option[Int]] = column[Option[Int]]("updated_by", O.Default(None))
/** Database column updated_on SqlType(timestamp), Default(None) */
val updatedOn: Rep[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("updated_on", O.Default(None))
}
/** Collection-like TableQuery object for table HeadLineBar */
lazy val headlines = new TableQuery(tag => new Headlines(tag))
}
Log output is too big to paste here so I put it in this gist.
As suggested I added a readLine to wait for the result, but it was already output the same stuff. I also added a completion handler on the Future to print it's Success or Failure. Apparently it fails with a RejectedExecutionException. Why?
Failure: java.util.concurrent.RejectedExecutionException: Task slick.backend.DatabaseComponent$DatabaseDef$$anon$2#2e4db0df rejected from java.util.concurrent.ThreadPoolExecutor#43760a50[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1]
This is just a guess, but maybe your testing framework is somehow confused by the fact that flatMap internally spawns a new task, requiring again the execution context (see e.g. this thread - it's about Scala 2.10 but I think this hasn't changed). So the resources are freed before your insert executes.
Have you tried putting a println in the finally block, to see if this is called before or after the message accompanying the insert?
Have you tried running both futures synchronously, using await? Likely you will not get this issue in this case.
You may consider testing with full asynchronous support, see e.g. this link.
This is a problem derived from another question. I need to be able to dynamically pass a column to be sorted on in a Slick query which has a left join. The problem in this particular situation is that left joined table becomes optional and I have no idea how to handle that. If I make table Company not optional I'm getting SlickException: Read NULL value for ResultSet column Path
Example:
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 auto generated table classes used in example above:
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))
}
.? is implemented using <> which prevents you from later accessing members. So you need to apply the sorting before you do the .?
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
I'm trying to convert anorm queries to slick in one of Play 2.3 samples, but I'm not sure how to implement dynamic sorting.
This is the original method:
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)
}
}
So far I've got this far with the first query:
val computers_ = (for {
(computer, company) <- Computer.where(_.name like filter) leftJoin
Company on (_.companyId === _.id)
} yield (computer, company.?)).list
How do I do the "order by" part in slick, bearing in mind it's a column name passed to the method dynamically as a parameter?
Scala 2.10.4 / Play 2.3 / Slick 2.0.2
Table classes generated by Slick code generator below:
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 - SOLUTION The final solution is in this question.
My first answer plugs in the sorting function at the right place, but quickly grows complicated because of Slick's complicated typing. You can avoid these typing issues by using Slick's query composition to modify the query directly based on the desired ordering.
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
}
The difference between Slick and Anorm is that Slick's queries are checked by the Scala compiler. Implementing such a dynamic parameter takes a bit more effort in Slick, but you get type safety in return. It is made particularly cumbersome to do in this case since your query ordering is a join of multiple tables.
In general, it should look roughly like this:
def orderings(code: Int): ((Computer, Company)) => Column[_] = {
code match {
case 1 => _._1.id
case 2 => _._1.description
// Other orderings
}
)
def list(page: Int = 0, pageSize: Int = 10, orderBy: Int = 1, filter: String = "%") = {
//..
val computers_ = (for {
(computer, company) <- Computer.where(_.name like filter) leftJoin
Company on (_.companyId === _.id)
} yield (computer, company.?))
.sortBy(orderings(orderBy).nullsLast)
.drop(page * pageSize)
.take(pageSize)
.list
//..
}
The general idea to map the integers you receive to the Slick columns on which you want to sort is the answer to your question.
Not sure if this is the best idea in the world but you could technically use shapeless to help you get a numbered tuple element, this will obviously be at the cost of compile-time safety. First convert the Company case class into a tuple with Company.unapply and then use shapeless's at(N) method (note that it's a 0-based index). Here's what that would look like:
def list(page: Int = 0, pageSize: Int = 10, orderBy: Int = 1, filter: String = "%") = {
//..
val computers_ = (for {
(computer, company) <- Computer.where(_.name like filter) leftJoin
Company on (_.companyId === _.id)
} yield (computer, company.?))
.sortBy(Company.unapply(_._1).get.at(orderBy-1).nullsLast)
.drop(page * pageSize)
.take(pageSize)
.list
//..
}
In order to do this you will need shapeless:
<dependency>
<groupId>com.chuusai</groupId>
<artifactId>shapeless_2.11</artifactId>
<version>2.3.1</version>
</dependency>
...and the following import:
import shapeless.syntax.std.tuple._
Use this technique at your own risk.
I am trying to start using slick. I have a h2-database and generated classes with scala.slick.model.codegen.SourceCodeGenerator. But when try I try following the examples and query my db using these classes I get scala-errors.
The generated code looks as follows:
/** Entity class storing rows of table User
* #param id Database column ID PrimaryKey
* #param firstname Database column FIRSTNAME
* #param lastname Database column LASTNAME */
case class UserRow(id: String, firstname: Option[String], lastname: Option[String])
/** GetResult implicit for fetching UserRow objects using plain SQL queries */
implicit def GetResultUserRow(implicit e0: GR[String], e1: GR[Option[String]]): GR[UserRow] = GR{
prs => import prs._
UserRow.tupled((<<[String], <<?[String], <<?[String]))
}
/** Table description of table USER. Objects of this class serve as prototypes for rows in queries. */
class User(tag: Tag) extends Table[UserRow](tag, "USER") {
def * = (id, firstname, lastname) <> (UserRow.tupled, UserRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = (id.?, firstname, lastname).shaped.<>({r=>import r._; _1.map(_=> UserRow.tupled((_1.get, _2, _3)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
/** Database column ID PrimaryKey */
val id: Column[String] = column[String]("ID", O.PrimaryKey)
/** Database column FIRSTNAME */
val firstname: Column[Option[String]] = column[Option[String]]("FIRSTNAME")
/** Database column LASTNAME */
val lastname: Column[Option[String]] = column[Option[String]]("LASTNAME")
}
/** Collection-like TableQuery object for table User */
lazy val User = new TableQuery(tag => new User(tag))
And this is my query:
val userResultList = for {
u <- User if u.id === "foo"
} yield u
which results in:
Error:(137, 29) value === is not a member of db.Tables.profile.simple.Column[String]
u <- User if u.id === user.id
^
What's wrong?
For slick 2.x, just import XXXDriver.simple._ and the compiler will be happy.
For slick 3.x, its XXXDriver.api._
I can't seem to understand how to make a simple insert into a Database using Slick. I'm using tables generator with my Oracle db and I'm getting something like this:
case class SimulatonRow(id: scala.math.BigDecimal, startDate: Option[java.sql.Timestamp], endDate: Option[java.sql.Timestamp], numberOfProc: Option[scala.math.BigDecimal], code: String)
/** GetResult implicit for fetching SimulatonRow objects using plain SQL queries */
implicit def GetResultSimulatonRow(implicit e0: GR[scala.math.BigDecimal], e1: GR[Option[java.sql.Timestamp]], e2: GR[Option[scala.math.BigDecimal]], e3: GR[String]): GR[SimulatonRow] = GR{
prs => import prs._
SimulatonRow.tupled((<<[scala.math.BigDecimal], <<?[java.sql.Timestamp], <<?[java.sql.Timestamp], <<?[scala.math.BigDecimal], <<[String]))
}
class Simulaton(tag: Tag) extends Table[SimulatonRow](tag, Some("BPRISK"), "SIMULATON") {
def * = (id, startDate, endDate, numberOfProc, code) <> (SimulatonRow.tupled, SimulatonRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = (id.?, startDate, endDate, numberOfProc, code.?).shaped.<>({r=>import r._; _1.map(_=> SimulatonRow.tupled((_1.get, _2, _3, _4, _5.get)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
/** Database column ID PrimaryKey */
val id: Column[scala.math.BigDecimal] = column[scala.math.BigDecimal]("ID", O.PrimaryKey, O.AutoInc)
/** Database column START_DATE */
val startDate: Column[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("START_DATE")
/** Database column END_DATE */
val endDate: Column[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("END_DATE")
/** Database column NUMBER_OF_PROC */
val numberOfProc: Column[Option[scala.math.BigDecimal]] = column[Option[scala.math.BigDecimal]]("NUMBER_OF_PROC")
/** Database column CODE */
val code: Column[String] = column[String]("CODE")
}
Then I add AutoInc option to the primary key.
I tried variations of 'insert' with TableQuery[Simulaton].map or InsertInvoker, but the code cannot be compiled.
How does it work?
I need to make an 'insert' with autoincremental PK and columns - code, startDate.
// import driver specific stuff
import MySQLDriver.simple._
// import generated code
import Tables._
// define db connection
val db = Database.for...
// create a connection
db.withSession{ implicit session =>
// select what should be inserted into
TableQuery[Simulaton].map(s => (s.code,s.startDate))
// insert instead or running the query
.insert( ("some code",Some(DateTime.now)) )
}