comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/db/SQL.java @ 473:d0ac790a6c89 dive4elements-move

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 10:57:18 +0200
parents artifact-database/src/main/java/de/intevation/artifactdatabase/db/SQL.java@c40729bfe06d
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
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 statements = loadStatements(clazz, resourcePath, driver);
29 }
30
31 public static final String driverToProperties(String driver) {
32 return driver.replace('.', '-').toLowerCase() + ".properties";
33 }
34
35 /**
36 * Returns key/value pairs of SQL statements for the used database
37 * backend.
38 * The concrete set of SQL statements is determined by the
39 * used JDBC database driver which is configured in conf.xml.
40 * The class name of the driver is transformed by replacing
41 * all '.' with '_' and lower case the resulting string.
42 * The transformed string is used to load a properties file
43 * in '/sql/' which should contain the statements.
44 * Example:<br>
45 * <code>org.postgresql.Driver</code> results in loading of
46 * <code>/sql/org-postgresql-driver.properties</code>.
47 * @return The key/value pairs of SQL statements.
48 */
49 protected Properties loadStatements(
50 Class clazz,
51 String resourcePath,
52 String driver
53 ) {
54 logger.debug("loadStatements");
55
56 Properties properties = new Properties();
57
58 String resDriver = driverToProperties(driver);
59
60 InputStream in = null;
61 try {
62 String res = resourcePath + "/" + resDriver;
63
64 in = clazz.getResourceAsStream(res);
65
66 if (in == null) {
67 logger.warn("No SQL file for driver '" + driver + "' found.");
68 resDriver = driverToProperties(DBConnection.DEFAULT_DRIVER);
69 res = resourcePath + "/" + resDriver;
70
71 in = clazz.getResourceAsStream(res);
72 if (in == null) {
73 logger.error("No SQL file for driver '" +
74 DBConnection.DEFAULT_DRIVER + "' found.");
75 }
76 }
77 else {
78 if (logger.isDebugEnabled()) {
79 logger.debug("found resource: " + res);
80 }
81 }
82
83 if (in != null) {
84 properties.load(in);
85 }
86 }
87 catch (IOException ioe) {
88 logger.error(ioe);
89 }
90
91 return properties;
92 }
93
94 public String get(String key) {
95 boolean debug = logger.isDebugEnabled();
96 if (debug) {
97 logger.debug("looking for SQL " + key);
98 logger.debug("statements != null: " + (statements != null));
99 }
100
101 String sql = statements.getProperty(key);
102
103 if (sql == null) {
104 logger.error("cannot find SQL for key '" + key + "'");
105 }
106
107 if (debug) {
108 logger.debug("-> '" + sql + "'");
109 }
110
111 return sql;
112 }
113 }
114 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org