Last active
August 29, 2015 14:04
-
-
Save gakuzzzz/43d2999c773fe8e63bec to your computer and use it in GitHub Desktop.
ScalikeTx
This file contains hidden or 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
import scalikejdbc._ | |
case class Tx[+A] private (underlying: DBSession => A) { | |
def apply(s: DBSession): A = underlying(s) | |
def map[B](f: A => B): Tx[B] = Tx(underlying andThen f) | |
def flatMap[B](f: A => Tx[B]): Tx[B] = Tx(s => f(underlying(s))(s)) | |
def withFilter(p: A => Boolean): Tx[A] = Tx { s => | |
val a = underlying(s) | |
if (p(a)) a else throw new java.sql.SQLException() | |
} | |
} | |
object Tx { | |
implicit class ToListTx[A](val value: SQLToList[A, HasExtractor]) extends AnyVal { | |
def toTx: Tx[List[A]] = Tx(s => value.apply(s)) | |
} | |
implicit class ToTraversableTx[A](val value: SQLToTraversable[A, HasExtractor]) extends AnyVal { | |
def toTx: Tx[Traversable[A]] = Tx(s => value.apply(s)) | |
} | |
implicit class ToOptionTx[A](val value: SQLToOption[A, HasExtractor]) extends AnyVal { | |
def toTx: Tx[Option[A]] = Tx(s => value.apply(s)) | |
} | |
implicit class ToListTx[A](val value: SQLToList[A, HasExtractor]) extends AnyVal { | |
def toTx: Tx[List[A]] = Tx(s => value.apply(s)) | |
} | |
implicit class UpdateToTx(val value: SQLUpdate) extends AnyVal { | |
def toTx: Tx[Int] = Tx(s => value.apply(s)) | |
} | |
implicit class UpdateWithGeneratedKeyToTx[A](val value: SQLUpdateWithGeneratedKey) extends AnyVal { | |
def toTx: Tx[Long] = Tx(s => value.apply(s)) | |
} | |
implicit class ExecutionToTx[A](val value: SQLExecution) extends AnyVal { | |
def toTx: Tx[Boolean] = Tx(s => value.apply(s)) | |
} | |
implicit class BatchToTx[A](val value: SQLBatch) extends AnyVal { | |
def toTx: Tx[Seq[Int]] = Tx(s => value.apply(s)) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment