diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dc2cb462..21e7cb3aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles 2. Added the ability to specify a table alias on DELETE and UPDATE statements. This is especially useful when working with a sub-query with an exists or not exists condition. ([#489](https://github.com/mybatis/mybatis-dynamic-sql/pull/489)) +3. Updated the Kotlin DSL to use Kotlin 1.7's new "definitely non-null" types where appropriate. This helps us to more + accurately represent the nullable/non-nullable expectations for API method calls. + ([#496](https://github.com/mybatis/mybatis-dynamic-sql/pull/496)) ## Release 1.4.0 - March 3, 2022 diff --git a/pom.xml b/pom.xml index 115294d7d..54741a889 100644 --- a/pom.xml +++ b/pom.xml @@ -230,6 +230,12 @@ true + + org.jetbrains.kotlin + kotlin-compiler + ${kotlin.version} + test + org.junit.jupiter junit-jupiter-api diff --git a/src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java b/src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java index eba241267..4c06507b9 100644 --- a/src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java +++ b/src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java @@ -59,7 +59,8 @@ public R build() { return adapterFunction.apply(deleteModel); } - public static DeleteDSL deleteFrom(Function adapterFunction, SqlTable table, String tableAlias) { + public static DeleteDSL deleteFrom(Function adapterFunction, SqlTable table, + String tableAlias) { return new DeleteDSL<>(table, tableAlias, adapterFunction); } diff --git a/src/main/java/org/mybatis/dynamic/sql/insert/render/DefaultInsertStatementProvider.java b/src/main/java/org/mybatis/dynamic/sql/insert/render/DefaultInsertStatementProvider.java index 9cdc67302..1fc9a54ae 100644 --- a/src/main/java/org/mybatis/dynamic/sql/insert/render/DefaultInsertStatementProvider.java +++ b/src/main/java/org/mybatis/dynamic/sql/insert/render/DefaultInsertStatementProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 the original author or authors. + * Copyright 2016-2022 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. @@ -17,6 +17,8 @@ import java.util.Objects; +import org.jetbrains.annotations.NotNull; + public class DefaultInsertStatementProvider implements InsertStatementProvider { private final String insertStatement; // need to keep both row and record for now so we don't break @@ -37,6 +39,7 @@ public T getRecord() { } @Override + @NotNull public T getRow() { return row; } diff --git a/src/main/java/org/mybatis/dynamic/sql/insert/render/InsertStatementProvider.java b/src/main/java/org/mybatis/dynamic/sql/insert/render/InsertStatementProvider.java index 65d31882d..b5bd1d68c 100644 --- a/src/main/java/org/mybatis/dynamic/sql/insert/render/InsertStatementProvider.java +++ b/src/main/java/org/mybatis/dynamic/sql/insert/render/InsertStatementProvider.java @@ -15,6 +15,8 @@ */ package org.mybatis.dynamic.sql.insert.render; +import org.jetbrains.annotations.NotNull; + public interface InsertStatementProvider { /** * Return the row associated with this insert statement. @@ -30,6 +32,7 @@ public interface InsertStatementProvider { * * @return the row associated with this insert statement. */ + @NotNull T getRow(); /** diff --git a/src/main/java/org/mybatis/dynamic/sql/update/render/SetPhraseVisitor.java b/src/main/java/org/mybatis/dynamic/sql/update/render/SetPhraseVisitor.java index 0b2ef7359..de1937349 100644 --- a/src/main/java/org/mybatis/dynamic/sql/update/render/SetPhraseVisitor.java +++ b/src/main/java/org/mybatis/dynamic/sql/update/render/SetPhraseVisitor.java @@ -55,7 +55,8 @@ public SetPhraseVisitor(AtomicInteger sequence, RenderingStrategy renderingStrat @Override public Optional visit(NullMapping mapping) { - return FragmentAndParameters.withFragment(mapping.mapColumn(aliasedColumnNameFunction) + " = null") //$NON-NLS-1$ + return FragmentAndParameters + .withFragment(mapping.mapColumn(aliasedColumnNameFunction) + " = null") //$NON-NLS-1$ .buildOptional(); } diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/GroupingCriteriaCollector.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/GroupingCriteriaCollector.kt index 2ccd6d4e4..5c3fdd697 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/GroupingCriteriaCollector.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/GroupingCriteriaCollector.kt @@ -243,7 +243,7 @@ class GroupingCriteriaCollector { fun BindableColumn<*>.isNotNull() = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isNotNull()) - infix fun BindableColumn.isEqualTo(value: T) = + infix fun BindableColumn.isEqualTo(value: T & Any) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isEqualTo(value)) infix fun BindableColumn<*>.isEqualTo(subQuery: KotlinSubQueryBuilder.() -> Unit) = @@ -252,10 +252,10 @@ class GroupingCriteriaCollector { infix fun BindableColumn<*>.isEqualTo(column: BasicColumn) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isEqualTo(column)) - infix fun BindableColumn.isEqualToWhenPresent(value: T?) = + infix fun BindableColumn.isEqualToWhenPresent(value: T?) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isEqualToWhenPresent(value)) - infix fun BindableColumn.isNotEqualTo(value: T) = + infix fun BindableColumn.isNotEqualTo(value: T & Any) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isNotEqualTo(value)) infix fun BindableColumn<*>.isNotEqualTo(subQuery: KotlinSubQueryBuilder.() -> Unit) = @@ -264,10 +264,10 @@ class GroupingCriteriaCollector { infix fun BindableColumn<*>.isNotEqualTo(column: BasicColumn) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isNotEqualTo(column)) - infix fun BindableColumn.isNotEqualToWhenPresent(value: T?) = + infix fun BindableColumn.isNotEqualToWhenPresent(value: T?) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isNotEqualToWhenPresent(value)) - infix fun BindableColumn.isGreaterThan(value: T) = + infix fun BindableColumn.isGreaterThan(value: T & Any) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isGreaterThan(value)) infix fun BindableColumn<*>.isGreaterThan(subQuery: KotlinSubQueryBuilder.() -> Unit) = @@ -276,10 +276,10 @@ class GroupingCriteriaCollector { infix fun BindableColumn<*>.isGreaterThan(column: BasicColumn) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isGreaterThan(column)) - infix fun BindableColumn.isGreaterThanWhenPresent(value: T?) = + infix fun BindableColumn.isGreaterThanWhenPresent(value: T?) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isGreaterThanWhenPresent(value)) - infix fun BindableColumn.isGreaterThanOrEqualTo(value: T) = + infix fun BindableColumn.isGreaterThanOrEqualTo(value: T & Any) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isGreaterThanOrEqualTo(value)) infix fun BindableColumn<*>.isGreaterThanOrEqualTo(subQuery: KotlinSubQueryBuilder.() -> Unit) = @@ -288,10 +288,10 @@ class GroupingCriteriaCollector { infix fun BindableColumn<*>.isGreaterThanOrEqualTo(column: BasicColumn) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isGreaterThanOrEqualTo(column)) - infix fun BindableColumn.isGreaterThanOrEqualToWhenPresent(value: T?) = + infix fun BindableColumn.isGreaterThanOrEqualToWhenPresent(value: T?) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isGreaterThanOrEqualToWhenPresent(value)) - infix fun BindableColumn.isLessThan(value: T) = + infix fun BindableColumn.isLessThan(value: T & Any) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isLessThan(value)) infix fun BindableColumn<*>.isLessThan(subQuery: KotlinSubQueryBuilder.() -> Unit) = @@ -300,10 +300,10 @@ class GroupingCriteriaCollector { infix fun BindableColumn<*>.isLessThan(column: BasicColumn) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isLessThan(column)) - infix fun BindableColumn.isLessThanWhenPresent(value: T?) = + infix fun BindableColumn.isLessThanWhenPresent(value: T?) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isLessThanWhenPresent(value)) - infix fun BindableColumn.isLessThanOrEqualTo(value: T) = + infix fun BindableColumn.isLessThanOrEqualTo(value: T & Any) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isLessThanOrEqualTo(value)) infix fun BindableColumn<*>.isLessThanOrEqualTo(subQuery: KotlinSubQueryBuilder.() -> Unit) = @@ -312,66 +312,66 @@ class GroupingCriteriaCollector { infix fun BindableColumn<*>.isLessThanOrEqualTo(column: BasicColumn) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isLessThanOrEqualTo(column)) - infix fun BindableColumn.isLessThanOrEqualToWhenPresent(value: T?) = + infix fun BindableColumn.isLessThanOrEqualToWhenPresent(value: T?) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isLessThanOrEqualToWhenPresent(value)) - fun BindableColumn.isIn(vararg values: T) = isIn(values.asList()) + fun BindableColumn.isIn(vararg values: T & Any) = isIn(values.asList()) - infix fun BindableColumn.isIn(values: Collection) = + infix fun BindableColumn.isIn(values: Collection) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isIn(values)) infix fun BindableColumn<*>.isIn(subQuery: KotlinSubQueryBuilder.() -> Unit) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isIn(subQuery)) - fun BindableColumn.isInWhenPresent(vararg values: T?) = isInWhenPresent(values.asList()) + fun BindableColumn.isInWhenPresent(vararg values: T?) = isInWhenPresent(values.asList()) - infix fun BindableColumn.isInWhenPresent(values: Collection?) = + infix fun BindableColumn.isInWhenPresent(values: Collection?) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isInWhenPresent(values)) - fun BindableColumn.isNotIn(vararg values: T) = isNotIn(values.asList()) + fun BindableColumn.isNotIn(vararg values: T & Any) = isNotIn(values.asList()) - infix fun BindableColumn.isNotIn(values: Collection) = + infix fun BindableColumn.isNotIn(values: Collection) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isNotIn(values)) infix fun BindableColumn<*>.isNotIn(subQuery: KotlinSubQueryBuilder.() -> Unit) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isNotIn(subQuery)) - fun BindableColumn.isNotInWhenPresent(vararg values: T?) = isNotInWhenPresent(values.asList()) + fun BindableColumn.isNotInWhenPresent(vararg values: T?) = isNotInWhenPresent(values.asList()) - infix fun BindableColumn.isNotInWhenPresent(values: Collection?) = + infix fun BindableColumn.isNotInWhenPresent(values: Collection?) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isNotInWhenPresent(values)) - infix fun BindableColumn.isBetween(value1: T) = - SecondValueCollector { + infix fun BindableColumn.isBetween(value1: T & Any) = + SecondValueCollector { invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isBetween(value1).and(it)) } - infix fun BindableColumn.isBetweenWhenPresent(value1: T?) = + infix fun BindableColumn.isBetweenWhenPresent(value1: T?) = NullableSecondValueCollector { invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isBetweenWhenPresent(value1).and(it)) } - infix fun BindableColumn.isNotBetween(value1: T) = - SecondValueCollector { + infix fun BindableColumn.isNotBetween(value1: T & Any) = + SecondValueCollector { invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isNotBetween(value1).and(it)) } - infix fun BindableColumn.isNotBetweenWhenPresent(value1: T?) = + infix fun BindableColumn.isNotBetweenWhenPresent(value1: T?) = NullableSecondValueCollector { invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isNotBetweenWhenPresent(value1).and(it)) } // for string columns, but generic for columns with type handlers - infix fun BindableColumn.isLike(value: T) = + infix fun BindableColumn.isLike(value: T & Any) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isLike(value)) - infix fun BindableColumn.isLikeWhenPresent(value: T?) = + infix fun BindableColumn.isLikeWhenPresent(value: T?) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isLikeWhenPresent(value)) - infix fun BindableColumn.isNotLike(value: T) = + infix fun BindableColumn.isNotLike(value: T & Any) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isNotLike(value)) - infix fun BindableColumn.isNotLikeWhenPresent(value: T?) = + infix fun BindableColumn.isNotLikeWhenPresent(value: T?) = invoke(org.mybatis.dynamic.sql.util.kotlin.elements.isNotLikeWhenPresent(value)) // shortcuts for booleans diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinBatchInsertBuilder.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinBatchInsertBuilder.kt index 0f278cd17..84e066dfb 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinBatchInsertBuilder.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinBatchInsertBuilder.kt @@ -25,7 +25,7 @@ import org.mybatis.dynamic.sql.util.Buildable typealias KotlinBatchInsertCompleter = KotlinBatchInsertBuilder.() -> Unit @MyBatisDslMarker -class KotlinBatchInsertBuilder (private val rows: Collection): Buildable> { +class KotlinBatchInsertBuilder (private val rows: Collection): Buildable> { private var table: SqlTable? = null private val columnMappings = mutableListOf() @@ -33,7 +33,7 @@ class KotlinBatchInsertBuilder (private val rows: Collection): Buildable map(column: SqlColumn) = MultiRowInsertColumnMapCompleter(column) { + fun map(column: SqlColumn) = MultiRowInsertColumnMapCompleter(column) { columnMappings.add(it) } diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinGeneralInsertBuilder.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinGeneralInsertBuilder.kt index b7d402bbf..4562680af 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinGeneralInsertBuilder.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinGeneralInsertBuilder.kt @@ -29,7 +29,7 @@ class KotlinGeneralInsertBuilder(private val table: SqlTable) : Buildable() - fun set(column: SqlColumn) = GeneralInsertColumnSetCompleter(column) { + fun set(column: SqlColumn) = GeneralInsertColumnSetCompleter(column) { columnMappings.add(it) } diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinInsertBuilder.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinInsertBuilder.kt index ca2bee86e..fb7df5d38 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinInsertBuilder.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinInsertBuilder.kt @@ -25,7 +25,7 @@ import org.mybatis.dynamic.sql.util.Buildable typealias KotlinInsertCompleter = KotlinInsertBuilder.() -> Unit @MyBatisDslMarker -class KotlinInsertBuilder (private val row: T): Buildable> { +class KotlinInsertBuilder (private val row: T): Buildable> { private var table: SqlTable? = null private val columnMappings = mutableListOf() @@ -33,7 +33,7 @@ class KotlinInsertBuilder (private val row: T): Buildable> { this.table = table } - fun map(column: SqlColumn) = SingleRowInsertColumnMapCompleter(column) { + fun map(column: SqlColumn) = SingleRowInsertColumnMapCompleter(column) { columnMappings.add(it) } diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinInsertColumnMapCompleters.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinInsertColumnMapCompleters.kt index bd6012dae..cd2fa81d0 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinInsertColumnMapCompleters.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinInsertColumnMapCompleters.kt @@ -27,7 +27,7 @@ import org.mybatis.dynamic.sql.util.ValueOrNullMapping import org.mybatis.dynamic.sql.util.ValueWhenPresentMapping @MyBatisDslMarker -sealed class AbstractInsertColumnMapCompleter( +sealed class AbstractInsertColumnMapCompleter( internal val column: SqlColumn, internal val mappingConsumer: (AbstractColumnMapping) -> Unit) { @@ -38,7 +38,7 @@ sealed class AbstractInsertColumnMapCompleter( infix fun toStringConstant(constant: String) = mappingConsumer.invoke(StringConstantMapping.of(column, constant)) } -class MultiRowInsertColumnMapCompleter( +class MultiRowInsertColumnMapCompleter( column: SqlColumn, mappingConsumer: (AbstractColumnMapping) -> Unit) : AbstractInsertColumnMapCompleter(column, mappingConsumer) { @@ -46,7 +46,7 @@ class MultiRowInsertColumnMapCompleter( infix fun toProperty(property: String) = mappingConsumer.invoke(PropertyMapping.of(column, property)) } -class SingleRowInsertColumnMapCompleter( +class SingleRowInsertColumnMapCompleter( column: SqlColumn, mappingConsumer: (AbstractColumnMapping) -> Unit) : AbstractInsertColumnMapCompleter(column, mappingConsumer) { @@ -57,14 +57,14 @@ class SingleRowInsertColumnMapCompleter( mappingConsumer.invoke(PropertyWhenPresentMapping.of(column, property, valueSupplier)) } -class GeneralInsertColumnSetCompleter( +class GeneralInsertColumnSetCompleter( column: SqlColumn, mappingConsumer: (AbstractColumnMapping) -> Unit) : AbstractInsertColumnMapCompleter(column, mappingConsumer) { - infix fun toValue(value: T) = toValue { value } + infix fun toValue(value: T & Any) = toValue { value } - infix fun toValue(value: () -> T) = mappingConsumer.invoke(ValueMapping.of(column, value)) + infix fun toValue(value: () -> T & Any) = mappingConsumer.invoke(ValueMapping.of(column, value)) infix fun toValueOrNull(value: T?) = toValueOrNull { value } diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinMultiRowInsertBuilder.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinMultiRowInsertBuilder.kt index 71f79f2a7..3a7a48a7f 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinMultiRowInsertBuilder.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinMultiRowInsertBuilder.kt @@ -25,7 +25,7 @@ import org.mybatis.dynamic.sql.util.Buildable typealias KotlinMultiRowInsertCompleter = KotlinMultiRowInsertBuilder.() -> Unit @MyBatisDslMarker -class KotlinMultiRowInsertBuilder (private val rows: Collection): Buildable> { +class KotlinMultiRowInsertBuilder (private val rows: Collection): Buildable> { private var table: SqlTable? = null private val columnMappings = mutableListOf() @@ -33,7 +33,7 @@ class KotlinMultiRowInsertBuilder (private val rows: Collection): Buildabl this.table = table } - fun map(column: SqlColumn) = MultiRowInsertColumnMapCompleter(column) { + fun map(column: SqlColumn) = MultiRowInsertColumnMapCompleter(column) { columnMappings.add(it) } diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinUpdateBuilder.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinUpdateBuilder.kt index 17933b69f..83f78c928 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinUpdateBuilder.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinUpdateBuilder.kt @@ -50,9 +50,9 @@ class KotlinUpdateBuilder(private val dsl: UpdateDSL) : set(column).equalToStringConstant(constant) } - infix fun equalTo(value: T): Unit = equalTo { value } + infix fun equalTo(value: T & Any): Unit = equalTo { value } - infix fun equalTo(value: () -> T): Unit = + infix fun equalTo(value: () -> T & Any): Unit = applyToDsl { set(column).equalTo(value) } diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/SqlElements.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/SqlElements.kt index 72e04a1bb..6190275cb 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/SqlElements.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/SqlElements.kt @@ -182,34 +182,34 @@ fun exists(subQuery: KotlinSubQueryBuilder.() -> Unit): ExistsPredicate = fun notExists(subQuery: KotlinSubQueryBuilder.() -> Unit): ExistsPredicate = SqlBuilder.notExists(KotlinSubQueryBuilder().apply(subQuery)) -fun isEqualTo(value: T): IsEqualTo = SqlBuilder.isEqualTo(value) +fun isEqualTo(value: T & Any): IsEqualTo = SqlBuilder.isEqualTo(value) fun isEqualTo(subQuery: KotlinSubQueryBuilder.() -> Unit): IsEqualToWithSubselect = SqlBuilder.isEqualTo(KotlinSubQueryBuilder().apply(subQuery)) fun isEqualTo(column: BasicColumn): IsEqualToColumn = SqlBuilder.isEqualTo(column) -fun isEqualToWhenPresent(value: T?): IsEqualTo = SqlBuilder.isEqualToWhenPresent(value) +fun isEqualToWhenPresent(value: T?): IsEqualTo = SqlBuilder.isEqualToWhenPresent(value) -fun isNotEqualTo(value: T): IsNotEqualTo = SqlBuilder.isNotEqualTo(value) +fun isNotEqualTo(value: T & Any): IsNotEqualTo = SqlBuilder.isNotEqualTo(value) fun isNotEqualTo(subQuery: KotlinSubQueryBuilder.() -> Unit): IsNotEqualToWithSubselect = SqlBuilder.isNotEqualTo(KotlinSubQueryBuilder().apply(subQuery)) fun isNotEqualTo(column: BasicColumn): IsNotEqualToColumn = SqlBuilder.isNotEqualTo(column) -fun isNotEqualToWhenPresent(value: T?): IsNotEqualTo = SqlBuilder.isNotEqualToWhenPresent(value) +fun isNotEqualToWhenPresent(value: T?): IsNotEqualTo = SqlBuilder.isNotEqualToWhenPresent(value) -fun isGreaterThan(value: T): IsGreaterThan = SqlBuilder.isGreaterThan(value) +fun isGreaterThan(value: T & Any): IsGreaterThan = SqlBuilder.isGreaterThan(value) fun isGreaterThan(subQuery: KotlinSubQueryBuilder.() -> Unit): IsGreaterThanWithSubselect = SqlBuilder.isGreaterThan(KotlinSubQueryBuilder().apply(subQuery)) fun isGreaterThan(column: BasicColumn): IsGreaterThanColumn = SqlBuilder.isGreaterThan(column) -fun isGreaterThanWhenPresent(value: T?): IsGreaterThan = SqlBuilder.isGreaterThanWhenPresent(value) +fun isGreaterThanWhenPresent(value: T?): IsGreaterThan = SqlBuilder.isGreaterThanWhenPresent(value) -fun isGreaterThanOrEqualTo(value: T): IsGreaterThanOrEqualTo = SqlBuilder.isGreaterThanOrEqualTo(value) +fun isGreaterThanOrEqualTo(value: T & Any): IsGreaterThanOrEqualTo = SqlBuilder.isGreaterThanOrEqualTo(value) fun isGreaterThanOrEqualTo(subQuery: KotlinSubQueryBuilder.() -> Unit): IsGreaterThanOrEqualToWithSubselect = SqlBuilder.isGreaterThanOrEqualTo(KotlinSubQueryBuilder().apply(subQuery)) @@ -217,67 +217,67 @@ fun isGreaterThanOrEqualTo(subQuery: KotlinSubQueryBuilder.() -> Unit): IsGr fun isGreaterThanOrEqualTo(column: BasicColumn): IsGreaterThanOrEqualToColumn = SqlBuilder.isGreaterThanOrEqualTo(column) -fun isGreaterThanOrEqualToWhenPresent(value: T?): IsGreaterThanOrEqualTo = +fun isGreaterThanOrEqualToWhenPresent(value: T?): IsGreaterThanOrEqualTo = SqlBuilder.isGreaterThanOrEqualToWhenPresent(value) -fun isLessThan(value: T): IsLessThan = SqlBuilder.isLessThan(value) +fun isLessThan(value: T & Any): IsLessThan = SqlBuilder.isLessThan(value) fun isLessThan(subQuery: KotlinSubQueryBuilder.() -> Unit): IsLessThanWithSubselect = SqlBuilder.isLessThan(KotlinSubQueryBuilder().apply(subQuery)) fun isLessThan(column: BasicColumn): IsLessThanColumn = SqlBuilder.isLessThan(column) -fun isLessThanWhenPresent(value: T?): IsLessThan = SqlBuilder.isLessThanWhenPresent(value) +fun isLessThanWhenPresent(value: T?): IsLessThan = SqlBuilder.isLessThanWhenPresent(value) -fun isLessThanOrEqualTo(value: T): IsLessThanOrEqualTo = SqlBuilder.isLessThanOrEqualTo(value) +fun isLessThanOrEqualTo(value: T & Any): IsLessThanOrEqualTo = SqlBuilder.isLessThanOrEqualTo(value) fun isLessThanOrEqualTo(subQuery: KotlinSubQueryBuilder.() -> Unit): IsLessThanOrEqualToWithSubselect = SqlBuilder.isLessThanOrEqualTo(KotlinSubQueryBuilder().apply(subQuery)) fun isLessThanOrEqualTo(column: BasicColumn): IsLessThanOrEqualToColumn = SqlBuilder.isLessThanOrEqualTo(column) -fun isLessThanOrEqualToWhenPresent(value: T?): IsLessThanOrEqualTo = +fun isLessThanOrEqualToWhenPresent(value: T?): IsLessThanOrEqualTo = SqlBuilder.isLessThanOrEqualToWhenPresent(value) -fun isIn(vararg values: T): IsIn = isIn(values.asList()) +fun isIn(vararg values: T & Any): IsIn = isIn(values.asList()) -fun isIn(values: Collection): IsIn = SqlBuilder.isIn(values) +fun isIn(values: Collection): IsIn = SqlBuilder.isIn(values) fun isIn(subQuery: KotlinSubQueryBuilder.() -> Unit): IsInWithSubselect = SqlBuilder.isIn(KotlinSubQueryBuilder().apply(subQuery)) -fun isInWhenPresent(vararg values: T?): IsIn = isInWhenPresent(values.asList()) +fun isInWhenPresent(vararg values: T?): IsIn = isInWhenPresent(values.asList()) -fun isInWhenPresent(values: Collection?): IsIn = SqlBuilder.isInWhenPresent(values) +fun isInWhenPresent(values: Collection?): IsIn = SqlBuilder.isInWhenPresent(values) -fun isNotIn(vararg values: T): IsNotIn = isNotIn(values.asList()) +fun isNotIn(vararg values: T & Any): IsNotIn = isNotIn(values.asList()) -fun isNotIn(values: Collection): IsNotIn = SqlBuilder.isNotIn(values) +fun isNotIn(values: Collection): IsNotIn = SqlBuilder.isNotIn(values) fun isNotIn(subQuery: KotlinSubQueryBuilder.() -> Unit): IsNotInWithSubselect = SqlBuilder.isNotIn(KotlinSubQueryBuilder().apply(subQuery)) -fun isNotInWhenPresent(vararg values: T?): IsNotIn = isNotInWhenPresent(values.asList()) +fun isNotInWhenPresent(vararg values: T?): IsNotIn = isNotInWhenPresent(values.asList()) -fun isNotInWhenPresent(values: Collection?): IsNotIn = SqlBuilder.isNotInWhenPresent(values) +fun isNotInWhenPresent(values: Collection?): IsNotIn = SqlBuilder.isNotInWhenPresent(values) -fun isBetween(value1: T): BetweenBuilder = BetweenBuilder(value1) +fun isBetween(value1: T & Any): BetweenBuilder = BetweenBuilder(value1) -fun isBetweenWhenPresent(value1: T?): BetweenWhenPresentBuilder = BetweenWhenPresentBuilder(value1) +fun isBetweenWhenPresent(value1: T?): BetweenWhenPresentBuilder = BetweenWhenPresentBuilder(value1) -fun isNotBetween(value1: T): NotBetweenBuilder = NotBetweenBuilder(value1) +fun isNotBetween(value1: T & Any): NotBetweenBuilder = NotBetweenBuilder(value1) -fun isNotBetweenWhenPresent(value1: T?): NotBetweenWhenPresentBuilder = +fun isNotBetweenWhenPresent(value1: T?): NotBetweenWhenPresentBuilder = NotBetweenWhenPresentBuilder(value1) // for string columns, but generic for columns with type handlers -fun isLike(value: T): IsLike = SqlBuilder.isLike(value) +fun isLike(value: T & Any): IsLike = SqlBuilder.isLike(value) -fun isLikeWhenPresent(value: T?): IsLike = SqlBuilder.isLikeWhenPresent(value) +fun isLikeWhenPresent(value: T?): IsLike = SqlBuilder.isLikeWhenPresent(value) -fun isNotLike(value: T): IsNotLike = SqlBuilder.isNotLike(value) +fun isNotLike(value: T & Any): IsNotLike = SqlBuilder.isNotLike(value) -fun isNotLikeWhenPresent(value: T?): IsNotLike = SqlBuilder.isNotLikeWhenPresent(value) +fun isNotLikeWhenPresent(value: T?): IsNotLike = SqlBuilder.isNotLikeWhenPresent(value) // shortcuts for booleans fun isTrue(): IsEqualTo = isEqualTo(true) diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/model/ModelBuilderFunctions.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/model/ModelBuilderFunctions.kt index 2e5216133..45478711e 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/model/ModelBuilderFunctions.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/model/ModelBuilderFunctions.kt @@ -65,16 +65,16 @@ fun deleteFrom(table: SqlTable, completer: DeleteCompleter): DeleteModel = fun deleteFrom(table: SqlTable, tableAlias: String, completer: DeleteCompleter): DeleteModel = KotlinDeleteBuilder(SqlBuilder.deleteFrom(table, tableAlias)).apply(completer).build() -fun insert(row: T, completer: KotlinInsertCompleter): InsertModel = +fun insert(row: T, completer: KotlinInsertCompleter): InsertModel = KotlinInsertBuilder(row).apply(completer).build() -fun insertBatch(rows: Collection, completer: KotlinBatchInsertCompleter): BatchInsertModel = +fun insertBatch(rows: Collection, completer: KotlinBatchInsertCompleter): BatchInsertModel = KotlinBatchInsertBuilder(rows).apply(completer).build() fun insertInto(table: SqlTable, completer: GeneralInsertCompleter): GeneralInsertModel = KotlinGeneralInsertBuilder(table).apply(completer).build() -fun insertMultiple(rows: Collection, completer: KotlinMultiRowInsertCompleter): MultiRowInsertModel = +fun insertMultiple(rows: Collection, completer: KotlinMultiRowInsertCompleter): MultiRowInsertModel = KotlinMultiRowInsertBuilder(rows).apply(completer).build() fun insertSelect(table: SqlTable, completer: InsertSelectCompleter): InsertSelectModel = diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/MapperSupportFunctions.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/MapperSupportFunctions.kt index fa4643563..941be61af 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/MapperSupportFunctions.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/MapperSupportFunctions.kt @@ -63,7 +63,7 @@ fun countFrom(mapper: (SelectStatementProvider) -> Long, table: SqlTable, comple fun deleteFrom(mapper: (DeleteStatementProvider) -> Int, table: SqlTable, completer: DeleteCompleter): Int = deleteFrom(table, completer).run(mapper) -fun insert( +fun insert( mapper: (InsertStatementProvider) -> Int, row: T, table: SqlTable, @@ -81,7 +81,7 @@ fun insert( * list will be [org.apache.ibatis.executor.BatchExecutor.BATCH_UPDATE_RETURN_VALUE]). * To retrieve update counts, execute [org.apache.ibatis.session.SqlSession.flushStatements]. */ -fun insertBatch( +fun insertBatch( mapper: (InsertStatementProvider) -> Int, records: Collection, table: SqlTable, @@ -99,7 +99,7 @@ fun insertInto( ): Int = insertInto(table, completer).run(mapper) -fun insertMultiple( +fun insertMultiple( mapper: (MultiRowInsertStatementProvider) -> Int, records: Collection, table: SqlTable, @@ -110,7 +110,7 @@ fun insertMultiple( run(completer) }.run(mapper) -fun insertMultipleWithGeneratedKeys( +fun insertMultipleWithGeneratedKeys( mapper: (String, List) -> Int, records: Collection, table: SqlTable, diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/ProviderBuilderFunctions.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/ProviderBuilderFunctions.kt index e58e7c707..2b1950b77 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/ProviderBuilderFunctions.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/ProviderBuilderFunctions.kt @@ -68,16 +68,16 @@ fun deleteFrom(table: SqlTable, completer: DeleteCompleter): DeleteStatementProv fun deleteFrom(table: SqlTable, tableAlias: String, completer: DeleteCompleter): DeleteStatementProvider = deleteFrom(table, tableAlias, completer).render(RenderingStrategies.MYBATIS3) -fun insert(row: T, completer: KotlinInsertCompleter): InsertStatementProvider = +fun insert(row: T, completer: KotlinInsertCompleter): InsertStatementProvider = insert(row, completer).render(RenderingStrategies.MYBATIS3) -fun insertBatch(rows: Collection, completer: KotlinBatchInsertCompleter): BatchInsert = +fun insertBatch(rows: Collection, completer: KotlinBatchInsertCompleter): BatchInsert = insertBatch(rows, completer).render(RenderingStrategies.MYBATIS3) fun insertInto(table: SqlTable, completer: GeneralInsertCompleter): GeneralInsertStatementProvider = insertInto(table, completer).render(RenderingStrategies.MYBATIS3) -fun insertMultiple( +fun insertMultiple( rows: Collection, completer: KotlinMultiRowInsertCompleter ): MultiRowInsertStatementProvider = diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/NamedParameterJdbcTemplateExtensions.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/NamedParameterJdbcTemplateExtensions.kt index 20d1b4663..6bf0ebe2b 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/NamedParameterJdbcTemplateExtensions.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/NamedParameterJdbcTemplateExtensions.kt @@ -89,10 +89,10 @@ fun NamedParameterJdbcTemplate.insertBatch(records: List): BatchIns BatchInsertHelper(records, this) // single row insert -fun NamedParameterJdbcTemplate.insert(insertStatement: InsertStatementProvider): Int = +fun NamedParameterJdbcTemplate.insert(insertStatement: InsertStatementProvider): Int = update(insertStatement.insertStatement, BeanPropertySqlParameterSource(insertStatement.row)) -fun NamedParameterJdbcTemplate.insert( +fun NamedParameterJdbcTemplate.insert( insertStatement: InsertStatementProvider, keyHolder: KeyHolder ): Int = @@ -120,8 +120,8 @@ fun NamedParameterJdbcTemplate.insertInto(table: SqlTable, completer: GeneralIns // multiple row insert fun NamedParameterJdbcTemplate.insertMultiple( - vararg records: T - , completer: KotlinMultiRowInsertCompleter + vararg records: T, + completer: KotlinMultiRowInsertCompleter ): Int = insertMultiple(records.asList(), completer) @@ -205,7 +205,7 @@ fun NamedParameterJdbcTemplate.selectList( fun NamedParameterJdbcTemplate.selectList( selectStatement: SelectStatementProvider, type: KClass -): List = +): List = queryForList(selectStatement.selectStatement, selectStatement.parameters, type.java) fun NamedParameterJdbcTemplate.selectOne( diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/ProviderBuilderFunctions.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/ProviderBuilderFunctions.kt index 7fcb6bd39..fcb70e746 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/ProviderBuilderFunctions.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/ProviderBuilderFunctions.kt @@ -68,16 +68,16 @@ fun deleteFrom(table: SqlTable, completer: DeleteCompleter): DeleteStatementProv fun deleteFrom(table: SqlTable, tableAlias: String, completer: DeleteCompleter): DeleteStatementProvider = deleteFrom(table, tableAlias, completer).render(RenderingStrategies.SPRING_NAMED_PARAMETER) -fun insert(row: T, completer: KotlinInsertCompleter): InsertStatementProvider = +fun insert(row: T, completer: KotlinInsertCompleter): InsertStatementProvider = insert(row, completer).render(RenderingStrategies.SPRING_NAMED_PARAMETER) -fun insertBatch(rows: Collection, completer: KotlinBatchInsertCompleter): BatchInsert = +fun insertBatch(rows: Collection, completer: KotlinBatchInsertCompleter): BatchInsert = insertBatch(rows, completer).render(RenderingStrategies.SPRING_NAMED_PARAMETER) fun insertInto(table: SqlTable, completer: GeneralInsertCompleter): GeneralInsertStatementProvider = insertInto(table, completer).render(RenderingStrategies.SPRING_NAMED_PARAMETER) -fun insertMultiple( +fun insertMultiple( rows: Collection, completer: KotlinMultiRowInsertCompleter ): MultiRowInsertStatementProvider = diff --git a/src/test/kotlin/nullability/test/BetweenTest.kt b/src/test/kotlin/nullability/test/BetweenTest.kt new file mode 100644 index 000000000..31da32afa --- /dev/null +++ b/src/test/kotlin/nullability/test/BetweenTest.kt @@ -0,0 +1,150 @@ +/* + * Copyright 2016-2022 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 nullability.test + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class BetweenTest { + @Test + fun `Test That First Null Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isBetween null and 4 } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(9, 30))) + } + + @Test + fun `Test That Second Null Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isBetween 4 and null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(9, 36))) + } + + @Test + fun `Test That Both Null Causes Two Compile Errors`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isBetween null and null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf( + ErrorLocation(9, 30), + ErrorLocation(9, 39) + )) + } + + @Test + fun `Test That First Null In Elements Method Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isBetween + + fun testFunction() { + countFrom(person) { + where { id (isBetween(null).and(4)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 36))) + } + + @Test + fun `Test That Second Null In Elements Method Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isBetween + + fun testFunction() { + countFrom(person) { + where { id (isBetween(4).and(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 38))) + } + + @Test + fun `Test That Both Null In Elements Method Causes Two Compile Errors`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isBetween + + fun testFunction() { + countFrom(person) { + where { id (isBetween(null).and(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf( + ErrorLocation(10, 36), + ErrorLocation(10, 46) + )) + } +} diff --git a/src/test/kotlin/nullability/test/BetweenWhenPresentTest.kt b/src/test/kotlin/nullability/test/BetweenWhenPresentTest.kt new file mode 100644 index 000000000..f0ec0076b --- /dev/null +++ b/src/test/kotlin/nullability/test/BetweenWhenPresentTest.kt @@ -0,0 +1,144 @@ +/* + * Copyright 2016-2022 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 nullability.test + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class BetweenWhenPresentTest { + @Test + fun `Test That First Null Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isBetweenWhenPresent null and 4 } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Second Null Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isBetweenWhenPresent 4 and null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Both Null Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isBetweenWhenPresent null and null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That First Null In Elements Method Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isBetweenWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isBetweenWhenPresent(null).and(4)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Second Null In Elements Method Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isBetweenWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isBetweenWhenPresent(4).and(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Both Null In Elements Method Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isBetweenWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isBetweenWhenPresent(null).and(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } +} diff --git a/src/test/kotlin/nullability/test/ComparisonTest.kt b/src/test/kotlin/nullability/test/ComparisonTest.kt new file mode 100644 index 000000000..3ca3cd088 --- /dev/null +++ b/src/test/kotlin/nullability/test/ComparisonTest.kt @@ -0,0 +1,513 @@ +/* + * Copyright 2016-2022 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 nullability.test + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class ComparisonTest { + @Test + fun `Test That Null In EqualTo Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isEqualTo null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(9, 30))) + } + + @Test + fun `Test That Null In EqualToWhenPresent is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isEqualToWhenPresent null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In EqualTo Elements Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isEqualTo + + fun testFunction() { + countFrom(person) { + where { id (isEqualTo(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 31))) + } + + @Test + fun `Test That Null In EqualToWhenPresent Elements is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isEqualToWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isEqualToWhenPresent(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In NotEqualTo Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isNotEqualTo null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(9, 33))) + } + + @Test + fun `Test That Null In NotEqualToWhenPresent is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isNotEqualToWhenPresent null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In NotEqualTo Elements Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotEqualTo + + fun testFunction() { + countFrom(person) { + where { id (isNotEqualTo(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 34))) + } + + @Test + fun `Test That Null In NotEqualToWhenPresent Elements is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotEqualToWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isNotEqualToWhenPresent(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In GreaterThan Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isGreaterThan null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(9, 34))) + } + + @Test + fun `Test That Null In GreaterThanWhenPresent is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isGreaterThanWhenPresent null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In GreaterThan Elements Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isGreaterThan + + fun testFunction() { + countFrom(person) { + where { id (isGreaterThan(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 35))) + } + + @Test + fun `Test That Null In GreaterThanWhenPresent Elements is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isGreaterThanWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isGreaterThanWhenPresent(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In GreaterThanOrEqualTo Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isGreaterThanOrEqualTo null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(9, 43))) + } + + @Test + fun `Test That Null In GreaterThanOrEqualToWhenPresent is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isGreaterThanOrEqualToWhenPresent null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In GreaterThanOrEqualTo Elements Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isGreaterThanOrEqualTo + + fun testFunction() { + countFrom(person) { + where { id (isGreaterThanOrEqualTo(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 44))) + } + + @Test + fun `Test That Null In GreaterThanOrEqualToWhenPresent Elements is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isGreaterThanOrEqualToWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isGreaterThanOrEqualToWhenPresent(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In LessThan Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isLessThan null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(9, 31))) + } + + @Test + fun `Test That Null In LessThanWhenPresent is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isLessThanWhenPresent null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In LessThan Elements Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isLessThan + + fun testFunction() { + countFrom(person) { + where { id (isLessThan(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 32))) + } + + @Test + fun `Test That Null In LessThanWhenPresent Elements is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isLessThanWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isLessThanWhenPresent(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In LessThanOrEqualTo Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isLessThanOrEqualTo null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(9, 40))) + } + + @Test + fun `Test That Null In LessThanOrEqualToWhenPresent is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isLessThanOrEqualToWhenPresent null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In LessThanOrEqualTo Elements Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isLessThanOrEqualTo + + fun testFunction() { + countFrom(person) { + where { id (isLessThanOrEqualTo(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 41))) + } + + @Test + fun `Test That Null In LessThanOrEqualToWhenPresent Elements is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isLessThanOrEqualToWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isLessThanOrEqualToWhenPresent(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } +} diff --git a/src/test/kotlin/nullability/test/CompilerUtilities.kt b/src/test/kotlin/nullability/test/CompilerUtilities.kt new file mode 100644 index 000000000..0e2266191 --- /dev/null +++ b/src/test/kotlin/nullability/test/CompilerUtilities.kt @@ -0,0 +1,72 @@ +/* + * Copyright 2016-2022 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 nullability.test + +import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments +import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity +import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation +import org.jetbrains.kotlin.cli.common.messages.MessageCollector +import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler +import org.jetbrains.kotlin.config.Services +import java.io.File + +/** + * Compiles the source lines and returns any error reports generated by the compiler. + */ +fun compile(source: String): CompilerErrorMessageCollector { + val file: File = File.createTempFile("KotlinTest", ".kt") + file.deleteOnExit() + // normalize line endings and write to temp file + file.writeText(source.trimIndent().lines().joinToString(separator = System.lineSeparator())) + + val compilerArgs = K2JVMCompilerArguments().apply { + freeArgs = listOf(file.path) + destination = System.getProperty("java.io.tmpdir") + classpath = System.getProperty("java.class.path") + noStdlib = true + } + + return CompilerErrorMessageCollector().apply { + K2JVMCompiler().exec(this, Services.EMPTY, compilerArgs) + } +} + +class CompilerErrorMessageCollector: MessageCollector { + val reports = mutableListOf() + override fun clear() = reports.clear() + + override fun hasErrors() = reports.any { it.severity.isError } + + override fun report( + severity: CompilerMessageSeverity, + message: String, + location: CompilerMessageSourceLocation? + ) { + reports.add(CompilerErrorReport(severity, message, location)) + } + + fun errorLocations() = + reports.filter { it.severity.isError } + .mapNotNull { it.location } + .map { ErrorLocation(it.line, it.column) } +} + +data class CompilerErrorReport( + val severity: CompilerMessageSeverity, + val message: String, + val location: CompilerMessageSourceLocation? +) +data class ErrorLocation(val line: Int, val column: Int) diff --git a/src/test/kotlin/nullability/test/EqualNotEqualTest.kt b/src/test/kotlin/nullability/test/EqualNotEqualTest.kt new file mode 100644 index 000000000..27dadff36 --- /dev/null +++ b/src/test/kotlin/nullability/test/EqualNotEqualTest.kt @@ -0,0 +1,187 @@ +/* + * Copyright 2016-2022 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 nullability.test + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class EqualNotEqualTest { + @Test + fun `Test That Null Equal Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { firstName isEqualTo null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(9, 37))) + } + + @Test + fun `Test That Null Equal When Present is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { firstName isEqualToWhenPresent null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null Not Equal Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + val ids = listOf(4, null) + countFrom(person) { + where { firstName isNotEqualTo null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 40))) + } + + @Test + fun `Test That Null Not Equal When Present is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { firstName isNotEqualToWhenPresent null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null Elements Equal Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isEqualTo + + fun testFunction() { + countFrom(person) { + where { firstName (isEqualTo(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 38))) + } + + @Test + fun `Test That Null Elements Equal When Present is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isEqualToWhenPresent + + fun testFunction() { + countFrom(person) { + where { firstName (isEqualToWhenPresent(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null Elements Not Equal Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotEqualTo + + fun testFunction() { + val ids = listOf(4, null) + countFrom(person) { + where { firstName (isNotEqualTo(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(11, 41))) + } + + @Test + fun `Test That Null Elements Not Equal When Present is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotEqualToWhenPresent + + fun testFunction() { + countFrom(person) { + where { firstName (isNotEqualToWhenPresent(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } +} diff --git a/src/test/kotlin/nullability/test/InTest.kt b/src/test/kotlin/nullability/test/InTest.kt new file mode 100644 index 000000000..7010b53e7 --- /dev/null +++ b/src/test/kotlin/nullability/test/InTest.kt @@ -0,0 +1,105 @@ +/* + * Copyright 2016-2022 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 nullability.test + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class InTest { + @Test + fun `Test That Null In VarAgs Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id.isIn(4, null) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(9, 28))) + } + + @Test + fun `Test That Null in List Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + val ids = listOf(4, null) + countFrom(person) { + where { id isIn ids } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 25))) + } + + @Test + fun `Test That Null In VarArgs Elements Method Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isIn + + fun testFunction() { + countFrom(person) { + where { id (isIn(4, null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 29))) + } + + @Test + fun `Test That Null In List Elements Method Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isIn + + fun testFunction() { + val ids = listOf(4, null) + countFrom(person) { + where { id (isIn(ids)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(11, 26))) + } +} diff --git a/src/test/kotlin/nullability/test/InWhenPresentTest.kt b/src/test/kotlin/nullability/test/InWhenPresentTest.kt new file mode 100644 index 000000000..307e5255b --- /dev/null +++ b/src/test/kotlin/nullability/test/InWhenPresentTest.kt @@ -0,0 +1,187 @@ +/* + * Copyright 2016-2022 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 nullability.test + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class InWhenPresentTest { + @Test + fun `Test That Null In VarAgs Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id.isInWhenPresent(4, null) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null in List Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + val ids = listOf(4, null) + countFrom(person) { + where { id isInWhenPresent ids } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Empty VarAgs Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id.isInWhenPresent() } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null List Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id.isInWhenPresent(null as List) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In VarArgs Elements Method Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isInWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isInWhenPresent(4, null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In List Elements Method Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isInWhenPresent + + fun testFunction() { + val ids = listOf(4, null) + countFrom(person) { + where { id (isInWhenPresent(ids)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Empty VarAgs In Elements Method Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isInWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isInWhenPresent()) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null List In Elements Method Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isInWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isInWhenPresent(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } +} diff --git a/src/test/kotlin/nullability/test/LikeNotLikeTest.kt b/src/test/kotlin/nullability/test/LikeNotLikeTest.kt new file mode 100644 index 000000000..2b9de75b9 --- /dev/null +++ b/src/test/kotlin/nullability/test/LikeNotLikeTest.kt @@ -0,0 +1,187 @@ +/* + * Copyright 2016-2022 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 nullability.test + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class LikeNotLikeTest { + @Test + fun `Test That Null Like Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { firstName isLike null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(9, 34))) + } + + @Test + fun `Test That Null Like When Present is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { firstName isLikeWhenPresent null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null Not Like Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + val ids = listOf(4, null) + countFrom(person) { + where { firstName isNotLike null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 37))) + } + + @Test + fun `Test That Null Not Like When Present is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { firstName isNotLikeWhenPresent null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null Elements Like Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isLike + + fun testFunction() { + countFrom(person) { + where { firstName (isLike(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 35))) + } + + @Test + fun `Test That Null Elements Like When Present is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isLikeWhenPresent + + fun testFunction() { + countFrom(person) { + where { firstName (isLikeWhenPresent(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null Elements Not Like Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotLike + + fun testFunction() { + val ids = listOf(4, null) + countFrom(person) { + where { firstName (isNotLike(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(11, 38))) + } + + @Test + fun `Test That Null Elements Not Like When Present is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.firstName + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotLikeWhenPresent + + fun testFunction() { + countFrom(person) { + where { firstName (isNotLikeWhenPresent(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } +} diff --git a/src/test/kotlin/nullability/test/NotBetweenTest.kt b/src/test/kotlin/nullability/test/NotBetweenTest.kt new file mode 100644 index 000000000..8bc76fcea --- /dev/null +++ b/src/test/kotlin/nullability/test/NotBetweenTest.kt @@ -0,0 +1,150 @@ +/* + * Copyright 2016-2022 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 nullability.test + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class NotBetweenTest { + @Test + fun `Test That First Null Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isNotBetween null and 4 } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(9, 33))) + } + + @Test + fun `Test That Second Null Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isNotBetween 4 and null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(9, 39))) + } + + @Test + fun `Test That Both Null Causes Compile Errors`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isNotBetween null and null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf( + ErrorLocation(9, 33), + ErrorLocation(9, 42) + )) + } + + @Test + fun `Test That First Null In Elements Method Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotBetween + + fun testFunction() { + countFrom(person) { + where { id (isNotBetween(null).and(4)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 39))) + } + + @Test + fun `Test That Second Null In Elements Method Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotBetween + + fun testFunction() { + countFrom(person) { + where { id (isNotBetween(4).and(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 41))) + } + + @Test + fun `Test That Both Null In Elements Method Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotBetween + + fun testFunction() { + countFrom(person) { + where { id (isNotBetween(null).and(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf( + ErrorLocation(10, 39), + ErrorLocation(10, 49) + )) + } +} diff --git a/src/test/kotlin/nullability/test/NotBetweenWhenPresentTest.kt b/src/test/kotlin/nullability/test/NotBetweenWhenPresentTest.kt new file mode 100644 index 000000000..3faa85a6b --- /dev/null +++ b/src/test/kotlin/nullability/test/NotBetweenWhenPresentTest.kt @@ -0,0 +1,144 @@ +/* + * Copyright 2016-2022 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 nullability.test + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class NotBetweenWhenPresentTest { + @Test + fun `Test That First Null Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isNotBetweenWhenPresent null and 4 } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Second Null Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isNotBetweenWhenPresent 4 and null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Both Null Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id isNotBetweenWhenPresent null and null } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That First Null In Elements Method Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotBetweenWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isNotBetweenWhenPresent(null).and(4)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Second Null In Elements Method Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotBetweenWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isNotBetweenWhenPresent(4).and(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Both Null In Elements Method Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotBetweenWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isNotBetweenWhenPresent(null).and(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } +} diff --git a/src/test/kotlin/nullability/test/NotInTest.kt b/src/test/kotlin/nullability/test/NotInTest.kt new file mode 100644 index 000000000..f8e7d7ff3 --- /dev/null +++ b/src/test/kotlin/nullability/test/NotInTest.kt @@ -0,0 +1,105 @@ +/* + * Copyright 2016-2022 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 nullability.test + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class NotInTest { + @Test + fun `Test That Null In VarAgs Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id.isNotIn(4, null) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(9, 31))) + } + + @Test + fun `Test That Null in List Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + val ids = listOf(4, null) + countFrom(person) { + where { id isNotIn ids } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 28))) + } + + @Test + fun `Test That Null In VarArgs Elements Method Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotIn + + fun testFunction() { + countFrom(person) { + where { id (isNotIn(4, null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(10, 32))) + } + + @Test + fun `Test That Null In List Elements Method Causes Compile Error`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotIn + + fun testFunction() { + val ids = listOf(4, null) + countFrom(person) { + where { id (isNotIn(ids)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.errorLocations()).isEqualTo(listOf(ErrorLocation(11, 29))) + } +} diff --git a/src/test/kotlin/nullability/test/NotInWhenPresentTest.kt b/src/test/kotlin/nullability/test/NotInWhenPresentTest.kt new file mode 100644 index 000000000..99e57f43c --- /dev/null +++ b/src/test/kotlin/nullability/test/NotInWhenPresentTest.kt @@ -0,0 +1,187 @@ +/* + * Copyright 2016-2022 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 nullability.test + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class NotInWhenPresentTest { + @Test + fun `Test That Null In VarAgs Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id.isNotInWhenPresent(4, null) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null in List Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + val ids = listOf(4, null) + countFrom(person) { + where { id isNotInWhenPresent ids } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Empty VarAgs Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id.isNotInWhenPresent() } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null List Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + + fun testFunction() { + countFrom(person) { + where { id.isNotInWhenPresent(null as List) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In VarArgs Elements Method Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotInWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isNotInWhenPresent(4, null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null In List Elements Method Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotInWhenPresent + + fun testFunction() { + val ids = listOf(4, null) + countFrom(person) { + where { id (isNotInWhenPresent(ids)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Empty VarAgs In Elements Method Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotInWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isNotInWhenPresent()) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } + + @Test + fun `Test That Null List In Elements Method Is OK`() { + val source = """ + package temp.kotlin.test + + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.id + import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person + import org.mybatis.dynamic.sql.util.kotlin.mybatis3.countFrom + import org.mybatis.dynamic.sql.util.kotlin.elements.isNotInWhenPresent + + fun testFunction() { + countFrom(person) { + where { id (isNotInWhenPresent(null)) } + } + } + """ + + val compilerMessageCollector = compile(source) + assertThat(compilerMessageCollector.hasErrors()).isFalse + } +}