View Javadoc
1   /*
2    *    Copyright 2016-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.dynamic.sql.util;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
22  import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
23  import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider;
24  import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
25  import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
26  import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
27  import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
28  
29  /**
30   * Adapter for use with MyBatis SQL provider annotations.
31   *
32   * @author Jeff Butler
33   */
34  public class SqlProviderAdapter {
35  
36      public String delete(DeleteStatementProvider deleteStatement) {
37          return deleteStatement.getDeleteStatement();
38      }
39  
40      public String generalInsert(GeneralInsertStatementProvider insertStatement) {
41          return insertStatement.getInsertStatement();
42      }
43  
44      public String insert(InsertStatementProvider<?> insertStatement) {
45          return insertStatement.getInsertStatement();
46      }
47  
48      public String insertMultiple(MultiRowInsertStatementProvider<?> insertStatement) {
49          return insertStatement.getInsertStatement();
50      }
51  
52      /**
53       * This adapter method is intended for use with MyBatis' &#064;InsertProvider annotation when there are generated
54       * values expected from executing the insert statement. The canonical method signature for using this adapter method
55       * is as follows:
56       *
57       * <pre>
58       * public interface FooMapper {
59       *     &#064;InsertProvider(type=SqlProviderAdapter.class, method="insertMultipleWithGeneratedKeys")
60       *     &#064;Options(useGeneratedKeys=true, keyProperty="records.id")
61       *     int insertMultiple(String insertStatement, &#064;Param("records") List&lt;Foo&gt; records)
62       * }
63       * </pre>
64       *
65       * @param parameterMap
66       *            The parameter map is automatically created by MyBatis when there are multiple parameters in the insert
67       *            method.
68       *
69       * @return the SQL statement contained in the parameter map. This is assumed to be the one and only map entry of
70       *         type String.
71       */
72      public String insertMultipleWithGeneratedKeys(Map<String, Object> parameterMap) {
73          List<String> entries = parameterMap.entrySet().stream()
74                  .filter(e -> e.getKey().startsWith("param")) //$NON-NLS-1$
75                  .map(Map.Entry::getValue)
76                  .filter(String.class::isInstance)
77                  .map(String.class::cast)
78                  .toList();
79  
80          if (entries.size() == 1) {
81              return entries.get(0);
82          } else {
83              throw new IllegalArgumentException(Messages.getString("ERROR.30")); //$NON-NLS-1$
84          }
85      }
86  
87      public String insertSelect(InsertSelectStatementProvider insertStatement) {
88          return insertStatement.getInsertStatement();
89      }
90  
91      public String select(SelectStatementProvider selectStatement) {
92          return selectStatement.getSelectStatement();
93      }
94  
95      public String update(UpdateStatementProvider updateStatement) {
96          return updateStatement.getUpdateStatement();
97      }
98  }