anorm

How to handle null in Anorm

江枫思渺然 提交于 2019-12-12 09:55:46
问题 I have a table with nullable column, and when query the null column, it threw error val row: List[(String,String)] = SQL("select top 10 Spare_Part part,Pricing_Category cat from Price_Point_Base") .as((str("part"))~ str("cat") map(flatten) *) I checked the link https://www.playframework.com/documentation/2.0/ScalaAnorm . It only gives away to handle nullable column using something like SQL("Select name,indepYear from Country")().map { row => row[String]("name") -> row[Option[Int]]("indepYear"

Anorm with Java 8 Date/Time API

房东的猫 提交于 2019-12-12 06:14:02
问题 I try to save timestamp field to postgresql using anorm. In scala code date/time stored as instance of java.time.Instant (someTime variable) DB.withConnection() { implicit c => SQL("INSERT INTO t_table(some_time) values ({someTime})").on('someTime -> someTime) } But anorm can't work with it now (Play 2.3.7). What is the best way to make it work? 回答1: I believe most people are using JodaTime so might explain why its missing. If its not part of Anorm you can write your own converter. This is

Anorm compare/search by java.time LocalDateTime

不羁岁月 提交于 2019-12-12 03:05:38
问题 Anorm 2.5.2 and java.time LocalDateTime as date val users = SQL( s"SELECT * FROM user WHERE name={name} AND registered_date={registeredDate}").on( "name" -> user.name, "registeredDate" -> user.registeredDate ).executeQuery().as(userParser.*) I found that even though I have a same name1 and same datetime(moment) in the database, the query returns empty users - can not find/match it . So it seems It can not compare by datetime. Why it could be? I was able to insert local-date-time correctly

Convert from MySQL query result or LIST to JSON

空扰寡人 提交于 2019-12-11 11:27:04
问题 I am using anorm with play-scala and have execute queries using: val result = SQL("SELECT * FROM users)() What I have been doing is the following: val queryResult = SQL(query)().map(result => result.asList ).toList However, I have realized that instead of converting it to a list, I want to actually receive JSON. Since I may change the query, I would like the ability for a JSON serializer that takes in any result and understands the column names and convert them and the results into JSON. EDIT

Anorm: WHERE condition, conditionally

狂风中的少年 提交于 2019-12-11 10:18:53
问题 Consider a repository/DAO method like this, which works great: def countReports(customerId: Long, createdSince: ZonedDateTime) = DB.withConnection { implicit c => SQL"""SELECT COUNT(*) FROM report WHERE customer_id = $customerId AND created >= $createdSince """.as(scalar[Int].single) } But what if the method is defined with optional parameters : def countReports(customerId: Option[Long], createdSince: Option[ZonedDateTime]) Point being, if either optional argument is present, use it in

Best way to pass the schema name as a variable to a query

一笑奈何 提交于 2019-12-11 06:53:19
问题 I have a PlayFramework server (with Anorm) which operates against a database with several schemas, all of them with the same tables. Most of my "access to database" functions look like: def findById(zoneName: String, id: Long): Option[Employee] = { DB.withConnection { implicit connection => SQL("""select * from """+zoneName+"""employee where employee._id = {id}""" .on( '_id -> id ).as(simpleParser.singleOpt) } } But I know this is a wrong approach, because it is not SQL-Injection-safe and of

Play Framework 2.0 correct way to represent a set in a query using Anorm

本秂侑毒 提交于 2019-12-10 23:28:20
问题 I am trying to return a list of results using Anorm using a query that returns matching rows for a set of ids. Eg. select * from example where id in (1,2,3,4,5) If I try SQL( """ select * from example where id in ({ids}) """ ).on('ids -> ids).as(int("id") ~ str("name") *) where ids is the String "1,2,3,4,5" it will only return the first row. What is the correct way to inject the set of ids? 回答1: There's no simple way of doing it AFAIK. This is how I solved it: def findSomething(ids: String) =

How to insert value of UUID?

一曲冷凌霜 提交于 2019-12-10 13:03:32
问题 I'm using anorm 2.4 in play framework 2.3 backed postgresql 9.4 Give a model like this: case class EmailQueue(id:UUID, send_from:String, send_to:String, subject:String, body:String, created_date:Date, is_sent:Boolean, email_template:String) This is my parser: val parser: RowParser[EmailQueue] = { get[UUID]("id") ~ get[String]("send_from") ~ get[String]("send_to") ~ get[String]("subject") ~ get[String]("body") ~ get[Date]("created_date") ~ get[Boolean]("is_sent") ~ get[String]("email_template"

Using Anorm RowParser

核能气质少年 提交于 2019-12-10 11:45:16
问题 I have been using Play framework 2.0 for about 6 months, I have been wondering why they use so many boilerplate code to parse from my SQL query returns, like below: case class Journal_accountDetail(amount: Double, states: Boolean) val Journal_AccountParser: RowParser[Journal_accountDetail] = { get[Double] ("amount") ~ get[Boolean] ("states") map({ case amount~states => Journal_accountDetail(amount,states) }) } Is it something that boost Play framework performance ?? 回答1: The parsing API can

How to better parse the same table twice with Anorm?

社会主义新天地 提交于 2019-12-10 02:50:01
问题 Suppose I have the following two classes: case class User(id: Long, name: String) case class Message(id: Long, body: String, to: User, from: User) The RowParser s might look something like this: val parser: RowParser[User] = { get[Long]("users.id") ~ get[String]("users.name") map { case id~name => User(id, name) } } val parser: RowParser[Message] = { get[Long]("messages.id") ~ get[String]("messages.name") ~ User.parser ~ User.parser map { case id~body~to~from => Message(id, body, to, from) }