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 | Fix bulkcopy failing when inserting non-unicode multibyte String #1421

Merged
merged 3 commits into from Sep 16, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -2289,15 +2289,16 @@ else if (null != sourceCryptoMeta) {
tdsWriter.writeShort((short) bytes.length);
tdsWriter.writeBytes(bytes);
} else {
tdsWriter.writeShort((short) (colValueStr.length()));
// converting string into destination collation using Charset

SQLCollation destCollation = destColumnMetadata.get(destColOrdinal).collation;
if (null != destCollation) {
tdsWriter.writeBytes(colValueStr.getBytes(
destColumnMetadata.get(destColOrdinal).collation.getCharset()));

if (null != destCollation) {
byte[] value = colValueStr.getBytes(
destColumnMetadata.get(destColOrdinal).collation.getCharset());
tdsWriter.writeShort((short) value.length);
tdsWriter.writeBytes(value);
} else {
tdsWriter.writeShort((short) (colValueStr.length()));
tdsWriter.writeBytes(colValueStr.getBytes());
}
}
Expand Down
Expand Up @@ -31,13 +31,15 @@


@RunWith(JUnitPlatform.class)
@Tag(Constants.xAzureSQLDW)
public class BulkCopyRowSetTest extends AbstractTest {

private static String tableName = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("BulkCopyFloatTest"));
private static String tableName2 = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("BulkCopyFloatTest2"));

@Test
@Tag(Constants.xAzureSQLDW)
public void testBulkCopyFloatRowSet() throws SQLException {
try (Connection con = getConnection(); Statement stmt = connection.createStatement()) {
RowSetFactory rsf = RowSetProvider.newFactory();
Expand Down Expand Up @@ -71,18 +73,50 @@ public void testBulkCopyFloatRowSet() throws SQLException {
}
}

@Test
public void testBulkCopyJapaneseCollation() throws SQLException {
try (Connection con = getConnection(); Statement stmt = connection.createStatement();
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(con);) {
RowSetFactory rsf = RowSetProvider.newFactory();
CachedRowSet crs = rsf.createCachedRowSet();
RowSetMetaData rsmd = new RowSetMetaDataImpl();
String unicodeData = "ああ";
rsmd.setColumnCount(1);
rsmd.setColumnName(1, "c1");
rsmd.setColumnType(1, java.sql.Types.VARCHAR);
rsmd.setTableName(1, tableName2);

crs.setMetaData(rsmd);
crs.moveToInsertRow();
crs.updateString("c1", unicodeData);
crs.insertRow();
crs.moveToCurrentRow();

bulkCopy.setDestinationTableName(tableName2);
bulkCopy.writeToServer(crs);
peterbae marked this conversation as resolved.
Show resolved Hide resolved

try (ResultSet rs = stmt.executeQuery("select * from " + tableName2)) {
rs.next();
assertEquals(unicodeData, (String) rs.getString(1));
}
}
}

@BeforeAll
public static void testSetup() throws TestAbortedException, Exception {
try (Statement stmt = connection.createStatement()) {
String sql1 = "create table " + tableName + " (c1 float, c2 real)";
stmt.execute(sql1);
String sql2 = "create table " + tableName2 + " (c1 varchar(10) COLLATE Japanese_CS_AS_KS_WS NOT NULL)";
stmt.execute(sql2);
}
}

@AfterAll
public static void terminateVariation() throws SQLException {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(tableName, stmt);
TestUtils.dropTableIfExists(tableName2, stmt);
}
}
}