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.config;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19  import static org.mybatis.generator.internal.util.messages.Messages.getString;
20  
21  import java.util.List;
22  
23  import org.mybatis.generator.internal.db.DatabaseDialects;
24  
25  /**
26   * This class specifies that a key is auto-generated, either as an identity
27   * column (post insert), or as some other query like a sequences (pre insert).
28   *
29   * @author Jeff Butler
30   */
31  public class GeneratedKey {
32  
33      private final String column;
34  
35      private final String runtimeSqlStatement;
36  
37      private final boolean isIdentity;
38  
39      private final String type;
40  
41      public GeneratedKey(String column, String configuredSqlStatement,
42              boolean isIdentity, String type) {
43          super();
44          this.column = column;
45          this.type = type;
46          this.isIdentity = isIdentity;
47  
48          DatabaseDialects dialect = DatabaseDialects
49                  .getDatabaseDialect(configuredSqlStatement);
50          if (dialect == null) {
51              this.runtimeSqlStatement = configuredSqlStatement;
52          } else {
53              this.runtimeSqlStatement = dialect.getIdentityRetrievalStatement();
54          }
55      }
56  
57      public String getColumn() {
58          return column;
59      }
60  
61      public boolean isIdentity() {
62          return isIdentity;
63      }
64  
65      public String getRuntimeSqlStatement() {
66          return runtimeSqlStatement;
67      }
68  
69      public String getMyBatis3Order() {
70          return isIdentity ? "AFTER" : "BEFORE"; //$NON-NLS-1$ //$NON-NLS-2$
71      }
72  
73      public void validate(List<String> errors, String tableName) {
74          if (!stringHasValue(runtimeSqlStatement)) {
75              errors.add(getString("ValidationError.7", //$NON-NLS-1$
76                      tableName));
77          }
78  
79          if (stringHasValue(type)
80                  && !"pre".equals(type) //$NON-NLS-1$
81                  && !"post".equals(type)) { //$NON-NLS-1$ //$NON-NLS-2$
82              errors.add(getString("ValidationError.15", tableName)); //$NON-NLS-1$
83          }
84  
85          if ("pre".equals(type) && isIdentity) { //$NON-NLS-1$
86              errors.add(getString("ValidationError.23", //$NON-NLS-1$
87                      tableName));
88          }
89  
90          if ("post".equals(type) && !isIdentity) { //$NON-NLS-1$
91              errors.add(getString("ValidationError.24", //$NON-NLS-1$
92                      tableName));
93          }
94      }
95  
96      public boolean isJdbcStandard() {
97          return "JDBC".equals(runtimeSqlStatement); //$NON-NLS-1$
98      }
99  }