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 InsertSelectiveMethodGenerator extends AbstractKotlinFunctionGenerator {
28      private final FullyQualifiedKotlinType recordType;
29      private final String mapperName;
30      private final String supportObjectImport;
31  
32      private InsertSelectiveMethodGenerator(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 + ".insertSelective") //$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              if (column.isSequenceColumn()) {
67                  function.addCodeLine("    map(" + fieldNameAndImport.fieldName() //$NON-NLS-1$
68                          + ").toProperty(\"" + column.getJavaProperty() //$NON-NLS-1$
69                          + "\")"); //$NON-NLS-1$
70              } else {
71                  function.addCodeLine("    map(" + fieldNameAndImport.fieldName() //$NON-NLS-1$
72                          + ").toPropertyWhenPresent(\"" + column.getJavaProperty() //$NON-NLS-1$
73                          + "\", row::" //$NON-NLS-1$
74                          + column.getJavaProperty() + ")"); //$NON-NLS-1$
75              }
76          }
77  
78          function.addCodeLine("}"); //$NON-NLS-1$
79  
80          return functionAndImports;
81      }
82  
83      @Override
84      public boolean callPlugins(KotlinFunction kotlinFunction, KotlinFile kotlinFile) {
85          return context.getPlugins().clientInsertSelectiveMethodGenerated(kotlinFunction, kotlinFile, introspectedTable);
86      }
87  
88      public static class Builder extends BaseBuilder<Builder> {
89          private FullyQualifiedKotlinType recordType;
90          private String mapperName;
91          private String supportObjectImport;
92  
93          public Builder withRecordType(FullyQualifiedKotlinType recordType) {
94              this.recordType = recordType;
95              return this;
96          }
97  
98          public Builder withMapperName(String mapperName) {
99              this.mapperName = mapperName;
100             return this;
101         }
102 
103         public Builder withSupportObjectImport(String supportObjectImport) {
104             this.supportObjectImport = supportObjectImport;
105             return this;
106         }
107 
108         @Override
109         public Builder getThis() {
110             return this;
111         }
112 
113         public InsertSelectiveMethodGenerator build() {
114             return new InsertSelectiveMethodGenerator(this);
115         }
116     }
117 }