Tidied up SQL execution in liaisons; fixed MySQL column renaming.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@2172 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -107,13 +107,7 @@ public abstract class BaseLiaison implements DatabaseLiaison
|
|||||||
}
|
}
|
||||||
update.append(")");
|
update.append(")");
|
||||||
|
|
||||||
Statement stmt = conn.createStatement();
|
executeQuery(conn, update.toString());
|
||||||
try {
|
|
||||||
stmt.executeUpdate(update.toString());
|
|
||||||
} finally {
|
|
||||||
JDBCUtil.close(stmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.info("Database index '" + ixName + "' added to table '" + table + "'");
|
Log.info("Database index '" + ixName + "' added to table '" + table + "'");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -132,26 +126,15 @@ public abstract class BaseLiaison implements DatabaseLiaison
|
|||||||
fields.append(")");
|
fields.append(")");
|
||||||
String update = "ALTER TABLE " + tableSQL(table) + " ADD PRIMARY KEY " + fields.toString();
|
String update = "ALTER TABLE " + tableSQL(table) + " ADD PRIMARY KEY " + fields.toString();
|
||||||
|
|
||||||
Statement stmt = conn.createStatement();
|
executeQuery(conn, update.toString());
|
||||||
try {
|
|
||||||
stmt.executeUpdate(update.toString());
|
|
||||||
} finally {
|
|
||||||
JDBCUtil.close(stmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.info("Primary key " + fields + " added to table '" + table + "'");
|
Log.info("Primary key " + fields + " added to table '" + table + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// from DatabaseLiaison
|
// from DatabaseLiaison
|
||||||
public void dropIndex (Connection conn, String table, String index) throws SQLException
|
public void dropIndex (Connection conn, String table, String index) throws SQLException
|
||||||
{
|
{
|
||||||
Statement stmt = conn.createStatement();
|
executeQuery(conn, "ALTER TABLE " + tableSQL(table) +
|
||||||
try {
|
" DROP CONSTRAINT " + columnSQL(index));
|
||||||
stmt.executeUpdate(
|
|
||||||
"ALTER TABLE " + tableSQL(table) + " DROP CONSTRAINT " + columnSQL(index));
|
|
||||||
} finally {
|
|
||||||
JDBCUtil.close(stmt);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// from DatabaseLiaison
|
// from DatabaseLiaison
|
||||||
@@ -170,14 +153,8 @@ public abstract class BaseLiaison implements DatabaseLiaison
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Statement stmt = conn.createStatement();
|
executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " ADD COLUMN " +
|
||||||
try {
|
columnSQL(column) + " " + definition);
|
||||||
stmt.executeUpdate("ALTER TABLE " + tableSQL(table) + " ADD COLUMN " +
|
|
||||||
columnSQL(column) + " " + definition);
|
|
||||||
} finally {
|
|
||||||
JDBCUtil.close(stmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.info("Database column '" + column + "' added to table '" + table + "'.");
|
Log.info("Database column '" + column + "' added to table '" + table + "'.");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -186,33 +163,21 @@ public abstract class BaseLiaison implements DatabaseLiaison
|
|||||||
public boolean changeColumn (Connection conn, String table, String column, String definition)
|
public boolean changeColumn (Connection conn, String table, String column, String definition)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
Statement stmt = conn.createStatement();
|
executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " CHANGE " +
|
||||||
try {
|
columnSQL(column) + " " + column + " " + definition);
|
||||||
stmt.executeUpdate("ALTER TABLE " + tableSQL(table) + " CHANGE " +
|
Log.info("Database column '" + column + "' of table '" + table + "' modified to have " +
|
||||||
columnSQL(column) + " " + column + " " + definition);
|
"definition '" + definition + "'.");
|
||||||
} finally {
|
|
||||||
JDBCUtil.close(stmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.info("Database column '" + column + "' of table '" + table +
|
|
||||||
"' modified to have definition '" + definition + "'.");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// from DatabaseLiaison
|
// 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
|
throws SQLException
|
||||||
{
|
{
|
||||||
Statement stmt = conn.createStatement();
|
executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " RENAME COLUMN " +
|
||||||
try {
|
columnSQL(from) + " TO " + columnSQL(to));
|
||||||
String query = "ALTER TABLE " + tableSQL(table) + " RENAME COLUMN " +
|
Log.info("Renamed column '" + from + "' on table '" + table + "' to '" + to + "'");
|
||||||
columnSQL(from) + " TO " + columnSQL(to);
|
|
||||||
if (stmt.executeUpdate(query) == 1) {
|
|
||||||
Log.info("Renamed column '" + from + "' on table '" + table + "' to '" + to + "'");
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
JDBCUtil.close(stmt);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,16 +187,8 @@ public abstract class BaseLiaison implements DatabaseLiaison
|
|||||||
if (!tableContainsColumn(conn, table, column)) {
|
if (!tableContainsColumn(conn, table, column)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " DROP COLUMN " + columnSQL(column));
|
||||||
Statement stmt = conn.createStatement();
|
Log.info("Database column '" + column + "' removed from table '" + table + "'.");
|
||||||
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);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,14 +240,19 @@ public abstract class BaseLiaison implements DatabaseLiaison
|
|||||||
|
|
||||||
builder.append(")");
|
builder.append(")");
|
||||||
|
|
||||||
Statement stmt = conn.createStatement();
|
executeQuery(conn, builder.toString());
|
||||||
try {
|
|
||||||
stmt.executeUpdate(builder.toString());
|
|
||||||
} finally {
|
|
||||||
JDBCUtil.close(stmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.info("Database table '" + table + "' created.");
|
Log.info("Database table '" + table + "' created.");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected int executeQuery (Connection conn, String query)
|
||||||
|
throws SQLException
|
||||||
|
{
|
||||||
|
Statement stmt = conn.createStatement();
|
||||||
|
try {
|
||||||
|
return stmt.executeUpdate(query);
|
||||||
|
} finally {
|
||||||
|
JDBCUtil.close(stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
* 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.
|
* 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;
|
throws SQLException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -99,14 +99,7 @@ public class MySQLLiaison extends BaseLiaison
|
|||||||
}
|
}
|
||||||
update.append(")");
|
update.append(")");
|
||||||
|
|
||||||
PreparedStatement stmt = null;
|
executeQuery(conn, update.toString());
|
||||||
try {
|
|
||||||
stmt = conn.prepareStatement(update.toString());
|
|
||||||
stmt.executeUpdate();
|
|
||||||
} finally {
|
|
||||||
JDBCUtil.close(stmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.info("Database index '" + ixName + "' added to table '" + table + "'");
|
Log.info("Database index '" + ixName + "' added to table '" + table + "'");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -115,25 +108,29 @@ public class MySQLLiaison extends BaseLiaison
|
|||||||
public void dropIndex (Connection conn, String table, String index)
|
public void dropIndex (Connection conn, String table, String index)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
Statement stmt = conn.createStatement();
|
executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " DROP INDEX " + columnSQL(index));
|
||||||
try {
|
|
||||||
stmt.executeUpdate(
|
|
||||||
"ALTER TABLE " + tableSQL(table) + " DROP INDEX " + columnSQL(index));
|
|
||||||
} finally {
|
|
||||||
JDBCUtil.close(stmt);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from BaseLiaison
|
@Override // from BaseLiaison
|
||||||
public void dropPrimaryKey (Connection conn, String table, String pkName)
|
public void dropPrimaryKey (Connection conn, String table, String pkName)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
Statement stmt = conn.createStatement();
|
executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " DROP PRIMARY KEY");
|
||||||
try {
|
}
|
||||||
stmt.executeUpdate("ALTER TABLE " + tableSQL(table) + " DROP PRIMARY KEY");
|
|
||||||
} finally {
|
@Override // from BaseLiaison
|
||||||
JDBCUtil.close(stmt);
|
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
|
// from DatabaseLiaison
|
||||||
|
|||||||
@@ -77,13 +77,8 @@ public class PostgreSQLLiaison extends BaseLiaison
|
|||||||
Connection conn, String table, String column, int first, int step)
|
Connection conn, String table, String column, int first, int step)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
Statement stmt = conn.createStatement();
|
executeQuery(conn, "alter sequence \"" + table + "_" + column + "_seq\" " +
|
||||||
try {
|
" restart with " + first + " increment " + step);
|
||||||
stmt.executeUpdate("alter sequence \"" + table + "_" + column + "_seq\" " +
|
|
||||||
" restart with " + first + " increment " + step);
|
|
||||||
} finally {
|
|
||||||
JDBCUtil.close(stmt);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// from DatabaseLiaison
|
// from DatabaseLiaison
|
||||||
|
|||||||
@@ -92,14 +92,20 @@ public abstract class EntityMigration extends Modifier
|
|||||||
|
|
||||||
log.info("Renaming '" + _oldColumnName + "' to '" + _newColumnName + "' in: " +
|
log.info("Renaming '" + _oldColumnName + "' to '" + _newColumnName + "' in: " +
|
||||||
_tableName);
|
_tableName);
|
||||||
return liaison.renameColumn(conn, _tableName, _oldColumnName, _newColumnName) ? 1 : 0;
|
return liaison.renameColumn(
|
||||||
|
conn, _tableName, _oldColumnName, _newColumnName, _newColumnDef) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean runBeforeDefault () {
|
public boolean runBeforeDefault () {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String _oldColumnName, _newColumnName;
|
protected void init (String tableName, Map<String,FieldMarshaller> marshallers) {
|
||||||
|
super.init(tableName, marshallers);
|
||||||
|
_newColumnDef = marshallers.get(_newColumnName).getColumnDefinition();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String _oldColumnName, _newColumnName, _newColumnDef;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user