Skip to content

Commit a7bb084

Browse files
committed
Fix the and, the or, the not operators
1 parent 975f863 commit a7bb084

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

doma-criteria/src/main/kotlin/org/seasar/doma/criteria/query/BuilderSupport.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,18 +256,28 @@ class BuilderSupport(
256256
if (index > 0) {
257257
buf.cutBackSql(5)
258258
}
259-
buf.appendSql(" $operator ")
259+
if (index != 0) {
260+
buf.appendSql(" $operator ")
261+
}
260262
buf.appendSql("(")
261-
list.forEachIndexed(::visitCriterion)
263+
list.forEachIndexed { i, c ->
264+
visitCriterion(i, c)
265+
buf.appendSql(" and ")
266+
}
267+
buf.cutBackSql(5)
262268
buf.appendSql(")")
263269
}
264270
}
265271

266272
private fun not(list: List<Criterion>) {
267273
if (list.isNotEmpty()) {
268-
buf.appendSql(" not ")
274+
buf.appendSql("not ")
269275
buf.appendSql("(")
270-
list.forEachIndexed(::visitCriterion)
276+
list.forEachIndexed { i, c ->
277+
visitCriterion(i, c)
278+
buf.appendSql(" and ")
279+
}
280+
buf.cutBackSql(5)
271281
buf.appendSql(")")
272282
}
273283
}

doma-criteria/src/test/kotlin/org/seasar/doma/criteria/EntityqlDslTest.kt

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,84 @@ class EntityqlDslTest {
320320
assertEquals(expected, sql.formattedSql)
321321
}
322322

323+
@Test
324+
fun where_or() {
325+
val query = entityql {
326+
from(::Emp_) { e ->
327+
where {
328+
e.id eq 1
329+
and {
330+
e.name eq "hoge"
331+
or {
332+
e.name eq "foo"
333+
e.salary eq BigDecimal("1000")
334+
}
335+
e.version eq 1
336+
}
337+
}
338+
}
339+
}
340+
val sql = query.asSql(config)
341+
val expected = "select t0_.ID, t0_.NAME, t0_.SALARY, t0_.VERSION from EMP t0_ where t0_.ID = 1 and (t0_.NAME = 'hoge' or (t0_.NAME = 'foo' and t0_.SALARY = 1000) and t0_.VERSION = 1)"
342+
assertEquals(expected, sql.formattedSql)
343+
}
344+
345+
@Test
346+
fun where_not() {
347+
val query = entityql {
348+
from(::Emp_) { e ->
349+
where {
350+
e.id eq 1
351+
or {
352+
e.name eq "hoge"
353+
not {
354+
e.name eq "foo"
355+
e.salary eq BigDecimal("1000")
356+
}
357+
e.version eq 1
358+
}
359+
}
360+
}
361+
}
362+
val sql = query.asSql(config)
363+
val expected = "select t0_.ID, t0_.NAME, t0_.SALARY, t0_.VERSION from EMP t0_ where t0_.ID = 1 or (t0_.NAME = 'hoge' and not (t0_.NAME = 'foo' and t0_.SALARY = 1000) and t0_.VERSION = 1)"
364+
assertEquals(expected, sql.formattedSql)
365+
}
366+
367+
@Test
368+
fun and_right_after_where() {
369+
val query = entityql {
370+
from(::Emp_) { e ->
371+
where {
372+
and {
373+
e.name eq "hoge"
374+
}
375+
}
376+
}
377+
}
378+
val sql = query.asSql(config)
379+
val expected = "select t0_.ID, t0_.NAME, t0_.SALARY, t0_.VERSION from EMP t0_ where (t0_.NAME = 'hoge')"
380+
assertEquals(expected, sql.formattedSql)
381+
}
382+
383+
@Test
384+
fun and_right_after_not() {
385+
val query = entityql {
386+
from(::Emp_) { e ->
387+
where {
388+
not {
389+
and {
390+
e.name eq "hoge"
391+
}
392+
}
393+
}
394+
}
395+
}
396+
val sql = query.asSql(config)
397+
val expected = "select t0_.ID, t0_.NAME, t0_.SALARY, t0_.VERSION from EMP t0_ where not ((t0_.NAME = 'hoge'))"
398+
assertEquals(expected, sql.formattedSql)
399+
}
400+
323401
@Test
324402
fun `in`() {
325403
val query = entityql {

0 commit comments

Comments
 (0)