Skip to content

Commit a76dbd7

Browse files
committed
Documentation
1 parent 9c900bd commit a76dbd7

File tree

3 files changed

+85
-32
lines changed

3 files changed

+85
-32
lines changed

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This log will detail notable changes to MyBatis Dynamic SQL. Full details are av
44

55
## Release 1.4.0 - Unreleased
66

7-
The release includes new function in the Where Clause DSL to support arbitrary grouping of conditions, and also use
7+
The release includes new functionality in the Where Clause DSL to support arbitrary grouping of conditions, and also use
88
of a "not" condition. It should now be possible to write any type of where clause.
99

1010
Additionally, there were significant updates to the Kotlin DSL - both to support the new functionality in the
@@ -44,6 +44,9 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
4444
insertBatch, and insertMultiple, the "into" function is moved inside the completer lambda. The old methods are now
4545
deprecated and will be removed in version 1.5.0 of the library. This also allowed us to make some insert DSL
4646
methods into infix functions. ([#452](https://github.com/mybatis/mybatis-dynamic-sql/pull/452))
47+
8. Updated the "exists" support to expose table aliases specified in the outer query to the query in the exists
48+
clause. This makes it easier to use exists without having to re-specify the aliases for columns from the outer query.
49+
([#437](https://github.com/mybatis/mybatis-dynamic-sql/issues/437))
4750

4851
## Release 1.3.1 - December 18, 2021
4952

src/site/markdown/docs/subQueries.md

+38-31
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,16 @@ SelectStatementProvider selectStatement = select(itemMaster.allColumns())
135135
.where(exists(
136136
select(orderLine.allColumns())
137137
.from(orderLine, "ol")
138-
.where(orderLine.itemId, isEqualTo(itemMaster.itemId.qualifiedWith("im")))
138+
.where(orderLine.itemId, isEqualTo(itemMaster.itemId))
139139
))
140140
.orderBy(itemMaster.itemId)
141141
.build()
142142
.render(RenderingStrategies.MYBATIS3);
143143
```
144144

145-
Note that we have to apply the qualifier for the outer query ("im") to the inner query. The qualifier
146-
for the inner query ("ol") is automatically applied.
145+
Note that the qualifier for the outer query ("im") is automatically applied to the inner query, as well as the
146+
qualifier for the inner query ("ol"). Carrying alias from an outer query to an inner query is only supported with
147+
exists or not exists sub queries.
147148

148149
An example of a column based subquery is as follows:
149150

@@ -168,25 +169,29 @@ An example of an exists subquery is as follows:
168169
```kotlin
169170
val selectStatement = select(ItemMaster.allColumns()) {
170171
from(ItemMaster, "im")
171-
where(exists {
172-
select(OrderLine.allColumns()) {
173-
from(OrderLine, "ol")
174-
where(OrderLine.itemId, isEqualTo(ItemMaster.itemId.qualifiedWith("im")))
175-
}
176-
})
177-
orderBy(ItemMaster.itemId)
172+
where {
173+
exists {
174+
select(OrderLine.allColumns()) {
175+
from(OrderLine, "ol")
176+
where { OrderLine.itemId isEqualTo ItemMaster.itemId }
177+
}
178+
}
179+
orderBy(ItemMaster.itemId)
180+
}
178181
}
179182
```
180183

181184
An example of a column based subquery is as follows:
182185
```kotlin
183186
val selectStatement = select(id, firstName, lastName, birthDate, employed, occupation, addressId) {
184187
from(Person)
185-
where(id, isEqualTo {
186-
select(max(id)) {
187-
from(Person)
188-
}
189-
})
188+
where {
189+
id isEqualTo {
190+
select(max(id)) {
191+
from(Person)
192+
}
193+
}
194+
}
190195
}
191196
```
192197

@@ -241,12 +246,12 @@ with the select DSL. You can write subqueries like this:
241246

242247
```kotlin
243248
val updateStatement = update(Person) {
244-
set(addressId).equalToQueryResult {
249+
set(addressId) equalToQueryResult {
245250
select(add(max(addressId), constant<Int>("1"))) {
246251
from(Person)
247252
}
248253
}
249-
where(id, isEqualTo(3))
254+
where { id isEqualTo 3 }
250255
}
251256
```
252257

@@ -289,12 +294,12 @@ val selectStatement =
289294
from {
290295
select(id, firstName) {
291296
from(Person)
292-
where(id, isLessThan(22))
297+
where { id isLessThan 22 }
293298
orderBy(firstName.descending())
294299
}
295300
}
296-
where(rowNum, isLessThan(5))
297-
and(firstName, isLike("%a%"))
301+
where { rowNum isLessThan 5 }
302+
and { firstName isLike "%a%" }
298303
}
299304
```
300305

@@ -307,13 +312,13 @@ val selectStatement =
307312
from {
308313
select(id, firstName) {
309314
from(Person, "a")
310-
where(id, isLessThan(22))
315+
where { id isLessThan 22 }
311316
orderBy(firstName.descending())
312317
}
313318
+ "b"
314319
}
315-
where(rowNum, isLessThan(5))
316-
and(firstName, isLike("%a%"))
320+
where { rowNum isLessThan 5 }
321+
and { firstName isLike "%a%" }
317322
}
318323
```
319324

@@ -356,15 +361,17 @@ val selectStatement = select(OrderLine.orderId, OrderLine.quantity,
356361
ItemMaster.itemId.qualifiedWith("im"), ItemMaster.description) {
357362
from(OrderMaster, "om")
358363
join(OrderLine, "ol") {
359-
on(OrderMaster.orderId, equalTo(OrderLine.orderId))
364+
on(OrderMaster.orderId) equalTo OrderLine.orderId
360365
}
361-
leftJoin({
362-
select(ItemMaster.allColumns()) {
363-
from(ItemMaster)
364-
}
365-
+ "im"
366-
}) {
367-
on(OrderLine.itemId, equalTo(ItemMaster.itemId.qualifiedWith("im")))
366+
leftJoin(
367+
{
368+
select(ItemMaster.allColumns()) {
369+
from(ItemMaster)
370+
}
371+
+ "im"
372+
}
373+
) {
374+
on(OrderLine.itemId) equalTo (ItemMaster.itemId qualifiedWith "im")
368375
}
369376
orderBy(OrderLine.orderId, ItemMaster.itemId)
370377
}

src/test/kotlin/examples/kotlin/mybatis3/joins/ExistsTest.kt

+43
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,49 @@ class ExistsTest {
9494
}
9595
}
9696

97+
@Test
98+
fun testExistsPropagatedAliases() {
99+
newSession().use { session ->
100+
val mapper = session.getMapper(CommonSelectMapper::class.java)
101+
102+
val selectStatement = select(itemMaster.allColumns()) {
103+
from(itemMaster, "im")
104+
where {
105+
exists {
106+
select(orderLine.allColumns()) {
107+
from(orderLine, "ol")
108+
where { orderLine.itemId isEqualTo itemMaster.itemId }
109+
}
110+
}
111+
}
112+
orderBy(itemMaster.itemId)
113+
}
114+
115+
val expectedStatement: String = "select im.* from ItemMaster im" +
116+
" where exists (select ol.* from OrderLine ol where ol.item_id = im.item_id)" +
117+
" order by item_id"
118+
assertThat(selectStatement.selectStatement).isEqualTo(expectedStatement)
119+
120+
val rows = mapper.selectManyMappedRows(selectStatement)
121+
assertThat(rows).hasSize(3)
122+
123+
with(rows[0]) {
124+
assertThat(this).containsEntry("ITEM_ID", 22)
125+
assertThat(this).containsEntry("DESCRIPTION", "Helmet")
126+
}
127+
128+
with(rows[1]) {
129+
assertThat(this).containsEntry("ITEM_ID", 33)
130+
assertThat(this).containsEntry("DESCRIPTION", "First Base Glove")
131+
}
132+
133+
with(rows[2]) {
134+
assertThat(this).containsEntry("ITEM_ID", 44)
135+
assertThat(this).containsEntry("DESCRIPTION", "Outfield Glove")
136+
}
137+
}
138+
}
139+
97140
@Test
98141
fun testNotExists() {
99142
newSession().use { session ->

0 commit comments

Comments
 (0)