Skip to content

Instantly share code, notes, and snippets.

@er1c
Created July 20, 2016 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save er1c/d548d62091e8d8fce21dd2a9df1c1d65 to your computer and use it in GitHub Desktop.
Save er1c/d548d62091e8d8fce21dd2a9df1c1d65 to your computer and use it in GitHub Desktop.
import _root_.scala.collection.convert.WrapAsScala
import _root_.scala.compat.java8.FutureConverters._
import _root_.scala.concurrent.Future
import _root_.scala.collection.JavaConverters._
object AsyncImplicits {
implicit class ScalaDSLContextAsync(val ctx: DSLContext) {
def fetchAnyOptionAsync[R <: Record] (table: Table[R]) : Future[Option[R]] = ctx.selectFrom(table).limit(1).fetchAsync.toScala.map{ _.asScala.headOption }
def fetchAnyOptionAsync[R <: Record] (table: Table[R], condition: Condition) : Future[Option[R]] = ctx.selectFrom(table).where(condition).limit(1).fetchAsync.toScala.map{ _.asScala.headOption }
def fetchOneOptionAsync[R <: Record] (query: ResultQuery[R]) : Future[Option[R]] = ctx.fetchAsync(query).toScala.map{ _.asScala.headOption }
def fetchOneOptionAsync (rs: ResultSet) : Future[Option[Record]] = ctx.fetchAsync(rs).toScala.map{ _.asScala.headOption }
def fetchOneOptionAsync (rs: ResultSet, types: Class[_]*)() : Future[Option[Record]] = ctx.fetchAsync(rs, types:_*).toScala.map{ _.asScala.headOption }
def fetchOneOptionAsync (rs: ResultSet, types: DataType[_]*)
(implicit d: DummyImplicit) : Future[Option[Record]] = ctx.fetchAsync(rs, types:_*).toScala.map{ _.asScala.headOption }
def fetchOneOptionAsync (rs: ResultSet, fields: Field[_]*)
(implicit d1: DummyImplicit, d2: DummyImplicit) : Future[Option[Record]] = ctx.fetchAsync(rs, fields:_*).toScala.map{ _.asScala.headOption }
def fetchOneOptionAsync (sql: String) : Future[Option[Record]] = ctx.fetchAsync(sql).toScala.map{ _.asScala.headOption }
def fetchOneOptionAsync (sql: String, bindings: AnyRef*) : Future[Option[Record]] = ctx.fetchAsync(sql, bindings).toScala.map{ _.asScala.headOption }
def fetchOneOptionAsync (sql: String, parts: QueryPart*) : Future[Option[Record]] = ctx.fetchAsync(sql, parts).toScala.map{ _.asScala.headOption }
def fetchOneOptionAsync[R <: Record] (table: Table[R]) : Future[Option[R]] = ctx.selectFrom(table).fetchAsync.toScala.map{ _.asScala.headOption }
def fetchOneOptionAsync[R <: Record] (table: Table[R], condition: Condition) : Future[Option[R]] = ctx.selectFrom(table).where(condition).fetchAsync.toScala.map{ _.asScala.headOption }
def fetchValueOptionAsync[T, R <: Record1[T]] (query: ResultQuery[R]) : Future[Option[T]] = ctx.fetchAsync(query).toScala.map{ _.asScala.headOption.map{ _.value1 } }
def fetchValueOptionAsync (rs: ResultSet) : Future[Option[AnyRef]] = ctx.fetchAsync(rs).toScala.map{ _.asScala.headOption.map{ _.getValue(0) } }
def fetchValueOptionAsync[T] (rs: ResultSet, newType: Class[T]) : Future[Option[T]] = ctx.fetchAsync(rs, newType).toScala.map{ _.asScala.headOption.map{ _.getValue(0, newType) } }
def fetchValueOptionAsync[T] (rs: ResultSet, newType: DataType[T]) : Future[Option[T]] = ctx.fetchAsync(rs, newType).toScala.map{ _.asScala.headOption.map{ r: Record => newType.convert(r.getValue(0)) } }
def fetchValueOptionAsync[T] (rs: ResultSet, field: Field[T]) : Future[Option[T]] = ctx.fetchAsync(rs).toScala.map{ _.asScala.headOption.map{ _.getValue(field) } }
def fetchValueOptionAsync (sql: String) : Future[Option[AnyRef]] = ctx.fetchAsync(sql).toScala.map{ _.asScala.headOption.map{ _.getValue(0) } }
def fetchValueOptionAsync (sql: String, bindings: AnyRef*) : Future[Option[AnyRef]] = ctx.fetchAsync(sql, bindings).toScala.map{ _.asScala.headOption.map{ _.getValue(0) } }
def fetchValueOptionAsync (sql: String, parts: QueryPart*)
(implicit d: DummyImplicit) : Future[Option[AnyRef]] = ctx.fetchAsync(sql, parts).toScala.map{ _.asScala.headOption.map{ _.getValue(0) } }
}
implicit class ScalaResultQueryAsync[R <: Record](val query: ResultQuery[R]) {
import _root_.scala.collection.mutable._
def fetchAnyOptionAsync(): Future[Option[R]] = query.fetchAsync.toScala.map{ _.asScala.headOption }
def fetchAnyOptionAsync[E](mapper: RecordMapper[_ >: R, E]): Future[Option[E]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ mapper.map } }
def fetchAnyOptionAsync[T](field: Field[T]): Future[Option[T]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(field) } }
def fetchAnyOptionAsync[T](field: Field[_], newType: Class[_ <: T]): Future[Option[T]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(field, newType) } }
def fetchAnyOptionAsync[T, U](field: Field[T], converter: Converter[_ >: T, _ <: U]) : Future[Option[U]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(field) }.map{ converter.from } }
def fetchAnyOptionAsync(fieldIndex: Int): Future[Option[_]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(fieldIndex) } }
def fetchAnyOptionAsync[T](fieldIndex: Int, newType: Class[_ <: T]): Future[Option[T]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(fieldIndex, newType) } }
def fetchAnyOptionAsync[T, U](fieldIndex: Int, converter: Converter[_ >: T, _ <: U]): Future[Option[U]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(fieldIndex).asInstanceOf[T] }.map{ converter.from } }
def fetchAnyOptionAsync(fieldName: String): Future[Option[_]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(fieldName) } }
def fetchAnyOptionAsync[T](fieldName: String, newType: Class[_ <: T]): Future[Option[T]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(fieldName, newType) } }
def fetchAnyOptionAsync[T, U](fieldName: String, converter: Converter[_ >: T, _ <: U]): Future[Option[U]]= query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(fieldName).asInstanceOf[T] }.map{ converter.from } }
def fetchAnyOptionArrayAsync(): Future[Option[Array[AnyRef]]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.intoArray() } }
//def fetchAnyOptionIntoAsync[E] (newType: Class[_ <: E]) : Future[Option[E]] = Option(query.fetchAnyInto(newType))
//def fetchAnyOptionIntoAsync[Z <: Record](table: Table[Z]) : Future[Option[Z]] = Option(query.fetchAnyInto(table))
def fetchAnyOptionMapAsync () : Future[Option[Map[String, AnyRef]]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.intoMap }.map{ WrapAsScala.mapAsScalaMap} }
def fetchOneOptionAsync(): Future[Option[R]] = query.fetchAsync.toScala.map{ _.asScala.headOption }
def fetchOneOptionAsync[E](mapper: RecordMapper[_ >: R, E]): Future[Option[E]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ mapper.map } }
def fetchOneOptionAsync[T](field: Field[T]): Future[Option[T]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(field) } }
def fetchOneOptionAsync[T](field: Field[_], newType: Class[_ <: T]): Future[Option[T]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(field, newType) } }
def fetchOneOptionAsync[T, U](field: Field[T], converter: Converter[_ >: T, _ <: U]) : Future[Option[U]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(field) }.map{ converter.from } }
def fetchOneOptionAsync(fieldIndex: Int): Future[Option[_]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(fieldIndex) } }
def fetchOneOptionAsync[T](fieldIndex: Int, newType: Class[_ <: T]): Future[Option[T]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(fieldIndex, newType) } }
def fetchOneOptionAsync[T, U](fieldIndex: Int, converter: Converter[_ >: T, _ <: U]): Future[Option[U]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(fieldIndex, converter.fromType).asInstanceOf[T] }.map{ converter.from } }
def fetchOneOptionAsync(fieldName: String): Future[Option[_]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(fieldName) } }
def fetchOneOptionAsync[T](fieldName: String, newType: Class[_ <: T]): Future[Option[T]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(fieldName, newType) } }
def fetchOneOptionAsync[T, U](fieldName: String, converter: Converter[_ >: T, _ <: U]): Future[Option[U]]= query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.get(fieldName, converter.fromType).asInstanceOf[T] }.map{ converter.from } }
def fetchOneOptionArrayAsync(): Future[Option[Array[AnyRef]]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.intoArray() } }
//def fetchOneOptionIntoAsync[E](newType: Class[_ <: E]): Future[Option[E]] = Option(query.fetchOneInto(newType))
//def fetchOneOptionIntoAsync[Z <: Record](table: Table[Z]): Future[Option[Z]] = Option(query.fetchOneInto(table))
def fetchOneOptionMapAsync(): Future[Option[Map[String, AnyRef]]] = query.fetchAsync.toScala.map{ _.asScala.headOption.map{ _.intoMap }.map{ WrapAsScala.mapAsScalaMap} }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment