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 1a3a04901..da7e3eab1 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 @@ -73,8 +73,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) { } @@ -91,8 +90,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) { } @@ -675,8 +673,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) { } @@ -712,6 +709,8 @@ public void testCancelGetOutParams() throws Exception { // Reexecute to prove CS is still good after last cancel cstmt.execute(); + + Utils.dropProcedureIfExists(procName, stmt); con.close(); } @@ -1043,14 +1042,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) { } @@ -1189,8 +1186,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) { } @@ -1222,8 +1218,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) { } @@ -1259,8 +1254,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"); @@ -1293,6 +1287,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 @@ -1300,8 +1295,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"); @@ -1323,9 +1317,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); } /** @@ -1338,8 +1330,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"); @@ -1357,6 +1348,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); } /** @@ -1371,15 +1363,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) { } @@ -1400,15 +1390,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) { } @@ -1430,15 +1418,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) { } @@ -1462,15 +1448,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) { } @@ -1487,8 +1471,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) { } @@ -1514,8 +1497,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) { } @@ -1534,15 +1516,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) { } @@ -1568,15 +1548,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) { } @@ -1596,15 +1574,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) { } @@ -1698,15 +1674,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) { } @@ -1732,8 +1706,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) { } @@ -1752,8 +1725,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) { } @@ -1971,8 +1943,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) { } @@ -2021,8 +1992,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) { } @@ -2322,22 +2292,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); @@ -2463,8 +2430,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) { } @@ -2679,8 +2645,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