View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.db.dialect;
11  
12  import java.sql.DatabaseMetaData;
13  import java.sql.SQLException;
14  
15  import ch.qos.logback.core.spi.ContextAwareBase;
16  
17  /**
18   * 
19   * @author Ceki Gulcu
20   * 
21   */
22  public class DBUtil extends ContextAwareBase {
23    private static final String POSTGRES_PART = "postgresql";
24    private static final String MYSQL_PART = "mysql";
25    private static final String ORACLE_PART = "oracle";
26    // private static final String MSSQL_PART = "mssqlserver4";
27    private static final String MSSQL_PART = "microsoft";
28    private static final String HSQL_PART = "hsql";
29  
30    public static SQLDialectCode discoverSQLDialect(DatabaseMetaData meta) {
31      SQLDialectCode dialectCode = SQLDialectCode.UNKNOWN_DIALECT;
32  
33      try {
34  
35        String dbName = meta.getDatabaseProductName().toLowerCase();
36  
37        if (dbName.indexOf(POSTGRES_PART) != -1) {
38          return SQLDialectCode.POSTGRES_DIALECT;
39        } else if (dbName.indexOf(MYSQL_PART) != -1) {
40          return SQLDialectCode.MYSQL_DIALECT;
41        } else if (dbName.indexOf(ORACLE_PART) != -1) {
42          return SQLDialectCode.ORACLE_DIALECT;
43        } else if (dbName.indexOf(MSSQL_PART) != -1) {
44          return SQLDialectCode.MSSQL_DIALECT;
45        } else if (dbName.indexOf(HSQL_PART) != -1) {
46          return SQLDialectCode.HSQL_DIALECT;
47        } else {
48          return SQLDialectCode.UNKNOWN_DIALECT;
49        }
50      } catch (SQLException sqle) {
51        // we can't do much here
52      }
53  
54      return dialectCode;
55    }
56  
57    public static SQLDialect getDialectFromCode(SQLDialectCode sqlDialectType) {
58      SQLDialect sqlDialect = null;
59  
60      switch (sqlDialectType) {
61      case POSTGRES_DIALECT:
62        sqlDialect = new PostgreSQLDialect();
63  
64        break;
65      case MYSQL_DIALECT:
66        sqlDialect = new MySQLDialect();
67  
68        break;
69      case ORACLE_DIALECT:
70        sqlDialect = new OracleDialect();
71  
72        break;
73      case MSSQL_DIALECT:
74        sqlDialect = new MsSQLDialect();
75  
76        break;
77      case HSQL_DIALECT:
78        sqlDialect = new HSQLDBDialect();
79  
80        break;
81      }
82      return sqlDialect;
83    }
84  
85    /**
86     * This method handles cases where the
87     * {@link DatabaseMetaData#supportsGetGeneratedKeys} method is missing in the
88     * JDBC driver implementation.
89     */
90    public boolean supportsGetGeneratedKeys(DatabaseMetaData meta) {
91      try {
92        //
93        // invoking JDK 1.4 method by reflection
94        //
95        return ((Boolean) DatabaseMetaData.class.getMethod(
96            "supportsGetGeneratedKeys", (Class[]) null).invoke(meta,
97            (Object[]) null)).booleanValue();
98      } catch (Throwable e) {
99        addInfo("Could not call supportsGetGeneratedKeys method. This may be recoverable");
100       return false;
101     }
102   }
103 
104   /**
105    * This method handles cases where the
106    * {@link DatabaseMetaData#supportsBatchUpdates} method is missing in the JDBC
107    * driver implementation.
108    */
109   public boolean supportsBatchUpdates(DatabaseMetaData meta) {
110     try {
111       return meta.supportsBatchUpdates();
112     } catch (Throwable e) {
113       addInfo("Missing DatabaseMetaData.supportsBatchUpdates method.");
114       return false;
115     }
116   }
117 }