@@ -6,9 +6,9 @@ DSL. You certainly can use the Java DSL with Kotlin. However, using the more spe
66benefits:
77
881 . 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
1212 Java DSL and will likely feel more natural to Kotlin developers
1313
1414We 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
4141 users these objects can be considered intermediate objects and will not need to be accessed directly. However, if
4242 you want to implement a custom rendering strategy then you might need to work with "model" objects (this is an
4343 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
4545 directly. Currently, the library supports rendering for MyBatis3 and Spring JDBC Template. Most users will interact
4646 with "provider" objects in some form or another
4747
@@ -87,7 +87,7 @@ import org.mybatis.dynamic.sql.util.kotlin.elements.column
8787import java.util.Date
8888
8989object PersonDynamicSqlSupport {
90- val person: Person ()
90+ val person = Person ()
9191 val id = person.id
9292 val firstName = person.firstName
9393 val lastName = person.lastName
@@ -114,9 +114,9 @@ object PersonDynamicSqlSupport {
114114Notes:
115115
1161161 . 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
118118 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
120120 parameters for the different attributes that can be assigned to a column (such as a MyBatis3 type handler, or a
121121 custom rendering strategy). We recommend using this extension function rather than the corresponding ` column ` and
122122 ` 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:
132132
1331331 . Count statements of various types - these are specialized select statements that return a single Long column, Count
134134 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:
137137 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
144146
145147## Count Statements
146148
@@ -150,8 +152,8 @@ where clause.
150152The library supports three types of count statements:
151153
1521541 . ` 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
155157
156158The DSL for count statements looks like this:
157159
@@ -174,11 +176,11 @@ val countDistinctColumnStatement = countDistinct(lastName) {
174176
175177These methods create models or providers depending on which package is used:
176178
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 |
180182| 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) |
182184
183185## Delete Statement
184186
@@ -202,11 +204,11 @@ val rows = template.deleteFrom(person) {
202204
203205This method creates models or providers depending on which package is used:
204206
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 |
208210| 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) |
210212
211213## Single Row Insert Statement
212214
@@ -247,11 +249,11 @@ as shown above.
247249
248250This method creates models or providers depending on which package is used:
249251
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 |
253255| 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) |
255257
256258## General Insert Statement
257259
@@ -274,11 +276,11 @@ val generalInsertStatement = insertInto(person) {
274276
275277This method creates models or providers depending on which package is used:
276278
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 |
280282| 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) |
282284
283285## Multi-Row Insert Statement
284286
@@ -324,11 +326,11 @@ the documentation pages for those utilities.
324326
325327This method creates models or providers depending on which package is used:
326328
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 |
330332| 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) |
332334
333335## Batch Insert Statement
334336
@@ -367,11 +369,11 @@ the documentation pages for those utilities.
367369
368370This method creates models or providers depending on which package is used:
369371
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 |
373375| 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) |
375377
376378## Insert Select Statement
377379
@@ -397,11 +399,11 @@ number of rows returned from the query.
397399
398400This method creates models or providers depending on which package is used:
399401
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 |
403405| 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) |
405407
406408## Select Statement
407409
@@ -458,11 +460,52 @@ val selectStatement = selectDistinct(id, firstName, lastName, birthDate, employe
458460
459461These methods create models or providers depending on which package is used:
460462
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 |
464466| 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) |
466509
467510## Update Statement
468511
@@ -482,8 +525,8 @@ If you omit the `where` clause, the statement will update every row in a table.
482525
483526This method creates models or providers depending on which package is used:
484527
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 |
488531| 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