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  import org.mybatis.generator.runtime.dynamic.sql.elements.Utils;
27  
28  public class InsertMultipleMethodGenerator extends AbstractKotlinFunctionGenerator {
29      private final FullyQualifiedKotlinType recordType;
30      private final String mapperName;
31      private final String supportObjectImport;
32  
33      private InsertMultipleMethodGenerator(Builder builder) {
34          super(builder);
35          recordType = builder.recordType;
36          mapperName = builder.mapperName;
37          supportObjectImport = builder.supportObjectImport;
38      }
39  
40      @Override
41      public KotlinFunctionAndImports generateMethodAndImports() {
42          if (!Utils.generateMultipleRowInsert(introspectedTable)) {
43              return null;
44          }
45  
46          // Kotlin type inference gets lost if we don't name the helper method something different from the
47          // regular mapper method
48          String functionImport;
49          String functionShortName;
50          if (Utils.canRetrieveMultiRowGeneratedKeys(introspectedTable)) {
51              functionImport =
52                      "org.mybatis.dynamic.sql.util.kotlin.mybatis3.insertMultipleWithGeneratedKeys"; //$NON-NLS-1$
53              functionShortName = "insertMultipleWithGeneratedKeys"; //$NON-NLS-1$
54          } else {
55              functionImport = "org.mybatis.dynamic.sql.util.kotlin.mybatis3.insertMultiple"; //$NON-NLS-1$
56              functionShortName = "insertMultiple"; //$NON-NLS-1$
57          }
58  
59          KotlinFunctionAndImports functionAndImports = KotlinFunctionAndImports.withFunction(
60                  KotlinFunction.newOneLineFunction(mapperName + ".insertMultiple") //$NON-NLS-1$
61                  .withArgument(KotlinArg.newArg("records") //$NON-NLS-1$
62                          .withDataType("Collection<" //$NON-NLS-1$
63                                  + recordType.getShortNameWithTypeArguments()
64                                  + ">") //$NON-NLS-1$
65                          .build())
66                  .build())
67                  .withImport(functionImport)
68                  .withImports(recordType.getImportList())
69                  .build();
70  
71          addFunctionComment(functionAndImports);
72  
73          KotlinFunction function = functionAndImports.getFunction();
74  
75          function.addCodeLine(functionShortName + "(this::insertMultiple" //$NON-NLS-1$
76                  + ", records, " + tableFieldName //$NON-NLS-1$
77                  + ") {"); //$NON-NLS-1$
78  
79          List<IntrospectedColumn> columns =
80                  ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
81          for (IntrospectedColumn column : columns) {
82              AbstractKotlinFunctionGenerator.FieldNameAndImport fieldNameAndImport =
83                      AbstractKotlinFunctionGenerator.calculateFieldNameAndImport(tableFieldName,
84                              supportObjectImport, column);
85              functionAndImports.getImports().add(fieldNameAndImport.importString());
86  
87              function.addCodeLine("    map(" + fieldNameAndImport.fieldName() //$NON-NLS-1$
88                      + ") toProperty \"" + column.getJavaProperty() //$NON-NLS-1$
89                      + "\""); //$NON-NLS-1$
90          }
91  
92          function.addCodeLine("}"); //$NON-NLS-1$
93  
94          return functionAndImports;
95      }
96  
97      @Override
98      public boolean callPlugins(KotlinFunction kotlinFunction, KotlinFile kotlinFile) {
99          return context.getPlugins().clientInsertMultipleMethodGenerated(kotlinFunction, kotlinFile, introspectedTable);
100     }
101 
102     public static class Builder extends BaseBuilder<Builder> {
103         private FullyQualifiedKotlinType recordType;
104         private String mapperName;
105         private String supportObjectImport;
106 
107         public Builder withRecordType(FullyQualifiedKotlinType recordType) {
108             this.recordType = recordType;
109             return this;
110         }
111 
112         public Builder withMapperName(String mapperName) {
113             this.mapperName = mapperName;
114             return this;
115         }
116 
117         public Builder withSupportObjectImport(String supportObjectImport) {
118             this.supportObjectImport = supportObjectImport;
119             return this;
120         }
121 
122         @Override
123         public Builder getThis() {
124             return this;
125         }
126 
127         public InsertMultipleMethodGenerator build() {
128             return new InsertMultipleMethodGenerator(this);
129         }
130     }
131 }