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