diff --git a/jOOQ-scala/src/main/scala/org/jooq/scala/Conversions.scala b/jOOQ-scala/src/main/scala/org/jooq/scala/Conversions.scala index 5d300306c4..f03c384157 100644 --- a/jOOQ-scala/src/main/scala/org/jooq/scala/Conversions.scala +++ b/jOOQ-scala/src/main/scala/org/jooq/scala/Conversions.scala @@ -40,12 +40,12 @@ */ package org.jooq.scala +import java.sql.ResultSet + import org.jooq._ import org.jooq.impl._ -import org.jooq.impl.DSL._ import _root_.scala.collection.convert.WrapAsScala -import _root_.scala.collection.convert.Wrappers.JMapWrapper /** * jOOQ type conversions used to enhance the jOOQ Java API with Scala Traits @@ -90,7 +90,37 @@ object Conversions { // Enhanced jOOQ types // ------------------------------------------------------------------------- - implicit class ScalaResultQuery[R <: Record](val query: ResultQuery[R]) { + 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)) + + def fetchOneOption[R <: Record] (query : ResultQuery[R]) : Option[R] = Option(ctx.fetchOne(query)) + def fetchOneOption (rs : ResultSet) : Option[Record] = Option(ctx.fetchOne(rs)) + +// Varargs overloading is not possible in Scala, as varargs T* translate to Seq[T] +// --> type erasure. Overloaded varargs arguments might be made available by emulating +// union types using implicit conversion. See http://stackoverflow.com/a/3508555/521799 + +// def fetchOneOption (rs : ResultSet, types : Class[_]*) : Option[Record] = Option(ctx.fetchOne(rs, types:_*)) +// def fetchOneOption (rs : ResultSet, types : DataType[_]*) : Option[Record] = Option(ctx.fetchOne(rs, types:_*)) +// def fetchOneOption (rs : ResultSet, fields : Field[_]*) : Option[Record] = Option(ctx.fetchOne(rs, fields:_*)) + def fetchOneOption (sql : String) : Option[Record] = Option(ctx.fetchOne(sql)) + def fetchOneOption (sql : String, bindings : AnyRef*) : Option[Record] = Option(ctx.fetchOne(sql, bindings:_*)) +// def fetchOneOption (sql : String, parts : QueryPart*) : Option[Record] = Option(ctx.fetchOne(sql, parts:_*)) + def fetchOneOption[R <: Record] (table : Table[R]) : Option[R] = Option(ctx.fetchOne(table)) + def fetchOneOption[R <: Record] (table : Table[R], condition : Condition) : Option[R] = Option(ctx.fetchOne(table, condition)) + + def fetchValueOption[T, R <: Record1[T]] (query : ResultQuery[R]) : Option[T] = Option(ctx.fetchValue[T, R](query)) + def fetchValueOption (rs : ResultSet) : Option[AnyRef] = Option(ctx.fetchValue(rs)) + def fetchValueOption[T] (rs : ResultSet, newType : Class[T]) : Option[T] = Option(ctx.fetchValue(rs, newType)) + def fetchValueOption[T] (rs : ResultSet, newType : DataType[T]) : Option[T] = Option(ctx.fetchValue(rs, newType)) + def fetchValueOption[T] (rs : ResultSet, field : Field[T]) : Option[T] = Option(ctx.fetchValue(rs, field)) + def fetchValueOption (sql : String) : Option[AnyRef] = Option(ctx.fetchValue(sql)) + def fetchValueOption (sql : String, bindings : AnyRef*) : Option[AnyRef] = Option(ctx.fetchValue(sql, bindings:_*)) +// def fetchValueOption (sql : String, parts : QueryPart*) : Option[AnyRef] = Option(ctx.fetchValue(sql, parts:_*)) + } + + implicit class ScalaResultQuery[R <: Record](val query : ResultQuery[R]) { import _root_.scala.collection.mutable._ def fetchAnyOption () : Option[R] = Option(query.fetchAny) diff --git a/jOOQ-scala/src/test/scala/org/jooq/scala/example/Test.scala b/jOOQ-scala/src/test/scala/org/jooq/scala/example/Test.scala index ebdc73669d..3db7156d05 100644 --- a/jOOQ-scala/src/test/scala/org/jooq/scala/example/Test.scala +++ b/jOOQ-scala/src/test/scala/org/jooq/scala/example/Test.scala @@ -40,6 +40,8 @@ */ package org.jooq.scala.example +import org.jooq.scala.Conversions + import collection.JavaConversions._ import java.sql.DriverManager @@ -110,13 +112,28 @@ object Test { ) // Option conversions - for (i <- 1 to 3) { - f.select (T_AUTHOR.FIRST_NAME, T_AUTHOR.LAST_NAME) - .from (T_AUTHOR) - .where (T_AUTHOR.ID === i) + for (i <- 1 to 5) { + f.select (T_BOOK.TITLE) + .from (T_BOOK) + .where (T_BOOK.ID === i) .fetchOneOption() match { - case Some(author) => println("Author found: " + author.getValue(T_AUTHOR.FIRST_NAME) + " " + author.getValue(T_AUTHOR.LAST_NAME)) - case None => println("No author found for ID: " + i) + case Some(record) => + println("Book found: " + record.value1) + case None => + println("No book found for ID: " + i) + } + } + + for (i <- 1 to 3) { + f fetchOneOption ( + select (T_AUTHOR.FIRST_NAME, T_AUTHOR.LAST_NAME) + .from (T_AUTHOR) + .where (T_AUTHOR.ID === i) + ) match { + case Some(record) => + println("Author found: " + record.value1 + " " + record.value2) + case None => + println("No author found for ID: " + i) } } }