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 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
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
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
95
96
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
109
110
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 }