Advertisement
Guest User

Untitled

a guest
Dec 8th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.03 KB | None | 0 0
  1. def execute[T](operation: Connection => T) : T = {
  2.     DB.withConnection {connection =>
  3.       connection.setAutoCommit(false)
  4.  
  5.       try {
  6.         val result = operation(connection)
  7.         connection.commit()
  8.         result
  9.       } catch {
  10.         case e:SQLException => {
  11.           connection.rollback()
  12.           throw e
  13.         }
  14.       }
  15.     }
  16.   }
  17.  
  18. def selectQuery[T](query:String, handler:Stream[Row] => Stream[T]) : Connection => Stream[T] = { implicit connection =>
  19.     val select = SQL(query)
  20.     handler(select())
  21.   }
  22.  
  23. def documentResultMapper(res:Stream[Row]) : Stream[Document] = {
  24.     res.collect {
  25.       case Row(id:Long, title:String, content:String, created:Date, lastModified:Date, owners:java.sql.Array) => new Document(id, title, Some(content), created, lastModified, owners.asInstanceOf[Seq[Long]])
  26.     }
  27.   }
  28.  
  29. def findDocumentsForUser(user:User) = {
  30.     val uid = user.id
  31.     val query = s"SELECT * FROM ce_docs WHERE '${uid}' = ANY(owners)"
  32.  
  33.     execute( selectQuery(query, documentResultMapper))
  34.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement