Repository: nifi
Updated Branches:
  refs/heads/master 892c74dff -> 2fb3b01eb


NIFI-2918 JDBC getColumnName may return empty string

This closes #1149


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/2fb3b01e
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/2fb3b01e
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/2fb3b01e

Branch: refs/heads/master
Commit: 2fb3b01ebe2a0519b98e4450cd89204b4225657d
Parents: 892c74d
Author: patricker <patric...@gmail.com>
Authored: Thu Oct 20 09:56:22 2016 -0600
Committer: Oleg Zhurakousky <o...@suitcase.io>
Committed: Wed Nov 2 13:13:34 2016 -0400

----------------------------------------------------------------------
 .../processors/standard/util/JdbcCommon.java    |  3 ++-
 .../standard/util/TestJdbcCommon.java           | 25 ++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/2fb3b01e/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/JdbcCommon.java
----------------------------------------------------------------------
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 0aa4c60..f19f7e0 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:

http://git-wip-us.apache.org/repos/asf/nifi/blob/2fb3b01e/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcCommon.java
----------------------------------------------------------------------
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 dd375aa..1e0fe2f 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
@@ -151,6 +151,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();
         st.executeUpdate("insert into restaurants values (1, 'Irifunes', 'San 
Mateo')");

Reply via email to