NIFI-2918 JDBC getColumnName may return empty string

This closes #1149
This commit is contained in:
patricker 2016-10-20 09:56:22 -06:00 committed by Oleg Zhurakousky
parent 892c74dff2
commit 2fb3b01ebe
2 changed files with 27 additions and 1 deletions

View File

@ -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:

View File

@ -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();