16
16
package org.mybatis.dynamic.sql.util.kotlin
17
17
18
18
import org.mybatis.dynamic.sql.BindableColumn
19
+ import org.mybatis.dynamic.sql.SqlCriterion
19
20
import org.mybatis.dynamic.sql.SqlTable
20
21
import org.mybatis.dynamic.sql.VisitableCondition
21
22
import org.mybatis.dynamic.sql.select.AbstractQueryExpressionDSL
@@ -31,39 +32,39 @@ typealias WhereApplier = AbstractWhereDSL<*>.() -> Unit
31
32
@MyBatisDslMarker
32
33
@Suppress(" TooManyFunctions" )
33
34
abstract class KotlinBaseBuilder <D : AbstractWhereSupportingDSL <* >, B : KotlinBaseBuilder <D , B >> {
34
- fun <T > where (column : BindableColumn <T >, condition : VisitableCondition <T >): B =
35
+ fun <T > where (column : BindableColumn <T >, condition : VisitableCondition <T >) =
35
36
applyToWhere {
36
37
where(column, condition)
37
38
}
38
39
39
- fun <T > where (column : BindableColumn <T >, condition : VisitableCondition <T >, subCriteria : CriteriaReceiver ): B =
40
- applyToWhere {
41
- where(column, condition, CriteriaCollector (). apply (subCriteria).criteria )
40
+ fun <T > where (column : BindableColumn <T >, condition : VisitableCondition <T >, subCriteria : CriteriaReceiver ) =
41
+ applyToWhere(subCriteria) { sq ->
42
+ where(column, condition, sq )
42
43
}
43
44
44
- fun applyWhere (whereApplier : WhereApplier ): B =
45
+ fun applyWhere (whereApplier : WhereApplier ) =
45
46
applyToWhere {
46
47
applyWhere(whereApplier)
47
48
}
48
49
49
- fun <T > and (column : BindableColumn <T >, condition : VisitableCondition <T >): B =
50
+ fun <T > and (column : BindableColumn <T >, condition : VisitableCondition <T >) =
50
51
applyToWhere {
51
52
and (column, condition)
52
53
}
53
54
54
- fun <T > and (column : BindableColumn <T >, condition : VisitableCondition <T >, subCriteria : CriteriaReceiver ): B =
55
- applyToWhere {
56
- and (column, condition, CriteriaCollector (). apply (subCriteria).criteria )
55
+ fun <T > and (column : BindableColumn <T >, condition : VisitableCondition <T >, subCriteria : CriteriaReceiver ) =
56
+ applyToWhere(subCriteria) { sq ->
57
+ and (column, condition, sq )
57
58
}
58
59
59
60
fun <T > or (column : BindableColumn <T >, condition : VisitableCondition <T >) =
60
61
applyToWhere {
61
62
or (column, condition)
62
63
}
63
64
64
- fun <T > or (column : BindableColumn <T >, condition : VisitableCondition <T >, subCriteria : CriteriaReceiver ): B =
65
- applyToWhere {
66
- or (column, condition, CriteriaCollector (). apply (subCriteria).criteria )
65
+ fun <T > or (column : BindableColumn <T >, condition : VisitableCondition <T >, subCriteria : CriteriaReceiver ) =
66
+ applyToWhere(subCriteria) { sq ->
67
+ or (column, condition, sq )
67
68
}
68
69
69
70
fun allRows () = self()
@@ -73,120 +74,106 @@ abstract class KotlinBaseBuilder<D: AbstractWhereSupportingDSL<*>, B: KotlinBase
73
74
return self()
74
75
}
75
76
77
+ private fun applyToWhere (
78
+ subCriteria : CriteriaReceiver ,
79
+ block : AbstractWhereDSL <* >.(List <SqlCriterion <* >>) -> Unit
80
+ ): B {
81
+ getDsl().where().block(CriteriaCollector ().apply (subCriteria).criteria)
82
+ return self()
83
+ }
84
+
76
85
protected abstract fun self (): B
77
86
78
87
protected abstract fun getDsl (): D
79
88
}
80
89
81
90
@Suppress(" TooManyFunctions" )
82
- abstract class KotlinBaseJoiningBuilder <D : AbstractQueryExpressionDSL <* , * , * >, B : KotlinBaseJoiningBuilder <D , B >> : KotlinBaseBuilder <D , B >() {
91
+ abstract class KotlinBaseJoiningBuilder <D : AbstractQueryExpressionDSL <* , * , * >, B : KotlinBaseJoiningBuilder <D , B >>
92
+ : KotlinBaseBuilder <D , B >() {
83
93
84
- fun join (table : SqlTable , joinCriteria : JoinReceiver ): B =
85
- buildAndApply(joinCriteria) {
86
- applyToDsl {
87
- join(table, it.onJoinCriterion, it.andJoinCriteria)
88
- }
94
+ fun join (table : SqlTable , joinCriteria : JoinReceiver ) =
95
+ applyToDsl(joinCriteria) { jc ->
96
+ join(table, jc.onJoinCriterion, jc.andJoinCriteria)
89
97
}
90
98
91
- fun join (table : SqlTable , alias : String , joinCriteria : JoinReceiver ): B =
92
- buildAndApply(joinCriteria) {
93
- applyToDsl {
94
- join(table, alias, it.onJoinCriterion, it.andJoinCriteria)
95
- }
99
+ fun join (table : SqlTable , alias : String , joinCriteria : JoinReceiver ) =
100
+ applyToDsl(joinCriteria) { jc ->
101
+ join(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
96
102
}
97
103
98
104
fun join (
99
105
subQuery : KotlinQualifiedSubQueryBuilder .() -> Unit ,
100
106
joinCriteria : JoinReceiver
101
- ): B =
102
- buildAndApply(joinCriteria) {
103
- val builder = KotlinQualifiedSubQueryBuilder ().apply (subQuery)
104
- applyToDsl {
105
- join(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
106
- }
107
+ ) =
108
+ applyToDsl(subQuery, joinCriteria) { sq, jc ->
109
+ join(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
107
110
}
108
111
109
- fun fullJoin (table : SqlTable , joinCriteria : JoinReceiver ): B =
110
- buildAndApply(joinCriteria) {
111
- applyToDsl {
112
- fullJoin(table, it.onJoinCriterion, it.andJoinCriteria)
113
- }
112
+ fun fullJoin (table : SqlTable , joinCriteria : JoinReceiver ) =
113
+ applyToDsl(joinCriteria) { jc ->
114
+ fullJoin(table, jc.onJoinCriterion, jc.andJoinCriteria)
114
115
}
115
116
116
- fun fullJoin (table : SqlTable , alias : String , joinCriteria : JoinReceiver ): B =
117
- buildAndApply(joinCriteria) {
118
- applyToDsl {
119
- fullJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
120
- }
117
+ fun fullJoin (table : SqlTable , alias : String , joinCriteria : JoinReceiver ) =
118
+ applyToDsl(joinCriteria) { jc ->
119
+ fullJoin(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
121
120
}
122
121
123
122
fun fullJoin (
124
123
subQuery : KotlinQualifiedSubQueryBuilder .() -> Unit ,
125
124
joinCriteria : JoinReceiver
126
- ): B =
127
- buildAndApply(joinCriteria) {
128
- val builder = KotlinQualifiedSubQueryBuilder ().apply (subQuery)
129
- applyToDsl {
130
- fullJoin(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
131
- }
125
+ ) =
126
+ applyToDsl(subQuery, joinCriteria) { sq, jc ->
127
+ fullJoin(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
132
128
}
133
129
134
- fun leftJoin (table : SqlTable , joinCriteria : JoinReceiver ): B =
135
- buildAndApply(joinCriteria) {
136
- applyToDsl {
137
- leftJoin(table, it.onJoinCriterion, it.andJoinCriteria)
138
- }
130
+ fun leftJoin (table : SqlTable , joinCriteria : JoinReceiver ) =
131
+ applyToDsl(joinCriteria) { jc ->
132
+ leftJoin(table, jc.onJoinCriterion, jc.andJoinCriteria)
139
133
}
140
134
141
- fun leftJoin (table : SqlTable , alias : String , joinCriteria : JoinReceiver ): B =
142
- buildAndApply(joinCriteria) {
143
- applyToDsl {
144
- leftJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
145
- }
135
+ fun leftJoin (table : SqlTable , alias : String , joinCriteria : JoinReceiver ) =
136
+ applyToDsl(joinCriteria) { jc ->
137
+ leftJoin(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
146
138
}
147
139
148
140
fun leftJoin (
149
141
subQuery : KotlinQualifiedSubQueryBuilder .() -> Unit ,
150
142
joinCriteria : JoinReceiver
151
- ): B =
152
- buildAndApply(joinCriteria) {
153
- val builder = KotlinQualifiedSubQueryBuilder ().apply (subQuery)
154
- applyToDsl {
155
- leftJoin(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
156
- }
143
+ ) =
144
+ applyToDsl(subQuery, joinCriteria) { sq, jc ->
145
+ leftJoin(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
157
146
}
158
147
159
- fun rightJoin (table : SqlTable , joinCriteria : JoinReceiver ): B =
160
- buildAndApply(joinCriteria) {
161
- applyToDsl {
162
- rightJoin(table, it.onJoinCriterion, it.andJoinCriteria)
163
- }
148
+ fun rightJoin (table : SqlTable , joinCriteria : JoinReceiver ) =
149
+ applyToDsl(joinCriteria) { jc ->
150
+ rightJoin(table, jc.onJoinCriterion, jc.andJoinCriteria)
164
151
}
165
152
166
- fun rightJoin (table : SqlTable , alias : String , joinCriteria : JoinReceiver ): B =
167
- buildAndApply(joinCriteria) {
168
- applyToDsl {
169
- rightJoin(table, alias, it.onJoinCriterion, it.andJoinCriteria)
170
- }
153
+ fun rightJoin (table : SqlTable , alias : String , joinCriteria : JoinReceiver ) =
154
+ applyToDsl(joinCriteria) { jc ->
155
+ rightJoin(table, alias, jc.onJoinCriterion, jc.andJoinCriteria)
171
156
}
172
157
173
158
fun rightJoin (
174
159
subQuery : KotlinQualifiedSubQueryBuilder .() -> Unit ,
175
160
joinCriteria : JoinReceiver
176
- ): B =
177
- buildAndApply(joinCriteria) {
178
- val builder = KotlinQualifiedSubQueryBuilder ().apply (subQuery)
179
- applyToDsl {
180
- rightJoin(builder, builder.correlationName, it.onJoinCriterion, it.andJoinCriteria)
181
- }
161
+ ) =
162
+ applyToDsl(subQuery, joinCriteria) { sq, jc ->
163
+ rightJoin(sq, sq.correlationName, jc.onJoinCriterion, jc.andJoinCriteria)
182
164
}
183
165
184
- private fun buildAndApply (joinCriteria : JoinReceiver , block : (JoinCollector ) -> B ): B {
185
- return block(JoinCollector ().apply (joinCriteria))
166
+ private fun applyToDsl (joinCriteria : JoinReceiver , applyJoin : D .(JoinCollector ) -> Unit ): B {
167
+ getDsl().applyJoin(JoinCollector ().apply (joinCriteria))
168
+ return self()
186
169
}
187
170
188
- private fun applyToDsl (block : D .() -> Unit ): B {
189
- getDsl().apply (block)
171
+ private fun applyToDsl (
172
+ subQuery : KotlinQualifiedSubQueryBuilder .() -> Unit ,
173
+ joinCriteria : JoinReceiver ,
174
+ applyJoin : D .(KotlinQualifiedSubQueryBuilder , JoinCollector ) -> Unit
175
+ ): B {
176
+ getDsl().applyJoin(KotlinQualifiedSubQueryBuilder ().apply (subQuery), JoinCollector ().apply (joinCriteria))
190
177
return self()
191
178
}
192
179
}
0 commit comments