diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/connection/TimeoutTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/connection/TimeoutTest.java index de8830a5f..777b3ede5 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/connection/TimeoutTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/connection/TimeoutTest.java @@ -20,6 +20,7 @@ import com.microsoft.sqlserver.jdbc.SQLServerConnection; import com.microsoft.sqlserver.jdbc.SQLServerException; import com.microsoft.sqlserver.testframework.AbstractTest; +import com.microsoft.sqlserver.testframework.Utils; import com.microsoft.sqlserver.testframework.util.RandomUtil; @RunWith(JUnitPlatform.class) @@ -129,9 +130,7 @@ public void testSocketTimeout() throws Exception { } private void dropWaitForDelayProcedure(SQLServerConnection conn) throws SQLException { - String sql = " IF EXISTS (select * from sysobjects where id = object_id(N'" + waitForDelaySPName - + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + " DROP PROCEDURE " + waitForDelaySPName; - conn.createStatement().execute(sql); + Utils.dropProcedureIfExists(waitForDelaySPName, conn.createStatement()); } private void createWaitForDelayPreocedure(SQLServerConnection conn) throws SQLException { diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/parametermetadata/ParameterMetaDataTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/parametermetadata/ParameterMetaDataTest.java index 41ccedb6d..c4e8bd0d9 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/parametermetadata/ParameterMetaDataTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/parametermetadata/ParameterMetaDataTest.java @@ -23,6 +23,7 @@ import com.microsoft.sqlserver.jdbc.SQLServerException; import com.microsoft.sqlserver.testframework.AbstractTest; +import com.microsoft.sqlserver.testframework.Utils; import com.microsoft.sqlserver.testframework.util.RandomUtil; @RunWith(JUnitPlatform.class) @@ -49,8 +50,7 @@ public void testParameterMetaDataWrapper() throws SQLException { assertSame(parameterMetaData, parameterMetaData.unwrap(ParameterMetaData.class)); } } finally { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java index 2c187563a..08d935703 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java @@ -25,6 +25,7 @@ import com.microsoft.sqlserver.jdbc.ISQLServerResultSet; import com.microsoft.sqlserver.testframework.AbstractTest; +import com.microsoft.sqlserver.testframework.Utils; import com.microsoft.sqlserver.testframework.util.RandomUtil; @RunWith(JUnitPlatform.class) @@ -97,8 +98,7 @@ public void testResultSetWrapper() throws SQLException { assertSame(rs, rs.unwrap(ResultSet.class)); assertSame(rs, rs.unwrap(ISQLServerResultSet.class)); } finally { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } } } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/NamedParamMultiPartTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/NamedParamMultiPartTest.java index 47c73ed95..be8139e39 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/NamedParamMultiPartTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/NamedParamMultiPartTest.java @@ -12,8 +12,6 @@ import java.sql.CallableStatement; import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; @@ -21,12 +19,12 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; import org.junit.runner.RunWith; import com.microsoft.sqlserver.testframework.AbstractTest; +import com.microsoft.sqlserver.testframework.Utils; /** * Multipart parameters @@ -46,8 +44,7 @@ public class NamedParamMultiPartTest extends AbstractTest { public static void beforeAll() throws SQLException { connection = DriverManager.getConnection(connectionString); Statement statement = connection.createStatement(); - statement.executeUpdate( - "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[mystoredproc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) DROP PROCEDURE [mystoredproc]"); + Utils.dropProcedureIfExists("mystoredproc", statement); statement.executeUpdate("CREATE PROCEDURE [mystoredproc] (@p_out varchar(255) OUTPUT) AS set @p_out = '" + dataPut + "'"); statement.close(); } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/RegressionTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/RegressionTest.java index d7549d73a..659141759 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/RegressionTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/RegressionTest.java @@ -23,6 +23,7 @@ import com.microsoft.sqlserver.jdbc.SQLServerConnection; import com.microsoft.sqlserver.testframework.AbstractTest; import com.microsoft.sqlserver.testframework.DBConnection; +import com.microsoft.sqlserver.testframework.Utils; @RunWith(JUnitPlatform.class) public class RegressionTest extends AbstractTest { @@ -126,11 +127,8 @@ public void testSelectIntoUpdateCount() throws SQLException { public static void terminate() throws SQLException { SQLServerConnection con = (SQLServerConnection) DriverManager.getConnection(connectionString); Statement stmt = con.createStatement(); - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + procName + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" - + " DROP PROCEDURE " + procName); - + Utils.dropTableIfExists(tableName, stmt); + Utils.dropProcedureIfExists(procName, stmt); } } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/StatementTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/StatementTest.java index 70551b606..b2ee14cad 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/StatementTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/StatementTest.java @@ -72,8 +72,7 @@ public void init() throws Exception { con.setAutoCommit(false); Statement stmt = con.createStatement(); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (SQLException e) { } @@ -90,8 +89,7 @@ public void terminate() throws Exception { Connection con = DriverManager.getConnection(connectionString); Statement stmt = con.createStatement(); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (SQLException e) { } @@ -674,8 +672,7 @@ public void testCancelGetOutParams() throws Exception { Statement stmt = con.createStatement(); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + procName - + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + " DROP PROCEDURE " + procName); + Utils.dropProcedureIfExists(procName, stmt); } catch (Exception ex) { } @@ -711,6 +708,8 @@ public void testCancelGetOutParams() throws Exception { // Reexecute to prove CS is still good after last cancel cstmt.execute(); + + Utils.dropProcedureIfExists(procName, stmt); con.close(); } @@ -1042,14 +1041,12 @@ public void testConsecutiveQueries() throws Exception { } try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + table1Name + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + table1Name); + Utils.dropTableIfExists(table1Name, stmt); } catch (SQLException e) { } try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + table2Name + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + table2Name); + Utils.dropTableIfExists(table2Name, stmt); } catch (SQLException e) { } @@ -1188,8 +1185,7 @@ public void testJdbc41CallableStatementMethods() throws Exception { Connection conn = DriverManager.getConnection(connectionString); Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + procName - + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + " DROP PROCEDURE " + procName); + Utils.dropProcedureIfExists(procName, stmt); } catch (Exception ex) { } @@ -1221,8 +1217,7 @@ public void testJdbc41CallableStatementMethods() throws Exception { } try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + procName - + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + " DROP PROCEDURE " + procName); + Utils.dropProcedureIfExists(procName, stmt); } catch (Exception ex) { } @@ -1258,8 +1253,7 @@ public void testStatementOutParamGetsTwice() throws Exception { log.fine("testStatementOutParamGetsTwice threw: " + e.getMessage()); } - stmt.executeUpdate( - "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_ouputP]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) DROP PROCEDURE [sp_ouputP]"); + Utils.dropProcedureIfExists("sp_ouputP", stmt); stmt.executeUpdate( "CREATE PROCEDURE [sp_ouputP] ( @p2_smallint smallint, @p3_smallint_out smallint OUTPUT) AS SELECT @p3_smallint_out=@p2_smallint RETURN @p2_smallint + 1"); @@ -1292,6 +1286,7 @@ public void testStatementOutParamGetsTwice() throws Exception { else { assertEquals((stmt).isClosed(), false, "testStatementOutParamGetsTwice: statement should be open since no resultset."); } + Utils.dropProcedureIfExists("sp_ouputP", stmt); } @Test @@ -1299,8 +1294,7 @@ public void testStatementOutManyParamGetsTwiceRandomOrder() throws Exception { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection con = DriverManager.getConnection(connectionString); Statement stmt = con.createStatement(); - stmt.executeUpdate( - "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_ouputMP]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) DROP PROCEDURE [sp_ouputMP]"); + Utils.dropProcedureIfExists("sp_ouputMP", stmt); stmt.executeUpdate( "CREATE PROCEDURE [sp_ouputMP] ( @p2_smallint smallint, @p3_smallint_out smallint OUTPUT, @p4_smallint smallint OUTPUT, @p5_smallint_out smallint OUTPUT) AS SELECT @p3_smallint_out=@p2_smallint, @p5_smallint_out=@p4_smallint RETURN @p2_smallint + 1"); @@ -1322,9 +1316,7 @@ public void testStatementOutManyParamGetsTwiceRandomOrder() throws Exception { assertEquals(cstmt.getInt(5), 24, "Wrong value"); assertEquals(cstmt.getInt(1), 35, "Wrong value"); - stmt.executeUpdate( - "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_ouputMP]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) DROP PROCEDURE [sp_ouputMP]"); - + Utils.dropProcedureIfExists("sp_ouputMP", stmt); } /** @@ -1337,8 +1329,7 @@ public void testStatementOutParamGetsTwiceInOut() throws Exception { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection con = DriverManager.getConnection(connectionString); Statement stmt = con.createStatement(); - stmt.executeUpdate( - "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_ouputP]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) DROP PROCEDURE [sp_ouputP]"); + Utils.dropProcedureIfExists("sp_ouputP", stmt); stmt.executeUpdate( "CREATE PROCEDURE [sp_ouputP] ( @p2_smallint smallint, @p3_smallint_out smallint OUTPUT) AS SELECT @p3_smallint_out=@p3_smallint_out +1 RETURN @p2_smallint + 1"); @@ -1356,6 +1347,7 @@ public void testStatementOutParamGetsTwiceInOut() throws Exception { assertEquals(cstmt.getInt(1), 11, "Wrong value"); assertEquals(cstmt.getInt(3), 101, "Wrong value"); + Utils.dropProcedureIfExists("sp_ouputP", stmt); } /** @@ -1370,15 +1362,13 @@ public void testResultSetParams() throws Exception { Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (Exception ex) { } ; try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + procName - + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + " DROP PROCEDURE " + procName); + Utils.dropProcedureIfExists(procName, stmt); } catch (Exception ex) { } @@ -1399,15 +1389,13 @@ public void testResultSetParams() throws Exception { assertEquals(cstmt.getString(2), "hi", "Wrong value"); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (Exception ex) { } ; try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + procName - + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + " DROP PROCEDURE " + procName); + Utils.dropProcedureIfExists(procName, stmt); } catch (Exception ex) { } @@ -1429,15 +1417,13 @@ public void testResultSetNullParams() throws Exception { Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (Exception ex) { } ; try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + procName - + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + " DROP PROCEDURE " + procName); + Utils.dropProcedureIfExists(procName, stmt); } catch (Exception ex) { } @@ -1461,15 +1447,13 @@ public void testResultSetNullParams() throws Exception { ; try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (Exception ex) { } ; try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + procName - + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + " DROP PROCEDURE " + procName); + Utils.dropProcedureIfExists(procName, stmt); } catch (Exception ex) { } @@ -1486,8 +1470,7 @@ public void testFailedToResumeTransaction() throws Exception { Connection conn = DriverManager.getConnection(connectionString); Statement stmt = conn.createStatement(); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (Exception ex) { } @@ -1513,8 +1496,7 @@ public void testFailedToResumeTransaction() throws Exception { catch (SQLException ex) { } try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (Exception ex) { } @@ -1533,15 +1515,13 @@ public void testResultSetErrors() throws Exception { Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (Exception ex) { } ; try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + procName - + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + " DROP PROCEDURE " + procName); + Utils.dropProcedureIfExists(procName, stmt); } catch (Exception ex) { } @@ -1567,15 +1547,13 @@ public void testResultSetErrors() throws Exception { assertEquals(null, cstmt.getString(2), "Wrong value"); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (Exception ex) { } ; try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + procName - + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + " DROP PROCEDURE " + procName); + Utils.dropProcedureIfExists(procName, stmt); } catch (Exception ex) { } @@ -1593,15 +1571,13 @@ public void testRowError() throws Exception { // Set up everything Statement stmt = conn.createStatement(); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (Exception ex) { } ; try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + procName - + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + " DROP PROCEDURE " + procName); + Utils.dropProcedureIfExists(procName, stmt); } catch (Exception ex) { } @@ -1695,15 +1671,13 @@ public void testRowError() throws Exception { } try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (Exception ex) { } ; try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + procName - + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + " DROP PROCEDURE " + procName); + Utils.dropProcedureIfExists(procName, stmt); } catch (Exception ex) { } @@ -1729,8 +1703,7 @@ private Connection createConnectionAndPopulateData() throws Exception { Statement stmt = con.createStatement(); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (SQLException e) { } @@ -1749,8 +1722,7 @@ private void cleanup(Connection con) throws Exception { con = DriverManager.getConnection(connectionString); } - con.createStatement().executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, con.createStatement()); } catch (SQLException e) { } @@ -1968,8 +1940,7 @@ public void testNBCRowForAllNulls() throws Exception { con = ds.getConnection(); Statement stmt = con.createStatement(); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (SQLException e) { } @@ -2018,8 +1989,7 @@ public void testNBCROWWithRandomAccess() throws Exception { con = ds.getConnection(); Statement stmt = con.createStatement(); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (SQLException e) { } @@ -2319,22 +2289,19 @@ private void setup() throws Exception { con.setAutoCommit(false); Statement stmt = con.createStatement(); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (SQLException e) { throw new SQLException(e); } try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + table2Name + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + table2Name); + Utils.dropTableIfExists(table2Name, stmt); } catch (SQLException e) { throw new SQLException(e); } try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + sprocName - + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + " DROP PROCEDURE " + sprocName); + Utils.dropProcedureIfExists(sprocName, stmt); } catch (SQLException e) { throw new SQLException(e); @@ -2460,8 +2427,7 @@ private void setup() throws Exception { con.setAutoCommit(false); Statement stmt = con.createStatement(); try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (SQLException e) { } @@ -2676,8 +2642,7 @@ private void setup() throws Exception { } try { - stmt.executeUpdate("IF EXISTS (select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsTable') = 1)" - + " DROP TABLE " + tableName); + Utils.dropTableIfExists(tableName, stmt); } catch (SQLException e) { } diff --git a/src/test/java/com/microsoft/sqlserver/testframework/Utils.java b/src/test/java/com/microsoft/sqlserver/testframework/Utils.java index eb21d2864..45aa08780 100644 --- a/src/test/java/com/microsoft/sqlserver/testframework/Utils.java +++ b/src/test/java/com/microsoft/sqlserver/testframework/Utils.java @@ -9,10 +9,8 @@ package com.microsoft.sqlserver.testframework; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.CharArrayReader; -import java.io.IOException; -import java.io.InputStream; +import java.sql.SQLException; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; @@ -249,5 +247,41 @@ public DBNCharacterStream(String value) { super(value); } } + + /** + * mimic "DROP TABLE IF EXISTS ..." for older versions of SQL Server + */ + public static void dropTableIfExists(String tableName, java.sql.Statement stmt) throws SQLException { + dropObjectIfExists(tableName, "IsTable", stmt); + } + + /** + * mimic "DROP PROCEDURE IF EXISTS ..." for older versions of SQL Server + */ + public static void dropProcedureIfExists(String procName, java.sql.Statement stmt) throws SQLException { + dropObjectIfExists(procName, "IsProcedure", stmt); + } + /** + * actually perform the "DROP TABLE / PROCEDURE" + */ + private static void dropObjectIfExists(String objectName, String objectProperty, java.sql.Statement stmt) throws SQLException { + StringBuilder sb = new StringBuilder(); + if (!objectName.startsWith("[")) { sb.append("["); } + sb.append(objectName); + if (!objectName.endsWith("]")) { sb.append("]"); } + String bracketedObjectName = sb.toString(); + String sql = String.format( + "IF EXISTS " + + "( " + + "SELECT * from sys.objects " + + "WHERE object_id = OBJECT_ID(N'%s') AND OBJECTPROPERTY(object_id, N'%s') = 1 " + + ") " + + "DROP %s %s ", + bracketedObjectName, + objectProperty, + "IsProcedure".equals(objectProperty) ? "PROCEDURE" : "TABLE", + bracketedObjectName); + stmt.executeUpdate(sql); + } } \ No newline at end of file