View Javadoc
1   /*
2    *    Copyright 2006-2023 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.generator.codegen.mybatis3.javamapper.elements.annotated;
17  
18  import static org.mybatis.generator.api.dom.OutputUtilities.javaIndent;
19  import static org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities.getEscapedColumnName;
20  import static org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities.getParameterClause;
21  import static org.mybatis.generator.internal.util.StringUtility.escapeStringForJava;
22  
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.mybatis.generator.api.IntrospectedColumn;
28  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
29  import org.mybatis.generator.api.dom.java.Interface;
30  import org.mybatis.generator.api.dom.java.Method;
31  import org.mybatis.generator.codegen.mybatis3.ListUtilities;
32  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.InsertMethodGenerator;
33  
34  public class AnnotatedInsertMethodGenerator extends InsertMethodGenerator {
35  
36      public AnnotatedInsertMethodGenerator(boolean isSimple) {
37          super(isSimple);
38      }
39  
40      @Override
41      public void addMapperAnnotations(Method method) {
42  
43          method.addAnnotation("@Insert({"); //$NON-NLS-1$
44          StringBuilder insertClause = new StringBuilder();
45          StringBuilder valuesClause = new StringBuilder();
46  
47          javaIndent(insertClause, 1);
48          javaIndent(valuesClause, 1);
49  
50          insertClause.append("\"insert into "); //$NON-NLS-1$
51          insertClause.append(escapeStringForJava(introspectedTable
52                  .getFullyQualifiedTableNameAtRuntime()));
53          insertClause.append(" ("); //$NON-NLS-1$
54  
55          valuesClause.append("\"values ("); //$NON-NLS-1$
56  
57          List<String> valuesClauses = new ArrayList<>();
58          Iterator<IntrospectedColumn> iter =
59                  ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())
60                  .iterator();
61          boolean hasFields = false;
62          while (iter.hasNext()) {
63              IntrospectedColumn introspectedColumn = iter.next();
64  
65              insertClause.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
66              valuesClause.append(getParameterClause(introspectedColumn));
67              hasFields = true;
68              if (iter.hasNext()) {
69                  insertClause.append(", "); //$NON-NLS-1$
70                  valuesClause.append(", "); //$NON-NLS-1$
71              }
72  
73              if (valuesClause.length() > 60) {
74                  if (!iter.hasNext()) {
75                      insertClause.append(')');
76                      valuesClause.append(')');
77                  }
78                  insertClause.append("\","); //$NON-NLS-1$
79                  valuesClause.append('\"');
80                  if (iter.hasNext()) {
81                      valuesClause.append(',');
82                  }
83  
84                  method.addAnnotation(insertClause.toString());
85                  insertClause.setLength(0);
86                  javaIndent(insertClause, 1);
87                  insertClause.append('\"');
88  
89                  valuesClauses.add(valuesClause.toString());
90                  valuesClause.setLength(0);
91                  javaIndent(valuesClause, 1);
92                  valuesClause.append('\"');
93                  hasFields = false;
94              }
95          }
96  
97          if (hasFields) {
98              insertClause.append(")\","); //$NON-NLS-1$
99              method.addAnnotation(insertClause.toString());
100 
101             valuesClause.append(")\""); //$NON-NLS-1$
102             valuesClauses.add(valuesClause.toString());
103         }
104 
105         for (String clause : valuesClauses) {
106             method.addAnnotation(clause);
107         }
108 
109         method.addAnnotation("})"); //$NON-NLS-1$
110 
111         buildGeneratedKeyAnnotation().ifPresent(method::addAnnotation);
112     }
113 
114     @Override
115     public void addExtraImports(Interface interfaze) {
116         interfaze.addImportedTypes(buildGeneratedKeyImportsIfRequired());
117         interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Insert")); //$NON-NLS-1$
118     }
119 }