View Javadoc
1   /**
2    *    Copyright 2006-2016 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    *       http://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.javamapper.elements.InsertMethodGenerator;
32  import org.mybatis.generator.config.GeneratedKey;
33  
34  /**
35   * 
36   * @author Jeff Butler
37   */
38  public class AnnotatedInsertMethodGenerator extends
39      InsertMethodGenerator {
40  
41      public AnnotatedInsertMethodGenerator(boolean isSimple) {
42          super(isSimple);
43      }
44  
45      @Override
46      public void addMapperAnnotations(Interface interfaze, Method method) {
47          interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Insert")); //$NON-NLS-1$
48          
49          GeneratedKey gk = introspectedTable.getGeneratedKey();
50          
51          method.addAnnotation("@Insert({"); //$NON-NLS-1$
52          StringBuilder insertClause = new StringBuilder();
53          StringBuilder valuesClause = new StringBuilder();
54          
55          javaIndent(insertClause, 1);
56          javaIndent(valuesClause, 1);
57  
58          insertClause.append("\"insert into "); //$NON-NLS-1$
59          insertClause.append(escapeStringForJava(introspectedTable
60                  .getFullyQualifiedTableNameAtRuntime()));
61          insertClause.append(" ("); //$NON-NLS-1$
62  
63          valuesClause.append("\"values ("); //$NON-NLS-1$
64  
65          List<String> valuesClauses = new ArrayList<String>();
66          Iterator<IntrospectedColumn> iter = introspectedTable.getAllColumns()
67                  .iterator();
68          boolean hasFields = false;
69          while (iter.hasNext()) {
70              IntrospectedColumn introspectedColumn = iter.next();
71              if (introspectedColumn.isIdentity()) {
72                  // cannot set values on identity fields
73                  continue;
74              }
75  
76              insertClause.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
77              valuesClause.append(getParameterClause(introspectedColumn));
78              hasFields = true;
79              if (iter.hasNext()) {
80                  insertClause.append(", "); //$NON-NLS-1$
81                  valuesClause.append(", "); //$NON-NLS-1$
82              }
83  
84              if (valuesClause.length() > 60) {
85                  if (!iter.hasNext()) {
86                      insertClause.append(')');
87                      valuesClause.append(')');
88                  }
89                  insertClause.append("\","); //$NON-NLS-1$
90                  valuesClause.append('\"');
91                  if (iter.hasNext()) {
92                      valuesClause.append(',');
93                  }
94                  
95                  method.addAnnotation(insertClause.toString());
96                  insertClause.setLength(0);
97                  javaIndent(insertClause, 1);
98                  insertClause.append('\"');
99                  
100                 valuesClauses.add(valuesClause.toString());
101                 valuesClause.setLength(0);
102                 javaIndent(valuesClause, 1);
103                 valuesClause.append('\"');
104                 hasFields = false;
105             }
106         }
107         
108         if (hasFields) {
109             insertClause.append(")\","); //$NON-NLS-1$
110             method.addAnnotation(insertClause.toString());
111 
112             valuesClause.append(")\""); //$NON-NLS-1$
113             valuesClauses.add(valuesClause.toString());
114         }
115 
116         for (String clause : valuesClauses) {
117             method.addAnnotation(clause);
118         }
119         
120         method.addAnnotation("})"); //$NON-NLS-1$
121 
122         if (gk != null) {
123             addGeneratedKeyAnnotation(interfaze, method, gk);
124         }
125     }
126 }