Skip to content

Commit

Permalink
Merge pull request scalikejdbc#415 from tkawachi/paren
Browse files Browse the repository at this point in the history
Add SQLSyntax#roundBracket
  • Loading branch information
seratch committed May 8, 2015
2 parents fae378e + ad42468 commit 5256e4a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion project/MimaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ object MimaSettings {
)} ++ Seq(
// since 2.2.5
exclude[MissingMethodProblem]("scalikejdbc.mapper.CodeGenerator.scalikejdbc$mapper$CodeGenerator$$toCamelCase"),
exclude[MissingMethodProblem]("scalikejdbc.mapper.CodeGenerator.scalikejdbc$mapper$CodeGenerator$$toProperCase")
exclude[MissingMethodProblem]("scalikejdbc.mapper.CodeGenerator.scalikejdbc$mapper$CodeGenerator$$toProperCase"),
// since 2.2.7
exclude[MissingMethodProblem]("scalikejdbc.QueryDSLFeature#ConditionSQLBuilder.roundBracket")
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class SQLSyntax private[scalikejdbc] (val value: String, val parameters: Seq[Any
def and = sqls"${this} and"
def or = sqls"${this} or"

def roundBracket(inner: SQLSyntax) = sqls"$this ($inner)"

def eq(column: SQLSyntax, value: Any): SQLSyntax = {
value match {
case null | None => sqls"${this} ${column} is null"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,15 @@ class SQLSyntaxSpec extends FlatSpec with Matchers {
s.parameters should equal(Seq(123, "Alice"))
}

it should "have #roundBracket" in {
val (id, name, age) = (123, "Alice", 12)
val s = SQLSyntax.eq(sqls"id", id).and.roundBracket(
SQLSyntax.eq(sqls"name", name).or.eq(sqls"age", age)
)
s.value should equal(" id = ? and ( name = ? or age = ?)")
s.parameters should equal(Seq(123, "Alice", 12))
}

it should "have #toAndConditionOpt (Some)" in {
val (id, name) = (123, "Alice")
val s = SQLSyntax.toAndConditionOpt(Some(sqls"id = ${id}"), Some(sqls"name = ${name} or name is null")).get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ trait QueryDSLFeature { self: SQLInterpolationFeature with SQLSyntaxSupportFeatu
ConditionSQLBuilder[A](sqls"${sql} (${insidePart(emptyBuilder).toSQLSyntax})")
}

def roundBracket(inner: SQLSyntax): ConditionSQLBuilder[A] = ConditionSQLBuilder[A](sql.roundBracket(inner))

/**
* Appends SQLSyntax directly.
* e.g. select.from(User as u).where.eq(u.id, 123).append(sqls"order by ${u.id} desc")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ class QueryInterfaceSpec extends FlatSpec with Matchers with DBSettings with SQL
preferredClients.size should equal(2)
preferredClients should equal(List((2, 360), (1, 440)))

// test withRoundBracket(ConditionSQLBuilder => ConditionSQLBuilder)
val bracketTestResults = withSQL {
select(o.result.id)
.from(Order as o)
Expand All @@ -272,6 +273,18 @@ class QueryInterfaceSpec extends FlatSpec with Matchers with DBSettings with SQL

bracketTestResults should equal(List(11, 12, 13, 14, 15, 26))

// test roundBracket(SQLSyntax)
val bracketTestResults2 = withSQL {
select(o.result.id)
.from(Order as o)
.where.roundBracket(
sqls.eq(o.productId, 1).and.isNotNull(o.accountId)
).or.isNull(o.accountId)
.orderBy(o.id)
}.map(_.int(o.resultName.id)).list.apply()

bracketTestResults2 should equal(List(11, 12, 13, 14, 15, 26))

{
val productId = Some(1)
val withConditionsTestResults = withSQL {
Expand Down

0 comments on commit 5256e4a

Please sign in to comment.