Skip to content

Instantly share code, notes, and snippets.

@annappropriate
Created June 10, 2016 08:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save annappropriate/7aab6ed4559ab98dac5227fbbee1b59a to your computer and use it in GitHub Desktop.
Save annappropriate/7aab6ed4559ab98dac5227fbbee1b59a to your computer and use it in GitHub Desktop.
Update returning for Slick 3.1.1
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)
}
@norcalli
Copy link

This code is incomplete. columnRegex is not defined.

@narma
Copy link

narma commented Jan 5, 2017

val columnRegex: Regex = "(\".*\")".r

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment