// JDBC Utils static void execute(String sql) throws SQLException { Connection connection = null; try { connection = getNewConnection(); connection.createStatement().execute(sql); } catch (SQLException e) { throw e; } finally { closeConnection(connection); } }
public static synchronized void checkEvolutionsState() { if (DB.datasource != null && evolutionsDirectory.exists()) { List<Evolution> evolutionScript = getEvolutionScript(); Connection connection = null; try { connection = getNewConnection(); ResultSet rs = connection .createStatement() .executeQuery( "select id, hash, apply_script, revert_script, state, last_problem from play_evolutions where state like 'applying_%'"); if (rs.next()) { int revision = rs.getInt("id"); String state = rs.getString("state"); String hash = rs.getString("hash").substring(0, 7); String script = ""; if (state.equals("applying_up")) { script = rs.getString("apply_script"); } else { script = rs.getString("revert_script"); } script = "# --- Rev:" + revision + "," + (state.equals("applying_up") ? "Ups" : "Downs") + " - " + hash + "\n\n" + script; String error = rs.getString("last_problem"); throw new InconsistentDatabase(script, error, revision); } } catch (SQLException e) { throw new UnexpectedException(e); } finally { closeConnection(connection); } if (!evolutionScript.isEmpty()) { throw new InvalidDatabaseRevision(toHumanReadableScript(evolutionScript)); } } }
public static synchronized Stack<Evolution> listDatabaseEvolutions() { Stack<Evolution> evolutions = new Stack<Evolution>(); evolutions.add(new Evolution(0, "", "", false)); Connection connection = null; try { connection = getNewConnection(); String tableName = "play_evolutions"; boolean tableExists = true; ResultSet rs = connection.getMetaData().getTables(null, null, tableName, null); if (!rs.next()) { // Table in lowercase does not exist // oracle gives table names in upper case tableName = tableName.toUpperCase(); Logger.trace("Checking " + tableName); rs.close(); rs = connection.getMetaData().getTables(null, null, tableName, null); // Does it exist? if (!rs.next()) { // did not find it in uppercase either tableExists = false; } } // Do we have a if (tableExists) { ResultSet databaseEvolutions = connection .createStatement() .executeQuery("select id, hash, apply_script, revert_script from play_evolutions"); while (databaseEvolutions.next()) { Evolution evolution = new Evolution( databaseEvolutions.getInt(1), databaseEvolutions.getString(3), databaseEvolutions.getString(4), false); evolutions.add(evolution); } } else { // If you are having problems with the default datatype text (clob for Oracle), you can // specify your own datatype using the 'evolution.PLAY_EVOLUTIONS.textType'-property String textDataType = Play.configuration.getProperty("evolution.PLAY_EVOLUTIONS.textType"); if (textDataType == null) { if (isOracleDialectInUse()) { textDataType = "clob"; } else { textDataType = "text"; } } execute( "create table play_evolutions (id int not null primary key, hash varchar(255) not null, applied_at timestamp not null, apply_script " + textDataType + ", revert_script " + textDataType + ", state varchar(255), last_problem " + textDataType + ")"); } } catch (SQLException e) { Logger.error(e, "SQL error while checking play evolutions"); } finally { closeConnection(connection); } Collections.sort(evolutions); return evolutions; }
public static synchronized boolean applyScript(boolean runScript) { try { Connection connection = getNewConnection(); int applying = -1; try { for (Evolution evolution : getEvolutionScript()) { applying = evolution.revision; // Insert into logs if (evolution.applyUp) { PreparedStatement ps = connection.prepareStatement( "insert into play_evolutions values(?, ?, ?, ?, ?, ?, ?)"); ps.setInt(1, evolution.revision); ps.setString(2, evolution.hash); ps.setDate(3, new Date(System.currentTimeMillis())); ps.setString(4, evolution.sql_up); ps.setString(5, evolution.sql_down); ps.setString(6, "applying_up"); ps.setString(7, ""); ps.execute(); } else { execute( "update play_evolutions set state = 'applying_down' where id = " + evolution.revision); } // Execute script if (runScript) { for (CharSequence sql : new SQLSplitter((evolution.applyUp ? evolution.sql_up : evolution.sql_down))) { final String s = sql.toString().trim(); if (StringUtils.isEmpty(s)) { continue; } connection.createStatement().execute(s); } } // Insert into logs if (evolution.applyUp) { execute( "update play_evolutions set state = 'applied' where id = " + evolution.revision); } else { execute("delete from play_evolutions where id = " + evolution.revision); } } return true; } catch (Exception e) { String message = e.getMessage(); if (e instanceof SQLException) { SQLException ex = (SQLException) e; message += " [ERROR:" + ex.getErrorCode() + ", SQLSTATE:" + ex.getSQLState() + "]"; } PreparedStatement ps = connection.prepareStatement("update play_evolutions set last_problem = ? where id = ?"); ps.setString(1, message); ps.setInt(2, applying); ps.execute(); closeConnection(connection); Logger.error(e, "Can't apply evolution"); return false; } } catch (Exception e) { throw new UnexpectedException(e); } }