From 2fb3b01ebe2a0519b98e4450cd89204b4225657d Mon Sep 17 00:00:00 2001 From: patricker Date: Thu, 20 Oct 2016 09:56:22 -0600 Subject: [PATCH] NIFI-2918 JDBC getColumnName may return empty string This closes #1149 --- .../processors/standard/util/JdbcCommon.java | 3 ++- .../standard/util/TestJdbcCommon.java | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/JdbcCommon.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/JdbcCommon.java index 0aa4c60e37..f19f7e0bcd 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/JdbcCommon.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/JdbcCommon.java @@ -258,7 +258,8 @@ public class JdbcCommon { * Some missing Avro types - Decimal, Date types. May need some additional work. */ for (int i = 1; i <= nrOfColumns; i++) { - String columnName = convertNames ? normalizeNameForAvro(meta.getColumnName(i)) : meta.getColumnName(i); + String nameOrLabel = StringUtils.isNotEmpty(meta.getColumnName(i)) ? meta.getColumnName(i) : meta.getColumnLabel(i); + String columnName = convertNames ? normalizeNameForAvro(nameOrLabel) : nameOrLabel; switch (meta.getColumnType(i)) { case CHAR: case LONGNVARCHAR: diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcCommon.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcCommon.java index dd375aa7f7..1e0fe2fd54 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcCommon.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcCommon.java @@ -150,6 +150,31 @@ public class TestJdbcCommon { } + @Test + public void testCreateSchemaOnlyColumnLabel() throws ClassNotFoundException, SQLException { + + final ResultSet resultSet = mock(ResultSet.class); + final ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class); + when(resultSet.getMetaData()).thenReturn(resultSetMetaData); + when(resultSetMetaData.getColumnCount()).thenReturn(2); + when(resultSetMetaData.getTableName(1)).thenReturn("TEST"); + when(resultSetMetaData.getColumnType(1)).thenReturn(Types.INTEGER); + when(resultSetMetaData.getColumnName(1)).thenReturn(""); + when(resultSetMetaData.getColumnLabel(1)).thenReturn("ID"); + when(resultSetMetaData.getColumnType(2)).thenReturn(Types.VARCHAR); + when(resultSetMetaData.getColumnName(2)).thenReturn("VCHARC"); + when(resultSetMetaData.getColumnLabel(2)).thenReturn("NOT_VCHARC"); + + final Schema schema = JdbcCommon.createSchema(resultSet); + assertNotNull(schema); + + assertNotNull(schema.getField("ID")); + assertNotNull(schema.getField("VCHARC")); + + // records name, should be result set first column table name + assertEquals("TEST", schema.getName()); + } + @Test public void testConvertToBytes() throws ClassNotFoundException, SQLException, IOException { final Statement st = con.createStatement();