NIFI-1613

- Truncate text data types only.
- Added conversion from a boolean to number.
This commit is contained in:
Koji Kawamura 2017-07-10 16:52:13 +09:00 committed by Matt Burgess
parent 3844a821f1
commit 8acee02393
4 changed files with 45 additions and 1 deletions

View File

@ -368,6 +368,7 @@
<exclude>src/test/resources/TestConvertJSONToSQL/person-without-code.json</exclude> <exclude>src/test/resources/TestConvertJSONToSQL/person-without-code.json</exclude>
<exclude>src/test/resources/TestConvertJSONToSQL/person-with-null-code.json</exclude> <exclude>src/test/resources/TestConvertJSONToSQL/person-with-null-code.json</exclude>
<exclude>src/test/resources/TestConvertJSONToSQL/person-without-id.json</exclude> <exclude>src/test/resources/TestConvertJSONToSQL/person-without-id.json</exclude>
<exclude>src/test/resources/TestConvertJSONToSQL/person-with-bool.json</exclude>
<exclude>src/test/resources/TestModifyBytes/noFooter.txt</exclude> <exclude>src/test/resources/TestModifyBytes/noFooter.txt</exclude>
<exclude>src/test/resources/TestModifyBytes/noFooter_noHeader.txt</exclude> <exclude>src/test/resources/TestModifyBytes/noFooter_noHeader.txt</exclude>
<exclude>src/test/resources/TestModifyBytes/noHeader.txt</exclude> <exclude>src/test/resources/TestModifyBytes/noHeader.txt</exclude>

View File

@ -518,6 +518,7 @@ public class ConvertJSONToSQL extends AbstractProcessor {
break; break;
// Don't truncate numeric types. // Don't truncate numeric types.
case Types.BIT:
case Types.TINYINT: case Types.TINYINT:
case Types.SMALLINT: case Types.SMALLINT:
case Types.INTEGER: case Types.INTEGER:
@ -527,6 +528,10 @@ public class ConvertJSONToSQL extends AbstractProcessor {
case Types.DOUBLE: case Types.DOUBLE:
case Types.DECIMAL: case Types.DECIMAL:
case Types.NUMERIC: case Types.NUMERIC:
if (fieldNode.isBoolean()) {
// Convert boolean to number representation for databases those don't support boolean type.
fieldValue = fieldNode.asBoolean() ? "0" : "1";
}
break; break;
// Don't truncate DATE, TIME and TIMESTAMP types. We assume date and time is already correct in long representation. // Don't truncate DATE, TIME and TIMESTAMP types. We assume date and time is already correct in long representation.
@ -539,7 +544,13 @@ public class ConvertJSONToSQL extends AbstractProcessor {
case Types.TIMESTAMP: case Types.TIMESTAMP:
break; break;
default: // Truncate string data types only.
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
case Types.LONGNVARCHAR:
if (colSize != null && fieldValue.length() > colSize) { if (colSize != null && fieldValue.length() > colSize) {
fieldValue = fieldValue.substring(0, colSize); fieldValue = fieldValue.substring(0, colSize);
} }

View File

@ -185,6 +185,33 @@ public class TestConvertJSONToSQL {
out.assertContentEquals("INSERT INTO PERSONS (ID, NAME, CODE) VALUES (?, ?, ?)"); out.assertContentEquals("INSERT INTO PERSONS (ID, NAME, CODE) VALUES (?, ?, ?)");
} }
@Test
public void testInsertBoolToInteger() throws InitializationException, ProcessException, SQLException, IOException {
final TestRunner runner = TestRunners.newTestRunner(ConvertJSONToSQL.class);
runner.addControllerService("dbcp", service);
runner.enableControllerService(service);
runner.setProperty(ConvertJSONToSQL.CONNECTION_POOL, "dbcp");
runner.setProperty(ConvertJSONToSQL.TABLE_NAME, "PERSONS");
runner.setProperty(ConvertJSONToSQL.STATEMENT_TYPE, "INSERT");
runner.enqueue(Paths.get("src/test/resources/TestConvertJSONToSQL/person-with-bool.json"));
runner.run();
runner.assertTransferCount(ConvertJSONToSQL.REL_ORIGINAL, 1);
runner.getFlowFilesForRelationship(ConvertJSONToSQL.REL_ORIGINAL).get(0).assertAttributeEquals(FRAGMENT_COUNT.key(), "1");
runner.assertTransferCount(ConvertJSONToSQL.REL_SQL, 1);
final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertJSONToSQL.REL_SQL).get(0);
out.assertAttributeEquals("sql.args.1.type", String.valueOf(java.sql.Types.INTEGER));
out.assertAttributeEquals("sql.args.1.value", "1");
out.assertAttributeEquals("sql.args.2.type", String.valueOf(java.sql.Types.VARCHAR));
out.assertAttributeEquals("sql.args.2.value", "Bool");
// Boolean value 'true' is converted to '0'
out.assertAttributeEquals("sql.args.3.type", String.valueOf(java.sql.Types.INTEGER));
out.assertAttributeEquals("sql.args.3.value", "0");
out.assertContentEquals("INSERT INTO PERSONS (ID, NAME, CODE) VALUES (?, ?, ?)");
}
@Test @Test
public void testUpdateWithNullValue() throws InitializationException, ProcessException, SQLException, IOException { public void testUpdateWithNullValue() throws InitializationException, ProcessException, SQLException, IOException {

View File

@ -0,0 +1,5 @@
{
"id": 1,
"name": "Bool",
"code": true
}