public List<Map<String, Object>> selectAll(String sql, Object... args) throws SQLException { PreparedStatement ps = connection.prepareStatement(sql); try { setParameters(ps, args); ResultSet rs = ps.executeQuery(); return getResults(rs); } finally { try { ps.close(); } catch (SQLException e) { //ignore } } }
public Map<String, Object> selectOne(String sql, Object... args) throws SQLException { List<Map<String, Object>> results = selectAll(sql, args); if (results.size() != 1) { throw new SQLException("Statement returned " + results.size() + " results where exactly one (1) was expected."); } return results.get(0); }
/** * Executes a DELETE statement. * * @param sql The SQL * @param args The arguments to be set on the statement. * @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched. * @throws SQLException If statement preparation or execution fails */ public int delete(String sql, Object... args) throws SQLException { return update(sql, args); }
protected boolean changelogExists() { SqlRunner runner = getSqlRunner(); try { runner.selectAll("select ID, APPLIED_AT, DESCRIPTION from " + changelogTable()); return true; } catch (SQLException e) { return false; } finally { runner.closeConnection(); } }
public int update(String sql, Object... args) throws SQLException { PreparedStatement ps = connection.prepareStatement(sql); try { setParameters(ps, args); return ps.executeUpdate(); } finally { try { ps.close(); } catch (SQLException e) { //ignore } } }
protected void deleteChange(Change change) { SqlRunner runner = getSqlRunner(); try { runner.delete("delete from " + changelogTable() + " where id = ?", change.getId()); } catch (SQLException e) { throw new MigrationException("Error querying last applied migration. Cause: " + e, e); } finally { runner.closeConnection(); } }
protected void insertChangelog(Change change) { SqlRunner runner = getSqlRunner(); change.setAppliedTimestamp(generateAppliedTimeStampAsString()); try { runner.insert("insert into " + changelogTable() + " (ID, APPLIED_AT, DESCRIPTION) values (?,?,?)", change.getId(), change.getAppliedTimestamp(), change.getDescription()); } catch (SQLException e) { throw new MigrationException("Error querying last applied migration. Cause: " + e, e); } finally { runner.closeConnection(); } }
protected SqlRunner getSqlRunner() { try { lazyInitializeDrivers(); Properties props = environmentProperties(); String driver = props.getProperty("driver"); String url = props.getProperty("url"); String username = props.getProperty("username"); String password = props.getProperty("password"); UnpooledDataSource dataSource = new UnpooledDataSource(driverClassLoader, driver, url, username, password); dataSource.setAutoCommit(true); return new SqlRunner(dataSource.getConnection()); } catch (SQLException e) { throw new MigrationException("Could not create SqlRunner. Cause: " + e, e); } }
protected List<Change> getChangelog() { SqlRunner runner = getSqlRunner(); try { List<Map<String, Object>> changelog = runner.selectAll("select ID, APPLIED_AT, DESCRIPTION from " + changelogTable() + " order by id"); List<Change> changes = new ArrayList<Change>(); for (Map<String, Object> change : changelog) { String id = change.get("ID") == null ? null : change.get("ID").toString(); String appliedAt = change.get("APPLIED_AT") == null ? null : change.get("APPLIED_AT").toString(); String description = change.get("DESCRIPTION") == null ? null : change.get("DESCRIPTION").toString(); changes.add(new Change(new BigDecimal(id), appliedAt, description)); } return changes; } catch (SQLException e) { throw new MigrationException("Error querying last applied migration. Cause: " + e, e); } finally { runner.closeConnection(); } }
/** * Executes an UPDATE statement. * * @param sql The SQL * @param args The arguments to be set on the statement. * @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched. * @throws SQLException If statement preparation or execution fails */ public int update(String sql, Object... args) throws SQLException { PreparedStatement ps = connection.prepareStatement(sql); try { setParameters(ps, args); return ps.executeUpdate(); } finally { try { ps.close(); } catch (SQLException e) { //ignore } } }
/** * Executes a SELECT statement that returns multiple rows. * * @param sql The SQL * @param args The arguments to be set on the statement. * @return The list of rows expected. * @throws SQLException If statement preparation or execution fails */ public List<Map<String, Object>> selectAll(String sql, Object... args) throws SQLException { PreparedStatement ps = connection.prepareStatement(sql); try { setParameters(ps, args); ResultSet rs = ps.executeQuery(); return getResults(rs); } finally { try { ps.close(); } catch (SQLException e) { //ignore } } }
/** * Executes a SELECT statement that returns one row. * * @param sql The SQL * @param args The arguments to be set on the statement. * @return The row expected. * @throws SQLException If less or more than one row is returned */ public Map<String, Object> selectOne(String sql, Object... args) throws SQLException { List<Map<String, Object>> results = selectAll(sql, args); if (results.size() != 1) { throw new SQLException("Statement returned " + results.size() + " results where exactly one (1) was expected."); } return results.get(0); }
public int delete(String sql, Object... args) throws SQLException { return update(sql, args); }
/** * Executes an UPDATE statement. * * @param sql The SQL * @param args The arguments to be set on the statement. * @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched. * @throws SQLException If statement prepration or execution fails */ public int update(String sql, Object... args) throws SQLException { PreparedStatement ps = connection.prepareStatement(sql); try { setParameters(ps, args); return ps.executeUpdate(); } finally { try { ps.close(); } catch (SQLException e) { //ignore } } }
setParameters(ps, args); ps.executeUpdate(); if (useGeneratedKeySupport) { List<Map<String, Object>> keys = getResults(ps.getGeneratedKeys()); if (keys.size() == 1) { Map<String, Object> key = keys.get(0);
/** * Executes a SELECT statement that returns one row. * * @param sql The SQL * @param args The arguments to be set on the statement. * @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched. * @throws SQLException If more than one row is returned */ public Map<String, Object> selectOne(String sql, Object... args) throws SQLException { List<Map<String, Object>> results = selectAll(sql, args); if (results.size() != 1) { throw new SQLException("Statement returned " + results.size() + " results where exactly one (1) was expected."); } return results.get(0); }
/** * Executes a DELETE statement. * * @param sql The SQL * @param args The arguments to be set on the statement. * @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched. * @throws SQLException If statement prepration or execution fails */ public int delete(String sql, Object... args) throws SQLException { return update(sql, args); }
setParameters(ps, args); ps.executeUpdate(); if (useGeneratedKeySupport) { List<Map<String, Object>> keys = getResults(ps.getGeneratedKeys()); if (keys.size() == 1) { Map<String, Object> key = keys.get(0);
/** * Executes a SELECT statement that returns multiple rows. * * @param sql The SQL * @param args The arguments to be set on the statement. * @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched. * @throws SQLException If statement prepration or execution fails */ public List<Map<String, Object>> selectAll(String sql, Object... args) throws SQLException { PreparedStatement ps = connection.prepareStatement(sql); try { setParameters(ps, args); ResultSet rs = ps.executeQuery(); return getResults(rs); } finally { try { ps.close(); } catch (SQLException e) { //ignore } } }
setParameters(ps, args); ps.executeUpdate(); if (useGeneratedKeySupport) { List<Map<String, Object>> keys = getResults(ps.getGeneratedKeys()); if (keys.size() == 1) { Map<String, Object> key = keys.get(0);