Skip to content
Permalink
Browse files
Fix: issue PgDatabaseMetaData.getFunctions() (#1774)
* Fix #1771

Only return functions and not procedures for servers v11 and up.

Co-authored-by: kfe <kfe@kili668>
  • Loading branch information
kili668 and kfe committed May 8, 2020
1 parent 9f398c5 commit 0eb5eea99d46e49c92b24776a9cd782b7fa8b57b
Showing 2 changed files with 40 additions and 2 deletions.
@@ -2749,6 +2749,10 @@ public ResultSet getFunctions(String catalog, String schemaPattern, String funct
+ "INNER JOIN pg_catalog.pg_namespace n ON p.pronamespace=n.oid "
+ "LEFT JOIN pg_catalog.pg_description d ON p.oid=d.objoid "
+ "WHERE true ";

if (connection.haveMinimumServerVersion(ServerVersion.v11)) {
sql += " AND p.prokind='f'";
}
/*
if the user provides a schema then search inside the schema for it
*/
@@ -7,10 +7,10 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import org.postgresql.core.ServerVersion;
@@ -106,7 +106,7 @@ public void testGetSchemas() throws SQLException {
}

@Test
public void testGetFunctionsInSchema() throws SQLException {
public void testGetFunctionsInSchemaForFunctions() throws SQLException {
DatabaseMetaData dbmd = conn.getMetaData();
ResultSet rs = dbmd.getFunctions("", "hasfunctions","");
int count = assertGetFunctionRS(rs);
@@ -131,6 +131,40 @@ public void testGetFunctionsInSchema() throws SQLException {

}

@Test
public void testGetFunctionsInSchemaForProcedures() throws SQLException {
// Due to the introduction of actual stored procedures in PostgreSQL 11, getFunctions should not return procedures for PostgreSQL versions 11+
if (TestUtil.haveMinimumServerVersion(conn, ServerVersion.v11)) {

DatabaseMetaData dbmd = conn.getMetaData();
Statement statement = conn.createStatement();

// Search for functions in schema "hasprocedures"
ResultSet rs = dbmd.getFunctions("", "hasprocedures", null);
Boolean recordFound = rs.next();
assertEquals("PostgreSQL11+ should not return procedures from getFunctions", recordFound, false);

// Search for functions in schema "noprocedures" (which should never expect records)
rs = dbmd.getFunctions("", "noprocedures", null);
recordFound = rs.next();
assertFalse(recordFound);

// Search for functions by procedure name "addprocedure" within schema "hasprocedures"
statement.execute("set search_path=hasprocedures");
rs = dbmd.getFunctions("", "", "addprocedure");
recordFound = rs.next();
assertEquals("PostgreSQL11+ should not return procedures from getFunctions", recordFound, false);

// Search for functions by procedure name "addprocedure" within schema "noprocedures" (which should never expect records)
statement.execute("set search_path=noprocedures");
rs = dbmd.getProcedures("", "", "addprocedure");
recordFound = rs.next();
assertFalse(recordFound);

statement.close();
}
}

@Test
public void testGetProceduresInSchemaForFunctions() throws SQLException {
// Due to the introduction of actual stored procedures in PostgreSQL 11, getProcedures should not return functions for PostgreSQL versions 11+

0 comments on commit 0eb5eea

Please sign in to comment.