Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#4600] Add support for plain SQL APIs via Scala string interpolation
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,41 @@ object Conversions { | |
// Enhanced jOOQ types | ||
// ------------------------------------------------------------------------- | ||
|
||
implicit class SQLInterpolation(val sc : StringContext) extends AnyVal { | ||
|
||
@PlainSQL | ||
def sql(args: Any*) : SQL = DSL.sql(string(args), args.asInstanceOf[Seq[AnyRef]] : _*) | ||
|
||
@PlainSQL | ||
def condition(args : Any*) : Condition = DSL.condition(string(args), args.asInstanceOf[Seq[AnyRef]] : _*) | ||
|
||
@PlainSQL | ||
def table(args : Any*) : Table[Record] = DSL.table(string(args), args.asInstanceOf[Seq[AnyRef]] : _*) | ||
|
||
@PlainSQL | ||
def query(args : Any*) : Query = DSL.query(string(args), args.asInstanceOf[Seq[AnyRef]] : _*) | ||
|
||
@PlainSQL | ||
def resultQuery(args : Any*) : ResultQuery[Record] = DSL.resultQuery(string(args), args.asInstanceOf[Seq[AnyRef]] : _*) | ||
|
||
private def string(args : Any*) = { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lukaseder
Author
Member
|
||
val pi = sc.parts.iterator | ||
val sb = new StringBuilder(pi.next()) | ||
var i = 0; | ||
|
||
while (pi.hasNext) { | ||
sb += '{' | ||
sb ++= (i toString) | ||
sb += '}' | ||
sb ++= pi.next() | ||
|
||
i = i + 1; | ||
} | ||
|
||
sb.result | ||
} | ||
} | ||
|
||
implicit class ScalaDSLContext (val ctx : DSLContext) { | ||
def fetchAnyOption[R <: Record] (table : Table[R]) : Option[R] = Option(ctx.fetchAny(table)) | ||
def fetchAnyOption[R <: Record] (table : Table[R], condition : Condition) : Option[R] = Option(ctx.fetchAny(table, condition)) | ||
|
What's the point of passing in args: Any* into this helper since it's not used anywhere in the implementation? I think I'm missing something here...