Skip to content

Add a Kotlin DSL #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 46 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
99b6d7d
Initial Kotlin Support and Tests
jeffgbutler Aug 8, 2019
6a5bce3
Merge branch 'master' into kotlin-support
jeffgbutler Aug 9, 2019
276ace8
Updates for join mapper support
jeffgbutler Aug 9, 2019
21ecba3
Start of docs for kotlin
jeffgbutler Aug 9, 2019
ad3213c
Reuse outer class methods for consistency
jeffgbutler Aug 10, 2019
784d7d6
Delegate more QueryDSL methods to the parent
jeffgbutler Aug 11, 2019
7aaaa11
Build QueryExpressionDSL only through SelectDSL
jeffgbutler Aug 14, 2019
8579872
Change paging support to be compatible with Kotlin
jeffgbutler Aug 14, 2019
6814534
Kotlin Compatibility for the DeleteDSL
jeffgbutler Aug 14, 2019
5dbc5fe
Kotlin DSL for Where Clauses
jeffgbutler Aug 15, 2019
590ebfa
Remove weird Select/Query build delegate methods
jeffgbutler Aug 15, 2019
329e215
Make UpdateDSL Compatible with Kotlin
jeffgbutler Aug 15, 2019
ca126e6
Cleanup bad usages of Optional
jeffgbutler Aug 15, 2019
36d8bcf
Kotlin Style updates
jeffgbutler Aug 15, 2019
15c152b
Progress on Kotlin Extensions
jeffgbutler Aug 15, 2019
cb88a73
Refactor to make the imports easier
jeffgbutler Aug 16, 2019
e6f7b50
Some simplification
jeffgbutler Aug 16, 2019
717870b
Merge branch 'master' into kotlin-support
jeffgbutler Aug 16, 2019
6627d6b
Merge branch 'master' into kotlin-support
jeffgbutler Aug 18, 2019
3378e2b
Updates after merging new Java code
jeffgbutler Aug 18, 2019
941f287
Merge branch 'master' into kotlin-support
jeffgbutler Aug 19, 2019
b8ae9bc
Updated and simplified Kotlin support
jeffgbutler Aug 19, 2019
d64f914
Updated and simplified Kotlin support
jeffgbutler Aug 19, 2019
235d741
Add a selectOptional method and then annotate for nullability
jeffgbutler Aug 19, 2019
b41c5dd
Repackage Kotlin extensions
jeffgbutler Aug 19, 2019
74803b3
Repackage Kotlin extensions
jeffgbutler Aug 19, 2019
ab58ff3
Coverage
jeffgbutler Aug 20, 2019
4030174
Coverage
jeffgbutler Aug 20, 2019
cb72e4e
Remove unnecessary methods added for Kotlin
jeffgbutler Aug 20, 2019
d7ba22d
Add some tests for joins in Kotlin
jeffgbutler Aug 20, 2019
a22eb7d
Merge branch 'master' into kotlin-support
jeffgbutler Aug 20, 2019
808fc8f
Kotlin DSL Implementation - Joins
jeffgbutler Aug 21, 2019
df3e178
Formatting
jeffgbutler Aug 21, 2019
23f7dec
Coverage
jeffgbutler Aug 21, 2019
77fb467
Code style
jeffgbutler Aug 21, 2019
e092142
Code style
jeffgbutler Aug 21, 2019
508f7f9
Code style
jeffgbutler Aug 21, 2019
2cb8f1f
Add some type aliases
jeffgbutler Aug 21, 2019
f47544b
Docs
jeffgbutler Aug 21, 2019
575212e
Docs
jeffgbutler Aug 22, 2019
80f15d0
Add a DSL specifically for count queries
jeffgbutler Aug 22, 2019
f11f258
Docs
jeffgbutler Aug 22, 2019
c9a401e
Docs
jeffgbutler Aug 26, 2019
740290a
Coverage
jeffgbutler Aug 26, 2019
1258dd1
Update Kotlin Version
jeffgbutler Aug 26, 2019
f711b30
See if we can get Coveralls working with Kotlin
jeffgbutler Aug 26, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 94 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@
limitations under the License.

