View Javadoc
1   /**
2    *    Copyright 2006-2016 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    *       http://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.api.dom.xml.Attribute;
24  import org.mybatis.generator.api.dom.xml.XmlElement;
25  import org.mybatis.generator.internal.db.DatabaseDialects;
26  
27  /**
28   * This class specifies that a key is auto-generated, either as an identity
29   * column (post insert), or as some other query like a sequences (pre insert).
30   * 
31   * @author Jeff Butler
32   */
33  public class GeneratedKey {
34      
35      /** The column. */
36      private String column;
37  
38      /** The configured sql statement. */
39      private String configuredSqlStatement;
40  
41      /** The runtime sql statement. */
42      private String runtimeSqlStatement;
43  
44      /** The is identity. */
45      private boolean isIdentity;
46  
47      /** The type. */
48      private String type;
49  
50      /**
51       * Instantiates a new generated key.
52       *
53       * @param column
54       *            the column
55       * @param configuredSqlStatement
56       *            the configured sql statement
57       * @param isIdentity
58       *            the is identity
59       * @param type
60       *            the type
61       */
62      public GeneratedKey(String column, String configuredSqlStatement,
63              boolean isIdentity, String type) {
64          super();
65          this.column = column;
66          this.type = type;
67          this.isIdentity = isIdentity;
68          this.configuredSqlStatement = configuredSqlStatement;
69  
70          DatabaseDialects dialect = DatabaseDialects
71                  .getDatabaseDialect(configuredSqlStatement);
72          if (dialect == null) {
73              this.runtimeSqlStatement = configuredSqlStatement;
74          } else {
75              this.runtimeSqlStatement = dialect.getIdentityRetrievalStatement();
76          }
77      }
78  
79      /**
80       * Gets the column.
81       *
82       * @return the column
83       */
84      public String getColumn() {
85          return column;
86      }
87  
88      /**
89       * Checks if is identity.
90       *
91       * @return true, if is identity
92       */
93      public boolean isIdentity() {
94          return isIdentity;
95      }
96  
97      /**
98       * Gets the runtime sql statement.
99       *
100      * @return the runtime sql statement
101      */
102     public String getRuntimeSqlStatement() {
103         return runtimeSqlStatement;
104     }
105 
106     /**
107      * Gets the type.
108      *
109      * @return the type
110      */
111     public String getType() {
112         return type;
113     }
114 
115     /**
116      * This method is used by the iBATIS2 generators to know if the XML <selectKey> element should be placed before the
117      * insert SQL statement.
118      *
119      * @return true, if is placed before insert in ibatis2
120      */
121     public boolean isPlacedBeforeInsertInIbatis2() {
122         boolean rc;
123         
124         if (stringHasValue(type)) {
125             rc = true;
126         } else {
127             rc = !isIdentity;
128         }
129         
130         return rc;
131     }
132     
133     /**
134      * Gets the my batis3 order.
135      *
136      * @return the my batis3 order
137      */
138     public String getMyBatis3Order() {
139         return isIdentity ? "AFTER" : "BEFORE"; //$NON-NLS-1$ //$NON-NLS-2$
140     }
141     
142     /**
143      * To xml element.
144      *
145      * @return the xml element
146      */
147     public XmlElement toXmlElement() {
148         XmlElement xmlElement = new XmlElement("generatedKey"); //$NON-NLS-1$
149         xmlElement.addAttribute(new Attribute("column", column)); //$NON-NLS-1$
150         xmlElement.addAttribute(new Attribute(
151                 "sqlStatement", configuredSqlStatement)); //$NON-NLS-1$
152         if (stringHasValue(type)) {
153             xmlElement.addAttribute(new Attribute("type", type)); //$NON-NLS-1$
154         }
155         xmlElement.addAttribute(new Attribute("identity", //$NON-NLS-1$
156                 isIdentity ? "true" : "false")); //$NON-NLS-1$ //$NON-NLS-2$
157 
158         return xmlElement;
159     }
160 
161     /**
162      * Validate.
163      *
164      * @param errors
165      *            the errors
166      * @param tableName
167      *            the table name
168      */
169     public void validate(List<String> errors, String tableName) {
170         if (!stringHasValue(runtimeSqlStatement)) {
171             errors.add(getString("ValidationError.7", //$NON-NLS-1$
172                     tableName));
173         }
174 
175         if (stringHasValue(type) &&
176                 !"pre".equals(type) && !"post".equals(type)) { //$NON-NLS-1$ //$NON-NLS-2$
177             errors.add(getString("ValidationError.15", tableName)); //$NON-NLS-1$
178         }
179         
180         if ("pre".equals(type) && isIdentity) { //$NON-NLS-1$
181             errors.add(getString("ValidationError.23", //$NON-NLS-1$
182                     tableName));
183         }
184         
185         if ("post".equals(type) && !isIdentity) { //$NON-NLS-1$
186             errors.add(getString("ValidationError.24", //$NON-NLS-1$
187                     tableName));
188         }
189     }
190     
191     /**
192      * Checks if is jdbc standard.
193      *
194      * @return true, if is jdbc standard
195      */
196     public boolean isJdbcStandard() {
197         return "JDBC".equals(runtimeSqlStatement); //$NON-NLS-1$
198     }
199 }