CREATE TABLE users (
id INTEGER PRIMARY KEY,
name VARCHAR(256) NOT NULL
)
CREATE TABLE groups (
id INTEGER PRIMARY KEY,
name VARCHAR(256) NOT NULL
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
trait Table[A] { | |
def tableName: String | |
def columns: Seq[String] | |
def c(columnName: String): SQLSyntax = SQLSyntax(columnNameWithTableName(columnName)) | |
def t(table: Table[_]): SQLSyntax = SQLSyntax(table.tableName) | |
private def columnNameWithTableName(columnName: String): String = s"${columnName}__ON__${tableName}" |
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
def findUsers(name: Option[String], offset: Option[Long])(implicit session: DBSession = DBSession.AutoSession): Seq[User] = { | |
val u = User.syntax | |
sql""" | |
select | |
${u.id}, ${u.name} | |
from | |
${User.as(u)} | |
${name.map(n => stx"where ${u.name} like '%${n}%'").getOrElse(stx"")} | |
limit 25 | |
${offset.map(o => stx"offset ${o}").getOrElse(stx"")} |
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
object SQLInterpolation { | |
... snip | |
case class SQLSyntax(value: String, parameters: Seq[Any] = Vector()) | |
... snip | |
@inline implicit def interpolation(s: StringContext) = new SQLInterpolation(s) | |
@inline implicit def syntaxInterpolation(s: StringContext) = new SyntaxInterpolation(s) |
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
case class Article(id: Long, title: String) | |
object Article extends SQLSyntaxSupport[Article] | |
case class ArticlePageView(articleId: Long, total: Long, avarage: Long, maxDwellTimeSec: Long, minDwellTimeSec: Long) | |
object ArticlePageView SQLSyntaxSupport[ArticlePageView] | |
case class Traslation(articleId: Long, body: String) | |
object Traslation SQLSyntaxSupport[Traslation] | |
val (a, t, apv) = (Article.syntax("a"), Traslation.syntax("t"), ArticlePageView.syntax("apv")) |
def cond[A](value: A)(f: PartialFunction[A, SQLBuilder => SQLBuilder]): SQLBuilder
こんな感じの(実際にはSQLBuilderはサブクラス型にしないとだけど)あれば下みたいに書けるかなー
val id: Option[Int] = ...
val name: Option[String] = ...
色んなSQLをSquerylで書くと? のパクリです。
テーブル名とかカラム名はちょっと弄りました。
SELECT *
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
scala> val l = List((1, Some("a")), (1, Some("b")), (2, None)) | |
l: List[(Int, Option[String])] = List((1,Some(a)), (1,Some(b)), (2,None)) | |
scala> l.foldRight(List.empty[(Int, List[String])]) { | |
| case ((t1y, Some(t2y)), r@((t1x, t2x) :: xs)) if t1x == t1y => (t1x, t2y :: t2x) :: xs | |
| case ((t1y, None), r@((t1x, t2x) :: xs)) if t1x == t1y => r | |
| case ((t1y, Some(t2y)), r) => (t1y, t2y :: Nil) :: r | |
| case ((t1y, None), r) => (t1y, Nil) :: r | |
| } | |
res13: List[(Int, List[String])] = List((1,List(a, b)), (2,List())) |
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
package disample | |
case class DiSampleConfig(foo: Int, bar: String) | |
object DiSampleConfig { | |
implicit val configs: Configs[DiSampleConfig] = Configs { c => | |
DiSampleConfig( | |
foo = c.get[Int]("foo"), | |
bar = c.get[String]("bar") | |
) | |
} |
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 scala.concurrent.{Future, ExecutionContext} | |
import scala.util.{Success, Failure} | |
object RetryUtil { | |
case class RetryException(throwables: Seq[Throwable]) extends Exception | |
def retry[T](retryLimit: Int, retryInterval: Int, shouldCatch: Throwable => Boolean)(f: => Future[T])(implicit ctx: ExecutionContext = ExecutionContext.Implicits.global): Future[T] = | |
retry(Stream.fill(retryLimit)(retryInterval), shouldCatch)(f) | |