Skip to content

Commit f888b83

Browse files
committed
New join syntax for Kotlin
1 parent 3cd872b commit f888b83

File tree

5 files changed

+192
-115
lines changed

5 files changed

+192
-115
lines changed

src/main/java/org/mybatis/dynamic/sql/select/render/JoinRenderer.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.mybatis.dynamic.sql.select.render;
1717

18-
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19-
2018
import java.util.Objects;
2119
import java.util.stream.Collectors;
2220

@@ -26,6 +24,7 @@
2624
import org.mybatis.dynamic.sql.select.join.JoinSpecification;
2725
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
2826
import org.mybatis.dynamic.sql.util.FragmentCollector;
27+
import org.mybatis.dynamic.sql.util.Messages;
2928

3029
public class JoinRenderer {
3130
private final JoinModel joinModel;
@@ -46,22 +45,17 @@ public FragmentAndParameters render() {
4645
}
4746

4847
private FragmentAndParameters renderJoinSpecification(JoinSpecification joinSpecification) {
49-
FragmentAndParameters renderedTable = joinSpecification.table().accept(tableExpressionRenderer);
50-
FragmentAndParameters renderedJoinSpecification = JoinSpecificationRenderer
48+
FragmentCollector fc = new FragmentCollector();
49+
fc.add(FragmentAndParameters.fromFragment(joinSpecification.joinType().type()));
50+
fc.add(joinSpecification.table().accept(tableExpressionRenderer));
51+
fc.add(JoinSpecificationRenderer
5152
.withJoinSpecification(joinSpecification)
5253
.withRenderingContext(renderingContext)
5354
.build()
5455
.render()
55-
.orElseThrow(() -> new InvalidSqlException("Join Specifications Must Render")); // TODO
56-
57-
String fragment = joinSpecification.joinType().type()
58-
+ spaceBefore(renderedTable.fragment())
59-
+ spaceBefore(renderedJoinSpecification.fragment());
56+
.orElseThrow(() -> new InvalidSqlException(Messages.getString("ERROR.46")))); //$NON-NLS-1$
6057

61-
return FragmentAndParameters.withFragment(fragment)
62-
.withParameters(renderedTable.parameters())
63-
.withParameters(renderedJoinSpecification.parameters())
64-
.build();
58+
return fc.toFragmentAndParameters(Collectors.joining(" ")); //$NON-NLS-1$
6559
}
6660

6761
public static Builder withJoinModel(JoinModel joinModel) {

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/JoinCollector.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,11 @@ class JoinCollector {
2828
internal fun initialCriterion() = invalidIfNull(criteriaCollector.initialCriterion, "ERROR.22") //$NON-NLS-1$
2929
internal fun subCriteria() = criteriaCollector.subCriteria
3030

31-
fun on (receiver: GroupingCriteriaReceiver) {
32-
assertNull(criteriaCollector.initialCriterion, "ERROR.45") //$NON-NLS-1$
33-
criteriaCollector.apply(receiver)
34-
}
35-
36-
@Deprecated("Please replace with the \"on\" lambda expression", level = DeprecationLevel.WARNING)
3731
fun <T> on(leftColumn: BindableColumn<T>): RightColumnCollector<T> = RightColumnCollector {
3832
assertNull(criteriaCollector.initialCriterion, "ERROR.45") //$NON-NLS-1$
3933
criteriaCollector.apply { leftColumn.invoke(it) }
4034
}
4135

42-
@Deprecated("Please move the \"and\" expression into an \"on\" lambda", level = DeprecationLevel.WARNING)
4336
fun <T> and(leftColumn: BindableColumn<T>): RightColumnCollector<T> = RightColumnCollector {
4437
criteriaCollector.and { leftColumn.invoke(it) }
4538
}

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinBaseBuilders.kt

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,19 @@ abstract class KotlinBaseBuilder<D : AbstractWhereStarter<*,*>> {
6464
@Suppress("TooManyFunctions")
6565
abstract class KotlinBaseJoiningBuilder<D : AbstractQueryExpressionDSL<*, *>> : KotlinBaseBuilder<D>() {
6666

67+
@Deprecated("Please use the new form with the \"on\" keyword outside the lambda")
6768
fun join(table: SqlTable, joinCriteria: JoinReceiver): Unit =
6869
applyToDsl(joinCriteria) { jc ->
6970
join(table, jc.initialCriterion(), jc.subCriteria())
7071
}
7172

73+
@Deprecated("Please use the new form with the \"on\" keyword outside the lambda")
7274
fun join(table: SqlTable, alias: String, joinCriteria: JoinReceiver): Unit =
7375
applyToDsl(joinCriteria) { jc ->
7476
join(table, alias, jc.initialCriterion(), jc.subCriteria())
7577
}
7678

79+
@Deprecated("Please use the new form with the \"on\" keyword outside the lambda")
7780
fun join(
7881
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
7982
joinCriteria: JoinReceiver
@@ -82,16 +85,36 @@ abstract class KotlinBaseJoiningBuilder<D : AbstractQueryExpressionDSL<*, *>> :
8285
join(sq, sq.correlationName, jc.initialCriterion(), jc.subCriteria())
8386
}
8487

88+
fun join(table: SqlTable): JoinCriteriaGatherer =
89+
JoinCriteriaGatherer {
90+
getDsl().join(table, it.initialCriterion, it.subCriteria)
91+
}
92+
93+
fun join(table: SqlTable, alias: String): JoinCriteriaGatherer =
94+
JoinCriteriaGatherer {
95+
getDsl().join(table, alias, it.initialCriterion, it.subCriteria)
96+
}
97+
98+
fun join(
99+
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit): JoinCriteriaGatherer =
100+
JoinCriteriaGatherer {
101+
val sq = KotlinQualifiedSubQueryBuilder().apply(subQuery)
102+
getDsl().join(sq, sq.correlationName, it.initialCriterion, it.subCriteria)
103+
}
104+
105+
@Deprecated("Please use the new form with the \"on\" keyword outside the lambda")
85106
fun fullJoin(table: SqlTable, joinCriteria: JoinReceiver): Unit =
86107
applyToDsl(joinCriteria) { jc ->
87108
fullJoin(table, jc.initialCriterion(), jc.subCriteria())
88109
}
89110

111+
@Deprecated("Please use the new form with the \"on\" keyword outside the lambda")
90112
fun fullJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): Unit =
91113
applyToDsl(joinCriteria) { jc ->
92114
fullJoin(table, alias, jc.initialCriterion(), jc.subCriteria())
93115
}
94116

117+
@Deprecated("Please use the new form with the \"on\" keyword outside the lambda")
95118
fun fullJoin(
96119
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
97120
joinCriteria: JoinReceiver
@@ -100,16 +123,36 @@ abstract class KotlinBaseJoiningBuilder<D : AbstractQueryExpressionDSL<*, *>> :
100123
fullJoin(sq, sq.correlationName, jc.initialCriterion(), jc.subCriteria())
101124
}
102125

126+
fun fullJoin(table: SqlTable): JoinCriteriaGatherer =
127+
JoinCriteriaGatherer {
128+
getDsl().fullJoin(table, it.initialCriterion, it.subCriteria)
129+
}
130+
131+
fun fullJoin(table: SqlTable, alias: String): JoinCriteriaGatherer =
132+
JoinCriteriaGatherer {
133+
getDsl().fullJoin(table, alias, it.initialCriterion, it.subCriteria)
134+
}
135+
136+
fun fullJoin(
137+
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit): JoinCriteriaGatherer =
138+
JoinCriteriaGatherer {
139+
val sq = KotlinQualifiedSubQueryBuilder().apply(subQuery)
140+
getDsl().fullJoin(sq, sq.correlationName, it.initialCriterion, it.subCriteria)
141+
}
142+
143+
@Deprecated("Please use the new form with the \"on\" keyword outside the lambda")
103144
fun leftJoin(table: SqlTable, joinCriteria: JoinReceiver): Unit =
104145
applyToDsl(joinCriteria) { jc ->
105146
leftJoin(table, jc.initialCriterion(), jc.subCriteria())
106147
}
107148

149+
@Deprecated("Please use the new form with the \"on\" keyword outside the lambda")
108150
fun leftJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): Unit =
109151
applyToDsl(joinCriteria) { jc ->
110152
leftJoin(table, alias, jc.initialCriterion(), jc.subCriteria())
111153
}
112154

155+
@Deprecated("Please use the new form with the \"on\" keyword outside the lambda")
113156
fun leftJoin(
114157
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
115158
joinCriteria: JoinReceiver
@@ -118,16 +161,36 @@ abstract class KotlinBaseJoiningBuilder<D : AbstractQueryExpressionDSL<*, *>> :
118161
leftJoin(sq, sq.correlationName, jc.initialCriterion(), jc.subCriteria())
119162
}
120163

164+
fun leftJoin(table: SqlTable): JoinCriteriaGatherer =
165+
JoinCriteriaGatherer {
166+
getDsl().leftJoin(table, it.initialCriterion, it.subCriteria)
167+
}
168+
169+
fun leftJoin(table: SqlTable, alias: String): JoinCriteriaGatherer =
170+
JoinCriteriaGatherer {
171+
getDsl().leftJoin(table, alias, it.initialCriterion, it.subCriteria)
172+
}
173+
174+
fun leftJoin(
175+
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit): JoinCriteriaGatherer =
176+
JoinCriteriaGatherer {
177+
val sq = KotlinQualifiedSubQueryBuilder().apply(subQuery)
178+
getDsl().leftJoin(sq, sq.correlationName, it.initialCriterion, it.subCriteria)
179+
}
180+
181+
@Deprecated("Please use the new form with the \"on\" keyword outside the lambda")
121182
fun rightJoin(table: SqlTable, joinCriteria: JoinReceiver): Unit =
122183
applyToDsl(joinCriteria) { jc ->
123184
rightJoin(table, jc.initialCriterion(), jc.subCriteria())
124185
}
125186

187+
@Deprecated("Please use the new form with the \"on\" keyword outside the lambda")
126188
fun rightJoin(table: SqlTable, alias: String, joinCriteria: JoinReceiver): Unit =
127189
applyToDsl(joinCriteria) { jc ->
128190
rightJoin(table, alias, jc.initialCriterion(), jc.subCriteria())
129191
}
130192

193+
@Deprecated("Please use the new form with the \"on\" keyword outside the lambda")
131194
fun rightJoin(
132195
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit,
133196
joinCriteria: JoinReceiver
@@ -136,6 +199,23 @@ abstract class KotlinBaseJoiningBuilder<D : AbstractQueryExpressionDSL<*, *>> :
136199
rightJoin(sq, sq.correlationName, jc.initialCriterion(), jc.subCriteria())
137200
}
138201

202+
fun rightJoin(table: SqlTable): JoinCriteriaGatherer =
203+
JoinCriteriaGatherer {
204+
getDsl().rightJoin(table, it.initialCriterion, it.subCriteria)
205+
}
206+
207+
fun rightJoin(table: SqlTable, alias: String): JoinCriteriaGatherer =
208+
JoinCriteriaGatherer {
209+
getDsl().rightJoin(table, alias, it.initialCriterion, it.subCriteria)
210+
}
211+
212+
fun rightJoin(
213+
subQuery: KotlinQualifiedSubQueryBuilder.() -> Unit): JoinCriteriaGatherer =
214+
JoinCriteriaGatherer {
215+
val sq = KotlinQualifiedSubQueryBuilder().apply(subQuery)
216+
getDsl().rightJoin(sq, sq.correlationName, it.initialCriterion, it.subCriteria)
217+
}
218+
139219
private fun applyToDsl(joinCriteria: JoinReceiver, applyJoin: D.(JoinCollector) -> Unit) {
140220
getDsl().applyJoin(JoinCollector().apply(joinCriteria))
141221
}
@@ -148,3 +228,11 @@ abstract class KotlinBaseJoiningBuilder<D : AbstractQueryExpressionDSL<*, *>> :
148228
getDsl().applyJoin(KotlinQualifiedSubQueryBuilder().apply(subQuery), JoinCollector().apply(joinCriteria))
149229
}
150230
}
231+
232+
class JoinCriteriaGatherer(private val consumer: (GroupingCriteriaCollector) -> Unit) {
233+
infix fun on (joinCriteria: GroupingCriteriaReceiver): Unit =
234+
with(GroupingCriteriaCollector().apply(joinCriteria)) {
235+
assertTrue(initialCriterion != null || subCriteria.isNotEmpty(), "ERROR.22") //$NON-NLS-1$
236+
consumer.invoke(this)
237+
}
238+
}

src/main/resources/org/mybatis/dynamic/sql/util/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,5 @@ ERROR.42=You cannot call `else` in a Kotlin case expression more than once
6262
ERROR.43=A Kotlin cast expression must have one, and only one, `as` element
6363
ERROR.44={0} conditions must contain at least one value
6464
ERROR.45=You cannot call "on" in a Kotlin join expression more than once
65+
ERROR.46=At least one join criterion must render
6566
INTERNAL.ERROR=Internal Error {0}

0 commit comments

Comments
 (0)