anorm

anorm dynamic filters

牧云@^-^@ 提交于 2019-11-30 16:44:02
问题 I'm studying a little the anorm documentation (from play framework) and is not clear if it supports a common query use case: dynamic filters, that is the user fills in 2 or 3 search criteria on a 10 fields search form. In this case how can I dynamically construct the query without the classic string manipulation? 回答1: Yes, I think the question referenced by Robin Green contains the answer. Just define your query with all the possible criteria using placeholders (e.g. {criterion1} ) and call

How to Retrieve the Primary Key When Saving a New Object in Anorm

∥☆過路亽.° 提交于 2019-11-30 06:33:47
I'm using Scala Play! framework with Anorm to persist the data model to the database. I followed the example code here : case class Bar(id: Pk[Long], name: String) object Bar { val simple = { get[Pk[Long]]("id") ~ get[String]("name") map { case id~name => Bar(id, name) } } def findAll(): Seq[Bar] = { DB.withConnection { implicit connection => SQL("select * from bar").as(Bar.simple *) } } def create(bar: Bar): Unit = { DB.withConnection { implicit connection => SQL("insert into bar(name) values ({name})").on( 'name -> bar.name ).executeUpdate() } } } Trying to expand on it, I want to retrieve

Play + Anorm + Postgres - load json value into a case class

一笑奈何 提交于 2019-11-29 15:17:08
I am using anorm to query and save elements into my postgres database. I have a json column which I want to read as class of my own. So for example if I have the following class case class Table(id: Long, name:String, myJsonColumn:Option[MyClass]) case class MyClass(site: Option[String], user:Option[String]) I am trying to write the following update: DB.withConnection { implicit conn => val updated = SQL( """UPDATE employee |SET name = {name}, my_json_column = {myClass} |WHERE id = {id} """.stripMargin) .on( 'name -> name, 'myClass -> myClass, 'custom -> id ).executeUpdate() } } I also defined

How to Retrieve the Primary Key When Saving a New Object in Anorm

拥有回忆 提交于 2019-11-29 06:14:42
问题 I'm using Scala Play! framework with Anorm to persist the data model to the database. I followed the example code here : case class Bar(id: Pk[Long], name: String) object Bar { val simple = { get[Pk[Long]]("id") ~ get[String]("name") map { case id~name => Bar(id, name) } } def findAll(): Seq[Bar] = { DB.withConnection { implicit connection => SQL("select * from bar").as(Bar.simple *) } } def create(bar: Bar): Unit = { DB.withConnection { implicit connection => SQL("insert into bar(name)

Where to see the logged sql statements in play2?

牧云@^-^@ 提交于 2019-11-27 17:50:25
I found there is such a configuration in application.conf : # If enabled, log SQL statements being executed. db.default.logStatements=true I've enabled it, but I can't find any log file which logged executed sqls. Where can I find it, or do I miss something? 1. application.conf make sure: db.default.logStatements=true This config is actually a setting of bonecp which is connection pool used in play2 2. custom logger Add a custom logger configuration to conf/logger.xml . The content may be: <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%

PostgreSQL jsonb, `?` and JDBC

左心房为你撑大大i 提交于 2019-11-27 14:52:14
I am using PostgreSQL 9.4 and the awesome JSONB field type. I am trying to query against a field in a document. The following works in the psql CLI SELECT id FROM program WHERE document -> 'dept' ? 'CS' When I try to run the same query via my Scala app, I'm getting the error below. I'm using Play framework and Anorm, so the query looks like this SQL(s"SELECT id FROM program WHERE document -> 'dept' ? {dept}") .on('dept -> "CS") .... SQLException: : No value specified for parameter 5. (SimpleParameterList.java:223) (in my actual queries there are more parameters) I can get around this by

Anorm LIKE clause with String Interpolation

核能气质少年 提交于 2019-11-27 07:53:32
问题 Is it possible to use LIKE clause with String Interpolation in Anorm? // e.g. this doesn't work SQL"SELECT * FROM users WHERE last_name LIKE $lastName%".as(userParser.*) UPDATED : I need SQL statement that selects all users with a last name starting with given letters e.g. : SELECT * FROM users WHERE last_name LIKE 'Smi%'; 回答1: If expected WHERE clause is something like WHERE last_name LIKE '%pattern%' you will have to prepare string before passing it as argument. SQL"""SELECT * FROM users

Where to see the logged sql statements in play2?

只谈情不闲聊 提交于 2019-11-26 19:14:49
问题 I found there is such a configuration in application.conf : # If enabled, log SQL statements being executed. db.default.logStatements=true I've enabled it, but I can't find any log file which logged executed sqls. Where can I find it, or do I miss something? 回答1: 1. application.conf make sure: db.default.logStatements=true This config is actually a setting of bonecp which is connection pool used in play2 2. custom logger Add a custom logger configuration to conf/logger.xml . The content may

PostgreSQL jsonb, `?` and JDBC

一个人想着一个人 提交于 2019-11-26 16:56:09
问题 I am using PostgreSQL 9.4 and the awesome JSONB field type. I am trying to query against a field in a document. The following works in the psql CLI SELECT id FROM program WHERE document -> 'dept' ? 'CS' When I try to run the same query via my Scala app, I'm getting the error below. I'm using Play framework and Anorm, so the query looks like this SQL(s"SELECT id FROM program WHERE document -> 'dept' ? {dept}") .on('dept -> "CS") .... SQLException: : No value specified for parameter 5.