Skip to content

Commit

Permalink
Fix | Fix bulkcopy failing when inserting non-unicode multibyte String (
Browse files Browse the repository at this point in the history
#1421)

Fix bulkcopy failing when inserting non-unicode multibyte String
  • Loading branch information
peterbae committed Sep 16, 2020
1 parent 37be51e commit bfa575d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
Expand Up @@ -2308,15 +2308,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);

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

0 comments on commit bfa575d

Please sign in to comment.