Skip to content

Commit c3343ce

Browse files
authored
Fix table name parsing issue for batch inserts (#2290)
1 parent 92cfe0d commit c3343ce

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2694,10 +2694,10 @@ private String parseUserSQLForTableNameDW(boolean hasInsertBeenFound, boolean ha
26942694

26952695
// At this point, the next chunk of string is the table name, without starting with [ or ".
26962696
while (localUserSQL.length() > 0) {
2697-
// Keep going until the end of the table name is signalled - either a ., whitespace, ; or comment is
2697+
// Keep going until the end of the table name is signalled - either a ., whitespace, bracket ; or comment is
26982698
// encountered.
2699-
if (localUserSQL.charAt(0) == '.' || Character.isWhitespace(localUserSQL.charAt(0))
2700-
|| checkAndRemoveCommentsAndSpace(false)) {
2699+
if (localUserSQL.charAt(0) == '.' || localUserSQL.charAt(0) == '('
2700+
|| Character.isWhitespace(localUserSQL.charAt(0)) || checkAndRemoveCommentsAndSpace(false)) {
27012701
return sb.toString() + parseUserSQLForTableNameDW(true, true, true, false);
27022702
} else if (localUserSQL.charAt(0) == ';') {
27032703
throw new IllegalArgumentException(SQLServerException.getErrString("R_endOfQueryDetected"));

src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithBulkCopyTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,44 @@ public void testComputedCols() throws Exception {
801801
}
802802
}
803803

804+
/**
805+
* Test bulk insert with no space after table name
806+
*
807+
* @throws Exception
808+
*/
809+
@Test
810+
public void testNoSpaceInsert() throws Exception {
811+
// table name with valid alphanumeric chars that don't need to be escaped, since escaping the table name would not test the space issue
812+
String testNoSpaceInsertTableName = "testNoSpaceInsertTable" + RandomData.generateInt(false);
813+
String valid = "insert into " + testNoSpaceInsertTableName + "(id, json)" + " values(?, ?)";
814+
815+
try (Connection connection = PrepUtil.getConnection(connectionString + ";useBulkCopyForBatchInsert=true;");
816+
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection.prepareStatement(valid);
817+
Statement stmt = (SQLServerStatement) connection.createStatement();) {
818+
Field f1 = SQLServerConnection.class.getDeclaredField("isAzureDW");
819+
f1.setAccessible(true);
820+
f1.set(connection, true);
821+
822+
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableNameBulkComputedCols), stmt);
823+
String createTable = "create table " + testNoSpaceInsertTableName
824+
+ " (id nvarchar(100) not null, json nvarchar(max) not null,"
825+
+ " vcol1 as json_value([json], '$.vcol1'), vcol2 as json_value([json], '$.vcol2'))";
826+
stmt.execute(createTable);
827+
828+
String jsonValue = "{\"vcol1\":\"" + UUID.randomUUID().toString() + "\",\"vcol2\":\""
829+
+ UUID.randomUUID().toString() + "\" }";
830+
String idValue = UUID.randomUUID().toString();
831+
pstmt.setString(1, idValue);
832+
pstmt.setString(2, jsonValue);
833+
pstmt.addBatch();
834+
pstmt.executeBatch();
835+
} finally {
836+
try (Statement stmt = connection.createStatement()) {
837+
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(testNoSpaceInsertTableName), stmt);
838+
}
839+
}
840+
}
841+
804842
@BeforeAll
805843
public static void setupTests() throws Exception {
806844
setConnection();

0 commit comments

Comments
 (0)