sascha@14: package de.intevation.artifactdatabase; sascha@14: sascha@14: import java.io.IOException; sascha@14: import java.io.InputStream; sascha@14: sascha@93: import java.util.Properties; sascha@93: sascha@17: import org.apache.log4j.Logger; sascha@17: sascha@14: /** sascha@87: * Singleton to provide SQL statement strings as key/value pairs. sascha@87: * This mechanism is used to encapsulate database specific SQL sascha@87: * dialects. sascha@87: * ingo@80: * @author Sascha L. Teichmann sascha@14: */ sascha@14: public final class SQL sascha@14: { sascha@17: private static Logger logger = Logger.getLogger(SQL.class); sascha@17: sascha@14: private SQL() { sascha@14: } sascha@14: sascha@14: private static Properties statements; sascha@14: sascha@87: /** sascha@87: * Returns key/value pairs of SQL statements for the used database sascha@87: * backend. sascha@87: * The concrete set of SQL statements is determined by the sascha@87: * used JDBC database driver which is configured in conf.xml. sascha@87: * The class name of the driver is transformed by replacing sascha@87: * all '.' with '_' and lower case the resulting string. sascha@87: * The transformed string is used to load a properties file sascha@87: * in '/sql/' which should contain the statements. sascha@87: * Example:
sascha@87: * org.postgresql.Driver results in loading of sascha@87: * /sql/org-postgresql-driver.properties. sascha@87: * @return The key/value pairs of SQL statements. sascha@87: */ sascha@14: public static final synchronized Properties getStatements() { sascha@14: if (statements == null) { sascha@14: statements = loadStatements(); sascha@14: } sascha@14: return statements; sascha@14: } sascha@14: sascha@14: private static final Properties loadStatements() { sascha@14: String driver = Config.getStringXPath( sascha@14: DBConnection.DB_DRIVER, DBConnection.DEFAULT_DRIVER); sascha@14: sascha@14: Properties properties = new Properties(); sascha@14: sascha@14: InputStream in = null; sascha@14: try { sascha@47: String res = "/sql/" + driver.replace('.', '-').toLowerCase() sascha@14: + ".properties"; sascha@14: in = SQL.class.getResourceAsStream(res); sascha@14: sascha@14: if (in == null) { sascha@17: logger.warn("No SQL file for driver '" + driver + "' found."); sascha@14: res = "/sql/" + DBConnection.DEFAULT_DRIVER.replace('.', '-').toLowerCase() sascha@14: + ".properties"; sascha@14: if ((in = SQL.class.getResourceAsStream(res)) == null) { sascha@17: logger.error("No SQL file found"); sascha@14: } sascha@14: } sascha@14: sascha@14: properties.load(in); sascha@14: } sascha@14: catch (IOException ioe) { sascha@17: logger.error(ioe.getLocalizedMessage(), ioe); sascha@14: } sascha@14: finally { sascha@14: if (in != null) { sascha@14: try { in.close(); } catch (IOException ioe) {} sascha@14: } sascha@14: } sascha@14: sascha@14: return properties; sascha@14: } sascha@14: sascha@87: /** sascha@87: * Returns a particular SQL statement for a given key. sascha@87: * @param key The key of the statement. sascha@87: * @return The corresponing SQL statement or null if none sascha@87: * is found. sascha@87: */ sascha@14: public static final String get(String key) { sascha@14: return getStatements().getProperty(key); sascha@14: } sascha@14: } sascha@87: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :