# HG changeset patch # User Sascha L. Teichmann # Date 1323709078 0 # Node ID 21e49e0a2307057e41019254a350d2bbaa5c41ae # Parent d5aed044ca0d250b0f41d92e2b399ec48a803a82 Add infrastructure to load SQL statements for databases. flys-aft/trunk@3389 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r d5aed044ca0d -r 21e49e0a2307 flys-aft/pom.xml --- 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 @@ - + + + log4j + log4j + 1.2.14 + + diff -r d5aed044ca0d -r 21e49e0a2307 flys-aft/src/main/java/de/intevation/db/Statements.java --- /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 : diff -r d5aed044ca0d -r 21e49e0a2307 flys-aft/src/main/resources/sql/aft-common.properties --- /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 + diff -r d5aed044ca0d -r 21e49e0a2307 flys-aft/src/main/resources/sql/flys-common.properties --- /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)