Skip to content

Commit

Permalink
fix: null pointer exception from PgResultSetMetaData when there's no …
Browse files Browse the repository at this point in the history
…column metadata (#1615)

* fix all possible NPE in PgResultSetMetaData when there's column metadata
  • Loading branch information
IvyDev0 authored and davecramer committed Dec 5, 2019
1 parent ec76bac commit 08bd46b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Expand Up @@ -113,7 +113,8 @@ public boolean isCurrency(int column) throws SQLException {
public int isNullable(int column) throws SQLException {
fetchFieldMetaData();
Field field = getField(column);
return field.getMetadata().nullable;
FieldMetadata metadata = field.getMetadata();
return metadata == null ? ResultSetMetaData.columnNullable : metadata.nullable;
}

/**
Expand Down Expand Up @@ -151,7 +152,8 @@ public String getBaseColumnName(int column) throws SQLException {
return "";
}
fetchFieldMetaData();
return field.getMetadata().columnName;
FieldMetadata metadata = field.getMetadata();
return metadata == null ? "" : metadata.columnName;
}

public String getSchemaName(int column) throws SQLException {
Expand Down Expand Up @@ -268,7 +270,8 @@ private void fetchFieldMetaData() throws SQLException {
public String getBaseSchemaName(int column) throws SQLException {
fetchFieldMetaData();
Field field = getField(column);
return field.getMetadata().schemaName;
FieldMetadata metadata = field.getMetadata();
return metadata == null ? "" : metadata.schemaName;
}

public int getPrecision(int column) throws SQLException {
Expand All @@ -288,7 +291,8 @@ public String getTableName(int column) throws SQLException {
public String getBaseTableName(int column) throws SQLException {
fetchFieldMetaData();
Field field = getField(column);
return field.getMetadata().tableName;
FieldMetadata metadata = field.getMetadata();
return metadata == null ? "" : metadata.tableName;
}

/**
Expand Down
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2019, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/

package org.postgresql.jdbc;

import static org.junit.Assert.assertTrue;

import org.postgresql.test.TestUtil;
import org.postgresql.test.jdbc2.BaseTest4;

import org.junit.Before;
import org.junit.Test;

import java.sql.ResultSet;
import java.sql.Statement;

/**
* If the SQL query has no column metadata, the driver shouldn't break by a null pointer exception.
* It should return the result correctly.
*
* @author Ivy (ivyyiyideng@gmail.com)
*
*/
public class NoColumnMetadataIssue1613 extends BaseTest4 {
@Override
@Before
public void setUp() throws Exception {
super.setUp();
TestUtil.createTempTable(con, "test_no_column_metadata","id int");
}

@Test
public void shouldBeNoNPE() throws Exception {
Statement statement = con.createStatement();
statement.executeQuery("INSERT INTO test_no_column_metadata values (1)");
ResultSet rs = statement.executeQuery("SELECT x FROM test_no_column_metadata x");
assertTrue(rs.next());
}
}

0 comments on commit 08bd46b

Please sign in to comment.