Skip to content

Instantly share code, notes, and snippets.

@gakuzzzz
Created May 21, 2013 09:14
Show Gist options
  • Save gakuzzzz/5618518 to your computer and use it in GitHub Desktop.
Save gakuzzzz/5618518 to your computer and use it in GitHub Desktop.
ScalikeJDBC つれづれ
  def cond[A](value: A)(f: PartialFunction[A, SQLBuilder => SQLBuilder]): SQLBuilder

こんな感じの(実際にはSQLBuilderはサブクラス型にしないとだけど)あれば下みたいに書けるかなー

  val id: Option[Int] = ...
  val name: Option[String] = ...

  select(c.*)
    .from(Customer as c)
    .where
    .cond(id) { case Some(i) => _.eq(c.id, i) }
    .and
    .cond(name) { case Some(n) => _.eq(c.name, n) }

と思うけど、 .where とか .and とかはどうしょうもなくてぐぬぬ

@gakuzzzz
Copy link
Author

名前むずかしい……。

僕は今上がってる中では andConditions, orConditions が良いように思ってます。

というのも、標準SQLの予約語じゃないメソッドは多少説明的で長いほうが見分けもついていいような気がするので。

@seratch
Copy link

seratch commented May 22, 2013

dynamicAndConditions / dynamicOrConditions という名前なら私はそんなに違和感ないですね。

https://github.com/seratch/scalikejdbc/blob/feature/dynamic-conditions/scalikejdbc-interpolation/src/test/scala/scalikejdbc/QueryInterfaceSpec.scala#L126-L177

@gakuzzzz
Copy link
Author

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment