Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def execute[T](operation: Connection => T) : T = {
- DB.withConnection {connection =>
- connection.setAutoCommit(false)
- try {
- val result = operation(connection)
- connection.commit()
- result
- } catch {
- case e:SQLException => {
- connection.rollback()
- throw e
- }
- }
- }
- }
- def selectQuery[T](query:String, handler:Stream[Row] => Stream[T]) : Connection => Stream[T] = { implicit connection =>
- val select = SQL(query)
- handler(select())
- }
- def documentResultMapper(res:Stream[Row]) : Stream[Document] = {
- res.collect {
- 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]])
- }
- }
- def findDocumentsForUser(user:User) = {
- val uid = user.id
- val query = s"SELECT * FROM ce_docs WHERE '${uid}' = ANY(owners)"
- execute( selectQuery(query, documentResultMapper))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement