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

Tvp server cursor fix #234

Merged
merged 20 commits into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
39 changes: 38 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4535,8 +4535,33 @@ void writeTVP(TVP value) throws SQLServerException {
void writeTVPRows(TVP value) throws SQLServerException {
boolean isShortValue, isNull;
int dataLength;


boolean tdsWritterCached = false;
ByteBuffer cachedStagingBuffer = null;
TDSCommand cachedCommand = null;

if (!value.isNull()) {

// If TVP is set with ResultSet and Server Cursor is used, the tdsWriter of the calling preparedStatement is overwritten
// by the SQLServerResultSet#next() method if the preparedStatement and the ResultSet are created by the same connection.
// Therefore, we need to cache the tdsWriter's values (stagingBuffer for sending data and command for retrieving data) and update
// stagingBuffer with new TDS values.
if (TVPType.ResultSet == value.tvpType) {
if ((null != value.sourceResultSet) && (value.sourceResultSet instanceof SQLServerResultSet)) {
SQLServerStatement src_stmt = (SQLServerStatement) ((SQLServerResultSet) value.sourceResultSet).getStatement();
int resultSetServerCursorId = ((SQLServerResultSet) value.sourceResultSet).getServerCursorId();

if (con.equals(src_stmt.getConnection()) && 0 != resultSetServerCursorId) {
cachedStagingBuffer = ByteBuffer.allocate(stagingBuffer.capacity()).order(stagingBuffer.order());
cachedStagingBuffer.put(stagingBuffer.array(), 0, stagingBuffer.position());

cachedCommand = this.command;

tdsWritterCached = true;
}
}
}

Map<Integer, SQLServerMetaData> columnMetadata = value.getColumnMetadata();
Iterator<Entry<Integer, SQLServerMetaData>> columnsIterator;

Expand Down Expand Up @@ -4749,8 +4774,20 @@ else if (DataTypes.UNKNOWN_STREAM_LENGTH == dataLength)
}
currentColumn++;
}

if (tdsWritterCached) {
cachedStagingBuffer.put(stagingBuffer.array(), 0, stagingBuffer.position());
stagingBuffer.clear();
}
}
}

if (tdsWritterCached) {
stagingBuffer.clear();
stagingBuffer.put(cachedStagingBuffer.array(), 0, cachedStagingBuffer.position());
this.command = cachedCommand;
}

// TVP_END_TOKEN
writeByte((byte) 0x00);
}
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/com/microsoft/sqlserver/jdbc/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,16 +331,6 @@ else if (value instanceof SQLServerDataTable) {
tvpValue = new TVP(tvpName, (SQLServerDataTable) value);
}
else if (value instanceof ResultSet) {
// if ResultSet and PreparedStatemet/CallableStatement are created from same connection object
// with property SelectMethod=cursor, TVP is not supported
if (con.getSelectMethod().equalsIgnoreCase("cursor") && (value instanceof SQLServerResultSet)) {
SQLServerStatement stmt = (SQLServerStatement) ((SQLServerResultSet) value).getStatement();

if (con.equals(stmt.connection)) {
throw new SQLServerException(SQLServerException.getErrString("R_invalidServerCursorForTVP"), null);
}
}

tvpValue = new TVP(tvpName, (ResultSet) value);
}
else if (value instanceof ISQLServerDataRecord) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ protected Object[][] getContents() {
{"R_invalidKeyStoreFile", "Cannot parse \"{0}\". Either the file format is not valid or the password is not correct."}, // for JKS/PKCS
{"R_invalidCEKCacheTtl", "Invalid column encryption key cache time-to-live specified. The columnEncryptionKeyCacheTtl value cannot be negative and timeUnit can only be DAYS, HOURS, MINUTES or SECONDS."},
{"R_sendTimeAsDateTimeForAE", "Use sendTimeAsDateTime=false with Always Encrypted."},
{"R_invalidServerCursorForTVP" , "Use different Connection for source ResultSet and prepared query, if selectMethod is set to cursor for Table-Valued Parameter."},
{"R_TVPnotWorkWithSetObjectResultSet" , "setObject() with ResultSet is not supported for Table-Valued Parameter. Please use setStructured()"},
{"R_invalidQueryTimeout", "The queryTimeout {0} is not valid."},
{"R_invalidSocketTimeout", "The socketTimeout {0} is not valid."},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ String getClassNameLogging() {
private boolean isClosed = false;

private final int serverCursorId;

int getServerCursorId() {
return serverCursorId;
}

/** the intended fetch direction to optimize cursor performance */
private int fetchDirection;
Expand Down
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);
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use Utils.dropTableIfExists to drop source & dest tables ?

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();
}
}

}