changeset 4068:21e49e0a2307

Add infrastructure to load SQL statements for databases. flys-aft/trunk@3389 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 12 Dec 2011 16:57:58 +0000
parents d5aed044ca0d
children a4e79e8e0aa0
files flys-aft/pom.xml flys-aft/src/main/java/de/intevation/db/Statements.java flys-aft/src/main/resources/sql/aft-common.properties flys-aft/src/main/resources/sql/flys-common.properties
diffstat 4 files changed, 108 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/flys-aft/pom.xml	Sun Dec 11 16:13:50 2011 +0000
+++ b/flys-aft/pom.xml	Mon Dec 12 16:57:58 2011 +0000
@@ -35,5 +35,11 @@
       </plugin>
     </plugins>
   </build>
-  <dependencies/>
+  <dependencies>
+    <dependency>
+      <groupId>log4j</groupId>
+      <artifactId>log4j</artifactId>
+      <version>1.2.14</version>
+    </dependency>
+  </dependencies>
 </project>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-aft/src/main/java/de/intevation/db/Statements.java	Mon Dec 12 16:57:58 2011 +0000
@@ -0,0 +1,98 @@
+package de.intevation.db;
+
+import java.util.Properties;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.log4j.Logger;
+
+public class Statements
+{
+    private static Logger log = Logger.getLogger(Statements.class);
+
+    public static final String RESOURCE_PATH = "/sql/";
+    public static final String COMMON_PROPERTIES = "-common.properties";
+
+    protected String type;
+    protected String driver;
+
+    protected Properties properties;
+
+    public Statements(String type, String driver) {
+        this.type   = type;
+        this.driver = driver;
+    }
+
+    protected String driverToProperties() {
+        return
+            type + "-" + 
+            driver.replace('.', '-').toLowerCase() + ".properties";
+    }
+
+    protected Properties loadCommon() {
+        Properties common = new Properties();
+
+        String path = RESOURCE_PATH + type + COMMON_PROPERTIES;
+
+        InputStream in = Statements.class.getResourceAsStream(path);
+
+        if (in != null) {
+            try {
+                common.load(in);
+            }
+            catch (IOException ioe) {
+                log.error("cannot load defaults: " + path, ioe);
+            }
+            finally {
+                try {
+                    in.close();
+                }
+                catch (IOException ioe) {
+                }
+            }
+        }
+        else {
+            log.warn("cannot find: " + path);
+        }
+
+        return common;
+    }
+
+    protected Properties getProperties() {
+
+        if (properties != null) {
+            return properties;
+        }
+
+        Properties common = loadCommon();
+
+        properties = new Properties(common);
+
+        String path = RESOURCE_PATH + driverToProperties();
+
+        InputStream in = Statements.class.getResourceAsStream(path);
+
+        if (in != null) {
+            try {
+                properties.load(in);
+            }
+            catch (IOException ioe) {
+                log.error("cannot load statements: " + path, ioe);
+            }
+            finally {
+                try {
+                    in.close();
+                }
+                catch (IOException ioe) {
+                }
+            }
+        }
+        else {
+            log.warn("cannot find: " + path);
+        }
+
+        return properties;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-aft/src/main/resources/sql/aft-common.properties	Mon Dec 12 16:57:58 2011 +0000
@@ -0,0 +1,2 @@
+select.gewaesser = SELECT GEWAESSER_NR, NAME FROM SL_GEWAESSER
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-aft/src/main/resources/sql/flys-common.properties	Mon Dec 12 16:57:58 2011 +0000
@@ -0,0 +1,1 @@
+select.river.id = SELECT id FROM rivers where lower(name) = lower(:name)

http://dive4elements.wald.intevation.org