Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for queries without parameters using preparedStatement #372

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,8 @@ private void buildExecSQLParams(TDSWriter tdsWriter) throws SQLServerException {
tdsWriter.writeRPCStringUnicode(preparedSQL);

// <formal parameter defn> IN
tdsWriter.writeRPCStringUnicode((preparedTypeDefinitions.length() > 0) ? preparedTypeDefinitions : null);
if (preparedTypeDefinitions.length() > 0)
tdsWriter.writeRPCStringUnicode(preparedTypeDefinitions);
}

private void buildServerCursorExecParams(TDSWriter tdsWriter) throws SQLServerException {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -505,4 +504,4 @@ public void testStatementPoolingPreparedStatementExecAndUnprepareConfig() throws
assertSame(0, con.getDiscardedServerPreparedStatementCount());
}
}
}
}