sascha@305: package de.intevation.artifactdatabase.db; sascha@305: sascha@305: import java.util.Properties; sascha@305: sascha@305: import java.io.IOException; sascha@305: import java.io.InputStream; sascha@305: sascha@305: import org.apache.log4j.Logger; sascha@305: sascha@305: public class SQL { sascha@305: sascha@305: private static Logger logger = Logger.getLogger(SQL.class); sascha@305: sascha@305: protected Properties statements; sascha@305: sascha@305: public SQL() { sascha@305: } sascha@305: sascha@305: public SQL(String driver) { sascha@305: this(SQL.class, driver); sascha@305: } sascha@305: sascha@305: public SQL(Class clazz, String driver) { sascha@305: this(clazz, "/sql", driver); sascha@305: } sascha@305: sascha@305: public SQL(Class clazz, String resourcePath, String driver) { sascha@307: statements = loadStatements(clazz, resourcePath, driver); sascha@305: } sascha@305: sascha@305: public static final String driverToProperties(String driver) { sascha@305: return driver.replace('.', '-').toLowerCase() + ".properties"; sascha@305: } sascha@305: sascha@305: /** sascha@305: * Returns key/value pairs of SQL statements for the used database sascha@305: * backend. sascha@305: * The concrete set of SQL statements is determined by the sascha@305: * used JDBC database driver which is configured in conf.xml. sascha@305: * The class name of the driver is transformed by replacing sascha@305: * all '.' with '_' and lower case the resulting string. sascha@305: * The transformed string is used to load a properties file sascha@305: * in '/sql/' which should contain the statements. sascha@305: * Example:
sascha@305: * org.postgresql.Driver results in loading of sascha@305: * /sql/org-postgresql-driver.properties. sascha@305: * @return The key/value pairs of SQL statements. sascha@305: */ sascha@305: protected Properties loadStatements( sascha@305: Class clazz, sascha@305: String resourcePath, sascha@305: String driver sascha@305: ) { sascha@307: logger.debug("loadStatements"); sascha@307: sascha@305: Properties properties = new Properties(); sascha@305: sascha@305: String resDriver = driverToProperties(driver); sascha@305: sascha@305: InputStream in = null; sascha@305: try { sascha@305: String res = resourcePath + "/" + resDriver; sascha@305: sascha@305: in = clazz.getResourceAsStream(res); sascha@305: sascha@305: if (in == null) { sascha@305: logger.warn("No SQL file for driver '" + driver + "' found."); sascha@305: resDriver = driverToProperties(DBConnection.DEFAULT_DRIVER); sascha@305: res = resourcePath + "/" + resDriver; sascha@305: sascha@305: in = clazz.getResourceAsStream(res); sascha@305: if (in == null) { sascha@305: logger.error("No SQL file for driver '" + sascha@305: DBConnection.DEFAULT_DRIVER + "' found."); sascha@305: } sascha@305: } sascha@307: else { sascha@307: if (logger.isDebugEnabled()) { sascha@307: logger.debug("found resource: " + res); sascha@307: } sascha@307: } sascha@305: sascha@305: if (in != null) { sascha@305: properties.load(in); sascha@305: } sascha@305: } sascha@305: catch (IOException ioe) { sascha@305: logger.error(ioe); sascha@305: } sascha@305: sascha@305: return properties; sascha@305: } sascha@305: sascha@305: public String get(String key) { sascha@307: boolean debug = logger.isDebugEnabled(); sascha@307: if (debug) { sascha@307: logger.debug("looking for SQL " + key); sascha@307: logger.debug("statements != null: " + (statements != null)); sascha@307: } sascha@307: sascha@307: String sql = statements.getProperty(key); sascha@307: sascha@307: if (sql == null) { sascha@307: logger.error("cannot find SQL for key '" + key + "'"); sascha@307: } sascha@307: sascha@307: if (debug) { sascha@307: logger.debug("-> '" + sql + "'"); sascha@307: } sascha@307: sascha@307: return sql; sascha@305: } sascha@305: } sascha@305: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :