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.kotlin.elements;
17  
18  import java.util.List;
19  
20  import org.mybatis.generator.api.IntrospectedColumn;
21  import org.mybatis.generator.api.dom.kotlin.FullyQualifiedKotlinType;
22  import org.mybatis.generator.api.dom.kotlin.KotlinArg;
23  import org.mybatis.generator.api.dom.kotlin.KotlinFile;
24  import org.mybatis.generator.api.dom.kotlin.KotlinFunction;
25  import org.mybatis.generator.codegen.mybatis3.ListUtilities;
26  
27  public class InsertMethodGenerator extends AbstractKotlinFunctionGenerator {
28      private final FullyQualifiedKotlinType recordType;
29      private final String mapperName;
30      private final String supportObjectImport;
31  
32      private InsertMethodGenerator(Builder builder) {
33          super(builder);
34          recordType = builder.recordType;
35          mapperName = builder.mapperName;
36          supportObjectImport = builder.supportObjectImport;
37      }
38  
39      @Override
40      public KotlinFunctionAndImports generateMethodAndImports() {
41          KotlinFunctionAndImports functionAndImports = KotlinFunctionAndImports.withFunction(
42                  KotlinFunction.newOneLineFunction(mapperName + ".insert") //$NON-NLS-1$
43                  .withArgument(KotlinArg.newArg("row") //$NON-NLS-1$
44                          .withDataType(recordType.getShortNameWithTypeArguments())
45                          .build())
46                  .build())
47                  .withImport("org.mybatis.dynamic.sql.util.kotlin.mybatis3.insert") //$NON-NLS-1$
48                  .withImports(recordType.getImportList())
49                  .build();
50  
51          addFunctionComment(functionAndImports);
52  
53          KotlinFunction function = functionAndImports.getFunction();
54  
55          function.addCodeLine("insert(this::insert, row, " + tableFieldName //$NON-NLS-1$
56                  + ") {"); //$NON-NLS-1$
57  
58          List<IntrospectedColumn> columns =
59                  ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
60          for (IntrospectedColumn column : columns) {
61              AbstractKotlinFunctionGenerator.FieldNameAndImport fieldNameAndImport =
62                      AbstractKotlinFunctionGenerator.calculateFieldNameAndImport(tableFieldName,
63                              supportObjectImport, column);
64              functionAndImports.getImports().add(fieldNameAndImport.importString());
65  
66              function.addCodeLine("    map(" + fieldNameAndImport.fieldName() //$NON-NLS-1$
67                      + ") toProperty \"" + column.getJavaProperty() //$NON-NLS-1$
68                      + "\""); //$NON-NLS-1$
69          }
70  
71          function.addCodeLine("}"); //$NON-NLS-1$
72  
73          return functionAndImports;
74      }
75  
76      @Override
77      public boolean callPlugins(KotlinFunction kotlinFunction, KotlinFile kotlinFile) {
78          return context.getPlugins().clientInsertMethodGenerated(kotlinFunction, kotlinFile, introspectedTable);
79      }
80  
81      public static class Builder extends BaseBuilder<Builder> {
82          private FullyQualifiedKotlinType recordType;
83          private String mapperName;
84          private String supportObjectImport;
85  
86          public Builder withRecordType(FullyQualifiedKotlinType recordType) {
87              this.recordType = recordType;
88              return this;
89          }
90  
91          public Builder withMapperName(String mapperName) {
92              this.mapperName = mapperName;
93              return this;
94          }
95  
96          public Builder withSupportObjectImport(String supportObjectImport) {
97              this.supportObjectImport = supportObjectImport;
98              return this;
99          }
100 
101         @Override
102         public Builder getThis() {
103             return this;
104         }
105 
106         public InsertMethodGenerator build() {
107             return new InsertMethodGenerator(this);
108         }
109     }
110 }