Skip to content

Commit

Permalink
0001666: SQL Server bulk loader does not handle comma
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Apr 11, 2014
1 parent 8cb95fe commit 683d643
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
Expand Up @@ -138,6 +138,15 @@ public void testInsertWithCommas() {
}
}

@Test
public void testInsertWithNonEscaped() {
if (shouldTestRun(platform)) {
String[] values = { getNextId(), null, "\n\0\r\t\f\'\"", null, "\n\0\r\t\f\'\"",
"2007-01-02 00:00:00.000", "2007-02-03 04:05:06.000", "1", "47", "67.89", "-0.0747663", encode("\n\0\r\t\f\'\"") };
insertAndVerify(values);
}
}

@Test
public void testInsertWithSpecialEscape() {
if (shouldTestRun(platform)) {
Expand Down
Expand Up @@ -34,8 +34,7 @@ public void setupTest() {
}

protected boolean shouldTestRun(IDatabasePlatform platform) {
return false;
//return platform != null && platform instanceof MsSqlDatabasePlatform;
return platform != null && platform instanceof MsSqlDatabasePlatform;
}

protected long writeData(List<CsvData> data) {
Expand Down
@@ -1,5 +1,6 @@
package org.jumpmind.symmetric.io.data.writer;

import java.io.OutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
Expand All @@ -11,9 +12,7 @@
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.sql.JdbcSqlTransaction;
import org.jumpmind.db.util.BinaryEncoding;
import org.jumpmind.symmetric.csv.CsvWriter;
import org.jumpmind.symmetric.io.data.CsvData;
import org.jumpmind.symmetric.io.data.CsvUtils;
import org.jumpmind.symmetric.io.data.DataEventType;
import org.jumpmind.symmetric.io.stage.IStagedResource;
import org.jumpmind.symmetric.io.stage.IStagingManager;
Expand Down Expand Up @@ -94,10 +93,18 @@ public void write(CsvData data) {
}
}
}
String formattedData = CsvUtils.escapeCsvData(parsedData, '\0', '\0', CsvWriter.ESCAPE_MODE_DOUBLED);
this.stagedInputFile.getOutputStream().write(formattedData.getBytes());
this.stagedInputFile.getOutputStream().write('\r');
this.stagedInputFile.getOutputStream().write('\n');

OutputStream out = this.stagedInputFile.getOutputStream();
for (int i = 0; i < parsedData.length; i++) {
if (parsedData[i] != null) {
out.write(parsedData[i].getBytes());
}
if (i + 1 < parsedData.length) {
out.write("||".getBytes());
}
}
out.write('\r');
out.write('\n');
loadedRows++;
} catch (Exception ex) {
throw getPlatform().getSqlTemplate().translate(ex);
Expand Down Expand Up @@ -128,7 +135,7 @@ protected void flush() {
String sql = String.format("BULK INSERT " +
this.getTargetTable().getFullyQualifiedTableName() +
" FROM '" + stagedInputFile.getFile().getAbsolutePath()) + "'" +
" WITH ( FIELDTERMINATOR=',', KEEPIDENTITY " + (fireTriggers ? ", FIRE_TRIGGERS" : "") + ");";
" WITH ( FIELDTERMINATOR='||', KEEPIDENTITY " + (fireTriggers ? ", FIRE_TRIGGERS" : "") + ");";
Statement stmt = c.createStatement();

//TODO: clean this up, deal with errors, etc.?
Expand Down

0 comments on commit 683d643

Please sign in to comment.