@@ -6,9 +6,9 @@ DSL. You certainly can use the Java DSL with Kotlin. However, using the more spe
6
6
benefits:
7
7
8
8
1 . The Kotlin DSL generally masks the platform types that are inferred with the underlying Java DSL
9
- 1 . The Kotlin DSL accurately expresses the nullability expectations of the underlying Java DSL
10
- 1 . Using the Kotlin DSL will avoid some confusion with overloaded function names that are present in the Java DSL
11
- 1 . The Kotlin DSL makes extensive use of Kotlin DSL construction features. It more closely mimics actual SQL than the
9
+ 2 . The Kotlin DSL accurately expresses the nullability expectations of the underlying Java DSL
10
+ 3 . Using the Kotlin DSL will avoid some confusion with overloaded function names that are present in the Java DSL
11
+ 4 . The Kotlin DSL makes extensive use of Kotlin DSL construction features. It more closely mimics actual SQL than the
12
12
Java DSL and will likely feel more natural to Kotlin developers
13
13
14
14
We take the customary approach to DSL building in Kotlin in that we attempt to create a somewhat natural feel for SQL,
@@ -41,7 +41,7 @@ generated by the DSL. In general, the DSL can be used to generate the following
41
41
users these objects can be considered intermediate objects and will not need to be accessed directly. However, if
42
42
you want to implement a custom rendering strategy then you might need to work with "model" objects (this is an
43
43
unusual use case)
44
- 1 . "Provider" objects have been rendered into a form that can be used with SQL execution engines
44
+ 2 . "Provider" objects have been rendered into a form that can be used with SQL execution engines
45
45
directly. Currently, the library supports rendering for MyBatis3 and Spring JDBC Template. Most users will interact
46
46
with "provider" objects in some form or another
47
47
@@ -87,7 +87,7 @@ import org.mybatis.dynamic.sql.util.kotlin.elements.column
87
87
import java.util.Date
88
88
89
89
object PersonDynamicSqlSupport {
90
- val person: Person ()
90
+ val person = Person ()
91
91
val id = person.id
92
92
val firstName = person.firstName
93
93
val lastName = person.lastName
@@ -114,9 +114,9 @@ object PersonDynamicSqlSupport {
114
114
Notes:
115
115
116
116
1 . The outer object is a singleton containing the ` SqlTable ` and ` SqlColumn ` objects that map to the database table.
117
- 1 . The inner ` SqlTable ` is declared as a ` class ` rather than an ` object ` - this allows you to create additional
117
+ 2 . The inner ` SqlTable ` is declared as a ` class ` rather than an ` object ` - this allows you to create additional
118
118
instances for use in self-joins.
119
- 1 . Note the use of the ` column ` extension function. This function accepts different
119
+ 3 . Note the use of the ` column ` extension function. This function accepts different
120
120
parameters for the different attributes that can be assigned to a column (such as a MyBatis3 type handler, or a
121
121
custom rendering strategy). We recommend using this extension function rather than the corresponding ` column ` and
122
122
` withXXX ` methods in the Java native DSL because the extension method will retain the non-nullable type information
@@ -132,15 +132,17 @@ The library supports the following types of statements:
132
132
133
133
1 . Count statements of various types - these are specialized select statements that return a single Long column, Count
134
134
statements support where clauses, joins, and subqueries.
135
- 1 . Delete statement with or without a where clause.
136
- 1 . Insert statements of various types:
135
+ 2 . Delete statement with or without a where clause.
136
+ 3 . Insert statements of various types:
137
137
1 . Single row insert - a statement where the insert values are obtained from a record class
138
- 1 . General insert - a statement where the insert values are set directly in the statement
139
- 1 . Multi-row Insert - a statement where the insert values are derived from a collection of records
140
- 1 . Batch insert - a set of insert statements appropriate for use as a JDBC batch
141
- 1 . Insert select - a statement where the insert values are obtained from a select statement
142
- 1 . Select statement that supports joins, subqueries, where clauses, order by clauses, group by clauses, etc.
143
- 1 . Update Statement with or without a where clause
138
+ 2 . General insert - a statement where the insert values are set directly in the statement
139
+ 3 . Multi-row Insert - a statement where the insert values are derived from a collection of records
140
+ 4 . Batch insert - a set of insert statements appropriate for use as a JDBC batch
141
+ 5 . Insert select - a statement where the insert values are obtained from a select statement
142
+ 4 . Select statement that supports joins, subqueries, where clauses, order by clauses, group by clauses, etc.
143
+ 5 . Multi-Select statements - multiple full select statements (including order by and paging clauses) merged together
144
+ with "union" or "union all" operators
145
+ 6 . Update Statement with or without a where clause
144
146
145
147
## Count Statements
146
148
@@ -150,8 +152,8 @@ where clause.
150
152
The library supports three types of count statements:
151
153
152
154
1 . ` count(*) ` - counts the number of rows that match a where clause
153
- 1 . ` count(column) ` - counts the number of non-null column values that match a where clause
154
- 1 . ` count(distinct column) ` - counts the number of unique column values that match a where clause
155
+ 2 . ` count(column) ` - counts the number of non-null column values that match a where clause
156
+ 3 . ` count(distinct column) ` - counts the number of unique column values that match a where clause
155
157
156
158
The DSL for count statements looks like this:
157
159
@@ -174,11 +176,11 @@ val countDistinctColumnStatement = countDistinct(lastName) {
174
176
175
177
These methods create models or providers depending on which package is used:
176
178
177
- | Package | Resulting Object |
178
- | ---| ---|
179
- | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.select.SelectModel |
179
+ | Package | Resulting Object |
180
+ | ---------------------------------------------- | ------------------------------------------------------------------------------------ ---|
181
+ | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.select.SelectModel |
180
182
| org.mybatis.dynamic.sql.util.kotlin.mybatis3 | org.mybatis.dynamic.sql.select.render.SelectStatementProvider (rendered for MyBatis3) |
181
- | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.select.render.SelectStatementProvider (rendered for Spring) |
183
+ | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.select.render.SelectStatementProvider (rendered for Spring) |
182
184
183
185
## Delete Statement
184
186
@@ -202,11 +204,11 @@ val rows = template.deleteFrom(person) {
202
204
203
205
This method creates models or providers depending on which package is used:
204
206
205
- | Package | Resulting Object |
206
- | ---| ---|
207
- | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.delete.DeleteModel |
207
+ | Package | Resulting Object |
208
+ | ---------------------------------------------- | ------------------------------------------------------------------------------------ ---|
209
+ | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.delete.DeleteModel |
208
210
| org.mybatis.dynamic.sql.util.kotlin.mybatis3 | org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider (rendered for MyBatis3) |
209
- | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider (rendered for Spring) |
211
+ | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider (rendered for Spring) |
210
212
211
213
## Single Row Insert Statement
212
214
@@ -247,11 +249,11 @@ as shown above.
247
249
248
250
This method creates models or providers depending on which package is used:
249
251
250
- | Package | Resulting Object |
251
- | ---| ---|
252
- | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.insert.InsertModel |
252
+ | Package | Resulting Object |
253
+ | ---------------------------------------------- | ------------------------------------------------------------------------------------ ---|
254
+ | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.insert.InsertModel |
253
255
| org.mybatis.dynamic.sql.util.kotlin.mybatis3 | org.mybatis.dynamic.sql.insert.render.InsertStatementProvider (rendered for MyBatis3) |
254
- | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.insert.render.InsertStatementProvider (rendered for Spring) |
256
+ | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.insert.render.InsertStatementProvider (rendered for Spring) |
255
257
256
258
## General Insert Statement
257
259
@@ -274,11 +276,11 @@ val generalInsertStatement = insertInto(person) {
274
276
275
277
This method creates models or providers depending on which package is used:
276
278
277
- | Package | Resulting Object |
278
- | ---| ---|
279
- | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.insert.GeneralInsertModel |
279
+ | Package | Resulting Object |
280
+ | ---------------------------------------------- | ------------------------------------------------------------------------------------------- ---|
281
+ | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.insert.GeneralInsertModel |
280
282
| org.mybatis.dynamic.sql.util.kotlin.mybatis3 | org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider (rendered for MyBatis3) |
281
- | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider (rendered for Spring) |
283
+ | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider (rendered for Spring) |
282
284
283
285
## Multi-Row Insert Statement
284
286
@@ -324,11 +326,11 @@ the documentation pages for those utilities.
324
326
325
327
This method creates models or providers depending on which package is used:
326
328
327
- | Package | Resulting Object |
328
- | ---| ---|
329
- | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.insert.MultiRowInsertModel |
329
+ | Package | Resulting Object |
330
+ | ---------------------------------------------- | -------------------------------------------------------------------------------------------- ---|
331
+ | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.insert.MultiRowInsertModel |
330
332
| org.mybatis.dynamic.sql.util.kotlin.mybatis3 | org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider (rendered for MyBatis3) |
331
- | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider (rendered for Spring) |
333
+ | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider (rendered for Spring) |
332
334
333
335
## Batch Insert Statement
334
336
@@ -367,11 +369,11 @@ the documentation pages for those utilities.
367
369
368
370
This method creates models or providers depending on which package is used:
369
371
370
- | Package | Resulting Object |
371
- | ---| ---|
372
- | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.insert.BatchInsertModel |
372
+ | Package | Resulting Object |
373
+ | ---------------------------------------------- | ------------------------------------------------------------------------ ---|
374
+ | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.insert.BatchInsertModel |
373
375
| org.mybatis.dynamic.sql.util.kotlin.mybatis3 | org.mybatis.dynamic.sql.insert.render.BatchInsert (rendered for MyBatis3) |
374
- | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.insert.render.BatchInsert (rendered for Spring) |
376
+ | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.insert.render.BatchInsert (rendered for Spring) |
375
377
376
378
## Insert Select Statement
377
379
@@ -397,11 +399,11 @@ number of rows returned from the query.
397
399
398
400
This method creates models or providers depending on which package is used:
399
401
400
- | Package | Resulting Object |
401
- | ---| ---|
402
- | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.insert.InsertSelectModel |
402
+ | Package | Resulting Object |
403
+ | ---------------------------------------------- | ------------------------------------------------------------------------------------------ ---|
404
+ | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.insert.InsertSelectModel |
403
405
| org.mybatis.dynamic.sql.util.kotlin.mybatis3 | org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider (rendered for MyBatis3) |
404
- | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider (rendered for Spring) |
406
+ | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider (rendered for Spring) |
405
407
406
408
## Select Statement
407
409
@@ -458,11 +460,52 @@ val selectStatement = selectDistinct(id, firstName, lastName, birthDate, employe
458
460
459
461
These methods create models or providers depending on which package is used:
460
462
461
- | Package | Resulting Object |
462
- | ---| ---|
463
- | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.select.SelectModel |
463
+ | Package | Resulting Object |
464
+ | ---------------------------------------------- | ------------------------------------------------------------------------------------ ---|
465
+ | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.select.SelectModel |
464
466
| org.mybatis.dynamic.sql.util.kotlin.mybatis3 | org.mybatis.dynamic.sql.select.render.SelectStatementProvider (rendered for MyBatis3) |
465
- | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.select.render.SelectStatementProvider (rendered for Spring) |
467
+ | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.select.render.SelectStatementProvider (rendered for Spring) |
468
+
469
+ ## Multi-Select Statement
470
+
471
+ A multi-select statement is a special case of a union query. In a multi-select statement, each select statement is
472
+ wrapped with parentheses. This means that you can use "order by" and paging clauses on the select statements that are
473
+ merged with a "union" or "union all" operator. You can also apply "order by" and paging clauses to the query as a whole.
474
+
475
+ The full DSL for multi-select statements looks like this:
476
+
477
+ ``` kotlin
478
+ val selectStatement = multiSelect {
479
+ selectDistinct(id, firstName, lastName, birthDate, employed, occupation, addressId) {
480
+ from(person)
481
+ where { id isLessThanOrEqualTo 2 }
482
+ orderBy(id)
483
+ limit(1 )
484
+ }
485
+ unionAll {
486
+ select(id, firstName, lastName, birthDate, employed, occupation, addressId) {
487
+ from(person)
488
+ where { id isGreaterThanOrEqualTo 4 }
489
+ orderBy(id.descending())
490
+ limit(1 )
491
+ }
492
+ }
493
+ orderBy(id)
494
+ fetchFirst(1 )
495
+ offset(1 )
496
+ }
497
+ ```
498
+
499
+ Each nested select statement can be either "select" or "selectDistinct". They can be merged with either
500
+ "union" or "unionAll". There is no limit to the number of statements that can be merged.
501
+
502
+ These methods create models or providers depending on which package is used:
503
+
504
+ | Package | Resulting Object |
505
+ | ----------------------------------------------| ---------------------------------------------------------------------------------------|
506
+ | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.select.MultiSelectModel |
507
+ | org.mybatis.dynamic.sql.util.kotlin.mybatis3 | org.mybatis.dynamic.sql.select.render.SelectStatementProvider (rendered for MyBatis3) |
508
+ | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.select.render.SelectStatementProvider (rendered for Spring) |
466
509
467
510
## Update Statement
468
511
@@ -482,8 +525,8 @@ If you omit the `where` clause, the statement will update every row in a table.
482
525
483
526
This method creates models or providers depending on which package is used:
484
527
485
- | Package | Resulting Object |
486
- | ---| ---|
487
- | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.update.UpdateModel |
528
+ | Package | Resulting Object |
529
+ | ---------------------------------------------- | ------------------------------------------------------------------------------------ ---|
530
+ | org.mybatis.dynamic.sql.util.kotlin.model | org.mybatis.dynamic.sql.update.UpdateModel |
488
531
| org.mybatis.dynamic.sql.util.kotlin.mybatis3 | org.mybatis.dynamic.sql.update.render.UpdateStatementProvider (rendered for MyBatis3) |
489
- | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.update.render.UpdateStatementProvider (rendered for Spring) |
532
+ | org.mybatis.dynamic.sql.util.kotlin.spring | org.mybatis.dynamic.sql.update.render.UpdateStatementProvider (rendered for Spring) |
0 commit comments