1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.log4j.db.dialect;
18
19 import org.apache.log4j.db.ConnectionSource;
20 import org.apache.log4j.spi.ComponentBase;
21
22 import java.sql.DatabaseMetaData;
23 import java.sql.SQLException;
24
25
26 /***
27 *
28 * @author Ceki Gulcu
29 *
30 */
31 public class Util extends ComponentBase {
32 private static final String POSTGRES_PART = "postgresql";
33 private static final String MYSQL_PART = "mysql";
34 private static final String ORACLE_PART = "oracle";
35
36 private static final String MSSQL_PART = "microsoft";
37 private static final String HSQL_PART = "hsql";
38
39 public static int discoverSQLDialect(DatabaseMetaData meta) {
40 int dialectCode = 0;
41
42 try {
43
44 String dbName = meta.getDatabaseProductName().toLowerCase();
45
46 if (dbName.indexOf(POSTGRES_PART) != -1) {
47 return ConnectionSource.POSTGRES_DIALECT;
48 } else if (dbName.indexOf(MYSQL_PART) != -1) {
49 return ConnectionSource.MYSQL_DIALECT;
50 } else if (dbName.indexOf(ORACLE_PART) != -1) {
51 return ConnectionSource.ORACLE_DIALECT;
52 } else if (dbName.indexOf(MSSQL_PART) != -1) {
53 return ConnectionSource.MSSQL_DIALECT;
54 } else if (dbName.indexOf(HSQL_PART) != -1) {
55 return ConnectionSource.HSQL_DIALECT;
56 } else {
57 return ConnectionSource.UNKNOWN_DIALECT;
58 }
59 } catch (SQLException sqle) {
60
61 }
62
63 return dialectCode;
64 }
65
66 public static SQLDialect getDialectFromCode(int dialectCode) {
67 SQLDialect sqlDialect = null;
68
69 switch (dialectCode) {
70 case ConnectionSource.POSTGRES_DIALECT:
71 sqlDialect = new PostgreSQLDialect();
72
73 break;
74 case ConnectionSource.MYSQL_DIALECT:
75 sqlDialect = new MySQLDialect();
76
77 break;
78 case ConnectionSource.ORACLE_DIALECT:
79 sqlDialect = new OracleDialect();
80
81 break;
82 case ConnectionSource.MSSQL_DIALECT:
83 sqlDialect = new MsSQLDialect();
84
85 break;
86 case ConnectionSource.HSQL_DIALECT:
87 sqlDialect = new HSQLDBDialect();
88
89 break;
90 }
91 return sqlDialect;
92 }
93
94 /***
95 * This method handles cases where the
96 * {@link DatabaseMetaData#supportsGetGeneratedKeys} method is missing in the
97 * JDBC driver implementation.
98 */
99 public boolean supportsGetGeneratedKeys(DatabaseMetaData meta) {
100 try {
101
102
103
104 return ((Boolean) DatabaseMetaData.class.getMethod("supportsGetGeneratedKeys", null).invoke(meta, null)).booleanValue();
105 } catch(Throwable e) {
106 getLogger().info("Could not call supportsGetGeneratedKeys method. This may be recoverable");
107 return false;
108 }
109 }
110
111 /***
112 * This method handles cases where the
113 * {@link DatabaseMetaData#supportsBatchUpdates} method is missing in the
114 * JDBC driver implementation.
115 */
116 public boolean supportsBatchUpdates(DatabaseMetaData meta) {
117 try {
118 return meta.supportsBatchUpdates();
119 } catch(Throwable e) {
120 getLogger().info("Missing DatabaseMetaData.supportsBatchUpdates method.");
121 return false;
122 }
123 }
124 }