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.runtime.dynamic.sql.elements;
17  
18  import java.util.HashSet;
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.mybatis.generator.api.IntrospectedColumn;
23  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
24  import org.mybatis.generator.api.dom.java.Interface;
25  import org.mybatis.generator.api.dom.java.Method;
26  import org.mybatis.generator.api.dom.java.Parameter;
27  import org.mybatis.generator.codegen.mybatis3.ListUtilities;
28  
29  public class InsertMethodGenerator extends AbstractMethodGenerator {
30      private final FullyQualifiedJavaType recordType;
31  
32      private InsertMethodGenerator(Builder builder) {
33          super(builder);
34          recordType = builder.recordType;
35      }
36  
37      @Override
38      public MethodAndImports generateMethodAndImports() {
39          Set<FullyQualifiedJavaType> imports = new HashSet<>();
40  
41          imports.add(new FullyQualifiedJavaType("org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils")); //$NON-NLS-1$
42          imports.add(recordType);
43  
44          Method method = new Method("insert"); //$NON-NLS-1$
45          method.setDefault(true);
46          context.getCommentGenerator().addGeneralMethodAnnotation(method, introspectedTable, imports);
47          method.setReturnType(FullyQualifiedJavaType.getIntInstance());
48          method.addParameter(new Parameter(recordType, "row")); //$NON-NLS-1$
49  
50          method.addBodyLine("return MyBatis3Utils.insert(this::insert, row, " + tableFieldName //$NON-NLS-1$
51                  + ", c ->"); //$NON-NLS-1$
52  
53          List<IntrospectedColumn> columns =
54                  ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
55          boolean first = true;
56          for (IntrospectedColumn column : columns) {
57              String fieldName = calculateFieldName(column);
58  
59              if (first) {
60                  method.addBodyLine("    c.map(" + fieldName //$NON-NLS-1$
61                          + ").toProperty(\"" + column.getJavaProperty() //$NON-NLS-1$
62                          + "\")"); //$NON-NLS-1$
63                  first = false;
64              } else {
65                  method.addBodyLine("    .map(" + fieldName //$NON-NLS-1$
66                          + ").toProperty(\"" + column.getJavaProperty() //$NON-NLS-1$
67                          + "\")"); //$NON-NLS-1$
68              }
69          }
70  
71          method.addBodyLine(");"); //$NON-NLS-1$
72  
73          return MethodAndImports.withMethod(method)
74                  .withImports(imports)
75                  .build();
76      }
77  
78      @Override
79      public boolean callPlugins(Method method, Interface interfaze) {
80          return context.getPlugins().clientInsertMethodGenerated(method, interfaze, introspectedTable);
81      }
82  
83      public static class Builder extends BaseBuilder<Builder> {
84          private FullyQualifiedJavaType recordType;
85  
86          public Builder withRecordType(FullyQualifiedJavaType recordType) {
87              this.recordType = recordType;
88              return this;
89          }
90  
91          @Override
92          public Builder getThis() {
93              return this;
94          }
95  
96          public InsertMethodGenerator build() {
97              return new InsertMethodGenerator(this);
98          }
99      }
100 }