diff --git a/src/java/com/samskivert/jdbc/BaseLiaison.java b/src/java/com/samskivert/jdbc/BaseLiaison.java index c7ed3c9a..7570bad0 100644 --- a/src/java/com/samskivert/jdbc/BaseLiaison.java +++ b/src/java/com/samskivert/jdbc/BaseLiaison.java @@ -107,13 +107,7 @@ public abstract class BaseLiaison implements DatabaseLiaison } update.append(")"); - Statement stmt = conn.createStatement(); - try { - stmt.executeUpdate(update.toString()); - } finally { - JDBCUtil.close(stmt); - } - + executeQuery(conn, update.toString()); Log.info("Database index '" + ixName + "' added to table '" + table + "'"); return true; } @@ -132,26 +126,15 @@ public abstract class BaseLiaison implements DatabaseLiaison fields.append(")"); String update = "ALTER TABLE " + tableSQL(table) + " ADD PRIMARY KEY " + fields.toString(); - Statement stmt = conn.createStatement(); - try { - stmt.executeUpdate(update.toString()); - } finally { - JDBCUtil.close(stmt); - } - + executeQuery(conn, update.toString()); Log.info("Primary key " + fields + " added to table '" + table + "'"); } // from DatabaseLiaison public void dropIndex (Connection conn, String table, String index) throws SQLException { - Statement stmt = conn.createStatement(); - try { - stmt.executeUpdate( - "ALTER TABLE " + tableSQL(table) + " DROP CONSTRAINT " + columnSQL(index)); - } finally { - JDBCUtil.close(stmt); - } + executeQuery(conn, "ALTER TABLE " + tableSQL(table) + + " DROP CONSTRAINT " + columnSQL(index)); } // from DatabaseLiaison @@ -170,14 +153,8 @@ public abstract class BaseLiaison implements DatabaseLiaison return false; } - Statement stmt = conn.createStatement(); - try { - stmt.executeUpdate("ALTER TABLE " + tableSQL(table) + " ADD COLUMN " + - columnSQL(column) + " " + definition); - } finally { - JDBCUtil.close(stmt); - } - + executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " ADD COLUMN " + + columnSQL(column) + " " + definition); Log.info("Database column '" + column + "' added to table '" + table + "'."); return true; } @@ -186,33 +163,21 @@ public abstract class BaseLiaison implements DatabaseLiaison public boolean changeColumn (Connection conn, String table, String column, String definition) throws SQLException { - Statement stmt = conn.createStatement(); - try { - stmt.executeUpdate("ALTER TABLE " + tableSQL(table) + " CHANGE " + - columnSQL(column) + " " + column + " " + definition); - } finally { - JDBCUtil.close(stmt); - } - - Log.info("Database column '" + column + "' of table '" + table + - "' modified to have definition '" + definition + "'."); + executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " CHANGE " + + columnSQL(column) + " " + column + " " + definition); + Log.info("Database column '" + column + "' of table '" + table + "' modified to have " + + "definition '" + definition + "'."); return true; } // from DatabaseLiaison - public boolean renameColumn (Connection conn, String table, String from, String to) + public boolean renameColumn (Connection conn, String table, String from, String to, + String newColumnDef) throws SQLException { - Statement stmt = conn.createStatement(); - try { - String query = "ALTER TABLE " + tableSQL(table) + " RENAME COLUMN " + - columnSQL(from) + " TO " + columnSQL(to); - if (stmt.executeUpdate(query) == 1) { - Log.info("Renamed column '" + from + "' on table '" + table + "' to '" + to + "'"); - } - } finally { - JDBCUtil.close(stmt); - } + executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " RENAME COLUMN " + + columnSQL(from) + " TO " + columnSQL(to)); + Log.info("Renamed column '" + from + "' on table '" + table + "' to '" + to + "'"); return true; } @@ -222,16 +187,8 @@ public abstract class BaseLiaison implements DatabaseLiaison if (!tableContainsColumn(conn, table, column)) { return false; } - - Statement stmt = conn.createStatement(); - try { - String query = "ALTER TABLE " + tableSQL(table) + " DROP COLUMN " + columnSQL(column); - if (stmt.executeUpdate(query) == 1) { - Log.info("Database index '" + column + "' removed from table '" + table + "'."); - } - } finally { - JDBCUtil.close(stmt); - } + executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " DROP COLUMN " + columnSQL(column)); + Log.info("Database column '" + column + "' removed from table '" + table + "'."); return true; } @@ -283,14 +240,19 @@ public abstract class BaseLiaison implements DatabaseLiaison builder.append(")"); - Statement stmt = conn.createStatement(); - try { - stmt.executeUpdate(builder.toString()); - } finally { - JDBCUtil.close(stmt); - } - + executeQuery(conn, builder.toString()); Log.info("Database table '" + table + "' created."); return true; } + + protected int executeQuery (Connection conn, String query) + throws SQLException + { + Statement stmt = conn.createStatement(); + try { + return stmt.executeUpdate(query); + } finally { + JDBCUtil.close(stmt); + } + } } diff --git a/src/java/com/samskivert/jdbc/DatabaseLiaison.java b/src/java/com/samskivert/jdbc/DatabaseLiaison.java index 2c4e84cb..1e232859 100644 --- a/src/java/com/samskivert/jdbc/DatabaseLiaison.java +++ b/src/java/com/samskivert/jdbc/DatabaseLiaison.java @@ -128,8 +128,12 @@ public interface DatabaseLiaison /** * Alter the name, but not the definition, of a given column on a given table. Returns true or * false if the database did or did not report a schema modification. + * + * @param newColumnDef the full definition of the new column, including its new name (MySQL + * requires this for a column rename). */ - public boolean renameColumn (Connection conn, String table, String oldColumn, String newColumn) + public boolean renameColumn ( + Connection conn, String table, String oldColumn, String newColumn, String newColumnDef) throws SQLException; /** diff --git a/src/java/com/samskivert/jdbc/MySQLLiaison.java b/src/java/com/samskivert/jdbc/MySQLLiaison.java index e8bada1d..c686373a 100644 --- a/src/java/com/samskivert/jdbc/MySQLLiaison.java +++ b/src/java/com/samskivert/jdbc/MySQLLiaison.java @@ -99,14 +99,7 @@ public class MySQLLiaison extends BaseLiaison } update.append(")"); - PreparedStatement stmt = null; - try { - stmt = conn.prepareStatement(update.toString()); - stmt.executeUpdate(); - } finally { - JDBCUtil.close(stmt); - } - + executeQuery(conn, update.toString()); Log.info("Database index '" + ixName + "' added to table '" + table + "'"); return true; } @@ -115,25 +108,29 @@ public class MySQLLiaison extends BaseLiaison public void dropIndex (Connection conn, String table, String index) throws SQLException { - Statement stmt = conn.createStatement(); - try { - stmt.executeUpdate( - "ALTER TABLE " + tableSQL(table) + " DROP INDEX " + columnSQL(index)); - } finally { - JDBCUtil.close(stmt); - } + executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " DROP INDEX " + columnSQL(index)); } @Override // from BaseLiaison public void dropPrimaryKey (Connection conn, String table, String pkName) throws SQLException { - Statement stmt = conn.createStatement(); - try { - stmt.executeUpdate("ALTER TABLE " + tableSQL(table) + " DROP PRIMARY KEY"); - } finally { - JDBCUtil.close(stmt); + executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " DROP PRIMARY KEY"); + } + + @Override // from BaseLiaison + public boolean renameColumn (Connection conn, String table, String oldColumnName, + String newColumnName, String newColumnDef) + throws SQLException + { + if (!tableContainsColumn(conn, table, oldColumnName)) { + return false; } + executeQuery(conn, "ALTER TABLE " + table + " CHANGE " + oldColumnName + " " + + newColumnName + " " + newColumnDef); + Log.info("Renamed column '" + oldColumnName + "' on table '" + table + "' to '" + + newColumnName + "'"); + return true; } // from DatabaseLiaison diff --git a/src/java/com/samskivert/jdbc/PostgreSQLLiaison.java b/src/java/com/samskivert/jdbc/PostgreSQLLiaison.java index b6de347c..585ae34b 100644 --- a/src/java/com/samskivert/jdbc/PostgreSQLLiaison.java +++ b/src/java/com/samskivert/jdbc/PostgreSQLLiaison.java @@ -77,13 +77,8 @@ public class PostgreSQLLiaison extends BaseLiaison Connection conn, String table, String column, int first, int step) throws SQLException { - Statement stmt = conn.createStatement(); - try { - stmt.executeUpdate("alter sequence \"" + table + "_" + column + "_seq\" " + - " restart with " + first + " increment " + step); - } finally { - JDBCUtil.close(stmt); - } + executeQuery(conn, "alter sequence \"" + table + "_" + column + "_seq\" " + + " restart with " + first + " increment " + step); } // from DatabaseLiaison diff --git a/src/java/com/samskivert/jdbc/depot/EntityMigration.java b/src/java/com/samskivert/jdbc/depot/EntityMigration.java index 9bea2bd9..e9765b7e 100644 --- a/src/java/com/samskivert/jdbc/depot/EntityMigration.java +++ b/src/java/com/samskivert/jdbc/depot/EntityMigration.java @@ -92,14 +92,20 @@ public abstract class EntityMigration extends Modifier log.info("Renaming '" + _oldColumnName + "' to '" + _newColumnName + "' in: " + _tableName); - return liaison.renameColumn(conn, _tableName, _oldColumnName, _newColumnName) ? 1 : 0; + return liaison.renameColumn( + conn, _tableName, _oldColumnName, _newColumnName, _newColumnDef) ? 1 : 0; } public boolean runBeforeDefault () { return true; } - protected String _oldColumnName, _newColumnName; + protected void init (String tableName, Map marshallers) { + super.init(tableName, marshallers); + _newColumnDef = marshallers.get(_newColumnName).getColumnDefinition(); + } + + protected String _oldColumnName, _newColumnName, _newColumnDef; } /**