Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: DatabaseMetaData.getFunctions should not limit the search to the search_path if the schema is provided #1633

Merged
merged 2 commits into from Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down