Skip to content
Permalink
Browse files

fix: DatabaseMetaData.getFunctions should not limit the search to the…

… search_path if the schema is provided (#1633)

* fix: DatabaseMetaData.getFunctions should not limit the search to the search_path if the schema is provided
  • Loading branch information
davecramer committed Dec 5, 2019
1 parent 97d32ca commit 8106d3df5c3f6ea3cbc3e621977df5542b182b56
@@ -1038,6 +1038,9 @@ public ResultSet getProcedures(String catalog, String schemaPattern, String proc
+ " WHERE p.pronamespace=n.oid ";
if (schemaPattern != null && !schemaPattern.isEmpty()) {
sql += " AND n.nspname LIKE " + escapeQuotes(schemaPattern);
} else {
/* limit to current schema if no schema given */
sql += "and pg_function_is_visible(p.oid)";
}
if (procedureNamePattern != null && !procedureNamePattern.isEmpty()) {
sql += " AND p.proname LIKE " + escapeQuotes(procedureNamePattern);
@@ -2679,9 +2682,15 @@ public ResultSet getFunctions(String catalog, String schemaPattern, String funct
+ "FROM pg_catalog.pg_proc p "
+ "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 pg_function_is_visible(p.oid) ";
+ "WHERE true ";
/*
if the user provides a schema then search inside the schema for it
*/
if (schemaPattern != null && !schemaPattern.isEmpty()) {
sql += " AND n.nspname LIKE " + escapeQuotes(schemaPattern);
} else {
/* if no schema is provided then limit the search inside the search_path */
sql += "and pg_function_is_visible(p.oid)";
}
if (functionNamePattern != null && !functionNamePattern.isEmpty()) {
sql += " AND p.proname LIKE " + escapeQuotes(functionNamePattern);
@@ -8,6 +8,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
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;
@@ -37,12 +38,20 @@ public void setUp() throws Exception {
conn = TestUtil.openDB();
TestUtil.dropSequence(conn, "sercoltest_a_seq");
TestUtil.createTable(conn, "sercoltest", "a serial, b int");
TestUtil.createSchema(conn, "hasfunctions");
TestUtil.createSchema(conn, "nofunctions");
TestUtil.execute("create function hasfunctions.addfunction (integer, integer) "
+ "RETURNS integer AS 'select $1 + $2;' LANGUAGE SQL IMMUTABLE", conn);


}

@After
public void tearDown() throws Exception {
TestUtil.dropSequence(conn, "sercoltest_a_seq");
TestUtil.dropTable(conn, "sercoltest");
TestUtil.dropSchema(conn, "hasfunctions");
TestUtil.dropSchema(conn, "nofunctions");
TestUtil.closeDB(conn);
}

@@ -88,6 +97,56 @@ public void testGetSchemas() throws SQLException {
assertTrue(!rs.next());
}

@Test
public void testGetFunctionsInSchema() throws SQLException {
DatabaseMetaData dbmd = conn.getMetaData();
ResultSet rs = dbmd.getFunctions("", "hasfunctions","");
int count = assertGetFunctionRS(rs);
assertThat( count, is(1));

Statement statement = conn.createStatement();
statement.execute("set search_path=hasfunctions");

rs = dbmd.getFunctions("", "","addfunction");
assertThat( assertGetFunctionRS(rs), is(1) );

statement.execute("set search_path=nofunctions");

rs = dbmd.getFunctions("", "","addfunction");
assertFalse(rs.next());

statement.execute("reset search_path");
statement.close();

rs = dbmd.getFunctions("", "nofunctions",null);
assertFalse(rs.next());

}

@Test
public void testGetProceduresInSchema() throws SQLException {
DatabaseMetaData dbmd = conn.getMetaData();
ResultSet rs = dbmd.getProcedures("", "hasfunctions",null);
assertTrue(rs.next());


Statement statement = conn.createStatement();
statement.execute("set search_path=hasfunctions");

rs = dbmd.getProcedures("", "","addfunction");
assertTrue(rs.next());

statement.execute("set search_path=nofunctions");
rs = dbmd.getProcedures("", "","addfunction");
assertFalse(rs.next());

statement.execute("reset search_path");
statement.close();
rs = dbmd.getProcedures("", "nofunctions",null);
assertFalse(rs.next());

}

@Test
public void testGetFunctionsWithBlankPatterns() throws SQLException {
int minFuncCount = 1000;

0 comments on commit 8106d3d

Please sign in to comment.
You can’t perform that action at this time.