comparison artifact-database/src/main/java/de/intevation/artifactdatabase/db/SQL.java @ 305:f33401ea2a6c

Artifact database: Refactorized the usage of dialect independent SQL to be reusable. artifacts/trunk@2412 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 28 Jul 2011 10:19:35 +0000
parents
children d96bcb40dbf9
comparison
equal deleted inserted replaced
304:40b64b4aafce 305:f33401ea2a6c
1 package de.intevation.artifactdatabase.db;
2
3 import java.util.Properties;
4
5 import java.io.IOException;
6 import java.io.InputStream;
7
8 import org.apache.log4j.Logger;
9
10 public class SQL {
11
12 private static Logger logger = Logger.getLogger(SQL.class);
13
14 protected Properties statements;
15
16 public SQL() {
17 }
18
19 public SQL(String driver) {
20 this(SQL.class, driver);
21 }
22
23 public SQL(Class clazz, String driver) {
24 this(clazz, "/sql", driver);
25 }
26
27 public SQL(Class clazz, String resourcePath, String driver) {
28 }
29
30 public static final String driverToProperties(String driver) {
31 return driver.replace('.', '-').toLowerCase() + ".properties";
32 }
33
34 /**
35 * Returns key/value pairs of SQL statements for the used database
36 * backend.
37 * The concrete set of SQL statements is determined by the
38 * used JDBC database driver which is configured in conf.xml.
39 * The class name of the driver is transformed by replacing
40 * all '.' with '_' and lower case the resulting string.
41 * The transformed string is used to load a properties file
42 * in '/sql/' which should contain the statements.
43 * Example:<br>
44 * <code>org.postgresql.Driver</code> results in loading of
45 * <code>/sql/org-postgresql-driver.properties</code>.
46 * @return The key/value pairs of SQL statements.
47 */
48 protected Properties loadStatements(
49 Class clazz,
50 String resourcePath,
51 String driver
52 ) {
53 Properties properties = new Properties();
54
55 String resDriver = driverToProperties(driver);
56
57 InputStream in = null;
58 try {
59 String res = resourcePath + "/" + resDriver;
60
61 in = clazz.getResourceAsStream(res);
62
63 if (in == null) {
64 logger.warn("No SQL file for driver '" + driver + "' found.");
65 resDriver = driverToProperties(DBConnection.DEFAULT_DRIVER);
66 res = resourcePath + "/" + resDriver;
67
68 in = clazz.getResourceAsStream(res);
69 if (in == null) {
70 logger.error("No SQL file for driver '" +
71 DBConnection.DEFAULT_DRIVER + "' found.");
72 }
73 }
74
75 if (in != null) {
76 properties.load(in);
77 }
78 }
79 catch (IOException ioe) {
80 logger.error(ioe);
81 }
82
83 return properties;
84 }
85
86 public String get(String key) {
87 return statements.getProperty(key);
88 }
89 }
90 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org