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.internal.db;
17  
18  /**
19   * Typesafe enum of known database dialects.
20   *
21   * @author Jeff Butler
22   */
23  public enum DatabaseDialects {
24  
25      DB2("VALUES IDENTITY_VAL_LOCAL()"), //$NON-NLS-1$
26      MYSQL("SELECT LAST_INSERT_ID()"), //$NON-NLS-1$
27      SQLSERVER("SELECT SCOPE_IDENTITY()"), //$NON-NLS-1$
28      CLOUDSCAPE("VALUES IDENTITY_VAL_LOCAL()"), //$NON-NLS-1$
29      DERBY("VALUES IDENTITY_VAL_LOCAL()"), //$NON-NLS-1$
30      HSQLDB("CALL IDENTITY()"), //$NON-NLS-1$
31      SYBASE("SELECT @@IDENTITY"), //$NON-NLS-1$
32      DB2_MF("SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1"), //$NON-NLS-1$
33      INFORMIX("select dbinfo('sqlca.sqlerrd1') from systables where tabid=1"); //$NON-NLS-1$
34  
35      private final String identityRetrievalStatement;
36  
37      DatabaseDialects(String identityRetrievalStatement) {
38          this.identityRetrievalStatement = identityRetrievalStatement;
39      }
40  
41      public String getIdentityRetrievalStatement() {
42          return identityRetrievalStatement;
43      }
44  
45      /**
46       * Gets the database dialect.
47       *
48       * @param database
49       *            the database
50       *
51       * @return the database dialect for the selected database. May return null if there is no known dialect for the
52       *         selected db
53       */
54      public static DatabaseDialects getDatabaseDialect(String database) {
55          DatabaseDialects returnValue = null;
56  
57          if ("DB2".equalsIgnoreCase(database)) { //$NON-NLS-1$
58              returnValue = DB2;
59          } else if ("MySQL".equalsIgnoreCase(database)) { //$NON-NLS-1$
60              returnValue = MYSQL;
61          } else if ("SqlServer".equalsIgnoreCase(database)) { //$NON-NLS-1$
62              returnValue = SQLSERVER;
63          } else if ("Cloudscape".equalsIgnoreCase(database)) { //$NON-NLS-1$
64              returnValue = CLOUDSCAPE;
65          } else if ("Derby".equalsIgnoreCase(database)) { //$NON-NLS-1$
66              returnValue = DERBY;
67          } else if ("HSQLDB".equalsIgnoreCase(database)) { //$NON-NLS-1$
68              returnValue = HSQLDB;
69          } else if ("SYBASE".equalsIgnoreCase(database)) { //$NON-NLS-1$
70              returnValue = SYBASE;
71          } else if ("DB2_MF".equalsIgnoreCase(database)) { //$NON-NLS-1$
72              returnValue = DB2_MF;
73          } else if ("Informix".equalsIgnoreCase(database)) { //$NON-NLS-1$
74              returnValue = INFORMIX;
75          }
76  
77          return returnValue;
78      }
79  }