-->
<!-- Copyright 2016-2019 the original author or authors. Licensed under the
Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
OR CONDITIONS OF ANY KIND, either express or implied. See the License for
the specific language governing permissions and limitations under the License. -->
<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">
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mybatis</groupId>
Expand All @@ -45,6 +40,11 @@
<spring.batch.version>4.1.2.RELEASE</spring.batch.version>
<clirr.comparisonVersion>1.1.0</clirr.comparisonVersion>
<module.name>org.mybatis.dynamic.sql</module.name>
<kotlin.version>1.3.50</kotlin.version>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
<sonar.sources>pom.xml,src/main/java,src/main/kotlin</sonar.sources>
<sonar.tests>src/test/java,src/test/kotlin</sonar.tests>
<jacoco.version>0.8.4</jacoco.version>
</properties>

<build>
Expand All @@ -60,6 +60,68 @@
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by
maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Copy the changelog into the generated site -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
Expand All @@ -82,6 +144,24 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.ozsie</groupId>
<artifactId>detekt-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>verify</phase>
<goals><goal>check</goal></goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<configuration>
<sourceDirectories>src/main/java,src/main/kotlin</sourceDirectories>
</configuration>
</plugin>
</plugins>
</build>

Expand All @@ -98,6 +178,12 @@
</reporting>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.mybatis.dynamic.sql.insert.InsertDSL;
import org.mybatis.dynamic.sql.insert.InsertSelectDSL;
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
import org.mybatis.dynamic.sql.select.CountDSL;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
import org.mybatis.dynamic.sql.select.SelectDSL;
import org.mybatis.dynamic.sql.select.SelectModel;
Expand All @@ -43,10 +44,9 @@
import org.mybatis.dynamic.sql.select.function.Substring;
import org.mybatis.dynamic.sql.select.function.Subtract;
import org.mybatis.dynamic.sql.select.function.Upper;
import org.mybatis.dynamic.sql.select.join.AndJoinCriterion;
import org.mybatis.dynamic.sql.select.join.EqualTo;
import org.mybatis.dynamic.sql.select.join.JoinCondition;
import org.mybatis.dynamic.sql.select.join.OnJoinCriterion;
import org.mybatis.dynamic.sql.select.join.JoinCriterion;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.util.Buildable;
Expand Down Expand Up @@ -103,6 +103,10 @@
public interface SqlBuilder {

// statements
static CountDSL<SelectModel> countFrom(SqlTable table) {
return CountDSL.countFrom(table);
}

static DeleteDSL<DeleteModel> deleteFrom(SqlTable table) {
return DeleteDSL.deleteFrom(table);
}
Expand Down Expand Up @@ -192,15 +196,17 @@ static <T> SqlCriterion<T> and(BindableColumn<T> column, VisitableCondition<T> c
}

// join support
static AndJoinCriterion and(BasicColumn joinColumn, JoinCondition joinCondition) {
return new AndJoinCriterion.Builder()
static JoinCriterion and(BasicColumn joinColumn, JoinCondition joinCondition) {
return new JoinCriterion.Builder()
.withConnector("and") //$NON-NLS-1$
.withJoinColumn(joinColumn)
.withJoinCondition(joinCondition)
.build();
}

static OnJoinCriterion on(BasicColumn joinColumn, JoinCondition joinCondition) {
return new OnJoinCriterion.Builder()
static JoinCriterion on(BasicColumn joinColumn, JoinCondition joinCondition) {
return new JoinCriterion.Builder()
.withConnector("on") //$NON-NLS-1$
.withJoinColumn(joinColumn)
.withJoinCondition(joinCondition)
.build();
Expand Down Expand Up @@ -547,7 +553,16 @@ static <T> IsNotLikeWhenPresent<T> isNotLikeWhenPresent(T value) {
static <T> IsNotLikeWhenPresent<T> isNotLikeWhenPresent(Supplier<T> valueSupplier) {
return IsNotLikeWhenPresent.of(valueSupplier);
}


// shortcuts for booleans
static IsEqualTo<Boolean> isTrue() {
return isEqualTo(Boolean.TRUE);
}

static IsEqualTo<Boolean> isFalse() {
return isEqualTo(Boolean.FALSE);
}

// conditions for strings only
static IsLikeCaseInsensitive isLikeCaseInsensitive(String value) {
return isLikeCaseInsensitive(() -> value);
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/mybatis/dynamic/sql/SqlTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import org.jetbrains.annotations.NotNull;

public class SqlTable {

Expand Down Expand Up @@ -84,15 +85,18 @@ public String tableNameAtRuntime() {
public <T> SqlColumn<T> allColumns() {
return SqlColumn.of("*", this); //$NON-NLS-1$
}


@NotNull
public <T> SqlColumn<T> column(String name) {
return SqlColumn.of(name, this);
}

@NotNull
public <T> SqlColumn<T> column(String name, JDBCType jdbcType) {
return SqlColumn.of(name, this, jdbcType);
}

@NotNull
public <T> SqlColumn<T> column(String name, JDBCType jdbcType, String typeHandler) {
return SqlColumn.of(name, this, jdbcType).withTypeHandler(typeHandler);
}
Expand Down
23 changes: 4 additions & 19 deletions src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,23 @@ public class DeleteDSL<R> implements Buildable<R> {

private Function<DeleteModel, R> adapterFunction;
private SqlTable table;
protected DeleteWhereBuilder whereBuilder;
private DeleteWhereBuilder whereBuilder = new DeleteWhereBuilder();

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

public DeleteWhereBuilder where() {
whereBuilder = new DeleteWhereBuilder();
return whereBuilder;
}

public <T> DeleteWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition) {
whereBuilder = new DeleteWhereBuilder(column, condition);
return whereBuilder;
}

public <T> DeleteWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition,
SqlCriterion<?>...subCriteria) {
whereBuilder = new DeleteWhereBuilder(column, condition, subCriteria);
whereBuilder.and(column, condition, subCriteria);
return whereBuilder;
}

/**
* WARNING! Calling this method could result in an delete statement that deletes
* all rows in a table.
Expand All @@ -64,7 +58,7 @@ public <T> DeleteWhereBuilder where(BindableColumn<T> column, VisitableCondition
@Override
public R build() {
DeleteModel deleteModel = DeleteModel.withTable(table)
.withWhereModel(whereBuilder == null ? null : whereBuilder.buildWhereModel())
.withWhereModel(whereBuilder.buildWhereModel())
.build();
return adapterFunction.apply(deleteModel);
}
Expand Down Expand Up @@ -99,15 +93,6 @@ private DeleteWhereBuilder() {
super();
}

private <T> DeleteWhereBuilder(BindableColumn<T> column, VisitableCondition<T> condition) {
super(column, condition);
}

private <T> DeleteWhereBuilder(BindableColumn<T> column, VisitableCondition<T> condition,
SqlCriterion<?>...subCriteria) {
super(column, condition, subCriteria);
}

@Override
public R build() {
return DeleteDSL.this.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;

/**
* Represents a function that can be used to create a general delete method in the style
* of MyBatis Generator. When using this function, you can create a method that does not require a user to
* call the build() and render() methods - making client code look a bit cleaner.
* Represents a function that can be used to create a simplified delete method. When using this function
* you can create a method that does not require a user to call the build() and render() methods - making
* client code look a bit cleaner.
*
* <p>This function is intended to be used in conjunction with a utility method like
* {@link MyBatis3Utils#deleteFrom(ToIntFunction, SqlTable, DeleteDSLCompleter)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Objects;
import java.util.Optional;

import org.jetbrains.annotations.NotNull;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.delete.render.DeleteRenderer;
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
Expand All @@ -40,7 +41,8 @@ public SqlTable table() {
public Optional<WhereModel> whereModel() {
return Optional.ofNullable(whereModel);
}


@NotNull
public DeleteStatementProvider render(RenderingStrategy renderingStrategy) {
return DeleteRenderer.withDeleteModel(this)
.withRenderingStrategy(renderingStrategy)
Expand Down
Loading