View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.db.dialect;
19  
20  import org.apache.log4j.db.ConnectionSource;
21  import org.apache.log4j.spi.ComponentBase;
22  
23  import java.sql.DatabaseMetaData;
24  import java.sql.SQLException;
25  
26  
27  /**
28   * 
29   * @author Ceki Gulcu
30   *
31   */
32  public class Util extends ComponentBase {
33    private static final String POSTGRES_PART = "postgresql";
34    private static final String MYSQL_PART = "mysql";
35    private static final String ORACLE_PART = "oracle";
36    //private static final String MSSQL_PART = "mssqlserver4";
37    private static final String MSSQL_PART = "microsoft";
38    private static final String HSQL_PART = "hsql";
39    
40    public static int discoverSQLDialect(DatabaseMetaData meta) {
41      int dialectCode = 0;
42  
43      try {
44  
45        String dbName = meta.getDatabaseProductName().toLowerCase();
46  
47        if (dbName.indexOf(POSTGRES_PART) != -1) {
48          return ConnectionSource.POSTGRES_DIALECT;
49        } else if (dbName.indexOf(MYSQL_PART) != -1) {
50          return ConnectionSource.MYSQL_DIALECT;
51        } else if (dbName.indexOf(ORACLE_PART) != -1) {
52          return ConnectionSource.ORACLE_DIALECT;
53        } else if (dbName.indexOf(MSSQL_PART) != -1) {
54          return ConnectionSource.MSSQL_DIALECT;
55        } else if (dbName.indexOf(HSQL_PART) != -1) {
56          return ConnectionSource.HSQL_DIALECT;
57        } else {
58          return ConnectionSource.UNKNOWN_DIALECT;
59        }
60      } catch (SQLException sqle) {
61        // we can't do much here
62      }
63  
64      return dialectCode;
65    }
66  
67    public static SQLDialect getDialectFromCode(int dialectCode) {
68      SQLDialect sqlDialect = null;
69  
70      switch (dialectCode) {
71      case ConnectionSource.POSTGRES_DIALECT:
72        sqlDialect = new PostgreSQLDialect();
73  
74        break;
75      case ConnectionSource.MYSQL_DIALECT:
76        sqlDialect = new MySQLDialect();
77  
78        break;
79      case ConnectionSource.ORACLE_DIALECT:
80        sqlDialect = new OracleDialect();
81  
82        break;
83      case ConnectionSource.MSSQL_DIALECT:
84        sqlDialect = new MsSQLDialect();
85  
86        break;
87      case ConnectionSource.HSQL_DIALECT:
88        sqlDialect = new HSQLDBDialect();
89  
90        break;
91      }
92      return sqlDialect;
93    }
94    
95    /**
96     * This method handles cases where the 
97     * {@link DatabaseMetaData#supportsGetGeneratedKeys} method is missing in the
98     * JDBC driver implementation.
99     */
100   public boolean supportsGetGeneratedKeys(DatabaseMetaData meta) {
101     try {
102       //
103       //   invoking JDK 1.4 method by reflection
104       //
105       return ((Boolean) DatabaseMetaData.class.getMethod("supportsGetGeneratedKeys", null).invoke(meta, null)).booleanValue();
106     } catch(Throwable e) {
107       getLogger().info("Could not call supportsGetGeneratedKeys method. This may be recoverable");
108       return false;
109     }
110   }
111   
112 /** 
113   * This method handles cases where the 
114   * {@link DatabaseMetaData#supportsBatchUpdates} method is missing in the
115   * JDBC driver implementation.
116   */
117   public boolean supportsBatchUpdates(DatabaseMetaData meta) {
118     try {
119       return meta.supportsBatchUpdates();
120     } catch(Throwable e) {
121       getLogger().info("Missing DatabaseMetaData.supportsBatchUpdates method.");
122       return false;
123     }
124   }
125 }