-
Notifications
You must be signed in to change notification settings - Fork 435
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
AfsanehR-zz
merged 4 commits into
microsoft:RTW_6.2.0
from
AfsanehR-zz:metaDataCachingRegression
Jul 12, 2017
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c21f000
fix for queries without parameters using preparedStatement
AfsanehR-zz c53a7db
Update test file
AfsanehR-zz 529a01b
Modified the test with change in behav. Will use sp_prepexec for quer…
AfsanehR-zz 97a597a
changed buildExecSQLParams to omit null in case of no preparedTypeDef…
AfsanehR-zz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/RegressionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
/* | ||
* 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; | ||
|
||
@BeforeAll | ||
public static void setupTest() throws SQLException { | ||
con = DriverManager.getConnection(connectionString); | ||
Statement stmt = con.createStatement(); | ||
Utils.dropTableIfExists("x", stmt); | ||
Utils.dropViewIfExists("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 | ||
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(); | ||
} | ||
} | ||
} | ||
|
||
@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(); | ||
} | ||
} | ||
} | ||
|
||
@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("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(); | ||
} | ||
if (null != pstmt4) { | ||
pstmt4.close(); | ||
} | ||
} | ||
} | ||
|
||
@AfterAll | ||
public static void cleanup() throws SQLException{ | ||
Statement stmt = con.createStatement(); | ||
Utils.dropTableIfExists("x", stmt); | ||
Utils.dropViewIfExists("x", stmt); | ||
if (null != stmt){ | ||
stmt.close(); | ||
} | ||
if (null != con) { | ||
con.close(); | ||
} | ||
if (null != pstmt1) { | ||
pstmt1.close(); | ||
} | ||
if (null != pstmt2) { | ||
pstmt2.close(); | ||
} | ||
|
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not make this check here. sp_executesql works fine without arguments. The problem is in the buildExecSQLParams method. Basically this works fine for sp_prepexec/buildPrepExecParams but not for sp_executesql:
tdsWriter.writeRPCStringUnicode((preparedTypeDefinitions.length() > 0) ? preparedTypeDefinitions : null);
So we should just change that to:
if (preparedTypeDefinitions.length() > 0) tdsWriter.writeRPCStringUnicode(preparedTypeDefinitions);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, thanks @TobiasSQL . That was my initial thought too. Will commit the changes.