-
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
Tvp server cursor fix #234
Merged
xiangyushawn
merged 20 commits into
microsoft:dev
from
xiangyushawn:TVP-Server-Cursor-Fix
Apr 25, 2017
Merged
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2337105
unclean fix works
xiangyushawn dc99efd
clean the logic
xiangyushawn b0f1bbc
detect if needs to read TVP response
xiangyushawn f472599
cache TDS command before overwriting it
xiangyushawn c942b5c
clean code
xiangyushawn 0503347
added tests
xiangyushawn 798021a
print out expected value and actual value
xiangyushawn 207cf4b
use calendar to specify time zone
xiangyushawn f998e19
use Utils.dropTableIfExists to drop source & dest tables
xiangyushawn 253b852
add test with multiple prepared statements and result sets
xiangyushawn 7b2a280
send TVP row by row to bypass MARS
xiangyushawn f432926
fix assertion errors
xiangyushawn 34cad3a
added tests for chars longer than 5000
xiangyushawn 4a3435c
fix issue with forward only cursor regarding to reading data from Res…
xiangyushawn 9d270e3
added tests to test long characters and cursor is a combination of Re…
xiangyushawn a6d29b1
fix log
xiangyushawn ed9dce6
after sending a row, throw exception in case of errors
xiangyushawn bd872d0
fixed assertion error and added tests for invalid SP name and invalid…
xiangyushawn 98be439
update cached variables in synchronized way
xiangyushawn c23e499
use setters and getters
xiangyushawn 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
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
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
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
177 changes: 177 additions & 0 deletions
177
src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPResultSetCursorTest.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,177 @@ | ||
/* | ||
* 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.tvp; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.math.BigDecimal; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.sql.Timestamp; | ||
import java.util.Properties; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
|
||
import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement; | ||
import com.microsoft.sqlserver.testframework.AbstractTest; | ||
|
||
@RunWith(JUnitPlatform.class) | ||
public class TVPResultSetCursorTest extends AbstractTest { | ||
|
||
private static Connection conn = null; | ||
static Statement stmt = null; | ||
|
||
static BigDecimal[] expectedBigDecimals = {new BigDecimal("12345.12345"), new BigDecimal("125.123"), new BigDecimal("45.12345")}; | ||
static String[] expectedBigDecimalStrings = {"12345.12345", "125.12300", "45.12345"}; | ||
|
||
static String[] expectedStrings = {"hello", "world", "!!!"}; | ||
|
||
static Timestamp[] expectedTimestamps = {new Timestamp(1433338533461L), new Timestamp(14917485583999L), new Timestamp(1491123533000L)}; | ||
static String[] expectedTimestampStrings = {"2015-06-03 06:35:33.4610000", "2442-09-18 18:59:43.9990000", "2017-04-02 01:58:53.0000000"}; | ||
|
||
private static String tvpName = "TVPResultSetCursorTest_TVP"; | ||
private static String srcTable = "TVPResultSetCursorTest_SourceTable"; | ||
private static String desTable = "TVPResultSetCursorTest_DestinationTable"; | ||
|
||
/** | ||
* Test a previous failure when using server cursor and using the same connection to create TVP and result set. | ||
* | ||
* @throws SQLException | ||
*/ | ||
@Test | ||
public void testServerCursors() throws SQLException { | ||
conn = DriverManager.getConnection(connectionString); | ||
stmt = conn.createStatement(); | ||
|
||
dropTVPS(); | ||
dropTables(); | ||
|
||
createTVPS(); | ||
createTables(); | ||
|
||
populateSourceTable(); | ||
|
||
ResultSet rs = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE).executeQuery("select * from " + srcTable); | ||
|
||
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) conn.prepareStatement("INSERT INTO " + desTable + " select * from ? ;"); | ||
pstmt.setStructured(1, tvpName, rs); | ||
pstmt.execute(); | ||
|
||
verifyDestinationTableData(); | ||
|
||
if (null != pstmt) { | ||
pstmt.close(); | ||
} | ||
if (null != rs) { | ||
rs.close(); | ||
} | ||
} | ||
|
||
/** | ||
* Test a previous failure when setting SelectMethod to cursor and using the same connection to create TVP and result set. | ||
* | ||
* @throws SQLException | ||
*/ | ||
@Test | ||
public void testSelectMethodSetToCursor() throws SQLException { | ||
Properties info = new Properties(); | ||
info.setProperty("SelectMethod", "cursor"); | ||
conn = DriverManager.getConnection(connectionString, info); | ||
|
||
stmt = conn.createStatement(); | ||
|
||
dropTVPS(); | ||
dropTables(); | ||
|
||
createTVPS(); | ||
createTables(); | ||
|
||
populateSourceTable(); | ||
|
||
ResultSet rs = conn.createStatement().executeQuery("select * from " + srcTable); | ||
|
||
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) conn.prepareStatement("INSERT INTO " + desTable + " select * from ? ;"); | ||
pstmt.setStructured(1, tvpName, rs); | ||
pstmt.execute(); | ||
|
||
verifyDestinationTableData(); | ||
|
||
if (null != pstmt) { | ||
pstmt.close(); | ||
} | ||
if (null != rs) { | ||
rs.close(); | ||
} | ||
} | ||
|
||
private static void verifyDestinationTableData() throws SQLException { | ||
ResultSet rs = conn.createStatement().executeQuery("select * from " + desTable); | ||
|
||
int i = 0; | ||
while (rs.next()) { | ||
assertTrue(rs.getString(1).equals(expectedBigDecimalStrings[i])); | ||
assertTrue(rs.getString(2).trim().equals(expectedStrings[i])); | ||
assertTrue(rs.getString(3).equals(expectedTimestampStrings[i])); | ||
i++; | ||
} | ||
|
||
assertTrue(i == expectedBigDecimals.length); | ||
} | ||
|
||
private static void populateSourceTable() throws SQLException { | ||
String sql = "insert into " + srcTable + " values (?,?,?)"; | ||
|
||
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) conn.prepareStatement(sql); | ||
|
||
for (int i = 0; i < expectedBigDecimals.length; i++) { | ||
pstmt.setBigDecimal(1, expectedBigDecimals[i]); | ||
pstmt.setString(2, expectedStrings[i]); | ||
pstmt.setTimestamp(3, expectedTimestamps[i]); | ||
pstmt.execute(); | ||
} | ||
} | ||
|
||
private static void dropTables() throws SQLException { | ||
stmt.executeUpdate("if object_id('" + srcTable + "','U') is not null" + " drop table " + srcTable); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use |
||
stmt.executeUpdate("if object_id('" + desTable + "','U') is not null" + " drop table " + desTable); | ||
} | ||
|
||
private static void createTables() throws SQLException { | ||
String sql = "create table " + srcTable + " (c1 decimal(10,5) null, c2 nchar(50) null, c3 datetime2(7) null);"; | ||
stmt.execute(sql); | ||
|
||
sql = "create table " + desTable + " (c1 decimal(10,5) null, c2 nchar(50) null, c3 datetime2(7) null);"; | ||
stmt.execute(sql); | ||
} | ||
|
||
private static void createTVPS() throws SQLException { | ||
String TVPCreateCmd = "CREATE TYPE " + tvpName + " as table (c1 decimal(10,5) null, c2 nchar(50) null, c3 datetime2(7) null)"; | ||
stmt.executeUpdate(TVPCreateCmd); | ||
} | ||
|
||
private static void dropTVPS() throws SQLException { | ||
stmt.executeUpdate("IF EXISTS (SELECT * FROM sys.types WHERE is_table_type = 1 AND name = '" + tvpName + "') " + " drop type " + tvpName); | ||
} | ||
|
||
@AfterEach | ||
private void terminateVariation() throws SQLException { | ||
if (null != conn) { | ||
conn.close(); | ||
} | ||
if (null != stmt) { | ||
stmt.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.
Can you add one test case for 3 result sets (2 TVPType and one Non-TVP) & 2 / 3 statements for these resultsets?
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.
done