diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java index 52e1d5424..f75f6e613 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java @@ -2694,10 +2694,10 @@ private String parseUserSQLForTableNameDW(boolean hasInsertBeenFound, boolean ha // At this point, the next chunk of string is the table name, without starting with [ or ". while (localUserSQL.length() > 0) { - // Keep going until the end of the table name is signalled - either a ., whitespace, ; or comment is + // Keep going until the end of the table name is signalled - either a ., whitespace, bracket ; or comment is // encountered. - if (localUserSQL.charAt(0) == '.' || Character.isWhitespace(localUserSQL.charAt(0)) - || checkAndRemoveCommentsAndSpace(false)) { + if (localUserSQL.charAt(0) == '.' || localUserSQL.charAt(0) == '(' + || Character.isWhitespace(localUserSQL.charAt(0)) || checkAndRemoveCommentsAndSpace(false)) { return sb.toString() + parseUserSQLForTableNameDW(true, true, true, false); } else if (localUserSQL.charAt(0) == ';') { throw new IllegalArgumentException(SQLServerException.getErrString("R_endOfQueryDetected")); diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithBulkCopyTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithBulkCopyTest.java index 17f46df23..d35ab21e8 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithBulkCopyTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithBulkCopyTest.java @@ -801,6 +801,45 @@ public void testComputedCols() throws Exception { } } + /** + * Test insert table with no space after table name + * + * @throws SQLException + * @throws ClassNotFoundException + */ + @Test + public void testNoSpaceInsert() throws Exception { + // table name with valid alphanumeric chars that don't need to be escaped, since escaping the table name would not test the space issue + String testNoSpaceInsertTableName = "testNoSpaceInsertTable" + RandomData.generateInt(false); + String valid = "insert into " + testNoSpaceInsertTableName + "(id, json)" + " values(?, ?)"; + + try (Connection connection = PrepUtil.getConnection(connectionString + ";useBulkCopyForBatchInsert=true;"); + SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection.prepareStatement(valid); + Statement stmt = (SQLServerStatement) connection.createStatement();) { + Field f1 = SQLServerConnection.class.getDeclaredField("isAzureDW"); + f1.setAccessible(true); + f1.set(connection, true); + + TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableNameBulkComputedCols), stmt); + String createTable = "create table " + testNoSpaceInsertTableName + + " (id nvarchar(100) not null, json nvarchar(max) not null," + + " vcol1 as json_value([json], '$.vcol1'), vcol2 as json_value([json], '$.vcol2'))"; + stmt.execute(createTable); + + String jsonValue = "{\"vcol1\":\"" + UUID.randomUUID().toString() + "\",\"vcol2\":\"" + + UUID.randomUUID().toString() + "\" }"; + String idValue = UUID.randomUUID().toString(); + pstmt.setString(1, idValue); + pstmt.setString(2, jsonValue); + pstmt.addBatch(); + pstmt.executeBatch(); + } finally { + try (Statement stmt = connection.createStatement()) { + TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(testNoSpaceInsertTableName), stmt); + } + } + } + @BeforeAll public static void setupTests() throws Exception { setConnection();