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