How to use Anorm outside of Play?
Asked Answered
I

3

9

How do you use Anorm outside of play in Scala? In the Anorm document for play, it simply uses something like:

DB.withConnection { implicit c =>
  val result: Boolean = SQL("Select 1").execute()    
} 

The DB object is only for Play. How do you use Anorm alone without using Play?

Interactive answered 30/8, 2014 at 13:12 Comment(1)
The c that you are supplying is the database connection object, which is a java.sql.Connection. You can supply a real connection and provide it as an implicit.Regression
S
14

There is no need of DB object (part of Play JDBC not Anorm). Anorm works as along as you provide it connection as implicit:

implicit val con: java.sql.Connection = ??? // whatever you want to resolve connection

SQL"SELECT * FROM Table".as(...)

You can resolve JDBC connection in many way: basic DriverManager.getConnection, JNDI, ...

As for dependency, it's easy to add it in SBT: How to declare dependency on Play's Anorm for a standalone application? .

Shipboard answered 30/8, 2014 at 16:39 Comment(1)
Note that now Anorm is fully standalone, including for its documentation: playframework.github.io/anormShipboard
C
1

You could also emulate the DB object as follows (i haven't tried this though)

 object DB {
    def withConnection[A](block: Connection => A): A = {
      val connection: Connection = ConnectionPool.borrow()

      try {
        block(connection)
      } finally {
        connection.close()
      }
    }
  }

Taken from https://github.com/TimothyKlim/anorm-without-play/blob/master/src/main/scala/Main.scala

Coating answered 8/9, 2015 at 13:5 Comment(0)
S
0

Documenting the code that works for me below:

Entry to include in dependencies in build.sbt:

// https://mvnrepository.com/artifact/org.playframework.anorm/anorm
libraryDependencies += "org.playframework.anorm" %% "anorm" % "2.6.7"

Write helper classes:

    @Singleton
    class DBUtils {
    
      val schema = AppConfig.defaultSchema
    
      def withDefaultConnection(sqlQuery: SqlQuery) = {
// could replace with DBCP, not got a chance yet
        val conn = DriverManager.getConnection(AppConfig.dbUrl,AppConfig.dbUser, AppConfig.dbPassword)
        val result = Try(sqlQuery.execute()(conn))
        conn.close()
        result
      }
    }
    object DBUtils extends DBUtils

Next, any query can use the withDefaultConnection method to execute:

  def saveReviews(listOfReviews: List[Review]):Try[Boolean]= {
    val query = SQL(
      s"""insert into aws.reviews
         | (                 reviewerId,
         |                 asin,
         |                 overall,
         |                 summary,
         |                 unixReviewTime,
         |                 reviewTime
         | )
         |values ${listOfReviews.mkString(",")}""".stripMargin)
    //println(query.toString())
    DBUtils.withDefaultConnection(query)
  }
Subjunction answered 28/7, 2020 at 22:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.