diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml index 5c525ab4fd..0df7ced138 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml @@ -368,6 +368,7 @@ src/test/resources/TestConvertJSONToSQL/person-without-code.json src/test/resources/TestConvertJSONToSQL/person-with-null-code.json src/test/resources/TestConvertJSONToSQL/person-without-id.json + src/test/resources/TestConvertJSONToSQL/person-with-bool.json src/test/resources/TestModifyBytes/noFooter.txt src/test/resources/TestModifyBytes/noFooter_noHeader.txt src/test/resources/TestModifyBytes/noHeader.txt diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ConvertJSONToSQL.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ConvertJSONToSQL.java index 2f2123a574..00db3b56f8 100755 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ConvertJSONToSQL.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ConvertJSONToSQL.java @@ -518,6 +518,7 @@ public class ConvertJSONToSQL extends AbstractProcessor { break; // Don't truncate numeric types. + case Types.BIT: case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: @@ -527,6 +528,10 @@ public class ConvertJSONToSQL extends AbstractProcessor { case Types.DOUBLE: case Types.DECIMAL: 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; // 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: 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) { fieldValue = fieldValue.substring(0, colSize); } diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestConvertJSONToSQL.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestConvertJSONToSQL.java index 2cd66eb321..2332e28b18 100755 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestConvertJSONToSQL.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestConvertJSONToSQL.java @@ -185,6 +185,33 @@ public class TestConvertJSONToSQL { 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 public void testUpdateWithNullValue() throws InitializationException, ProcessException, SQLException, IOException { diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestConvertJSONToSQL/person-with-bool.json b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestConvertJSONToSQL/person-with-bool.json new file mode 100644 index 0000000000..b42f6feb7c --- /dev/null +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestConvertJSONToSQL/person-with-bool.json @@ -0,0 +1,5 @@ +{ + "id": 1, + "name": "Bool", + "code": true +} \ No newline at end of file