Skip to content

Commit

Permalink
Merge branch 'user/divang/json-datatype-support' of https://github.co…
Browse files Browse the repository at this point in the history
…m/microsoft/mssql-jdbc into user/divang/json-datatype-support
  • Loading branch information
Divang Sharma committed Feb 17, 2025
2 parents 6fe9854 + b34bc80 commit 46b87a1
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -605,11 +605,19 @@ public void setEscapeColumnDelimitersCSV(boolean escapeDelimiters) {
this.escapeDelimiters = escapeDelimiters;
}

private static boolean isJson(String token) {
return token.startsWith("{") && token.endsWith("}");
}

private static String[] escapeQuotesRFC4180(String[] tokens) throws SQLServerException {
if (null == tokens) {
return tokens;
}
for (int i = 0; i < tokens.length; i++) {
if (isJson(tokens[i])) {
continue; // Skip JSON strings
}

boolean escaped = false;
int j = 0;
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.lang.StackOverflowError;
import java.net.URL;
import java.sql.Connection;
import java.sql.JDBCType;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
Expand Down Expand Up @@ -64,7 +65,9 @@
public class BulkCopyCSVTest extends AbstractTest {

static String inputFile = "BulkCopyCSVTestInput.csv";
static String jsonInputFile = "BulkCopyCSVTestInputWithJson.csv";
static String inputFileNoColumnName = "BulkCopyCSVTestInputNoColumnName.csv";
static String jsonInputFileNoColumnName = "BulkCopyCSVTestInputNoColumnNameWithJson.csv";
static String inputFileDelimiterEscape = "BulkCopyCSVTestInputDelimiterEscape.csv";
static String inputFileDelimiterEscapeNoNewLineAtEnd = "BulkCopyCSVTestInputDelimiterEscapeNoNewLineAtEnd.csv";
static String inputFileMultipleDoubleQuotes = "BulkCopyCSVTestInputMultipleDoubleQuotes.csv";
Expand Down Expand Up @@ -93,13 +96,13 @@ public static void setUpConnection() throws Exception {
@Test
@DisplayName("Test SQLServerBulkCSVFileRecord")
public void testCSV() {
String fileName = filePath + inputFile;
String fileName = filePath + jsonInputFile;
try (SQLServerBulkCSVFileRecord f1 = new SQLServerBulkCSVFileRecord(fileName, encoding, delimiter, true);
SQLServerBulkCSVFileRecord f2 = new SQLServerBulkCSVFileRecord(fileName, encoding, delimiter, true);) {
testBulkCopyCSV(f1, true);
testBulkCopyCSV(f1, true, jsonInputFile, jsonInputFileNoColumnName);

f2.setEscapeColumnDelimitersCSV(true);
testBulkCopyCSV(f2, true);
testBulkCopyCSV(f2, true, jsonInputFile, jsonInputFileNoColumnName);
} catch (SQLException e) {
fail(e.getMessage());
}
Expand All @@ -111,13 +114,13 @@ public void testCSV() {
@Test
@DisplayName("Test SQLServerBulkCSVFileRecord First line not being column name")
public void testCSVFirstLineNotColumnName() {
String fileName = filePath + inputFileNoColumnName;
String fileName = filePath + jsonInputFileNoColumnName;
try (SQLServerBulkCSVFileRecord f1 = new SQLServerBulkCSVFileRecord(fileName, encoding, delimiter, false);
SQLServerBulkCSVFileRecord f2 = new SQLServerBulkCSVFileRecord(fileName, encoding, delimiter, false)) {
testBulkCopyCSV(f1, false);
testBulkCopyCSV(f1, false, jsonInputFile, jsonInputFileNoColumnName);

f2.setEscapeColumnDelimitersCSV(true);
testBulkCopyCSV(f2, false);
testBulkCopyCSV(f2, false, jsonInputFile, jsonInputFileNoColumnName);
} catch (SQLException e) {
fail(e.getMessage());
}
Expand All @@ -136,7 +139,7 @@ public void testCSVFromURL() throws SQLException {
.openStream();
SQLServerBulkCSVFileRecord fileRecord = new SQLServerBulkCSVFileRecord(csvFileInputStream, encoding,
delimiter, true)) {
testBulkCopyCSV(fileRecord, true);
testBulkCopyCSV(fileRecord, true, inputFile, inputFileNoColumnName);
} catch (Exception e) {
fail(e.getMessage());
}
Expand Down Expand Up @@ -269,13 +272,13 @@ public void testEscapeColumnDelimitersCSVNoNewLineAtEnd() throws Exception {
@Test
@DisplayName("Test SQLServerBulkCSVFileRecord GitHb 1391")
public void testCSV1391() {
String fileName = filePath + inputFile;
String fileName = filePath + jsonInputFile;
try (SQLServerBulkCSVFileRecord f1 = new BulkData1391(fileName, encoding, delimiter, true);
SQLServerBulkCSVFileRecord f2 = new BulkData1391(fileName, encoding, delimiter, true);) {
testBulkCopyCSV(f1, true);
testBulkCopyCSV(f1, true, jsonInputFile, jsonInputFileNoColumnName);

f2.setEscapeColumnDelimitersCSV(true);
testBulkCopyCSV(f2, true);
testBulkCopyCSV(f2, true, jsonInputFile, jsonInputFileNoColumnName);
} catch (SQLException e) {
fail(e.getMessage());
}
Expand All @@ -299,10 +302,10 @@ public Set<Integer> getColumnOrdinals() {
}
}

private void testBulkCopyCSV(SQLServerBulkCSVFileRecord fileRecord, boolean firstLineIsColumnNames) {
private void testBulkCopyCSV(SQLServerBulkCSVFileRecord fileRecord, boolean firstLineIsColumnNames, String csvFileName, String csvInputFileNoColumnName) {
DBTable destTable = null;
try (BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(filePath + inputFile), encoding))) {
new InputStreamReader(new FileInputStream(filePath + csvFileName), encoding))) {
// read the first line from csv and parse it to get datatypes to create destination column
String[] columnTypes = br.readLine().substring(1)/* Skip the Byte order mark */.split(delimiter, -1);
br.close();
Expand Down Expand Up @@ -340,18 +343,18 @@ private void testBulkCopyCSV(SQLServerBulkCSVFileRecord fileRecord, boolean firs
} else {
sqlType = SqlTypeMapping.valueOf(columnType.toUpperCase()).sqlType;
}

int vendorTypeNumber = sqlType.getJdbctype() != JDBCType.NULL ? sqlType.getJdbctype().getVendorTypeNumber() : sqlType.getVendorTypeNumber();
destTable.addColumn(sqlType);
fileRecord.addColumnMetadata(i + 1, "", sqlType.getJdbctype().getVendorTypeNumber(),
fileRecord.addColumnMetadata(i + 1, "", vendorTypeNumber,
(-1 == precision) ? 0 : precision, (-1 == scale) ? 0 : scale);
}
stmt.createTable(destTable);
bulkCopy.writeToServer(fileRecord);
}
if (firstLineIsColumnNames)
validateValuesFromCSV(destTable, inputFile);
validateValuesFromCSV(destTable, csvFileName);
else
validateValuesFromCSV(destTable, inputFileNoColumnName);
validateValuesFromCSV(destTable, csvInputFileNoColumnName);

} catch (Exception e) {
fail(e.getMessage());
Expand All @@ -370,7 +373,7 @@ private void testBulkCopyCSV(SQLServerBulkCSVFileRecord fileRecord, boolean firs
static void validateValuesFromCSV(DBTable destinationTable, String inputFile) {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(filePath + inputFile), encoding))) {
if (inputFile.equalsIgnoreCase("BulkCopyCSVTestInput.csv"))
if (inputFile.equalsIgnoreCase("BulkCopyCSVTestInput.csv") || inputFile.equalsIgnoreCase("BulkCopyCSVTestInputWithJson.csv"))
br.readLine(); // skip first line as it is header

try (DBResultSet dstResultSet = stmt
Expand Down
Loading

0 comments on commit 46b87a1

Please sign in to comment.