Skip to content

Commit

Permalink
Add more info about currentSchema property
Browse files Browse the repository at this point in the history
Add in documentation that currentSchema property can be multiple
and fixed behavior by tests.
  • Loading branch information
Vsevolodk authored and Vsevolod Kaimashnikov committed Jun 24, 2019
1 parent 95ba7b2 commit 31a99e8
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ In addition to the standard connection parameters the driver supports a number o
| readOnly | Boolean | true | Puts this connection in read-only mode |
| disableColumnSanitiser | Boolean | false | Enable optimization that disables column name sanitiser |
| assumeMinServerVersion | String | null | Assume the server is at least that version |
| currentSchema | String | null | Specify the schema to be set in the search-path |
| currentSchema | String | null | Specify the schema (or several schema separated by comma's) to be set in the search-path |
| targetServerType | String | any | Specifies what kind of server to connect, possible values: any, master, slave (deprecated), secondary, preferSlave (deprecated), preferSecondary |
| hostRecheckSeconds | Integer | 10 | Specifies period (seconds) after which the host status is checked again in case it has changed |
| loadBalanceHosts | Boolean | false | If disabled hosts are connected in the given order. If enabled hosts are chosen randomly from the set of suitable candidates |
Expand Down
2 changes: 1 addition & 1 deletion docs/documentation/head/connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ Connection conn = DriverManager.getConnection(url);

* **currentSchema** = String

Specify the schema to be set in the search-path.
Specify the schema (or several schema separated by comma's) to be set in the search-path.
This schema will be used to resolve unqualified object names used in statements over this connection.

* **targetServerType** = String
Expand Down
4 changes: 2 additions & 2 deletions pgjdbc/src/main/java/org/postgresql/PGProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,10 @@ public enum PGProperty {
ALLOW_ENCODING_CHANGES("allowEncodingChanges", "false", "Allow for changes in client_encoding"),

/**
* Specify the schema to be set in the search-path. This schema will be used to resolve
* Specify the schema (or several schema separated by comma's) to be set in the search-path. This schema will be used to resolve
* unqualified object names used in statements over this connection.
*/
CURRENT_SCHEMA("currentSchema", null, "Specify the schema to be set in the search-path"),
CURRENT_SCHEMA("currentSchema", null, "Specify the schema (or several schema separated by comma's) to be set in the search-path"),

TARGET_SERVER_TYPE("targetServerType", "any", "Specifies what kind of server to connect", false,
"any", "master", "slave", "secondary", "preferSlave", "preferSecondary"),
Expand Down
1 change: 1 addition & 0 deletions pgjdbc/src/main/java/org/postgresql/util/PSQLState.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public enum PSQLState {
INVALID_NAME("42602"),
DATATYPE_MISMATCH("42804"),
CANNOT_COERCE("42846"),
UNDEFINED_TABLE("42P01"),

OUT_OF_MEMORY("53200"),
OBJECT_NOT_IN_STATE("55000"),
Expand Down
12 changes: 12 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/test/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -915,4 +915,16 @@ private static void waitStopReplicationSlot(Connection connection, String slotNa
throw new TimeoutException("Wait stop replication slot " + timeInWait + " timeout occurs");
}
}

public static void execute(String sql, Connection connection) throws SQLException {
Statement stmt = connection.createStatement();
try {
stmt.execute(sql);
} finally {
try {
stmt.close();
} catch (SQLException e) {
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@

package org.postgresql.test.jdbc4.jdbc41;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import org.postgresql.PGProperty;
import org.postgresql.test.TestUtil;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;

import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -217,6 +223,90 @@ public void testSearchPathPreparedStatement() throws SQLException {
ps.close();
}

@Test
public void testCurrentSchemaPropertyVisibilityTableDuringFunctionCreation() throws SQLException {
Properties properties = new Properties();
properties.setProperty(PGProperty.CURRENT_SCHEMA.getName(), "public,schema1,schema2");
Connection connection = TestUtil.openDB(properties);

TestUtil.execute("create table schema1.check_table (test_col text)", connection);
TestUtil.execute("insert into schema1.check_table (test_col) values ('test_value')", connection);
TestUtil.execute("create or replace function schema2.check_fun () returns text as $$"
+ " select test_col from check_table"
+ "$$ language sql immutable", connection);
connection.close();
}

@Test
public void testCurrentSchemaPropertyNotVisibilityTableDuringFunctionCreation() throws SQLException {
Properties properties = new Properties();
properties.setProperty(PGProperty.CURRENT_SCHEMA.getName(), "public,schema2");

try (Connection connection = TestUtil.openDB(properties)) {
TestUtil.execute("create table schema1.check_table (test_col text)", connection);
TestUtil.execute("insert into schema1.check_table (test_col) values ('test_value')", connection);
TestUtil.execute("create or replace function schema2.check_fun (txt text) returns text as $$"
+ " select test_col from check_table"
+ "$$ language sql immutable", connection);
} catch (PSQLException e) {
String sqlState = e.getSQLState();
String message = e.getMessage();
assertThat("Test creates function in schema 'schema2' and this function try use table \"check_table\" "
+ "from schema 'schema1'. We expect here sql error code - "
+ PSQLState.UNDEFINED_TABLE + ", because search_path does not contains schema 'schema1' and "
+ "postgres does not see table \"check_table\"",
sqlState,
equalTo(PSQLState.UNDEFINED_TABLE.getState())
);
assertThat(
"Test creates function in schema 'schema2' and this function try use table \"check_table\" "
+ "from schema 'schema1'. We expect here that sql error message will be contains \"check_table\", "
+ "because search_path does not contains schema 'schema1' and postgres does not see "
+ "table \"check_table\"",
message,
containsString("\"check_table\"")
);
}
}

@Test
public void testCurrentSchemaPropertyVisibilityFunction() throws SQLException {
testCurrentSchemaPropertyVisibilityTableDuringFunctionCreation();
Properties properties = new Properties();
properties.setProperty(PGProperty.CURRENT_SCHEMA.getName(), "public,schema1,schema2");
Connection connection = TestUtil.openDB(properties);

TestUtil.execute("select check_fun()", connection);
connection.close();
}

@Test
public void testCurrentSchemaPropertyNotVisibilityTableInsideFunction() throws SQLException {
testCurrentSchemaPropertyVisibilityTableDuringFunctionCreation();
Properties properties = new Properties();
properties.setProperty(PGProperty.CURRENT_SCHEMA.getName(), "public,schema2");

try (Connection connection = TestUtil.openDB(properties)) {
TestUtil.execute("select check_fun()", connection);
} catch (PSQLException e) {
String sqlState = e.getSQLState();
String message = e.getMessage();
assertThat("Test call function in schema 'schema2' and this function uses table \"check_table\" "
+ "from schema 'schema1'. We expect here sql error code - " + PSQLState.UNDEFINED_TABLE + ", "
+ "because search_path does not contains schema 'schema1' and postgres does not see table \"check_table\".",
sqlState,
equalTo(PSQLState.UNDEFINED_TABLE.getState())
);
assertThat(
"Test call function in schema 'schema2' and this function uses table \"check_table\" "
+ "from schema 'schema1'. We expect here that sql error message will be contains \"check_table\", because "
+ " search_path does not contains schema 'schema1' and postgres does not see table \"check_table\"",
message,
containsString("\"check_table\"")
);
}
}

private void assertColType(PreparedStatement ps, String message, int expected) throws SQLException {
ResultSet rs = ps.executeQuery();
ResultSetMetaData md = rs.getMetaData();
Expand Down

0 comments on commit 31a99e8

Please sign in to comment.