View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.db.dialect;
15  
16  import java.sql.DatabaseMetaData;
17  import java.sql.SQLException;
18  
19  import ch.qos.logback.core.spi.ContextAwareBase;
20  
21  /**
22   * 
23   * @author Ceki Gulcu
24   * 
25   */
26  public class DBUtil extends ContextAwareBase {
27      private static final String POSTGRES_PART = "postgresql";
28      private static final String MYSQL_PART = "mysql";
29      private static final String ORACLE_PART = "oracle";
30      // private static final String MSSQL_PART = "mssqlserver4";
31      private static final String MSSQL_PART = "microsoft";
32      private static final String HSQL_PART = "hsql";
33      private static final String H2_PART = "h2";
34      private static final String SYBASE_SQLANY_PART = "sql anywhere";
35      private static final String SQLITE_PART = "sqlite";
36  
37      public static SQLDialectCode discoverSQLDialect(DatabaseMetaData meta) {
38          SQLDialectCode dialectCode = SQLDialectCode.UNKNOWN_DIALECT;
39  
40          try {
41  
42              String dbName = meta.getDatabaseProductName().toLowerCase();
43  
44              if (dbName.indexOf(POSTGRES_PART) != -1) {
45                  return SQLDialectCode.POSTGRES_DIALECT;
46              } else if (dbName.indexOf(MYSQL_PART) != -1) {
47                  return SQLDialectCode.MYSQL_DIALECT;
48              } else if (dbName.indexOf(ORACLE_PART) != -1) {
49                  return SQLDialectCode.ORACLE_DIALECT;
50              } else if (dbName.indexOf(MSSQL_PART) != -1) {
51                  return SQLDialectCode.MSSQL_DIALECT;
52              } else if (dbName.indexOf(HSQL_PART) != -1) {
53                  return SQLDialectCode.HSQL_DIALECT;
54              } else if (dbName.indexOf(H2_PART) != -1) {
55                  return SQLDialectCode.H2_DIALECT;
56              } else if (dbName.indexOf(SYBASE_SQLANY_PART) != -1) {
57                  return SQLDialectCode.SYBASE_SQLANYWHERE_DIALECT;
58              } else if (dbName.indexOf(SQLITE_PART) != -1) {
59                  return SQLDialectCode.SQLITE_DIALECT;
60              } else {
61                  return SQLDialectCode.UNKNOWN_DIALECT;
62              }
63          } catch (SQLException sqle) {
64              // we can't do much here
65          }
66  
67          return dialectCode;
68      }
69  
70      public static SQLDialect getDialectFromCode(SQLDialectCode sqlDialectType) {
71          SQLDialect sqlDialect = null;
72  
73          switch (sqlDialectType) {
74          case POSTGRES_DIALECT:
75              sqlDialect = new PostgreSQLDialect();
76              break;
77  
78          case MYSQL_DIALECT:
79              sqlDialect = new MySQLDialect();
80              break;
81  
82          case ORACLE_DIALECT:
83              sqlDialect = new OracleDialect();
84              break;
85  
86          case MSSQL_DIALECT:
87              sqlDialect = new MsSQLDialect();
88              break;
89  
90          case HSQL_DIALECT:
91              sqlDialect = new HSQLDBDialect();
92              break;
93  
94          case H2_DIALECT:
95              sqlDialect = new H2Dialect();
96              break;
97  
98          case SYBASE_SQLANYWHERE_DIALECT:
99              sqlDialect = new SybaseSqlAnywhereDialect();
100             break;
101 
102         case SQLITE_DIALECT:
103             sqlDialect = new SQLiteDialect();
104             break;
105             
106         case UNKNOWN_DIALECT:
107             // nothing to do 
108 
109         }
110         return sqlDialect;
111     }
112 
113     /**
114      * This method handles cases where the
115      * {@link DatabaseMetaData#supportsGetGeneratedKeys} method is missing in the
116      * JDBC driver implementation.
117      */
118     public boolean supportsGetGeneratedKeys(DatabaseMetaData meta) {
119         try {
120             //
121             // invoking JDBC 1.4 method by reflection
122             //
123             return ((Boolean) DatabaseMetaData.class.getMethod("supportsGetGeneratedKeys", (Class[]) null).invoke(meta, (Object[]) null)).booleanValue();
124         } catch (Throwable e) {
125             addInfo("Could not call supportsGetGeneratedKeys method. This may be recoverable");
126             return false;
127         }
128     }
129 
130     /**
131      * This method handles cases where the
132      * {@link DatabaseMetaData#supportsBatchUpdates} method is missing in the JDBC
133      * driver implementation.
134      */
135     public boolean supportsBatchUpdates(DatabaseMetaData meta) {
136         try {
137             return meta.supportsBatchUpdates();
138         } catch (Throwable e) {
139             addInfo("Missing DatabaseMetaData.supportsBatchUpdates method.");
140             return false;
141         }
142     }
143 }