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 | SQLServerBulkCSVFileRecord dropping empty trailing columns when escapeDelimiters = true #1438

Merged
merged 3 commits into from
Oct 9, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,8 @@ private void initFileReader(InputStreamReader sr, String encoding, String demlim
if (firstLineIsColumnNames) {
currentLine = fileReader.readLine();
if (null != currentLine) {
columnNames = (escapeDelimiters && currentLine.contains("\""))
? escapeQuotesRFC4180(currentLine.split(
delimiter + escapeSplitPattern))
: currentLine.split(delimiter, -1);
columnNames = (escapeDelimiters && currentLine.contains("\"")) ? escapeQuotesRFC4180(
currentLine.split(delimiter + escapeSplitPattern, -1)) : currentLine.split(delimiter, -1);
}
}
}
Expand Down Expand Up @@ -241,10 +239,8 @@ public Object[] getRowData() throws SQLServerException {
* Binary data may be corrupted The limit in split() function should be a negative value, otherwise trailing
* empty strings are discarded. Empty string is returned if there is no value.
*/
String[] data = (escapeDelimiters && currentLine.contains("\""))
? escapeQuotesRFC4180(currentLine.split(
delimiter + escapeSplitPattern))
: currentLine.split(delimiter, -1);
String[] data = (escapeDelimiters && currentLine.contains("\"")) ? escapeQuotesRFC4180(
currentLine.split(delimiter + escapeSplitPattern, -1)) : currentLine.split(delimiter, -1);

// Cannot go directly from String[] to Object[] and expect it to act as an array.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,21 @@ public void testEscapeColumnDelimitersCSV() throws Exception {
String tableName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("BulkEscape"));
String fileName = filePath + inputFileDelimiterEscape;
/*
* The list below is the copy of inputFileDelimiterEscape with quotes removed.
* The list below is the copy of inputFileDelimiterEsc ape with quotes removed.
*/
String[][] expectedEscaped = new String[11][4];
expectedEscaped[0] = new String[] {"test", " test\"", "no@split", " testNoQuote"};
expectedEscaped[1] = new String[] {null, null, null, null};
expectedEscaped[2] = new String[] {"\"", "test\"test", "test@\" test", null};
expectedEscaped[0] = new String[] {"test", " test\"", "no@split", " testNoQuote", ""};
expectedEscaped[1] = new String[] {null, null, null, null, ""};
expectedEscaped[2] = new String[] {"\"", "test\"test", "test@\" test", null, ""};
expectedEscaped[3] = new String[] {"testNoQuote ", " testSpaceAround ", " testSpaceInside ",
" testSpaceQuote\" "};
expectedEscaped[4] = new String[] {null, null, null, " testSpaceInside "};
expectedEscaped[5] = new String[] {"1997", "Ford", "E350", "E63"};
expectedEscaped[6] = new String[] {"1997", "Ford", "E350", "E63"};
expectedEscaped[7] = new String[] {"1997", "Ford", "E350", "Super@ luxurious truck"};
expectedEscaped[8] = new String[] {"1997", "Ford", "E350", "Super@ \"luxurious\" truck"};
expectedEscaped[9] = new String[] {"1997", "Ford", "E350", "E63"};
expectedEscaped[10] = new String[] {"1997", "Ford", "E350", " Super luxurious truck "};
" testSpaceQuote\" ", ""};
expectedEscaped[4] = new String[] {null, null, null, " testSpaceInside ", ""};
expectedEscaped[5] = new String[] {"1997", "Ford", "E350", "E63", ""};
expectedEscaped[6] = new String[] {"1997", "Ford", "E350", "E63", ""};
expectedEscaped[7] = new String[] {"1997", "Ford", "E350", "Super@ luxurious truck", ""};
expectedEscaped[8] = new String[] {"1997", "Ford", "E350", "Super@ \"luxurious\" truck", ""};
expectedEscaped[9] = new String[] {"1997", "Ford", "E350", "E63", ""};
expectedEscaped[10] = new String[] {"1997", "Ford", "E350", " Super luxurious truck ", ""};

try (Connection con = getConnection(); Statement stmt = con.createStatement();
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(con);
Expand All @@ -176,8 +176,9 @@ public void testEscapeColumnDelimitersCSV() throws Exception {
fileRecord.addColumnMetadata(3, null, java.sql.Types.VARCHAR, 50, 0);
fileRecord.addColumnMetadata(4, null, java.sql.Types.VARCHAR, 50, 0);
fileRecord.addColumnMetadata(5, null, java.sql.Types.VARCHAR, 50, 0);
fileRecord.addColumnMetadata(6, null, java.sql.Types.VARCHAR, 50, 0);
stmt.executeUpdate("CREATE TABLE " + tableName
+ " (id INT IDENTITY(1,1), c1 VARCHAR(50), c2 VARCHAR(50), c3 VARCHAR(50), c4 VARCHAR(50))");
+ " (id INT IDENTITY(1,1), c1 VARCHAR(50), c2 VARCHAR(50), c3 VARCHAR(50), c4 VARCHAR(50), c5 VARCHAR(50))");
bulkCopy.writeToServer(fileRecord);

int i = 0;
Expand All @@ -193,10 +194,10 @@ public void testEscapeColumnDelimitersCSV() throws Exception {
}
}
}

/**
* test simple csv file for bulkcopy, for GitHub issue 1391
* Tests to ensure that the set returned by getColumnOrdinals doesn't have to be ordered
* test simple csv file for bulkcopy, for GitHub issue 1391 Tests to ensure that the set returned by
* getColumnOrdinals doesn't have to be ordered
*/
@Test
@DisplayName("Test SQLServerBulkCSVFileRecord GitHb 1391")
Expand All @@ -212,7 +213,7 @@ public void testCSV1391() {
fail(e.getMessage());
}
}

// Used for testing issue reported in https://github.com/microsoft/mssql-jdbc/issues/1391
private class BulkData1391 extends SQLServerBulkCSVFileRecord {

Expand Down
22 changes: 11 additions & 11 deletions src/test/resources/BulkCopyCSVTestInputDelimiterEscape.csv
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
1@"test"@ " test"""@ "no@split" @ testNoQuote
2@""@ ""@ ""@ ""
3@""""@ "test""test"@ "test@"" test"@ ""
4@testNoQuote @ testSpaceAround @ " testSpaceInside "@ " testSpaceQuote"" "
5@""@ ""@ ""@ " testSpaceInside "
6@1997@Ford@E350@E63
7@"1997"@"Ford"@"E350"@"E63"
8@1997@Ford@E350@"Super@ luxurious truck"
9@1997@Ford@E350@"Super@ ""luxurious"" truck"
10@1997@ "Ford" @E350@ "E63"
11@1997@Ford@E350@" Super luxurious truck "
1@"test"@ " test"""@ "no@split" @ testNoQuote@
2@""@ ""@ ""@ ""@
3@""""@ "test""test"@ "test@"" test"@ ""@
4@testNoQuote @ testSpaceAround @ " testSpaceInside "@ " testSpaceQuote"" "@
5@""@ ""@ ""@ " testSpaceInside "@
6@1997@Ford@E350@E63@
7@"1997"@"Ford"@"E350"@"E63"@
8@1997@Ford@E350@"Super@ luxurious truck"@
9@1997@Ford@E350@"Super@ ""luxurious"" truck"@
10@1997@ "Ford" @E350@ "E63"@
11@1997@Ford@E350@" Super luxurious truck "@