From e10b9c82d6aff68620bf303def08cfc790f3bad6 Mon Sep 17 00:00:00 2001 From: eric Date: Sat, 17 Jul 2004 02:19:37 +0000 Subject: [PATCH] - Modified tableContainsIndex to allow optionally looking for a named index as opposed to checking if a column contains an index. - Modified addIndexToTable to use the new call and report back more usefully. - Added tableContainsPrimaryKey and dropPrimaryKey, which are not actually getting used, but I could seem them being useful in the future, so checking them in. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1467 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/jdbc/JDBCUtil.java | 58 +++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/JDBCUtil.java b/projects/samskivert/src/java/com/samskivert/jdbc/JDBCUtil.java index cd074f4b..e22247b8 100644 --- a/projects/samskivert/src/java/com/samskivert/jdbc/JDBCUtil.java +++ b/projects/samskivert/src/java/com/samskivert/jdbc/JDBCUtil.java @@ -207,16 +207,44 @@ public class JDBCUtil /** * Returns true if the index on the specified column exists for the - * specified table. false if it does not. Note: the names are - * case sensitive. + * specified table, false if it does not. Optionally you can specifiy + * a non null index name, and the table will be checked to see if it + * contains that specifically named index. Note: the names + * are case sensitive. */ public static boolean tableContainsIndex (Connection conn, String table, - String column) + String column, String index) throws SQLException { boolean matched = false; ResultSet rs = conn.getMetaData().getIndexInfo("", "", table, false, true); + while (rs.next()) { + String tname = rs.getString("TABLE_NAME"); + String cname = rs.getString("COLUMN_NAME"); + String iname = rs.getString("INDEX_NAME"); + + if (iname == null) { + if (tname.equals(table) && cname.equals(column)) { + matched = true; + } + } else if (iname.equals(index)) { + matched = true; + } + } + return matched; + } + + /** + * Returns true if the specified table contains a primary key on the + * specified column. + */ + public static boolean tableContainsPrimaryKey (Connection conn, + String table, String column) + throws SQLException + { + boolean matched = false; + ResultSet rs = conn.getMetaData().getPrimaryKeys("", "", table); while (rs.next()) { String tname = rs.getString("TABLE_NAME"); String cname = rs.getString("COLUMN_NAME"); @@ -299,6 +327,25 @@ public class JDBCUtil "'."); } + /** + * Removes the primary key from the specified table. + */ + public static void dropPrimaryKey (Connection conn, String table) + throws SQLException + { + String update = "ALTER TABLE " + table + " DROP PRIMARY KEY"; + + PreparedStatement stmt = null; + try { + stmt = conn.prepareStatement(update); + stmt.executeUpdate(); + } finally { + close(stmt); + } + + Log.info("Database primary key removed from table '" + table + "'."); + } + /** * Adds an index on the specified column (cname) to the specified * table. Optionally supply an index name, otherwise the index is @@ -308,9 +355,10 @@ public class JDBCUtil String cname, String iname) throws SQLException { - if (JDBCUtil.tableContainsIndex(conn, table, cname)) { + if (JDBCUtil.tableContainsIndex(conn, table, cname, iname)) { Log.info("Database table '" + table + "' already has an index on " + - "column '" + cname + "'."); + "column '" + cname + "'" + + (iname != null ? " named '" + iname + "'." : ".")); return; }