Created
June 10, 2016 08:54
-
-
Save annappropriate/7aab6ed4559ab98dac5227fbbee1b59a to your computer and use it in GitHub Desktop.
Update returning for Slick 3.1.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private def updateReturning[A, F]( | |
returningQuery: Query[A, F, C], v: U): SqlStreamingAction[Vector[F], F, Effect.All] = { | |
val ResultSetMapping( | |
_, CompiledStatement(_, sres: SQLBuilder.Result, _), CompiledMapping(_updateConverter, _)) = | |
updateCompiler.run(updateQuery.toNode).tree | |
val pconv: SetParameter[U] = { | |
val ResultSetMapping(_, compiled, CompiledMapping(_converter, _)) = | |
updateCompiler.run(updateQuery.toNode).tree | |
val converter = _converter.asInstanceOf[ResultConverter[JdbcResultConverterDomain, U]] | |
SetParameter[U] { (value, params) ⇒ | |
converter.set(value, params.ps) | |
} | |
} | |
// extract the result/converter to build our RETURNING {columns} and re-use it for result conversion | |
val ResultSetMapping(_, | |
CompiledStatement(_, returningResult: SQLBuilder.Result, _), | |
CompiledMapping(resultConverter, _)) = | |
queryCompiler.run(returningQuery.toNode).tree | |
val rconv: GetResult[F] = { | |
val converter = resultConverter.asInstanceOf[ResultConverter[JdbcResultConverterDomain, F]] | |
GetResult[F] { p ⇒ | |
converter.read(p.rs) | |
} | |
} | |
// extract columns from the `SELECT {columns} FROM {table}` after dropping `FROM .*` from query str | |
val columns = columnRegex.findAllIn(returningResult.sql.replaceAll(" from .*", "")).toList | |
val fieldsExp = columns.mkString(", ") | |
val returningSql = sres.sql + s" RETURNING $fieldsExp" | |
SQLActionBuilder(returningSql, pconv.applied(v)).as[F](rconv) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is incomplete. columnRegex is not defined.