Skip to content

Commit 8222fb2

Browse files
authored
Add a Kotlin DSL (#133)
Squashed commit from the dev branch
1 parent baabdb0 commit 8222fb2

File tree

61 files changed

+3973
-265
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+3973
-265
lines changed

pom.xml

Lines changed: 94 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@
1515
limitations under the License.
1616
1717
-->
18-
<!-- Copyright 2016-2019 the original author or authors. Licensed under the
19-
Apache License, Version 2.0 (the "License"); you may not use this file except
20-
in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
21-
Unless required by applicable law or agreed to in writing, software distributed
22-
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
23-
OR CONDITIONS OF ANY KIND, either express or implied. See the License for
24-
the specific language governing permissions and limitations under the License. -->
25-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2621
<modelVersion>4.0.0</modelVersion>
2722
<parent>
2823
<groupId>org.mybatis</groupId>
@@ -45,6 +40,11 @@
4540
<spring.batch.version>4.1.2.RELEASE</spring.batch.version>
4641
<clirr.comparisonVersion>1.1.0</clirr.comparisonVersion>
4742
<module.name>org.mybatis.dynamic.sql</module.name>
43+
<kotlin.version>1.3.50</kotlin.version>
44+
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
45+
<sonar.sources>pom.xml,src/main/java,src/main/kotlin</sonar.sources>
46+
<sonar.tests>src/test/java,src/test/kotlin</sonar.tests>
47+
<jacoco.version>0.8.4</jacoco.version>
4848
</properties>
4949

5050
<build>
@@ -60,6 +60,68 @@
6060
</plugins>
6161
</pluginManagement>
6262
<plugins>
63+
<plugin>
64+
<groupId>org.jetbrains.kotlin</groupId>
65+
<artifactId>kotlin-maven-plugin</artifactId>
66+
<version>${kotlin.version}</version>
67+
<executions>
68+
<execution>
69+
<id>compile</id>
70+
<goals>
71+
<goal>compile</goal>
72+
</goals>
73+
<configuration>
74+
<sourceDirs>
75+
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
76+
<sourceDir>${project.basedir}/src/main/java</sourceDir>
77+
</sourceDirs>
78+
</configuration>
79+
</execution>
80+
<execution>
81+
<id>test-compile</id>
82+
<goals>
83+
<goal>test-compile</goal>
84+
</goals>
85+
<configuration>
86+
<sourceDirs>
87+
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
88+
<sourceDir>${project.basedir}/src/test/java</sourceDir>
89+
</sourceDirs>
90+
</configuration>
91+
</execution>
92+
</executions>
93+
</plugin>
94+
<plugin>
95+
<groupId>org.apache.maven.plugins</groupId>
96+
<artifactId>maven-compiler-plugin</artifactId>
97+
<executions>
98+
<!-- Replacing default-compile as it is treated specially by maven -->
99+
<execution>
100+
<id>default-compile</id>
101+
<phase>none</phase>
102+
</execution>
103+
<!-- Replacing default-testCompile as it is treated specially by
104+
maven -->
105+
<execution>
106+
<id>default-testCompile</id>
107+
<phase>none</phase>
108+
</execution>
109+
<execution>
110+
<id>java-compile</id>
111+
<phase>compile</phase>
112+
<goals>
113+
<goal>compile</goal>
114+
</goals>
115+
</execution>
116+
<execution>
117+
<id>java-test-compile</id>
118+
<phase>test-compile</phase>
119+
<goals>
120+
<goal>testCompile</goal>
121+
</goals>
122+
</execution>
123+
</executions>
124+
</plugin>
63125
<!-- Copy the changelog into the generated site -->
64126
<plugin>
65127
<artifactId>maven-resources-plugin</artifactId>
@@ -82,6 +144,24 @@
82144
</execution>
83145
</executions>
84146
</plugin>
147+
<plugin>
148+
<groupId>com.github.ozsie</groupId>
149+
<artifactId>detekt-maven-plugin</artifactId>
150+
<version>1.0.0</version>
151+
<executions>
152+
<execution>
153+
<phase>verify</phase>
154+
<goals><goal>check</goal></goals>
155+
</execution>
156+
</executions>
157+
</plugin>
158+
<plugin>
159+
<groupId>org.eluder.coveralls</groupId>
160+
<artifactId>coveralls-maven-plugin</artifactId>
161+
<configuration>
162+
<sourceDirectories>src/main/java,src/main/kotlin</sourceDirectories>
163+
</configuration>
164+
</plugin>
85165
</plugins>
86166
</build>
87167

@@ -98,6 +178,12 @@
98178
</reporting>
99179

100180
<dependencies>
181+
<dependency>
182+
<groupId>org.jetbrains.kotlin</groupId>
183+
<artifactId>kotlin-stdlib-jdk8</artifactId>
184+
<version>${kotlin.version}</version>
185+
<scope>provided</scope>
186+
</dependency>
101187
<dependency>
102188
<groupId>org.junit.jupiter</groupId>
103189
<artifactId>junit-jupiter-api</artifactId>

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.mybatis.dynamic.sql.insert.InsertDSL;
2626
import org.mybatis.dynamic.sql.insert.InsertSelectDSL;
2727
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
28+
import org.mybatis.dynamic.sql.select.CountDSL;
2829
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
2930
import org.mybatis.dynamic.sql.select.SelectDSL;
3031
import org.mybatis.dynamic.sql.select.SelectModel;
@@ -43,10 +44,9 @@
4344
import org.mybatis.dynamic.sql.select.function.Substring;
4445
import org.mybatis.dynamic.sql.select.function.Subtract;
4546
import org.mybatis.dynamic.sql.select.function.Upper;
46-
import org.mybatis.dynamic.sql.select.join.AndJoinCriterion;
4747
import org.mybatis.dynamic.sql.select.join.EqualTo;
4848
import org.mybatis.dynamic.sql.select.join.JoinCondition;
49-
import org.mybatis.dynamic.sql.select.join.OnJoinCriterion;
49+
import org.mybatis.dynamic.sql.select.join.JoinCriterion;
5050
import org.mybatis.dynamic.sql.update.UpdateDSL;
5151
import org.mybatis.dynamic.sql.update.UpdateModel;
5252
import org.mybatis.dynamic.sql.util.Buildable;
@@ -103,6 +103,10 @@
103103
public interface SqlBuilder {
104104

105105
// statements
106+
static CountDSL<SelectModel> countFrom(SqlTable table) {
107+
return CountDSL.countFrom(table);
108+
}
109+
106110
static DeleteDSL<DeleteModel> deleteFrom(SqlTable table) {
107111
return DeleteDSL.deleteFrom(table);
108112
}
@@ -192,15 +196,17 @@ static <T> SqlCriterion<T> and(BindableColumn<T> column, VisitableCondition<T> c
192196
}
193197

194198
// join support
195-
static AndJoinCriterion and(BasicColumn joinColumn, JoinCondition joinCondition) {
196-
return new AndJoinCriterion.Builder()
199+
static JoinCriterion and(BasicColumn joinColumn, JoinCondition joinCondition) {
200+
return new JoinCriterion.Builder()
201+
.withConnector("and") //$NON-NLS-1$
197202
.withJoinColumn(joinColumn)
198203
.withJoinCondition(joinCondition)
199204
.build();
200205
}
201206

202-
static OnJoinCriterion on(BasicColumn joinColumn, JoinCondition joinCondition) {
203-
return new OnJoinCriterion.Builder()
207+
static JoinCriterion on(BasicColumn joinColumn, JoinCondition joinCondition) {
208+
return new JoinCriterion.Builder()
209+
.withConnector("on") //$NON-NLS-1$
204210
.withJoinColumn(joinColumn)
205211
.withJoinCondition(joinCondition)
206212
.build();
@@ -547,7 +553,16 @@ static <T> IsNotLikeWhenPresent<T> isNotLikeWhenPresent(T value) {
547553
static <T> IsNotLikeWhenPresent<T> isNotLikeWhenPresent(Supplier<T> valueSupplier) {
548554
return IsNotLikeWhenPresent.of(valueSupplier);
549555
}
550-
556+
557+
// shortcuts for booleans
558+
static IsEqualTo<Boolean> isTrue() {
559+
return isEqualTo(Boolean.TRUE);
560+
}
561+
562+
static IsEqualTo<Boolean> isFalse() {
563+
return isEqualTo(Boolean.FALSE);
564+
}
565+
551566
// conditions for strings only
552567
static IsLikeCaseInsensitive isLikeCaseInsensitive(String value) {
553568
return isLikeCaseInsensitive(() -> value);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Objects;
2020
import java.util.Optional;
2121
import java.util.function.Supplier;
22+
import org.jetbrains.annotations.NotNull;
2223

2324
public class SqlTable {
2425

@@ -84,15 +85,18 @@ public String tableNameAtRuntime() {
8485
public <T> SqlColumn<T> allColumns() {
8586
return SqlColumn.of("*", this); //$NON-NLS-1$
8687
}
87-
88+
89+
@NotNull
8890
public <T> SqlColumn<T> column(String name) {
8991
return SqlColumn.of(name, this);
9092
}
9193

94+
@NotNull
9295
public <T> SqlColumn<T> column(String name, JDBCType jdbcType) {
9396
return SqlColumn.of(name, this, jdbcType);
9497
}
9598

99+
@NotNull
96100
public <T> SqlColumn<T> column(String name, JDBCType jdbcType, String typeHandler) {
97101
return SqlColumn.of(name, this, jdbcType).withTypeHandler(typeHandler);
98102
}

src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,23 @@ public class DeleteDSL<R> implements Buildable<R> {
3232

3333
private Function<DeleteModel, R> adapterFunction;
3434
private SqlTable table;
35-
protected DeleteWhereBuilder whereBuilder;
35+
private DeleteWhereBuilder whereBuilder = new DeleteWhereBuilder();
3636

3737
private DeleteDSL(SqlTable table, Function<DeleteModel, R> adapterFunction) {
3838
this.table = Objects.requireNonNull(table);
3939
this.adapterFunction = Objects.requireNonNull(adapterFunction);
4040
}
4141

4242
public DeleteWhereBuilder where() {
43-
whereBuilder = new DeleteWhereBuilder();
44-
return whereBuilder;
45-
}
46-
47-
public <T> DeleteWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition) {
48-
whereBuilder = new DeleteWhereBuilder(column, condition);
4943
return whereBuilder;
5044
}
5145

5246
public <T> DeleteWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition,
5347
SqlCriterion<?>...subCriteria) {
54-
whereBuilder = new DeleteWhereBuilder(column, condition, subCriteria);
48+
whereBuilder.and(column, condition, subCriteria);
5549
return whereBuilder;
5650
}
57-
51+
5852
/**
5953
* WARNING! Calling this method could result in an delete statement that deletes
6054
* all rows in a table.
@@ -64,7 +58,7 @@ public <T> DeleteWhereBuilder where(BindableColumn<T> column, VisitableCondition
6458
@Override
6559
public R build() {
6660
DeleteModel deleteModel = DeleteModel.withTable(table)
67-
.withWhereModel(whereBuilder == null ? null : whereBuilder.buildWhereModel())
61+
.withWhereModel(whereBuilder.buildWhereModel())
6862
.build();
6963
return adapterFunction.apply(deleteModel);
7064
}
@@ -99,15 +93,6 @@ private DeleteWhereBuilder() {
9993
super();
10094
}
10195

102-
private <T> DeleteWhereBuilder(BindableColumn<T> column, VisitableCondition<T> condition) {
103-
super(column, condition);
104-
}
105-
106-
private <T> DeleteWhereBuilder(BindableColumn<T> column, VisitableCondition<T> condition,
107-
SqlCriterion<?>...subCriteria) {
108-
super(column, condition, subCriteria);
109-
}
110-
11196
@Override
11297
public R build() {
11398
return DeleteDSL.this.build();

src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSLCompleter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
2424

2525
/**
26-
* Represents a function that can be used to create a general delete method in the style
27-
* of MyBatis Generator. When using this function, you can create a method that does not require a user to
28-
* call the build() and render() methods - making client code look a bit cleaner.
26+
* Represents a function that can be used to create a simplified delete method. When using this function
27+
* you can create a method that does not require a user to call the build() and render() methods - making
28+
* client code look a bit cleaner.
2929
*
3030
* <p>This function is intended to be used in conjunction with a utility method like
3131
* {@link MyBatis3Utils#deleteFrom(ToIntFunction, SqlTable, DeleteDSLCompleter)}

src/main/java/org/mybatis/dynamic/sql/delete/DeleteModel.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Objects;
1919
import java.util.Optional;
2020

21+
import org.jetbrains.annotations.NotNull;
2122
import org.mybatis.dynamic.sql.SqlTable;
2223
import org.mybatis.dynamic.sql.delete.render.DeleteRenderer;
2324
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
@@ -40,7 +41,8 @@ public SqlTable table() {
4041
public Optional<WhereModel> whereModel() {
4142
return Optional.ofNullable(whereModel);
4243
}
43-
44+
45+
@NotNull
4446
public DeleteStatementProvider render(RenderingStrategy renderingStrategy) {
4547
return DeleteRenderer.withDeleteModel(this)
4648
.withRenderingStrategy(renderingStrategy)

0 commit comments

Comments
 (0)