view artifact-database/src/main/java/de/intevation/artifactdatabase/SQL.java @ 93:e27cf9c84eb8

Unified imports. artifacts/trunk@849 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 28 Mar 2010 15:57:40 +0000
parents 0f48188a6e02
children 5332d956729c
line wrap: on
line source
package de.intevation.artifactdatabase;

import java.io.IOException;
import java.io.InputStream;

import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * Singleton to provide SQL statement strings as key/value pairs.
 * This mechanism is used to encapsulate database specific SQL
 * dialects.
 *
 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 */
public final class SQL
{
    private static Logger logger = Logger.getLogger(SQL.class);

    private SQL() {
    }

    private static Properties statements;

    /**
     * Returns key/value pairs of SQL statements for the used database
     * backend.
     * The concrete set of SQL statements is determined by the
     * used JDBC database driver which is configured in conf.xml.
     * The class name of the driver is transformed by replacing
     * all '.' with '_' and lower case the resulting string.
     * The transformed string is used to load a properties file
     * in '/sql/' which should contain the statements.
     * Example:<br>
     * <code>org.postgresql.Driver</code> results in loading of
     * <code>/sql/org-postgresql-driver.properties</code>.
     * @return The key/value pairs of SQL statements.
     */
    public static final synchronized Properties getStatements() {
        if (statements == null) {
            statements = loadStatements();
        }
        return statements;
    }

    private static final Properties loadStatements() {
        String driver = Config.getStringXPath(
            DBConnection.DB_DRIVER, DBConnection.DEFAULT_DRIVER);

        Properties properties = new Properties();

        InputStream in = null;
        try {
            String res = "/sql/" + driver.replace('.', '-').toLowerCase()
                + ".properties";
            in = SQL.class.getResourceAsStream(res);

            if (in == null) {
                logger.warn("No SQL file for driver '" + driver + "' found.");
                res = "/sql/" + DBConnection.DEFAULT_DRIVER.replace('.', '-').toLowerCase()
                    + ".properties";
                if ((in = SQL.class.getResourceAsStream(res)) == null) {
                    logger.error("No SQL file found");
                }
            }

            properties.load(in);
        }
        catch (IOException ioe) {
            logger.error(ioe.getLocalizedMessage(), ioe);
        }
        finally {
            if (in != null) {
                try { in.close(); } catch (IOException ioe) {}
            }
        }

        return properties;
    }

    /**
     * Returns a particular SQL statement for a given key.
     * @param key The key of the statement.
     * @return The corresponing SQL statement or null if none
     * is found.
     */
    public static final String get(String key) {
        return getStatements().getProperty(key);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org