Skip to content

Commit 449004d

Browse files
committed
Add methods that allow us to remove use of the Kotlin spread operator
Arrays are always turned into lists eventually, so we can support that at the edges.
1 parent 0dbd06f commit 449004d

File tree

8 files changed

+72
-19
lines changed

8 files changed

+72
-19
lines changed

src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,18 @@ static FromGatherer<SelectModel> select(BasicColumn...selectList) {
141141
return SelectDSL.select(selectList);
142142
}
143143

144+
static FromGatherer<SelectModel> select(Collection<BasicColumn> selectList) {
145+
return SelectDSL.select(selectList);
146+
}
147+
144148
static FromGatherer<SelectModel> selectDistinct(BasicColumn...selectList) {
145149
return SelectDSL.selectDistinct(selectList);
146150
}
147151

152+
static FromGatherer<SelectModel> selectDistinct(Collection<BasicColumn> selectList) {
153+
return SelectDSL.selectDistinct(selectList);
154+
}
155+
148156
static UpdateDSL<UpdateModel> update(SqlTable table) {
149157
return UpdateDSL.update(table);
150158
}

src/main/java/org/mybatis/dynamic/sql/select/QueryExpressionDSL.java

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

18+
import java.util.ArrayList;
1819
import java.util.Arrays;
20+
import java.util.Collection;
1921
import java.util.List;
2022
import java.util.Objects;
2123

