fix issue #1802 -- escape single quote in SQL export

This commit is contained in:
jackyq2015 2018-11-26 19:40:22 -05:00
parent c7cc84a282
commit 02871e1b6f
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

@ -360,6 +360,32 @@ public class SqlExporterTests extends RefineTest {
}
@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
public int countWordInString(String input, String word){
@ -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)) {