|
| 1 | +package examples.kotlin.mybatis3.general |
| 2 | + |
| 3 | +import examples.kotlin.mybatis3.general.FooDynamicSqlSupport.A |
| 4 | +import examples.kotlin.mybatis3.general.FooDynamicSqlSupport.B |
| 5 | +import examples.kotlin.mybatis3.general.FooDynamicSqlSupport.C |
| 6 | +import examples.kotlin.mybatis3.general.FooDynamicSqlSupport.foo |
| 7 | +import org.assertj.core.api.Assertions.assertThat |
| 8 | +import org.junit.jupiter.api.Test |
| 9 | +import org.mybatis.dynamic.sql.SqlTable |
| 10 | +import org.mybatis.dynamic.sql.util.kotlin.elements.exists |
| 11 | +import org.mybatis.dynamic.sql.util.kotlin.elements.group |
| 12 | +import org.mybatis.dynamic.sql.util.kotlin.elements.isEqualTo |
| 13 | +import org.mybatis.dynamic.sql.util.kotlin.elements.isGreaterThan |
| 14 | +import org.mybatis.dynamic.sql.util.kotlin.elements.isLessThan |
| 15 | +import org.mybatis.dynamic.sql.util.kotlin.mybatis3.select |
| 16 | + |
| 17 | +object FooDynamicSqlSupport { |
| 18 | + class Foo : SqlTable("Foo") { |
| 19 | + var A = column<Int>("A") |
| 20 | + var B = column<Int>("B") |
| 21 | + var C = column<Int>("C") |
| 22 | + } |
| 23 | + |
| 24 | + val foo = Foo() |
| 25 | + val A = foo.A |
| 26 | + val B = foo.B |
| 27 | + val C = foo.C |
| 28 | +} |
| 29 | + |
| 30 | +class KGroupingTest { |
| 31 | + @Test |
| 32 | + fun testSimpleGrouping() { |
| 33 | + val selectStatement = select(A, B, C) { |
| 34 | + from(foo) |
| 35 | + where (A, isEqualTo(1)) { |
| 36 | + or(A, isEqualTo(2)) |
| 37 | + } |
| 38 | + and(B, isEqualTo(3)) |
| 39 | + } |
| 40 | + |
| 41 | + val expected = "select A, B, C" + |
| 42 | + " from Foo" + |
| 43 | + " where (A = #{parameters.p1} or A = #{parameters.p2}) and B = #{parameters.p3}" |
| 44 | + |
| 45 | + assertThat(selectStatement.selectStatement).isEqualTo(expected) |
| 46 | + assertThat(selectStatement.parameters).containsEntry("p1", 1) |
| 47 | + assertThat(selectStatement.parameters).containsEntry("p2", 2) |
| 48 | + assertThat(selectStatement.parameters).containsEntry("p3", 3) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun testComplexGrouping() { |
| 53 | + val selectStatement = select(A, B, C) { |
| 54 | + from(foo) |
| 55 | + where(group(A, isEqualTo(1)) { |
| 56 | + or(A, isGreaterThan(5)) |
| 57 | + }) { |
| 58 | + and(B, isEqualTo(1)) |
| 59 | + or(A, isLessThan(0)) { |
| 60 | + and(B, isEqualTo(2)) |
| 61 | + } |
| 62 | + } |
| 63 | + and(C, isEqualTo(1)) |
| 64 | + } |
| 65 | + |
| 66 | + val expected = "select A, B, C" + |
| 67 | + " from Foo" + |
| 68 | + " where ((A = #{parameters.p1} or A > #{parameters.p2}) and B = #{parameters.p3} or (A < #{parameters.p4} and B = #{parameters.p5})) and C = #{parameters.p6}" |
| 69 | + |
| 70 | + assertThat(selectStatement.selectStatement).isEqualTo(expected) |
| 71 | + assertThat(selectStatement.parameters).containsEntry("p1", 1) |
| 72 | + assertThat(selectStatement.parameters).containsEntry("p2", 5) |
| 73 | + assertThat(selectStatement.parameters).containsEntry("p3", 1) |
| 74 | + assertThat(selectStatement.parameters).containsEntry("p4", 0) |
| 75 | + assertThat(selectStatement.parameters).containsEntry("p5", 2) |
| 76 | + assertThat(selectStatement.parameters).containsEntry("p6", 1) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun testGroupAndExists() { |
| 81 | + val selectStatement = select(A, B, C) { |
| 82 | + from(foo) |
| 83 | + where(group(exists { |
| 84 | + select(foo.allColumns()) { |
| 85 | + from (foo) |
| 86 | + where(A, isEqualTo(3)) |
| 87 | + } |
| 88 | + }) { |
| 89 | + and(A, isEqualTo((1))) |
| 90 | + or(A, isGreaterThan(5)) |
| 91 | + }) { |
| 92 | + and(B, isEqualTo(1)) |
| 93 | + or(A, isLessThan(0)) { |
| 94 | + and(B, isEqualTo(2)) |
| 95 | + } |
| 96 | + } |
| 97 | + and(C, isEqualTo(1)) |
| 98 | + } |
| 99 | + |
| 100 | + val expected = "select A, B, C" + |
| 101 | + " from Foo" + |
| 102 | + " where ((exists (select * from Foo where A = #{parameters.p1}) and A = #{parameters.p2} or A > #{parameters.p3}) and B = #{parameters.p4} or (A < #{parameters.p5} and B = #{parameters.p6})) and C = #{parameters.p7}" |
| 103 | + assertThat(selectStatement.selectStatement).isEqualTo(expected) |
| 104 | + assertThat(selectStatement.parameters).containsEntry("p1", 3) |
| 105 | + assertThat(selectStatement.parameters).containsEntry("p2", 1) |
| 106 | + assertThat(selectStatement.parameters).containsEntry("p3", 5) |
| 107 | + assertThat(selectStatement.parameters).containsEntry("p4", 1) |
| 108 | + assertThat(selectStatement.parameters).containsEntry("p5", 0) |
| 109 | + assertThat(selectStatement.parameters).containsEntry("p6", 2) |
| 110 | + assertThat(selectStatement.parameters).containsEntry("p7", 1) |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + fun testNestedGrouping() { |
| 115 | + val selectStatement = select(A, B, C) { |
| 116 | + from(foo) |
| 117 | + where( |
| 118 | + group(group(A, isEqualTo(1)) { |
| 119 | + or(A, isGreaterThan(5)) |
| 120 | + }) { |
| 121 | + and(A, isGreaterThan(5)) |
| 122 | + } |
| 123 | + ) { |
| 124 | + and(group(A, isEqualTo(1)) { |
| 125 | + or(A, isGreaterThan(5)) |
| 126 | + }) { |
| 127 | + or(B, isEqualTo(1)) |
| 128 | + } |
| 129 | + or(group(A, isEqualTo(1)) { |
| 130 | + or(A, isGreaterThan(5)) |
| 131 | + }) { |
| 132 | + and(A, isLessThan(0)) { |
| 133 | + and(B, isEqualTo(2)) |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + and(C, isEqualTo(1)) |
| 138 | + } |
| 139 | + |
| 140 | + val expected = "select A, B, C" + |
| 141 | + " from Foo" + |
| 142 | + " where (((A = #{parameters.p1} or A > #{parameters.p2}) and A > #{parameters.p3}) and ((A = #{parameters.p4} or A > #{parameters.p5}) or B = #{parameters.p6}) or ((A = #{parameters.p7} or A > #{parameters.p8}) and (A < #{parameters.p9} and B = #{parameters.p10}))) and C = #{parameters.p11}" |
| 143 | + |
| 144 | + assertThat(selectStatement.selectStatement).isEqualTo(expected) |
| 145 | + assertThat(selectStatement.parameters).containsEntry("p1", 1) |
| 146 | + assertThat(selectStatement.parameters).containsEntry("p2", 5) |
| 147 | + assertThat(selectStatement.parameters).containsEntry("p3", 5) |
| 148 | + assertThat(selectStatement.parameters).containsEntry("p4", 1) |
| 149 | + assertThat(selectStatement.parameters).containsEntry("p5", 5) |
| 150 | + assertThat(selectStatement.parameters).containsEntry("p6", 1) |
| 151 | + assertThat(selectStatement.parameters).containsEntry("p7", 1) |
| 152 | + assertThat(selectStatement.parameters).containsEntry("p8", 5) |
| 153 | + assertThat(selectStatement.parameters).containsEntry("p9", 0) |
| 154 | + assertThat(selectStatement.parameters).containsEntry("p10", 2) |
| 155 | + assertThat(selectStatement.parameters).containsEntry("p11", 1) |
| 156 | + } |
| 157 | +} |
0 commit comments