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   * @author Ceki Gulcu
29   */
30  public class Util extends ComponentBase {
31      private static final String POSTGRES_PART = "postgresql";
32      private static final String MYSQL_PART = "mysql";
33      private static final String ORACLE_PART = "oracle";
34      //private static final String MSSQL_PART = "mssqlserver4";
35      private static final String MSSQL_PART = "microsoft";
36      private static final String HSQL_PART = "hsql";
37  
38      public static int discoverSQLDialect(DatabaseMetaData meta) {
39          int dialectCode = 0;
40  
41          try {
42  
43              String dbName = meta.getDatabaseProductName().toLowerCase();
44  
45              if (dbName.contains(POSTGRES_PART)) {
46                  return ConnectionSource.POSTGRES_DIALECT;
47              } else if (dbName.contains(MYSQL_PART)) {
48                  return ConnectionSource.MYSQL_DIALECT;
49              } else if (dbName.contains(ORACLE_PART)) {
50                  return ConnectionSource.ORACLE_DIALECT;
51              } else if (dbName.contains(MSSQL_PART)) {
52                  return ConnectionSource.MSSQL_DIALECT;
53              } else if (dbName.contains(HSQL_PART)) {
54                  return ConnectionSource.HSQL_DIALECT;
55              } else {
56                  return ConnectionSource.UNKNOWN_DIALECT;
57              }
58          } catch (SQLException sqle) {
59              // we can't do much here
60          }
61  
62          return dialectCode;
63      }
64  
65      public static SQLDialect getDialectFromCode(int dialectCode) {
66          SQLDialect sqlDialect = null;
67  
68          switch (dialectCode) {
69              case ConnectionSource.POSTGRES_DIALECT:
70                  sqlDialect = new PostgreSQLDialect();
71  
72                  break;
73              case ConnectionSource.MYSQL_DIALECT:
74                  sqlDialect = new MySQLDialect();
75  
76                  break;
77              case ConnectionSource.ORACLE_DIALECT:
78                  sqlDialect = new OracleDialect();
79  
80                  break;
81              case ConnectionSource.MSSQL_DIALECT:
82                  sqlDialect = new MsSQLDialect();
83  
84                  break;
85              case ConnectionSource.HSQL_DIALECT:
86                  sqlDialect = new HSQLDBDialect();
87  
88                  break;
89          }
90          return sqlDialect;
91      }
92  
93      /**
94       * This method handles cases where the
95       * {@link DatabaseMetaData#supportsGetGeneratedKeys} method is missing in the
96       * JDBC driver implementation.
97       */
98      public boolean supportsGetGeneratedKeys(DatabaseMetaData meta) {
99          try {
100             return meta.supportsGetGeneratedKeys();
101         } catch (Throwable e) {
102             getLogger().info("Could not call supportsGetGeneratedKeys method. This may be recoverable");
103             return false;
104         }
105     }
106 
107     /**
108      * This method handles cases where the
109      * {@link DatabaseMetaData#supportsBatchUpdates} method is missing in the
110      * JDBC driver implementation.
111      */
112     public boolean supportsBatchUpdates(DatabaseMetaData meta) {
113         try {
114             return meta.supportsBatchUpdates();
115         } catch (Throwable e) {
116             getLogger().info("Missing DatabaseMetaData.supportsBatchUpdates method.");
117             return false;
118         }
119     }
120 }