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