1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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
97
98
99
100 public boolean supportsGetGeneratedKeys(DatabaseMetaData meta) {
101 try {
102
103
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
114
115
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 }