Skip to content

Improve Kotlin Support #154

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 8 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
### Added

- Added support for reusing WHERE clauses among count, delete, select, and update statements ([#152](https://github.com/mybatis/mybatis-dynamic-sql/pull/152))

- Improved Kotlin support. Previously, several overloaded methods could collide causing queries to be fragile and very dependent on having the correct imports in a Kotlin file. With this improved support there is no longer any ambiguity. ([#154](https://github.com/mybatis/mybatis-dynamic-sql/pull/154))

### Bugs Fixed

Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<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>
<kotlin.code.style>official</kotlin.code.style>
</properties>

<build>
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ public <T> DeleteWhereBuilder where(BindableColumn<T> column, VisitableCondition
return whereBuilder;
}

@SuppressWarnings("unchecked")
public DeleteWhereBuilder applyWhere(WhereApplier whereApplier) {
return (DeleteWhereBuilder) whereApplier.apply(whereBuilder);
return whereBuilder.applyWhere(whereApplier);
}

/**
Expand Down Expand Up @@ -99,7 +98,7 @@ public class DeleteWhereBuilder extends AbstractWhereDSL<DeleteWhereBuilder> imp
private DeleteWhereBuilder() {
super();
}

@Override
public R build() {
return DeleteDSL.this.build();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/mybatis/dynamic/sql/select/CountDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ public <T> CountWhereBuilder where(BindableColumn<T> column, VisitableCondition<
return whereBuilder;
}

@SuppressWarnings("unchecked")
public CountWhereBuilder applyWhere(WhereApplier whereApplier) {
return (CountWhereBuilder) whereApplier.apply(whereBuilder);
return whereBuilder.applyWhere(whereApplier);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, Visitable
return whereBuilder;
}

@SuppressWarnings("unchecked")
public QueryExpressionWhereBuilder applyWhere(WhereApplier whereApplier) {
return (QueryExpressionWhereBuilder) whereApplier.apply(whereBuilder);
return whereBuilder.applyWhere(whereApplier);
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ public <T> UpdateWhereBuilder where(BindableColumn<T> column, VisitableCondition
return whereBuilder;
}

@SuppressWarnings("unchecked")
public UpdateWhereBuilder applyWhere(WhereApplier whereApplier) {
return (UpdateWhereBuilder) whereApplier.apply(whereBuilder);
return whereBuilder.applyWhere(whereApplier);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ public <S> T where(BindableColumn<S> column, VisitableCondition<S> condition, Li
addCriterion(column, condition, subCriteria);
return getThis();
}


@SuppressWarnings("unchecked")
public T applyWhere(WhereApplier whereApplier) {
return (T) whereApplier.apply(this);
}

public <S> T and(BindableColumn<S> column, VisitableCondition<S> condition) {
addCriterion("and", column, condition); //$NON-NLS-1$
return getThis();
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* 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.
*/
package org.mybatis.dynamic.sql.util.kotlin

import org.mybatis.dynamic.sql.BindableColumn
import org.mybatis.dynamic.sql.SqlTable
import org.mybatis.dynamic.sql.VisitableCondition
import org.mybatis.dynamic.sql.select.AbstractQueryExpressionDSL
import org.mybatis.dynamic.sql.select.SelectModel
import org.mybatis.dynamic.sql.util.Buildable
import org.mybatis.dynamic.sql.where.AbstractWhereDSL

abstract class KotlinBaseBuilder<M, W : AbstractWhereDSL<W>, B : KotlinBaseBuilder<M, W, B>> : Buildable<M> {
fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>): B {
getWhere().where(column, condition)
return getThis()
}

fun <T> where(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver): B {
getWhere().where(column, condition, collect)
return getThis()
}

fun applyWhere(whereApplier: WhereApplier): B {
getWhere().applyWhere(whereApplier)
return getThis()
}

fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>): B {
getWhere().and(column, condition)
return getThis()
}

fun <T> and(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver): B {
getWhere().and(column, condition, collect)
return getThis()
}

fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>): B {
getWhere().or(column, condition)
return getThis()
}

fun <T> or(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver): B {
getWhere().or(column, condition, collect)
return getThis()
}

protected abstract fun getWhere(): W
protected abstract fun getThis(): B
}

abstract class KotlinBaseJoiningBuilder<T : AbstractQueryExpressionDSL<T, SelectModel>, W: AbstractWhereDSL<W>, B : KotlinBaseJoiningBuilder<T, W, B>>(private val dsl: AbstractQueryExpressionDSL<T, SelectModel>) :
KotlinBaseBuilder<SelectModel, W, B>() {

fun join(table: SqlTable, receiver: JoinReceiver) =
apply {
dsl.join(table, receiver)
}

fun join(table: SqlTable, alias: String, receiver: JoinReceiver) =
apply {
dsl.join(table, alias, receiver)
}

fun fullJoin(table: SqlTable, receiver: JoinReceiver) =
apply {
dsl.fullJoin(table, receiver)
}

fun fullJoin(table: SqlTable, alias: String, receiver: JoinReceiver) =
apply {
dsl.fullJoin(table, alias, receiver)
}

fun leftJoin(table: SqlTable, receiver: JoinReceiver) =
apply {
dsl.leftJoin(table, receiver)
}

fun leftJoin(table: SqlTable, alias: String, receiver: JoinReceiver) =
apply {
dsl.leftJoin(table, alias, receiver)
}

fun rightJoin(table: SqlTable, receiver: JoinReceiver) =
apply {
dsl.rightJoin(table, receiver)
}

fun rightJoin(table: SqlTable, alias: String, receiver: JoinReceiver) =
apply {
dsl.rightJoin(table, alias, receiver)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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.
*/
package org.mybatis.dynamic.sql.util.kotlin

import org.mybatis.dynamic.sql.select.CountDSL
import org.mybatis.dynamic.sql.select.SelectModel
import org.mybatis.dynamic.sql.util.Buildable

typealias CountCompleter = KotlinCountBuilder.() -> Buildable<SelectModel>

class KotlinCountBuilder(private val dsl: CountDSL<SelectModel>) :
KotlinBaseJoiningBuilder<CountDSL<SelectModel>, CountDSL<SelectModel>.CountWhereBuilder, KotlinCountBuilder>(dsl) {

fun allRows() = this

override fun build(): SelectModel = dsl.build()

override fun getWhere(): CountDSL<SelectModel>.CountWhereBuilder = dsl.where()

override fun getThis() = this
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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.
*/
package org.mybatis.dynamic.sql.util.kotlin

import org.mybatis.dynamic.sql.delete.DeleteDSL
import org.mybatis.dynamic.sql.delete.DeleteModel
import org.mybatis.dynamic.sql.util.Buildable

typealias DeleteCompleter = KotlinDeleteBuilder.() -> Buildable<DeleteModel>

class KotlinDeleteBuilder(private val dsl: DeleteDSL<DeleteModel>) :
KotlinBaseBuilder<DeleteModel, DeleteDSL<DeleteModel>.DeleteWhereBuilder, KotlinDeleteBuilder>() {

fun allRows() = this

override fun build(): DeleteModel = dsl.build()

override fun getWhere(): DeleteDSL<DeleteModel>.DeleteWhereBuilder = dsl.where()

override fun getThis(): KotlinDeleteBuilder = this
}
Loading