fixed issue 1684 with decimal values
This commit is contained in:
parent
d271b430ec
commit
c1af2f1179
@ -35,6 +35,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
@ -131,13 +133,23 @@ public class SqlInsertBuilder {
|
||||
|
||||
}else {//value not null
|
||||
|
||||
try {
|
||||
Integer.parseInt(val.getText());
|
||||
} catch (NumberFormatException nfe) {
|
||||
throw new SqlExporterException(
|
||||
val.getText() + " is not compatible with column type :" + type);
|
||||
if(type.equals(SqlData.SQL_TYPE_NUMERIC)) {//test if number is numeric (decimal number is valid)
|
||||
|
||||
if(!NumberUtils.isNumber(val.getText())){
|
||||
throw new SqlExporterException(
|
||||
val.getText() + " is not compatible with column type :" + type);
|
||||
}
|
||||
}else {
|
||||
|
||||
try { //number should be an integer
|
||||
Integer.parseInt(val.getText());
|
||||
} catch (NumberFormatException nfe) {
|
||||
throw new SqlExporterException(
|
||||
val.getText() + " is not compatible with column type :" + type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
rowValue.append(val.getText());
|
||||
|
||||
}
|
||||
@ -153,9 +165,7 @@ public class SqlInsertBuilder {
|
||||
rowValue.append(",");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
idx++;
|
||||
String rowValString = rowValue.toString();
|
||||
// logger.info("rowValString::" + rowValString);
|
||||
@ -249,5 +259,5 @@ public class SqlInsertBuilder {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -57,6 +57,7 @@ import org.testng.annotations.Test;
|
||||
import com.google.refine.ProjectManager;
|
||||
import com.google.refine.browsing.Engine;
|
||||
import com.google.refine.exporters.sql.SqlCreateBuilder;
|
||||
import com.google.refine.exporters.sql.SqlData;
|
||||
import com.google.refine.exporters.sql.SqlExporter;
|
||||
import com.google.refine.exporters.sql.SqlInsertBuilder;
|
||||
import com.google.refine.model.Cell;
|
||||
@ -116,6 +117,54 @@ public class SqlExporterTests extends RefineTest {
|
||||
sqlCreateBuilder = null;
|
||||
sqlInsertBuilder = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExportSqlWithNonZeroScaleNumericValue(){
|
||||
createNonZeroScaleNumericGrid(2, 2);
|
||||
String tableName = "sql_table_test";
|
||||
String optionsString = createOptionsFromProject(tableName, SqlData.SQL_TYPE_NUMERIC,null).toString();
|
||||
when(options.getProperty("options")).thenReturn(optionsString);
|
||||
|
||||
try {
|
||||
SUT.export(project, options, engine, writer);
|
||||
} catch (IOException e) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
String result = writer.toString();
|
||||
logger.info("result = \n" + result);
|
||||
Assert.assertNotNull(result);
|
||||
assertNotEquals(writer.toString(), SqlExporter.NO_OPTIONS_PRESENT_ERROR);
|
||||
boolean checkResult = result.contains("CREATE TABLE " + tableName);
|
||||
//logger.info("checkResult1 =" + checkResult);
|
||||
checkResult = result.contains("INSERT INTO " + tableName);
|
||||
// logger.info("checkResult2 =" + checkResult);
|
||||
Assert.assertEquals(checkResult, true);
|
||||
|
||||
}
|
||||
|
||||
private void createNonZeroScaleNumericGrid(int noOfRows, int noOfColumns) {
|
||||
createColumnsWithScaleEqualsTwo(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(generateRandomNumericValues() , null));
|
||||
}
|
||||
project.rows.add(row);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void createColumnsWithScaleEqualsTwo(int noOfColumns){
|
||||
for(int i = 0; i < noOfColumns; i++){
|
||||
try {
|
||||
project.columnModel.addColumn(i, new Column(i, "column" + i), true);
|
||||
} catch (ModelException e1) {
|
||||
Assert.fail("Could not create column");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExportSimpleSql(){
|
||||
@ -370,6 +419,45 @@ public class SqlExporterTests extends RefineTest {
|
||||
project.rows.add(row);
|
||||
}
|
||||
}
|
||||
|
||||
protected JSONObject createNumericColOptionsFromProject(String tableName, String type, String size) {
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
JSONArray columns = new JSONArray();
|
||||
json.put("columns", columns);
|
||||
json.put("tableName", tableName);
|
||||
|
||||
List<Column> cols = project.columnModel.columns;
|
||||
|
||||
cols.forEach(c -> {
|
||||
//logger.info("Column Name = " + c.getName());
|
||||
JSONObject columnModel = new JSONObject();
|
||||
columnModel.put("name", c.getName());
|
||||
if(type != null) {
|
||||
columnModel.put("type", type);
|
||||
}else {
|
||||
columnModel.put("type", "VARCHAR");
|
||||
}
|
||||
if(size != null) {
|
||||
columnModel.put("size", size);
|
||||
}else {
|
||||
columnModel.put("size", "100");
|
||||
}
|
||||
|
||||
if(type != null) {
|
||||
columnModel.put("type", type);
|
||||
}
|
||||
if(size != null) {
|
||||
// logger.info(" Size = " + size);
|
||||
columnModel.put("size", size);
|
||||
}
|
||||
|
||||
columns.put(columnModel);
|
||||
|
||||
});
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
protected JSONObject createOptionsFromProject(String tableName, String type, String size) {
|
||||
|
||||
@ -452,6 +540,13 @@ public class SqlExporterTests extends RefineTest {
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
double generateRandomNumericValues(){
|
||||
int precision = 100; //scale = 2
|
||||
double randomnum = Math.floor(Math.random() * (10 * precision - 1 * precision) + 1 * precision) / (1*precision);
|
||||
return randomnum;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user