2013-04-12 2 views
1

나는 미래에 Anorm의 SQL 함수 호출을 래핑하는 기능이 있습니다 모델에 사용암시 적 매개 변수가 고차 함수에 전달하지

def sqlWithFuture[T](sql: => T) = Future(DB.withConnection(con => sql)) 

:

def userQuery = sqlWithFuture(SQL("select init()").as(...)) 

수율 :

어떤 방법 :

"java.sql.Connection에 암시 적 매개 변수 연결에 대한 값을 찾을 수 없습니다" 암시 적 연결 매개 변수 (con)를 범위로 다시 가져 오겠습니까?

답변

1

Play 2.0으로 작업하고 있다고 가정합니다. DB http://www.playframework.com/documentation/api/2.0/scala/play/api/db/DB $ .html 중에서

def 
withConnection [A] (block: (Connection) ⇒ A)(implicit app: Application): A 

에서

살펴 보자는 JDBC 연결을 제공하는 코드 블록을 실행합니다. 연결 및 작성된 모든 명령문은 자동으로 해제됩니다.

는 SQL 객체는 당신이 anorm.SqlQuery 만들 수 있습니다 :

def sqlWithFuture[T](sql: => T) = Future(DB.withConnection(con => sql)) 

아무것도 연결이 SQL에 전달해야한다고 말한다 : http://www.playframework.com/documentation/api/2.0/scala/index.html#anorm.SqlQuery

def as [T] (parser: ResultSetParser[T])(implicit connection: Connection): T 

그래서 여기에 문제가 서명 함께 블록.

def sqlWithFuture[T](sql: Connection => T) = Future(DB.withConnection(con => sql(con)) 
관련 문제