@@ -45,7 +47,7 @@ public class QueryExpressionDSL<R> extends AbstractQueryExpressionDSL<QueryExpre
4547
QueryExpressionDSL(FromGatherer<R> fromGatherer) {
4648
super(fromGatherer.table);
4749
connector = fromGatherer.connector;
48-
selectList = Arrays.asList(fromGatherer.selectList);
50+
selectList = fromGatherer.selectList;
4951
isDistinct = fromGatherer.isDistinct;
5052
selectDSL = Objects.requireNonNull(fromGatherer.selectDSL);
5153
}
@@ -155,7 +157,7 @@ protected QueryExpressionDSL<R> getThis() {
155157

156158
public static class FromGatherer<R> {
157159
private String connector;
158-
private BasicColumn[] selectList;
160+
private List<BasicColumn> selectList;
159161
private SelectDSL<R> selectDSL;
160162
private boolean isDistinct;
161163
private SqlTable table;
@@ -179,7 +181,7 @@ public QueryExpressionDSL<R> from(SqlTable table, String tableAlias) {
179181

180182
public static class Builder<R> {
181183
private String connector;
182-
private BasicColumn[] selectList;
184+
private List<BasicColumn> selectList = new ArrayList<>();
183185
private SelectDSL<R> selectDSL;
184186
private boolean isDistinct;
185187

@@ -188,8 +190,8 @@ public Builder<R> withConnector(String connector) {
188190
return this;
189191
}
190192

191-
public Builder<R> withSelectList(BasicColumn[] selectList) {
192-
this.selectList = selectList;
193+
public Builder<R> withSelectList(Collection<BasicColumn> selectList) {
194+
this.selectList.addAll(selectList);
193195
return this;
194196
}
195197

@@ -430,6 +432,10 @@ public UnionBuilder(String connector) {
430432
}
431433

432434
public FromGatherer<R> select(BasicColumn...selectList) {
435+
return select(Arrays.asList(selectList));
436+
}
437+
438+
public FromGatherer<R> select(List<BasicColumn> selectList) {
433439
return new FromGatherer.Builder<R>()
434440
.withConnector(connector)
435441
.withSelectList(selectList)
@@ -438,6 +444,10 @@ public FromGatherer<R> select(BasicColumn...selectList) {
438444
}
439445

440446
public FromGatherer<R> selectDistinct(BasicColumn...selectList) {
447+
return selectDistinct(Arrays.asList(selectList));
448+
}
449+
450+
public FromGatherer<R> selectDistinct(List<BasicColumn> selectList) {
441451
return new FromGatherer.Builder<R>()
442452
.withConnector(connector)
443453
.withSelectList(selectList)

src/main/java/org/mybatis/dynamic/sql/select/SelectDSL.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package org.mybatis.dynamic.sql.select;
1717

1818
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.Collection;
1921
import java.util.List;
2022
import java.util.Objects;
2123
import java.util.function.Function;
@@ -49,23 +51,41 @@ private SelectDSL(Function<SelectModel, R> adapterFunction) {
4951
}
5052

5153
public static QueryExpressionDSL.FromGatherer<SelectModel> select(BasicColumn...selectList) {
54+
return select(Arrays.asList(selectList));
55+
}
56+
57+
public static QueryExpressionDSL.FromGatherer<SelectModel> select(Collection<BasicColumn> selectList) {
5258
return select(Function.identity(), selectList);
5359
}
5460

5561
public static <R> QueryExpressionDSL.FromGatherer<R> select(Function<SelectModel, R> adapterFunction,
5662
BasicColumn...selectList) {
63+
return select(adapterFunction, Arrays.asList(selectList));
64+
}
65+
66+
public static <R> QueryExpressionDSL.FromGatherer<R> select(Function<SelectModel, R> adapterFunction,
67+
Collection<BasicColumn> selectList) {
5768
return new FromGatherer.Builder<R>()
5869
.withSelectList(selectList)
5970
.withSelectDSL(new SelectDSL<>(adapterFunction))
6071
.build();
6172
}
6273

6374
public static QueryExpressionDSL.FromGatherer<SelectModel> selectDistinct(BasicColumn...selectList) {
75+
return selectDistinct(Arrays.asList(selectList));
76+
}
77+
78+
public static QueryExpressionDSL.FromGatherer<SelectModel> selectDistinct(Collection<BasicColumn> selectList) {
6479
return selectDistinct(Function.identity(), selectList);
6580
}
6681

6782
public static <R> QueryExpressionDSL.FromGatherer<R> selectDistinct(Function<SelectModel, R> adapterFunction,
6883
BasicColumn...selectList) {
84+
return selectDistinct(adapterFunction, Arrays.asList(selectList));
85+
}
86+
87+
public static <R> QueryExpressionDSL.FromGatherer<R> selectDistinct(Function<SelectModel, R> adapterFunction,
88+
Collection<BasicColumn> selectList) {
6989
return new FromGatherer.Builder<R>()
7090
.withSelectList(selectList)
7191
.withSelectDSL(new SelectDSL<>(adapterFunction))

src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3Utils.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public static <R> List<R> selectDistinct(Function<SelectStatementProvider, List<
8888
return selectDistinct(mapper, SqlBuilder.selectDistinct(selectList).from(table), completer);
8989
}
9090

91+
public static <R> List<R> selectDistinct(Function<SelectStatementProvider, List<R>> mapper,
92+
Collection<BasicColumn> selectList, SqlTable table, SelectDSLCompleter completer) {
93+
return selectDistinct(mapper, SqlBuilder.selectDistinct(selectList).from(table), completer);
94+
}
95+
9196
public static <R> List<R> selectDistinct(Function<SelectStatementProvider, List<R>> mapper,
9297
QueryExpressionDSL<SelectModel> start, SelectDSLCompleter completer) {
9398
return mapper.apply(completer.apply(start).build().render(RenderingStrategies.MYBATIS3));
@@ -98,6 +103,11 @@ public static <R> List<R> selectList(Function<SelectStatementProvider, List<R>>
98103
return selectList(mapper, SqlBuilder.select(selectList).from(table), completer);
99104
}
100105

106+
public static <R> List<R> selectList(Function<SelectStatementProvider, List<R>> mapper,
107+
Collection<BasicColumn> selectList, SqlTable table, SelectDSLCompleter completer) {
108+
return selectList(mapper, SqlBuilder.select(selectList).from(table), completer);
109+
}
110+
101111
public static <R> List<R> selectList(Function<SelectStatementProvider, List<R>> mapper,
102112
QueryExpressionDSL<SelectModel> start, SelectDSLCompleter completer) {
103113
return mapper.apply(completer.apply(start).build().render(RenderingStrategies.MYBATIS3));
@@ -109,6 +119,12 @@ public static <R> R selectOne(Function<SelectStatementProvider, R> mapper,
109119
return selectOne(mapper, SqlBuilder.select(selectList).from(table), completer);
110120
}
111121

122+
@Nullable
123+
public static <R> R selectOne(Function<SelectStatementProvider, R> mapper,
124+
Collection<BasicColumn> selectList, SqlTable table, SelectDSLCompleter completer) {
125+
return selectOne(mapper, SqlBuilder.select(selectList).from(table), completer);
126+
}
127+
112128
@Nullable
113129
public static <R> R selectOne(Function<SelectStatementProvider, R> mapper,
114130
QueryExpressionDSL<SelectModel> start,
@@ -117,15 +133,14 @@ public static <R> R selectOne(Function<SelectStatementProvider, R> mapper,
117133
}
118134

119135
@NotNull
120-
public static <R> Optional<R> selectOptional(Function<SelectStatementProvider, Optional<R>> mapper,
121-
BasicColumn[] selectList, SqlTable table, SelectDSLCompleter completer) {
136+
public static <R> Optional<R> selectOptional(Function<SelectStatementProvider, Optional<R>> mapper,
137+
BasicColumn[] selectList, SqlTable table, SelectDSLCompleter completer) {
122138
return selectOptional(mapper, SqlBuilder.select(selectList).from(table), completer);
123139
}
124140

125141
@NotNull
126-
public static <R> Optional<R> selectOptional(Function<SelectStatementProvider, Optional<R>> mapper,
127-
QueryExpressionDSL<SelectModel> start,
128-
SelectDSLCompleter completer) {
142+
public static <R> Optional<R> selectOptional(Function<SelectStatementProvider, Optional<R>> mapper,
143+
QueryExpressionDSL<SelectModel> start, SelectDSLCompleter completer) {
129144
return mapper.apply(completer.apply(start).build().render(RenderingStrategies.MYBATIS3));
130145
}
131146

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/UtilityFunctions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ fun update(table: SqlTable, complete: UpdateCompleter) =
5757
* Function to handle platform type issues. Use this function, instead of calling MyBatis3Utils directly, to
5858
* avoid having to specify a return type explicitly.
5959
*/
60-
fun <T> selectDistinct(mapper: (SelectStatementProvider) -> List<T>, selectList: Array<out BasicColumn>, table: SqlTable,
60+
fun <T> selectDistinct(mapper: (SelectStatementProvider) -> List<T>, selectList: List<BasicColumn>, table: SqlTable,
6161
completer: SelectCompleter): List<T> =
6262
MyBatis3Utils.selectDistinct(mapper, selectList, table, completer)
6363

6464
/**
6565
* Function to handle platform type issues. Use this function, instead of calling MyBatis3Utils directly, to
6666
* avoid having to specify a return type explicitly.
6767
*/
68-
fun <T> selectList(mapper: (SelectStatementProvider) -> List<T>, selectList: Array<out BasicColumn>, table: SqlTable,
68+
fun <T> selectList(mapper: (SelectStatementProvider) -> List<T>, selectList: List<BasicColumn>, table: SqlTable,
6969
completer: SelectCompleter): List<T> =
7070
MyBatis3Utils.selectList(mapper, selectList, table, completer)

src/site/markdown/docs/kotlin.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ interface PersonMapper {
5454
And then extensions could be added to make a shortcut method as follows:
5555

5656
```kotlin
57-
private val columnList = arrayOf(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId)
57+
private val columnList = listOf(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId)
5858

59-
fun PersonMapper.select(completer: SelectCompleter): List<PersonRecord> =
59+
fun PersonMapper.select(completer: SelectCompleter) =
6060
selectList(this::selectMany, columnList, Person, completer)
6161
```
6262

@@ -251,7 +251,7 @@ interface PersonMapper {
251251
These methods can be used to create simplified select methods with Kotlin extension methods:
252252

253253
```kotlin
254-
private val columnList = arrayOf(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId)
254+
private val columnList = listOf(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId)
255255

256256
fun PersonMapper.selectOne(completer: SelectCompleter) =
257257
MyBatis3Utils.selectOne(this::selectOne, columnList, Person, completer)

src/test/kotlin/examples/kotlin/canonical/PersonMapperExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fun PersonMapper.insertSelective(record: PersonRecord) =
8383
map(addressId).toPropertyWhenPresent("addressId", record::addressId)
8484
}
8585

86-
private val columnList = arrayOf(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId)
86+
private val columnList = listOf(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId)
8787

8888
fun PersonMapper.selectOne(completer: SelectCompleter) =
8989
MyBatis3Utils.selectOne(this::selectOne, columnList, Person, completer)

src/test/kotlin/examples/kotlin/canonical/PersonWithAddressMapperExtensions.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ import org.mybatis.dynamic.sql.util.kotlin.fromJoining
2929
import org.mybatis.dynamic.sql.util.kotlin.fullJoin
3030
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils
3131

32-
private val columnList = arrayOf(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, Address.id,
32+
private val columnList = listOf(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, Address.id,
3333
Address.streetAddress, Address.city, Address.state)
3434

3535
fun PersonWithAddressMapper.selectOne(completer: SelectCompleter): PersonWithAddress? {
36-
val start = select(*columnList).fromJoining(Person) {
36+
val start = select(columnList).fromJoining(Person) {
3737
fullJoin(Address) {
3838
on(Person.addressId, equalTo(Address.id))
3939
}
@@ -43,7 +43,7 @@ fun PersonWithAddressMapper.selectOne(completer: SelectCompleter): PersonWithAdd
4343
}
4444

4545
fun PersonWithAddressMapper.select(completer: SelectCompleter): List<PersonWithAddress> {
46-
val start = select(*columnList).fromJoining(Person, "p") {
46+
val start = select(columnList).fromJoining(Person, "p") {
4747
fullJoin(Address) {
4848
on(Person.addressId, equalTo(Address.id))
4949
}

0 commit comments

Comments
 (0)