Merge pull request #1864 from OpenRefine/issue/1802

fix issue #1802 -- escape single quote in SQL export
This commit is contained in:
Antonin Delpeuch 2018-11-27 19:34:50 +09:00 committed by GitHub
commit d5ef0dfbc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View File

@ -121,7 +121,7 @@ public class SqlInsertBuilder {
handleNullField(allowNullChkBox, defaultValue, nullValueNull, val.getColumnName(), rowValue, true);
}else {
rowValue.append("'" + val.getText() + "'");
rowValue.append("'" + val.getText().replace("'","''") + "'");
}

View File

@ -359,6 +359,32 @@ public class SqlExporterTests extends RefineTest {
Assert.assertEquals(countNull, noOfCols);
}
@Test
public void testExportSqlWithSingleQuote(){
int noOfCols = 4;
int noOfRows = 1;
createGridWithSingleQuote(noOfRows, noOfCols);
String tableName = "sql_table_test";
JSONObject optionsJson = createOptionsFromProject(tableName, null, null, null, false);
optionsJson.put("includeStructure", true);
optionsJson.put("includeDropStatement", true);
optionsJson.put("convertNulltoEmptyString", true);
when(options.getProperty("options")).thenReturn(optionsJson.toString());
try {
SUT.export(project, options, engine, writer);
} catch (IOException e) {
Assert.fail();
}
String result = writer.toString();
logger.info("\nresult:={} ", result);
Assert.assertTrue(result.contains("INSERT INTO sql_table_test (column0,column1,column2,column3) VALUES \n" +
"( 'It''s row0cell0','It''s row0cell1','It''s row0cell2','It''s row0cell3' )"));
}
//helper methods
@ -399,6 +425,18 @@ public class SqlExporterTests extends RefineTest {
}
}
protected void createGridWithSingleQuote(int noOfRows, int noOfColumns){
createColumns(noOfColumns);
for(int i = 0; i < noOfRows; i++){
Row row = new Row(noOfColumns);
for(int j = 0; j < noOfColumns; j++){
row.cells.add(new Cell("It's row" + i + "cell" + j, null));
}
project.rows.add(row);
}
}
protected void createGridWithNullFields(int noOfRows, int noOfColumns, int noOfNullFields){
createColumns(noOfColumns);
if(noOfNullFields > (noOfColumns * noOfRows)) {