diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java index 4bfcb58e2..0102d6104 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java @@ -704,7 +704,8 @@ private void buildExecSQLParams(TDSWriter tdsWriter) throws SQLServerException { tdsWriter.writeRPCStringUnicode(preparedSQL); // IN - tdsWriter.writeRPCStringUnicode((preparedTypeDefinitions.length() > 0) ? preparedTypeDefinitions : null); + if (preparedTypeDefinitions.length() > 0) + tdsWriter.writeRPCStringUnicode(preparedTypeDefinitions); } private void buildServerCursorExecParams(TDSWriter tdsWriter) throws SQLServerException { diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/RegressionTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/RegressionTest.java new file mode 100644 index 000000000..574726f5b --- /dev/null +++ b/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/RegressionTest.java @@ -0,0 +1,228 @@ +/* + * Microsoft JDBC Driver for SQL Server + * + * Copyright(c) Microsoft Corporation All rights reserved. + * + * This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information. + */ +package com.microsoft.sqlserver.jdbc.preparedStatement; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Statement; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +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; + +import static org.junit.jupiter.api.Assertions.fail; + +/** + * Tests with sql queries using preparedStatement without parameters + * + * + */ +@RunWith(JUnitPlatform.class) +public class RegressionTest extends AbstractTest { + static Connection con = null; + static PreparedStatement pstmt1 = null; + static PreparedStatement pstmt2 = null; + static PreparedStatement pstmt3 = null; + static PreparedStatement pstmt4 = null; + + /** + * Setup before test + * + * @throws SQLException + */ + @BeforeAll + public static void setupTest() throws SQLException { + con = DriverManager.getConnection(connectionString); + Statement stmt = con.createStatement(); + Utils.dropTableIfExists("x", stmt); + if (null != stmt) { + stmt.close(); + } + } + + /** + * Tests creating view using preparedStatement + * + * @throws SQLException + */ + @Test + public void createViewTest() throws SQLException { + try { + pstmt1 = con.prepareStatement("create view x as select 1 a"); + pstmt2 = con.prepareStatement("drop view x"); + pstmt1.execute(); + pstmt2.execute(); + } + catch (SQLException e) { + fail("Create/drop view with preparedStatement failed! Error message: " + e.getMessage()); + } + + finally { + if (null != pstmt1) { + pstmt1.close(); + } + if (null != pstmt2) { + pstmt2.close(); + } + } + } + + /** + * Tests creating schema using preparedStatement + * + * @throws SQLException + */ + @Test + public void createSchemaTest() throws SQLException { + try { + pstmt1 = con.prepareStatement("create schema x"); + pstmt2 = con.prepareStatement("drop schema x"); + pstmt1.execute(); + pstmt2.execute(); + } + catch (SQLException e) { + fail("Create/drop schema with preparedStatement failed! Error message:" + e.getMessage()); + } + + finally { + if (null != pstmt1) { + pstmt1.close(); + } + if (null != pstmt2) { + pstmt2.close(); + } + } + } + + /** + * Test creating and dropping tabel with preparedStatement + * + * @throws SQLException + */ + @Test + public void createTableTest() throws SQLException { + try { + pstmt1 = con.prepareStatement("create table x (col1 int)"); + pstmt2 = con.prepareStatement("drop table x"); + pstmt1.execute(); + pstmt2.execute(); + } + catch (SQLException e) { + fail("Create/drop table with preparedStatement failed! Error message:" + e.getMessage()); + } + + finally { + if (null != pstmt1) { + pstmt1.close(); + } + if (null != pstmt2) { + pstmt2.close(); + } + } + } + + /** + * Tests creating/altering/dropping table + * + * @throws SQLException + */ + @Test + public void alterTableTest() throws SQLException { + try { + pstmt1 = con.prepareStatement("create table x (col1 int)"); + pstmt2 = con.prepareStatement("ALTER TABLE x ADD column_name char;"); + pstmt3 = con.prepareStatement("drop table x"); + pstmt1.execute(); + pstmt2.execute(); + pstmt3.execute(); + } + catch (SQLException e) { + fail("Create/drop/alter table with preparedStatement failed! Error message:" + e.getMessage()); + } + + finally { + if (null != pstmt1) { + pstmt1.close(); + } + if (null != pstmt2) { + pstmt2.close(); + } + if (null != pstmt3) { + pstmt3.close(); + } + } + } + + /** + * Tests with grant queries + * + * @throws SQLException + */ + @Test + public void grantTest() throws SQLException { + try { + pstmt1 = con.prepareStatement("create table x (col1 int)"); + pstmt2 = con.prepareStatement("grant select on x to public"); + pstmt3 = con.prepareStatement("revoke select on x from public"); + pstmt4 = con.prepareStatement("drop table x"); + pstmt1.execute(); + pstmt2.execute(); + pstmt3.execute(); + pstmt4.execute(); + } + catch (SQLException e) { + fail("grant with preparedStatement failed! Error message:" + e.getMessage()); + } + + finally { + if (null != pstmt1) { + pstmt1.close(); + } + if (null != pstmt2) { + pstmt2.close(); + } + if (null != pstmt3) { + pstmt3.close(); + } + if (null != pstmt4) { + pstmt4.close(); + } + } + } + + /** + * Cleanup after test + * + * @throws SQLException + */ + @AfterAll + public static void cleanup() throws SQLException { + Statement stmt = con.createStatement(); + Utils.dropTableIfExists("x", stmt); + if (null != stmt) { + stmt.close(); + } + if (null != con) { + con.close(); + } + if (null != pstmt1) { + pstmt1.close(); + } + if (null != pstmt2) { + pstmt2.close(); + } + + } + +} diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/PreparedStatementTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/PreparedStatementTest.java index c274e3611..a5bf529d5 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/PreparedStatementTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/PreparedStatementTest.java @@ -32,7 +32,6 @@ import com.microsoft.sqlserver.jdbc.SQLServerDataSource; import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement; import com.microsoft.sqlserver.testframework.AbstractTest; -import com.microsoft.sqlserver.testframework.util.RandomUtil; @RunWith(JUnitPlatform.class) public class PreparedStatementTest extends AbstractTest { @@ -505,4 +504,4 @@ public void testStatementPoolingPreparedStatementExecAndUnprepareConfig() throws assertSame(0, con.getDiscardedServerPreparedStatementCount()); } } -} +} \ No newline at end of file