package com.tyndalehouse.step.dataloader.loaders; import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.io.IOUtils; import com.tyndalehouse.step.dataloader.ClientDbProvider; public abstract class AbstractLoader { final protected Connection c; public AbstractLoader() throws SQLException { c = ClientDbProvider.getConnection(System.getProperty("db.location")); } protected void executeFile(final String filename) throws IOException, SQLException { executeFile(filename, false); } //TODO: there must be a better way, since at the moment it doesn't cope with // semi colons in the sql file comments., nor i guess in default values and so on... protected void executeFile(final String filename, boolean allowErrors) throws IOException, SQLException { String sql = IOUtils.toString(getClass().getResourceAsStream((filename))); runSQLText(sql, allowErrors); } protected void executeSQLText(final String sqlText) throws SQLException { runSQLText(sqlText, false); } protected void runSQLText(final String sqlText, boolean allowErrors) throws SQLException { QueryRunner qr = new QueryRunner(); String[] statements = sqlText.split(";"); for(String s : statements) { if(s.trim().length() != 0) { try { qr.update(c, s.replace("\n", " ")); } catch(SQLException ex) { System.err.println(ex.getMessage()); if(!allowErrors) { throw ex; } } } } } protected void error(String errorMsg) { error(errorMsg, true); } private void error(String errorMsg, boolean exit) { System.out.println("errorMsg"); if(exit) { System.exit(-1); } } }