diff --git a/CHANGELOG.md b/CHANGELOG.md index de304acca..31e9fa8b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,10 @@ This log will detail notable changes to MyBatis Dynamic SQL. Full details are av ## Release 2.0.0 - Unreleased -The library now requires Java 17. +Significant changes: + +- The library now requires Java 17 +- Deprecated code from prior releases is removed ## Release 1.5.2 - June 3, 2024 diff --git a/README.md b/README.md index a9172a1b3..812dc3b9d 100644 --- a/README.md +++ b/README.md @@ -80,4 +80,4 @@ The library test cases provide several complete examples of using the library in ## Requirements -The library has no dependencies. Version 2.x requires Java 17. Version 1.8 requires Java 8. +The library has no dependencies. Version 2.x requires Java 17. Version 1.x requires Java 8. diff --git a/src/main/java/org/mybatis/dynamic/sql/AliasableSqlTable.java b/src/main/java/org/mybatis/dynamic/sql/AliasableSqlTable.java index 4e6c32386..7474b00ba 100644 --- a/src/main/java/org/mybatis/dynamic/sql/AliasableSqlTable.java +++ b/src/main/java/org/mybatis/dynamic/sql/AliasableSqlTable.java @@ -32,7 +32,7 @@ protected AliasableSqlTable(String tableName, Supplier constructor) { public T withAlias(String alias) { T newTable = constructor.get(); ((AliasableSqlTable) newTable).tableAlias = alias; - newTable.nameSupplier = nameSupplier; + newTable.tableName = tableName; return newTable; } @@ -48,7 +48,7 @@ public T withName(String name) { Objects.requireNonNull(name); T newTable = constructor.get(); ((AliasableSqlTable) newTable).tableAlias = tableAlias; - newTable.nameSupplier = () -> name; + newTable.tableName = name; return newTable; } diff --git a/src/main/java/org/mybatis/dynamic/sql/BasicColumn.java b/src/main/java/org/mybatis/dynamic/sql/BasicColumn.java index aaee827b1..b73757a71 100644 --- a/src/main/java/org/mybatis/dynamic/sql/BasicColumn.java +++ b/src/main/java/org/mybatis/dynamic/sql/BasicColumn.java @@ -17,11 +17,8 @@ import java.util.Optional; -import org.mybatis.dynamic.sql.exception.DynamicSqlException; import org.mybatis.dynamic.sql.render.RenderingContext; -import org.mybatis.dynamic.sql.render.TableAliasCalculator; import org.mybatis.dynamic.sql.util.FragmentAndParameters; -import org.mybatis.dynamic.sql.util.Messages; /** * Describes attributes of columns that are necessary for rendering if the column is not expected to @@ -59,25 +56,7 @@ public interface BasicColumn { * @return a rendered SQL fragment and, optionally, parameters associated with the fragment * @since 1.5.1 */ - default FragmentAndParameters render(RenderingContext renderingContext) { - // the default implementation ensures compatibility with prior releases. When the - // deprecated renderWithTableAlias method is removed, this function can become purely abstract. - // Also remove the method tableAliasCalculator() from RenderingContext. - return FragmentAndParameters.fromFragment(renderWithTableAlias(renderingContext.tableAliasCalculator())); - } - - /** - * Returns the name of the item aliased with a table name if appropriate. - * For example, "a.foo". This is appropriate for where clauses and order by clauses. - * - * @param tableAliasCalculator the table alias calculator for the current renderer - * @return the item name with the table alias applied - * @deprecated Please replace this method by overriding the more general "render" method - */ - @Deprecated - default String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) { - throw new DynamicSqlException(Messages.getString("ERROR.36")); //$NON-NLS-1$ - } + FragmentAndParameters render(RenderingContext renderingContext); /** * Utility method to make it easier to build column lists for methods that require an diff --git a/src/main/java/org/mybatis/dynamic/sql/SqlTable.java b/src/main/java/org/mybatis/dynamic/sql/SqlTable.java index d3b34eff0..40b6ccf17 100644 --- a/src/main/java/org/mybatis/dynamic/sql/SqlTable.java +++ b/src/main/java/org/mybatis/dynamic/sql/SqlTable.java @@ -18,93 +18,19 @@ import java.sql.JDBCType; import java.util.Objects; import java.util.Optional; -import java.util.function.Supplier; import org.jetbrains.annotations.NotNull; public class SqlTable implements TableExpression { - protected Supplier nameSupplier; + protected String tableName; protected SqlTable(String tableName) { - Objects.requireNonNull(tableName); - - this.nameSupplier = () -> tableName; - } - - /** - * Creates an SqlTable whose name can be changed at runtime. - * - * @param tableNameSupplier table name supplier - * @deprecated please use {@link AliasableSqlTable} if you need to change the table name at runtime - */ - @Deprecated - protected SqlTable(Supplier tableNameSupplier) { - Objects.requireNonNull(tableNameSupplier); - - this.nameSupplier = tableNameSupplier; - } - - /** - * Creates an SqlTable whose name can be changed at runtime. - * - * @param schemaSupplier schema supplier - * @param tableName table name - * @deprecated please use {@link AliasableSqlTable} if you need to change the table name at runtime - */ - @Deprecated - protected SqlTable(Supplier> schemaSupplier, String tableName) { - this(Optional::empty, schemaSupplier, tableName); - } - - /** - * Creates an SqlTable whose name can be changed at runtime. - * - * @param catalogSupplier catalog supplier - * @param schemaSupplier schema supplier - * @param tableName table name - * @deprecated please use {@link AliasableSqlTable} if you need to change the table name at runtime - */ - @Deprecated - protected SqlTable(Supplier> catalogSupplier, Supplier> schemaSupplier, - String tableName) { - Objects.requireNonNull(catalogSupplier); - Objects.requireNonNull(schemaSupplier); - Objects.requireNonNull(tableName); - - this.nameSupplier = () -> compose(catalogSupplier, schemaSupplier, tableName); - } - - private String compose(Supplier> catalogSupplier, Supplier> schemaSupplier, - String tableName) { - return catalogSupplier.get().map(c -> compose(c, schemaSupplier, tableName)) - .orElseGet(() -> compose(schemaSupplier, tableName)); - } - - private String compose(String catalog, Supplier> schemaSupplier, String tableName) { - return schemaSupplier.get().map(s -> composeCatalogSchemaAndTable(catalog, s, tableName)) - .orElseGet(() -> composeCatalogAndTable(catalog, tableName)); - } - - private String compose(Supplier> schemaSupplier, String tableName) { - return schemaSupplier.get().map(s -> composeSchemaAndTable(s, tableName)) - .orElse(tableName); - } - - private String composeCatalogAndTable(String catalog, String tableName) { - return catalog + ".." + tableName; //$NON-NLS-1$ - } - - private String composeSchemaAndTable(String schema, String tableName) { - return schema + "." + tableName; //$NON-NLS-1$ - } - - private String composeCatalogSchemaAndTable(String catalog, String schema, String tableName) { - return catalog + "." + schema + "." + tableName; //$NON-NLS-1$ //$NON-NLS-2$ + this.tableName = Objects.requireNonNull(tableName); } - public String tableNameAtRuntime() { - return nameSupplier.get(); + public String tableName() { + return tableName; } public BasicColumn allColumns() { diff --git a/src/main/java/org/mybatis/dynamic/sql/exception/DuplicateTableAliasException.java b/src/main/java/org/mybatis/dynamic/sql/exception/DuplicateTableAliasException.java index d32e66e5e..ee8c1e2d1 100644 --- a/src/main/java/org/mybatis/dynamic/sql/exception/DuplicateTableAliasException.java +++ b/src/main/java/org/mybatis/dynamic/sql/exception/DuplicateTableAliasException.java @@ -43,6 +43,6 @@ public DuplicateTableAliasException(SqlTable table, String newAlias, String exis } private static String generateMessage(SqlTable table, String newAlias, String existingAlias) { - return Messages.getString("ERROR.1", table.tableNameAtRuntime(), newAlias, existingAlias); //$NON-NLS-1$ + return Messages.getString("ERROR.1", table.tableName(), newAlias, existingAlias); //$NON-NLS-1$ } } 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 aae15c34e..d75e90e27 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 @@ -21,24 +21,11 @@ public class DefaultInsertStatementProvider implements InsertStatementProvider { private final String insertStatement; - // need to keep both row and record for now so we don't break - // old code. The MyBatis reflection utilities don't handle - // the case where the attribute name is different from the getter. - // - // MyBatis Generator version 1.4.1 (March 8, 2022) changed to use "row" instead of "record". - // Target March 2023 for removing "record" from MyBatis Dynamic SQL. - private final T record; private final T row; private DefaultInsertStatementProvider(Builder builder) { insertStatement = Objects.requireNonNull(builder.insertStatement); row = Objects.requireNonNull(builder.row); - record = row; - } - - @Override - public T getRecord() { - return record; } @Override diff --git a/src/main/java/org/mybatis/dynamic/sql/insert/render/InsertRenderingUtilities.java b/src/main/java/org/mybatis/dynamic/sql/insert/render/InsertRenderingUtilities.java index 31bbf02ed..e22143109 100644 --- a/src/main/java/org/mybatis/dynamic/sql/insert/render/InsertRenderingUtilities.java +++ b/src/main/java/org/mybatis/dynamic/sql/insert/render/InsertRenderingUtilities.java @@ -31,6 +31,6 @@ public static String calculateInsertStatement(SqlTable table, FieldAndValueColle } public static String calculateInsertStatementStart(SqlTable table) { - return "insert into " + table.tableNameAtRuntime(); //$NON-NLS-1$ + return "insert into " + table.tableName(); //$NON-NLS-1$ } } 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 6658320f9..ea0d0db18 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 @@ -18,16 +18,6 @@ import org.jetbrains.annotations.NotNull; public interface InsertStatementProvider { - /** - * Return the row associated with this insert statement. - * - * @return the row associated with this insert statement. - * - * @deprecated in favor of {@link InsertStatementProvider#getRow()} - */ - @Deprecated - T getRecord(); - /** * Return the row associated with this insert statement. * diff --git a/src/main/java/org/mybatis/dynamic/sql/render/GuaranteedTableAliasCalculator.java b/src/main/java/org/mybatis/dynamic/sql/render/GuaranteedTableAliasCalculator.java index 59948b03f..4d4a3b883 100644 --- a/src/main/java/org/mybatis/dynamic/sql/render/GuaranteedTableAliasCalculator.java +++ b/src/main/java/org/mybatis/dynamic/sql/render/GuaranteedTableAliasCalculator.java @@ -38,7 +38,7 @@ public Optional aliasForColumn(SqlTable table) { if (alias.isPresent()) { return alias; } else { - return Optional.of(table.tableNameAtRuntime()); + return Optional.of(table.tableName()); } } diff --git a/src/main/java/org/mybatis/dynamic/sql/render/RenderingContext.java b/src/main/java/org/mybatis/dynamic/sql/render/RenderingContext.java index a23c8d07a..bc953f847 100644 --- a/src/main/java/org/mybatis/dynamic/sql/render/RenderingContext.java +++ b/src/main/java/org/mybatis/dynamic/sql/render/RenderingContext.java @@ -53,11 +53,6 @@ private RenderingContext(Builder builder) { : builder.parameterName + "." + RenderingStrategy.DEFAULT_PARAMETER_PREFIX; //$NON-NLS-1$ } - public TableAliasCalculator tableAliasCalculator() { - // this method can be removed when the renderWithTableAlias method is removed from BasicColumn - return tableAliasCalculator; - } - private String nextMapKey() { return renderingStrategy.formatParameterMapKey(sequence); } @@ -93,8 +88,8 @@ public String aliasedColumnName(SqlColumn column, String explicitAlias) { public String aliasedTableName(SqlTable table) { return tableAliasCalculator.aliasForTable(table) - .map(a -> table.tableNameAtRuntime() + spaceBefore(a)) - .orElseGet(table::tableNameAtRuntime); + .map(a -> table.tableName() + spaceBefore(a)) + .orElseGet(table::tableName); } public boolean isNonRenderingClauseAllowed() { diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinBaseBuilders.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinBaseBuilders.kt index 2f6bc175b..4207905c6 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinBaseBuilders.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinBaseBuilders.kt @@ -25,15 +25,6 @@ import org.mybatis.dynamic.sql.where.AbstractWhereStarter @DslMarker annotation class MyBatisDslMarker -@Deprecated("Please use GroupingCriteriaCollector.where") -typealias WhereApplier = KotlinBaseBuilder<*>.() -> Unit - -@Deprecated("Please use GroupingCriteriaCollector.where") -fun WhereApplier.andThen(after: WhereApplier): WhereApplier = { - invoke(this) - after(this) -} - @MyBatisDslMarker @Suppress("TooManyFunctions") abstract class KotlinBaseBuilder> { @@ -51,31 +42,6 @@ abstract class KotlinBaseBuilder> { getDsl().where(criteria) } - @Deprecated("Please move the \"and\" function into the where lambda. If the where lambda has more than one condition, you may need to surround the existing conditions with \"group\" first.") - fun and(criteria: GroupingCriteriaReceiver): Unit = - GroupingCriteriaCollector().apply(criteria).let { - getDsl().where().and(it.initialCriterion, it.subCriteria) - } - - @Deprecated("Please move the \"and\" function into the where lambda. If the where lambda has more than one condition, you may need to surround the existing conditions with \"group\" first.") - fun and(criteria: List) { - getDsl().where().and(criteria) - } - - @Deprecated("Please move the \"or\" function into the where lambda. If the where lambda has more than one condition, you may need to surround the existing conditions with \"group\" first.") - fun or(criteria: GroupingCriteriaReceiver): Unit = - GroupingCriteriaCollector().apply(criteria).let { - getDsl().where().or(it.initialCriterion, it.subCriteria) - } - - @Deprecated("Please move the \"or\" function into the where lambda. If the where lambda has more than one condition, you may need to surround the existing conditions with \"group\" first.") - fun or(criteria: List) { - getDsl().where().or(criteria) - } - - @Deprecated("Please use GroupingCriteriaCollector.where, then pass it to the \"where\" method") - fun applyWhere(whereApplier: WhereApplier) = whereApplier.invoke(this) - /** * This function does nothing, but it can be used to make some code snippets more understandable. * diff --git a/src/main/resources/org/mybatis/dynamic/sql/util/messages.properties b/src/main/resources/org/mybatis/dynamic/sql/util/messages.properties index 9927dbeec..c25eb02c5 100644 --- a/src/main/resources/org/mybatis/dynamic/sql/util/messages.properties +++ b/src/main/resources/org/mybatis/dynamic/sql/util/messages.properties @@ -52,7 +52,7 @@ ERROR.33=Calling "select" or "selectDistinct" more than once is not allowed. Add union or unionAll expression ERROR.34=You must specify "select" or "selectDistinct" before any other clauses in a multi-select statement ERROR.35=Multi-select statements must have at least one "union" or "union all" expression -ERROR.36=You must either implement the "render" or "renderWithTableAlias" method in a column or function +ERROR.36=Obsolete Message - Not Used ERROR.37=The "{0}" function does not support conditions that fail to render ERROR.38=Bound values cannot be aliased ERROR.39=When clauses in case expressions must render diff --git a/src/test/java/examples/joins/JoinMapperTest.java b/src/test/java/examples/joins/JoinMapperTest.java index e84571231..50c8e30f5 100644 --- a/src/test/java/examples/joins/JoinMapperTest.java +++ b/src/test/java/examples/joins/JoinMapperTest.java @@ -957,7 +957,7 @@ void testSelfWithDuplicateAlias() { .from(user, "u1"); assertThatExceptionOfType(DuplicateTableAliasException.class).isThrownBy(() -> dsl.join(user, "u2")) - .withMessage(Messages.getString("ERROR.1", user.tableNameAtRuntime(), "u2", "u1")); + .withMessage(Messages.getString("ERROR.1", user.tableName(), "u2", "u1")); } @Test diff --git a/src/test/java/examples/schema_supplier/SchemaSupplier.java b/src/test/java/examples/schema_supplier/SchemaSupplier.java deleted file mode 100644 index b5a3e8fbe..000000000 --- a/src/test/java/examples/schema_supplier/SchemaSupplier.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2016-2024 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 - * - * https://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 examples.schema_supplier; - -import java.util.Optional; - -public class SchemaSupplier { - public static final String schema_property = "schemaToUse"; - - public static Optional schemaPropertyReader() { - return Optional.ofNullable(System.getProperty(schema_property)); - } -} diff --git a/src/test/java/examples/schema_supplier/SchemaSupplierTest.java b/src/test/java/examples/schema_supplier/SchemaSupplierTest.java deleted file mode 100644 index 7cc70fc7c..000000000 --- a/src/test/java/examples/schema_supplier/SchemaSupplierTest.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright 2016-2024 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 - * - * https://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 examples.schema_supplier; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.io.InputStream; -import java.io.InputStreamReader; -import java.sql.Connection; -import java.sql.DriverManager; -import java.util.List; - -import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; -import org.apache.ibatis.exceptions.PersistenceException; -import org.apache.ibatis.jdbc.ScriptRunner; -import org.apache.ibatis.mapping.Environment; -import org.apache.ibatis.session.Configuration; -import org.apache.ibatis.session.SqlSession; -import org.apache.ibatis.session.SqlSessionFactory; -import org.apache.ibatis.session.SqlSessionFactoryBuilder; -import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mybatis.dynamic.sql.select.SelectDSLCompleter; - -class SchemaSupplierTest { - - private static final String JDBC_URL = "jdbc:hsqldb:mem:aname"; - private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver"; - - private SqlSessionFactory sqlSessionFactory; - - @BeforeEach - void setup() throws Exception { - Class.forName(JDBC_DRIVER); - InputStream is = getClass().getResourceAsStream("/examples/schema_supplier/CreateDB.sql"); - assert is != null; - try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) { - ScriptRunner sr = new ScriptRunner(connection); - sr.setLogWriter(null); - sr.runScript(new InputStreamReader(is)); - } - - UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", ""); - Environment environment = new Environment("test", new JdbcTransactionFactory(), ds); - Configuration config = new Configuration(environment); - config.addMapper(UserMapper.class); - sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); - } - - @Test - void testUnsetSchemaProperty() { - System.clearProperty(SchemaSupplier.schema_property); - - try (SqlSession session = sqlSessionFactory.openSession()) { - UserMapper mapper = session.getMapper(UserMapper.class); - - User user = new User(); - user.setId(1); - user.setName("Fred"); - - assertThrows(PersistenceException.class, () -> mapper.insert(user)); - } - } - - @Test - void testSchemaProperty() { - System.setProperty(SchemaSupplier.schema_property, "schema1"); - - try (SqlSession session = sqlSessionFactory.openSession()) { - UserMapper mapper = session.getMapper(UserMapper.class); - - insertFlintstones(mapper); - - List records = mapper.select(SelectDSLCompleter.allRows()); - assertThat(records).hasSize(2); - } - } - - @Test - void testSchemaSwitchingProperty() { - try (SqlSession session = sqlSessionFactory.openSession()) { - UserMapper mapper = session.getMapper(UserMapper.class); - - System.setProperty(SchemaSupplier.schema_property, "schema1"); - insertFlintstones(mapper); - - List records = mapper.select(SelectDSLCompleter.allRows()); - assertThat(records).hasSize(2); - - System.setProperty(SchemaSupplier.schema_property, "schema2"); - insertRubbles(mapper); - - records = mapper.select(SelectDSLCompleter.allRows()); - assertThat(records).hasSize(3); - } - } - - private void insertFlintstones(UserMapper mapper) { - User user = new User(); - user.setId(1); - user.setName("Fred"); - int rows = mapper.insert(user); - assertThat(rows).isEqualTo(1); - - user = new User(); - user.setId(2); - user.setName("Wilma"); - rows = mapper.insert(user); - assertThat(rows).isEqualTo(1); - } - - private void insertRubbles(UserMapper mapper) { - User user = new User(); - user.setId(1); - user.setName("Barney"); - int rows = mapper.insert(user); - assertThat(rows).isEqualTo(1); - - user = new User(); - user.setId(2); - user.setName("Betty"); - rows = mapper.insert(user); - assertThat(rows).isEqualTo(1); - - user = new User(); - user.setId(3); - user.setName("Bamm Bamm"); - rows = mapper.insert(user); - assertThat(rows).isEqualTo(1); - } -} diff --git a/src/test/java/examples/schema_supplier/User.java b/src/test/java/examples/schema_supplier/User.java deleted file mode 100644 index 02acc9c53..000000000 --- a/src/test/java/examples/schema_supplier/User.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2016-2024 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 - * - * https://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 examples.schema_supplier; - -public class User { - private int id; - private String name; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/src/test/java/examples/schema_supplier/UserDynamicSqlSupport.java b/src/test/java/examples/schema_supplier/UserDynamicSqlSupport.java deleted file mode 100644 index fdb38f0ea..000000000 --- a/src/test/java/examples/schema_supplier/UserDynamicSqlSupport.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2016-2024 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 - * - * https://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 examples.schema_supplier; - -import java.sql.JDBCType; - -import org.mybatis.dynamic.sql.SqlColumn; -import org.mybatis.dynamic.sql.SqlTable; - -public class UserDynamicSqlSupport { - public static final User user = new User(); - public static final SqlColumn id = user.id; - public static final SqlColumn name = user.name; - - public static final class User extends SqlTable { - public final SqlColumn id = column("user_id", JDBCType.INTEGER); - public final SqlColumn name = column("user_name", JDBCType.VARCHAR); - - public User() { - super(SchemaSupplier::schemaPropertyReader, "User"); - } - } -} diff --git a/src/test/java/examples/schema_supplier/UserMapper.java b/src/test/java/examples/schema_supplier/UserMapper.java deleted file mode 100644 index 602ed49a3..000000000 --- a/src/test/java/examples/schema_supplier/UserMapper.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2016-2024 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 - * - * https://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 examples.schema_supplier; - -import static examples.schema_supplier.UserDynamicSqlSupport.*; - -import java.util.List; - -import org.apache.ibatis.annotations.Result; -import org.apache.ibatis.annotations.Results; -import org.apache.ibatis.annotations.SelectProvider; -import org.apache.ibatis.type.JdbcType; -import org.mybatis.dynamic.sql.BasicColumn; -import org.mybatis.dynamic.sql.SqlBuilder; -import org.mybatis.dynamic.sql.render.RenderingStrategies; -import org.mybatis.dynamic.sql.select.SelectDSLCompleter; -import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; -import org.mybatis.dynamic.sql.util.SqlProviderAdapter; -import org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper; -import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils; - -public interface UserMapper extends CommonInsertMapper { - - @SelectProvider(type=SqlProviderAdapter.class, method="select") - @Results(id="UserResult", value= { - @Result(column="USER_ID", property="id", jdbcType=JdbcType.INTEGER, id=true), - @Result(column="USER_NAME", property="name", jdbcType=JdbcType.VARCHAR) - }) - List selectMany(SelectStatementProvider selectStatement); - - default List select(SelectDSLCompleter completer) { - return MyBatis3Utils.selectList(this::selectMany, BasicColumn.columnList(id, name), user, completer); - } - - default int insert(User row) { - return insert(SqlBuilder.insert(row) - .into(user) - .map(id).toProperty("id") - .map(name).toProperty("name") - .build() - .render(RenderingStrategies.MYBATIS3)); - } -} diff --git a/src/test/java/org/mybatis/dynamic/sql/InvalidSQLTest.java b/src/test/java/org/mybatis/dynamic/sql/InvalidSQLTest.java index 155ccb9cf..bfec86e73 100644 --- a/src/test/java/org/mybatis/dynamic/sql/InvalidSQLTest.java +++ b/src/test/java/org/mybatis/dynamic/sql/InvalidSQLTest.java @@ -19,7 +19,6 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mybatis.dynamic.sql.SqlBuilder.insert; import static org.mybatis.dynamic.sql.SqlBuilder.insertInto; -import static org.mybatis.dynamic.sql.SqlBuilder.select; import static org.mybatis.dynamic.sql.SqlBuilder.update; import static org.mybatis.dynamic.sql.SqlBuilder.value; @@ -32,7 +31,6 @@ import org.junit.jupiter.api.Test; import org.mybatis.dynamic.sql.common.OrderByModel; import org.mybatis.dynamic.sql.configuration.StatementConfiguration; -import org.mybatis.dynamic.sql.exception.DynamicSqlException; import org.mybatis.dynamic.sql.exception.InvalidSqlException; import org.mybatis.dynamic.sql.insert.BatchInsertModel; import org.mybatis.dynamic.sql.insert.GeneralInsertModel; @@ -41,7 +39,6 @@ import org.mybatis.dynamic.sql.insert.MultiRowInsertModel; import org.mybatis.dynamic.sql.render.RenderingContext; import org.mybatis.dynamic.sql.render.RenderingStrategies; -import org.mybatis.dynamic.sql.render.TableAliasCalculator; import org.mybatis.dynamic.sql.select.GroupByModel; import org.mybatis.dynamic.sql.select.PagingModel; import org.mybatis.dynamic.sql.select.QueryExpressionModel; @@ -50,7 +47,6 @@ import org.mybatis.dynamic.sql.select.join.JoinSpecification; import org.mybatis.dynamic.sql.select.join.JoinType; import org.mybatis.dynamic.sql.select.render.FetchFirstPagingModelRenderer; -import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; import org.mybatis.dynamic.sql.update.UpdateModel; import org.mybatis.dynamic.sql.util.InternalError; import org.mybatis.dynamic.sql.util.Messages; @@ -265,23 +261,6 @@ void testInvalidValueAlias() { .withMessage(Messages.getString("ERROR.38")); } - @Test - void testBadColumn() { - SelectModel selectModel = select(new BadCount<>()).from(person).build(); - assertThatExceptionOfType(DynamicSqlException.class) - .isThrownBy(() -> selectModel.render(RenderingStrategies.MYBATIS3)) - .withMessage(Messages.getString("ERROR.36")); - } - - @Test - void testDeprecatedColumn() { - SelectStatementProvider selectStatement = select(new DeprecatedCount<>()) - .from(person) - .build() - .render(RenderingStrategies.MYBATIS3); - assertThat(selectStatement.getSelectStatement()).isEqualTo("select count(*) from person"); - } - static class TestRow { private Integer id; @@ -293,42 +272,4 @@ public void setId(Integer id) { this.id = id; } } - - static class BadCount implements BindableColumn { - private String alias; - - @Override - public Optional alias() { - return Optional.ofNullable(alias); - } - - @Override - public BindableColumn as(String alias) { - BadCount newCount = new BadCount<>(); - newCount.alias = alias; - return newCount; - } - } - - static class DeprecatedCount implements BindableColumn { - private String alias; - - @Override - @Deprecated - public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) { - return "count(*)"; - } - - @Override - public Optional alias() { - return Optional.ofNullable(alias); - } - - @Override - public BindableColumn as(String alias) { - DeprecatedCount newCount = new DeprecatedCount<>(); - newCount.alias = alias; - return newCount; - } - } } diff --git a/src/test/java/org/mybatis/dynamic/sql/SqlTableTest.java b/src/test/java/org/mybatis/dynamic/sql/SqlTableTest.java deleted file mode 100644 index 32d336572..000000000 --- a/src/test/java/org/mybatis/dynamic/sql/SqlTableTest.java +++ /dev/null @@ -1,182 +0,0 @@ -/* - * Copyright 2016-2024 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 - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.mybatis.dynamic.sql; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.Optional; -import java.util.function.Supplier; - -import org.junit.jupiter.api.Test; - -class SqlTableTest { - - private static final String NAME_PROPERTY = "nameProperty"; - - @Test - void testFullName() { - SqlTable table = new SqlTable("my_table"); - assertThat(table.tableNameAtRuntime()).isEqualTo("my_table"); - } - - @Test - void testFullNameSupplier() { - - System.setProperty(NAME_PROPERTY, "my_table"); - SqlTable table = new SqlTable(SqlTableTest::namePropertyReader); - assertThat(table.tableNameAtRuntime()).isEqualTo("my_table"); - System.clearProperty(NAME_PROPERTY); - } - - @Test - void testSchemaSupplierEmpty() { - SqlTable table = new SqlTable(Optional::empty, "my_table"); - assertThat(table.tableNameAtRuntime()).isEqualTo("my_table"); - } - - @Test - void testSchemaSupplierWithValue() { - SqlTable table = new SqlTable(() -> Optional.of("my_schema"), "my_table"); - assertThat(table.tableNameAtRuntime()).isEqualTo("my_schema.my_table"); - } - - @Test - void testSingletonSchemaSupplier() { - SqlTable table = new SqlTable(MySchemaSupplier.instance(), "my_table"); - assertThat(table.tableNameAtRuntime()).isEqualTo("first_schema.my_table"); - } - - @Test - void testThatSchemaSupplierDoesDelay() { - MySchemaSupplier schemaSupplier = new MySchemaSupplier(); - SqlTable table = new SqlTable(schemaSupplier, "my_table"); - assertThat(table.tableNameAtRuntime()).isEqualTo("first_schema.my_table"); - - schemaSupplier.setFirst(false); - assertThat(table.tableNameAtRuntime()).isEqualTo("second_schema.my_table"); - } - - @Test - void testCatalogAndSchemaSupplierEmpty() { - SqlTable table = new SqlTable(Optional::empty, Optional::empty, "my_table"); - assertThat(table.tableNameAtRuntime()).isEqualTo("my_table"); - } - - @Test - void testCatalogSupplierWithValue() { - SqlTable table = new SqlTable(() -> Optional.of("my_catalog"), Optional::empty, "my_table"); - assertThat(table.tableNameAtRuntime()).isEqualTo("my_catalog..my_table"); - } - - @Test - void testThatCatalogSupplierDoesDelay() { - MyCatalogSupplier catalogSupplier = new MyCatalogSupplier(); - SqlTable table = new SqlTable(catalogSupplier, Optional::empty, "my_table"); - assertThat(table.tableNameAtRuntime()).isEqualTo("first_catalog..my_table"); - - catalogSupplier.setFirst(false); - assertThat(table.tableNameAtRuntime()).isEqualTo("second_catalog..my_table"); - } - - @Test - void testThatCatalogSupplierAndSchemaSupplierBothDelay() { - MyCatalogSupplier catalogSupplier = new MyCatalogSupplier(); - MySchemaSupplier schemaSupplier = new MySchemaSupplier(); - SqlTable table = new SqlTable(catalogSupplier, schemaSupplier, "my_table"); - assertThat(table.tableNameAtRuntime()).isEqualTo("first_catalog.first_schema.my_table"); - - catalogSupplier.setFirst(false); - assertThat(table.tableNameAtRuntime()).isEqualTo("second_catalog.first_schema.my_table"); - - catalogSupplier.setFirst(true); - schemaSupplier.setFirst(false); - assertThat(table.tableNameAtRuntime()).isEqualTo("first_catalog.second_schema.my_table"); - - catalogSupplier.setFirst(false); - assertThat(table.tableNameAtRuntime()).isEqualTo("second_catalog.second_schema.my_table"); - - catalogSupplier.setEmpty(true); - assertThat(table.tableNameAtRuntime()).isEqualTo("second_schema.my_table"); - - schemaSupplier.setEmpty(true); - assertThat(table.tableNameAtRuntime()).isEqualTo("my_table"); - - catalogSupplier.setEmpty(false); - assertThat(table.tableNameAtRuntime()).isEqualTo("second_catalog..my_table"); - } - - private static String namePropertyReader() { - return System.getProperty(NAME_PROPERTY); - } - - static class MySchemaSupplier implements Supplier> { - private static final MySchemaSupplier instance = new MySchemaSupplier(); - - static MySchemaSupplier instance() { - return instance; - } - - private boolean first = true; - private boolean empty; - - void setFirst(boolean first) { - this.first = first; - } - - void setEmpty(boolean empty) { - this.empty = empty; - } - - @Override - public Optional get() { - if (empty) { - return Optional.empty(); - } - - if (first) { - return Optional.of("first_schema"); - } else { - return Optional.of("second_schema"); - } - } - } - - static class MyCatalogSupplier implements Supplier> { - private boolean first = true; - private boolean empty; - - void setFirst(boolean first) { - this.first = first; - } - - void setEmpty(boolean empty) { - this.empty = empty; - } - - @Override - public Optional get() { - if (empty) { - return Optional.empty(); - } - - if (first) { - return Optional.of("first_catalog"); - } else { - return Optional.of("second_catalog"); - } - } - } -} diff --git a/src/test/java/org/mybatis/dynamic/sql/insert/InsertStatementTest.java b/src/test/java/org/mybatis/dynamic/sql/insert/InsertStatementTest.java index 350532cb2..e0ebe5d19 100644 --- a/src/test/java/org/mybatis/dynamic/sql/insert/InsertStatementTest.java +++ b/src/test/java/org/mybatis/dynamic/sql/insert/InsertStatementTest.java @@ -115,24 +115,6 @@ void testSelectiveInsertStatementBuilder() { assertThat(insertStatement.getInsertStatement()).isEqualTo(expected); } - @Test - void testDeprecatedMethod() { - TestRecord row = new TestRecord(); - row.setLastName("jones"); - row.setOccupation("dino driver"); - - InsertStatementProvider insertStatement = insert(row) - .into(foo) - .map(id).toPropertyWhenPresent("id", row::getId) - .map(firstName).toPropertyWhenPresent("firstName", row::getFirstName) - .map(lastName).toPropertyWhenPresent("lastName", row::getLastName) - .map(occupation).toPropertyWhenPresent("occupation", row::getOccupation) - .build() - .render(RenderingStrategies.MYBATIS3); - - assertThat(insertStatement.getRow()).isEqualTo(insertStatement.getRecord()); - } - static class TestRecord { private Integer id; private String firstName; diff --git a/src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapperTest.kt b/src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapperTest.kt index 8276ebe9c..5dc24ede2 100644 --- a/src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapperTest.kt +++ b/src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapperTest.kt @@ -242,7 +242,7 @@ class PersonMapperTest { } @Test - fun testInsertSelect() { + fun testInsertSelectExtensionFunction() { sqlSessionFactory.openSession().use { session -> val mapper = session.getMapper(PersonMapper::class.java) @@ -259,7 +259,7 @@ class PersonMapperTest { } @Test - fun testDeprecatedInsertSelect() { + fun testInsertSelect() { sqlSessionFactory.openSession().use { session -> val mapper = session.getMapper(PersonMapper::class.java) diff --git a/src/test/kotlin/examples/kotlin/mybatis3/canonical/ReusableWhereTest.kt b/src/test/kotlin/examples/kotlin/mybatis3/canonical/ReusableWhereTest.kt index 7feca7e63..db54364e7 100644 --- a/src/test/kotlin/examples/kotlin/mybatis3/canonical/ReusableWhereTest.kt +++ b/src/test/kotlin/examples/kotlin/mybatis3/canonical/ReusableWhereTest.kt @@ -27,7 +27,6 @@ import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance import org.mybatis.dynamic.sql.util.kotlin.GroupingCriteriaCollector.Companion.where -import org.mybatis.dynamic.sql.util.kotlin.WhereApplier import org.mybatis.dynamic.sql.util.kotlin.andThen import org.mybatis.dynamic.sql.util.kotlin.mybatis3.select @@ -44,19 +43,6 @@ class ReusableWhereTest { } } - @Test - fun testCountWithDeprecatedClause() { - sqlSessionFactory.openSession().use { session -> - val mapper = session.getMapper(PersonMapper::class.java) - - val rows = mapper.count { - applyWhere(commonWhere) - } - - assertThat(rows).isEqualTo(3) - } - } - @Test fun testDelete() { sqlSessionFactory.openSession().use { session -> @@ -98,27 +84,6 @@ class ReusableWhereTest { } } - @Test - fun testDeprecatedComposition() { - val composedWhereClause = commonWhere.andThen { - and { birthDate.isNotNull() } - }.andThen { - or { addressId isLessThan 3 } - } - - val selectStatement = select(person.allColumns()) { - from(person) - applyWhere(composedWhereClause) - } - - assertThat(selectStatement.selectStatement).isEqualTo( - "select * from Person " + - "where id = #{parameters.p1,jdbcType=INTEGER} or occupation is null " + - "and birth_date is not null " + - "or address_id < #{parameters.p2,jdbcType=INTEGER}" - ) - } - @Test fun testComposition() { val composedWhereClause = commonWhereClause.andThen { @@ -140,11 +105,6 @@ class ReusableWhereTest { ) } - private val commonWhere: WhereApplier = { - where { id isEqualTo 1 } - or { occupation.isNull() } - } - private val commonWhereClause = where { id isEqualTo 1 or { occupation.isNull() } diff --git a/src/test/kotlin/issues/kotlin/gh430/KNoInitialConditionsTest.kt b/src/test/kotlin/issues/kotlin/gh430/KNoInitialConditionsTest.kt index 0f78c6d13..b11b4f3aa 100644 --- a/src/test/kotlin/issues/kotlin/gh430/KNoInitialConditionsTest.kt +++ b/src/test/kotlin/issues/kotlin/gh430/KNoInitialConditionsTest.kt @@ -175,8 +175,10 @@ class KNoInitialConditionsTest { val selectStatement = select(column1, column2) { from(foo) - where { column1 isLessThan Date() } - or(criteria) + where { + column1 isLessThan Date() + or(criteria) + } } val expected = "select column1, column2 from foo where column1 < :p1 " + @@ -187,7 +189,9 @@ class KNoInitialConditionsTest { private fun buildSelectStatement(criteria: List) = select(column1, column2) { from(foo) - where { column1 isLessThan Date() } - and(criteria) + where { + column1 isLessThan Date() + and(criteria) + } } }