NIFI-3372 Adding capability for PutSQL to handle BIT fields generated by ConvertJSONToSQL.

This closes #1432.
This commit is contained in:
Ryan Persaud 2017-01-18 20:15:16 -07:00 committed by Pierre Villard
parent 46fa6a167a
commit ea0f5690cc
2 changed files with 119 additions and 0 deletions

View File

@ -761,6 +761,8 @@ public class PutSQL extends AbstractProcessor {
} else {
switch (jdbcType) {
case Types.BIT:
stmt.setBoolean(parameterIndex, "1".equals(parameterValue) || "t".equalsIgnoreCase(parameterValue) || Boolean.parseBoolean(parameterValue));
break;
case Types.BOOLEAN:
stmt.setBoolean(parameterIndex, Boolean.parseBoolean(parameterValue));
break;

View File

@ -351,6 +351,123 @@ public class TestPutSQL {
}
}
@Test
public void testBitType() throws SQLException, InitializationException {
final TestRunner runner = TestRunners.newTestRunner(PutSQL.class);
try (final Connection conn = service.getConnection()) {
try (final Statement stmt = conn.createStatement()) {
stmt.executeUpdate("CREATE TABLE BITTESTS (id integer primary key, bt1 BOOLEAN)");
}
}
runner.addControllerService("dbcp", service);
runner.enableControllerService(service);
runner.setProperty(PutSQL.CONNECTION_POOL, "dbcp");
final byte[] insertStatement = "INSERT INTO BITTESTS (ID, bt1) VALUES (?, ?)".getBytes();
Map<String, String> attributes = new HashMap<>();
attributes.put("sql.args.1.type", String.valueOf(Types.INTEGER));
attributes.put("sql.args.1.value", "1");
attributes.put("sql.args.2.type", String.valueOf(Types.BIT));
attributes.put("sql.args.2.value", "1");
runner.enqueue(insertStatement, attributes);
attributes.put("sql.args.1.type", String.valueOf(Types.INTEGER));
attributes.put("sql.args.1.value", "2");
attributes.put("sql.args.2.type", String.valueOf(Types.BIT));
attributes.put("sql.args.2.value", "0");
runner.enqueue(insertStatement, attributes);
attributes.put("sql.args.1.type", String.valueOf(Types.INTEGER));
attributes.put("sql.args.1.value", "3");
attributes.put("sql.args.2.type", String.valueOf(Types.BIT));
attributes.put("sql.args.2.value", "-5");
runner.enqueue(insertStatement, attributes);
attributes.put("sql.args.1.type", String.valueOf(Types.INTEGER));
attributes.put("sql.args.1.value", "4");
attributes.put("sql.args.2.type", String.valueOf(Types.BIT));
attributes.put("sql.args.2.value", "t");
runner.enqueue(insertStatement, attributes);
attributes.put("sql.args.1.type", String.valueOf(Types.INTEGER));
attributes.put("sql.args.1.value", "5");
attributes.put("sql.args.2.type", String.valueOf(Types.BIT));
attributes.put("sql.args.2.value", "f");
runner.enqueue(insertStatement, attributes);
attributes.put("sql.args.1.type", String.valueOf(Types.INTEGER));
attributes.put("sql.args.1.value", "6");
attributes.put("sql.args.2.type", String.valueOf(Types.BIT));
attributes.put("sql.args.2.value", "T");
runner.enqueue(insertStatement, attributes);
attributes.put("sql.args.1.type", String.valueOf(Types.INTEGER));
attributes.put("sql.args.1.value", "7");
attributes.put("sql.args.2.type", String.valueOf(Types.BIT));
attributes.put("sql.args.2.value", "true");
runner.enqueue(insertStatement, attributes);
attributes.put("sql.args.1.type", String.valueOf(Types.INTEGER));
attributes.put("sql.args.1.value", "8");
attributes.put("sql.args.2.type", String.valueOf(Types.BIT));
attributes.put("sql.args.2.value", "false");
runner.enqueue(insertStatement, attributes);
runner.run();
runner.assertAllFlowFilesTransferred(PutSQL.REL_SUCCESS, 8);
try (final Connection conn = service.getConnection()) {
try (final Statement stmt = conn.createStatement()) {
final ResultSet rs = stmt.executeQuery("SELECT * FROM BITTESTS");
//First test (true)
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertTrue(rs.getBoolean(2));
//Second test (false)
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertFalse(rs.getBoolean(2));
//Third test (false)
assertTrue(rs.next());
assertEquals(3, rs.getInt(1));
assertFalse(rs.getBoolean(2));
//Fourth test (true)
assertTrue(rs.next());
assertEquals(4, rs.getInt(1));
assertTrue(rs.getBoolean(2));
//Fifth test (false)
assertTrue(rs.next());
assertEquals(5, rs.getInt(1));
assertFalse(rs.getBoolean(2));
//Sixth test (true)
assertTrue(rs.next());
assertEquals(6, rs.getInt(1));
assertTrue(rs.getBoolean(2));
//Seventh test (true)
assertTrue(rs.next());
assertEquals(7, rs.getInt(1));
assertTrue(rs.getBoolean(2));
//Eighth test (false)
assertTrue(rs.next());
assertEquals(8, rs.getInt(1));
assertFalse(rs.getBoolean(2));
assertFalse(rs.next());
}
}
}
@Test
public void testBinaryColumnTypes() throws InitializationException, ProcessException, SQLException, IOException, ParseException {
final TestRunner runner = TestRunners.newTestRunner(PutSQL.class);