anorm

Pattern matching on Stream, Option and creating a Tuple

给你一囗甜甜゛ 提交于 2019-12-24 22:28:34
问题 I have a table table1 : id: Int externalId: Int value: String For a given externalId value can be NULL or it might not exist at all. I want to return a tuple depending on this condition: If it doesn't exist then return ("notExists", Nil) If it exists but NULL then return ("existsButNull", Nil) If it exists and not NULL then return ("existsWithValue", ???) where ??? should be a list of value for all records with the given externalId . I tried to do this: DB.withConnection { implicit c => SQL(

Using `Option[BigDecimal]` with Anorm?

亡梦爱人 提交于 2019-12-24 05:51:36
问题 Given: build.sbt $cat build.sbt scalaVersion := "2.11.8" libraryDependencies += "com.typesafe.play" % "anorm_2.11" % "2.5.1" REPL import anorm._ val x: Option[BigDecimal] = Some(42) val none: Option[BigDecimal] = None // invoke Oracle function `f` scala> SQL""" select f(#$x) from dual """.sql.statement res0: String = " select f(Some(42)) from dual " scala> SQL""" select f(#$none) from dual """.sql.statement res1: String = " select f(None) from dual " I'd like for res0 to equal: select f(42)

object User extends Magic[User]().using(“users”) can not compiled

孤街浪徒 提交于 2019-12-24 02:43:07
问题 When using Anorm I want to use a different table name for the User case class: object User extends Magic[User]().using("users") But I get the following compilation error: The file /app/models/User.scala could not be compiled. Error raised is : ';' expected but '.' found. object User extends MagicUser↓.using("users") Is this a bug of Anorm? 回答1: Clearly not a bug, your code is not valid scala. You could do that instead : lazy val User = new Magic[User].using("users") (the convention would be

How to Resolve 'not found: object anorm' for play 2.4.3

Deadly 提交于 2019-12-23 21:15:41
问题 I'm using play 2.4.3 , When I import anorm like import anorm._ import anorm.SqlParser._ this brings up the error message below in the IDE... not found: object anorm But my project will execute (without throwing any error). Dependencies libraryDependencies ++= Seq("com.typesafe.play" %% "anorm" % "2.3.6") Editor eclipse Luna scala version scalaVersion := "2.11.6" Activator Version 1.3.6 OS Windows 8 回答1: Your imports are incorrect - they should be: import anorm._ import anorm.SqlParser._ Note

How should I use MayErr[IntegrityConstraintViolation,Int] in Scala and Anorm?

放肆的年华 提交于 2019-12-23 15:43:57
问题 I use Anorm to do database queries. When I do an executeUpdate() , how should I do proper error handling? it has return type MayErr[IntegrityConstraintViolation,Int] , is this a Set or a Map? There is an example, but I don't understand how I should handle the return value: val result = SQL("delete from City where id = 99").executeUpdate().fold( e => "Oops, there was an error" , c => c + " rows were updated!" ) How do I check if the query failed? (using result ), and how do I get the numer of

How to show proper json response from Scala controller class?

点点圈 提交于 2019-12-20 06:58:27
问题 I am trying to get json response from Postgresql database(v9.5) table to display on my view page, I have tried the following, as my application is executing fine, but I am not getting/displaying my required proper json data(as it is containing: Stream, slashes() and question mark(?), like Stream type json). Please let me know that how to show my desired output like below ? my output: Stream("[{\"_testid\":{\"testid0\":\"testnumber\"},\"testtitle\":\"TestTitle\"}]", ?) but my desired output: [

How to show proper json response from Scala controller class?

那年仲夏 提交于 2019-12-20 06:58:11
问题 I am trying to get json response from Postgresql database(v9.5) table to display on my view page, I have tried the following, as my application is executing fine, but I am not getting/displaying my required proper json data(as it is containing: Stream, slashes() and question mark(?), like Stream type json). Please let me know that how to show my desired output like below ? my output: Stream("[{\"_testid\":{\"testid0\":\"testnumber\"},\"testtitle\":\"TestTitle\"}]", ?) but my desired output: [

How to show proper json response from Scala controller class?

我与影子孤独终老i 提交于 2019-12-20 06:58:09
问题 I am trying to get json response from Postgresql database(v9.5) table to display on my view page, I have tried the following, as my application is executing fine, but I am not getting/displaying my required proper json data(as it is containing: Stream, slashes() and question mark(?), like Stream type json). Please let me know that how to show my desired output like below ? my output: Stream("[{\"_testid\":{\"testid0\":\"testnumber\"},\"testtitle\":\"TestTitle\"}]", ?) but my desired output: [

Database transactions in Play framework scala applications (anorm)

匆匆过客 提交于 2019-12-20 03:48:14
问题 I am developing an application using Play framework and scala. I am using anorm for data-access layer. And I've got a problem I could not solve. Brief: I want to be able to have methods in data-access objects (dao) to work inside transactions as well as being called alone. Details: I have data-access layer consist of class with methods that only executes particular SQL over database. Traditionally they looks like: def list() = DB.withConnection { implicit cn => ... } Now I want to have some

DB Plugin is not registered in Play 2.0

冷暖自知 提交于 2019-12-19 14:57:08
问题 I just started working with play, and I modified the way I'm doing a SQL read and I'm now getting the following error: [Exception: DB plugin is not registered.] The code I have for this class is: package models import play.api.db._ import play.api.Play.current import anorm._ case class Housing(id: Long, rent: String, address: String, street0: String, street1: String, neighbourhood: String) object Housing { def all(): List[Housing] = DB.withConnection { implicit c => SQL("select * from housing