Skip to content

Commit

Permalink
fix space issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgreenbird committed Jan 7, 2024
1 parent 9a3e471 commit 10858a3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 10858a3

Please sign in to comment.