Skip to content

Commit

Permalink
refactor: consolidate repeated drop code in TestUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremymailen authored and sehrope committed Feb 13, 2021
1 parent 6669081 commit f5abc65
Showing 1 changed file with 12 additions and 54 deletions.
66 changes: 12 additions & 54 deletions pgjdbc/src/test/java/org/postgresql/test/TestUtil.java
Expand Up @@ -406,18 +406,7 @@ public static void createSchema(Connection con, String schema) throws SQLExcepti
* Helper - drops a schema
*/
public static void dropSchema(Connection con, String schema) throws SQLException {
Statement stmt = con.createStatement();
try {
if (con.getAutoCommit()) {
// Not in a transaction so ignore error for missing object
stmt.executeUpdate("DROP SCHEMA IF EXISTS " + schema + " CASCADE");
} else {
// In a transaction so do not ignore errors for missing object
stmt.executeUpdate("DROP SCHEMA " + schema + " CASCADE");
}
} finally {
closeQuietly(stmt);
}
dropObject(con, "SCHEMA", schema);
}

/*
Expand Down Expand Up @@ -445,7 +434,6 @@ public static void createTable(Connection con, String table, String columns) thr
* @param table String
* @param columns String
*/

public static void createTempTable(Connection con, String table, String columns)
throws SQLException {
Statement st = con.createStatement();
Expand Down Expand Up @@ -487,7 +475,6 @@ public static void createUnloggedTable(Connection con, String table, String colu
* @param name String
* @param values String
*/

public static void createEnumType(Connection con, String name, String values)
throws SQLException {
Statement st = con.createStatement();
Expand Down Expand Up @@ -537,22 +524,11 @@ public static void createCompositeType(Connection con, String name, String value
* Drops a domain.
*
* @param con Connection
* @param name String
* @param domain String
*/
public static void dropDomain(Connection con, String name)
public static void dropDomain(Connection con, String domain)
throws SQLException {
Statement stmt = con.createStatement();
try {
if (con.getAutoCommit()) {
// Not in a transaction so ignore error for missing object
stmt.executeUpdate("DROP DOMAIN IF EXISTS " + name + " CASCADE");
} else {
// In a transaction so do not ignore errors for missing object
stmt.executeUpdate("DROP DOMAIN " + name + " CASCADE");
}
} finally {
closeQuietly(stmt);
}
dropObject(con, "DOMAIN", domain);
}

/**
Expand All @@ -578,50 +554,32 @@ public static void createDomain(Connection con, String name, String values)
* drop a sequence because older versions don't have dependency information for serials
*/
public static void dropSequence(Connection con, String sequence) throws SQLException {
Statement stmt = con.createStatement();
try {
if (con.getAutoCommit()) {
// Not in a transaction so ignore error for missing object
stmt.executeUpdate("DROP SEQUENCE IF EXISTS " + sequence + " CASCADE");
} else {
// In a transaction so do not ignore errors for missing object
stmt.executeUpdate("DROP SEQUENCE " + sequence + " CASCADE");
}
} finally {
closeQuietly(stmt);
}
dropObject(con, "SEQUENCE", sequence);
}

/*
* Helper - drops a table
*/
public static void dropTable(Connection con, String table) throws SQLException {
Statement stmt = con.createStatement();
try {
if (con.getAutoCommit()) {
// Not in a transaction so ignore error for missing object
stmt.executeUpdate("DROP TABLE IF EXISTS " + table + " CASCADE ");
} else {
// In a transaction so do not ignore errors for missing object
stmt.executeUpdate("DROP TABLE " + table + " CASCADE ");
}
} finally {
closeQuietly(stmt);
}
dropObject(con, "TABLE", table);
}

/*
* Helper - drops a type
*/
public static void dropType(Connection con, String type) throws SQLException {
dropObject(con, "TYPE", type);
}

private static void dropObject(Connection con, String type, String name) throws SQLException {
Statement stmt = con.createStatement();
try {
if (con.getAutoCommit()) {
// Not in a transaction so ignore error for missing object
stmt.executeUpdate("DROP TYPE IF EXISTS " + type + " CASCADE");
stmt.executeUpdate("DROP " + type + " IF EXISTS " + name + " CASCADE");
} else {
// In a transaction so do not ignore errors for missing object
stmt.executeUpdate("DROP TYPE " + type + " CASCADE");
stmt.executeUpdate("DROP " + type + " " + name + " CASCADE");
}
} finally {
closeQuietly(stmt);
Expand Down

0 comments on commit f5abc65

Please sign in to comment.