Skip to content
Permalink
Browse files

Add more info about currentSchema property. (#1481)

Add in documentation that currentSchema property can be multiple
and fix test behavior.
  • Loading branch information...
vsevolodk authored and davecramer committed Jun 28, 2019
1 parent 58804e9 commit d96ed59eba6a2279da337684e696b198ec60685c
@@ -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 commas) 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 |
@@ -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 commas) 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
@@ -367,10 +367,10 @@
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 commas) 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 commas) 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"),
@@ -83,6 +83,7 @@
INVALID_NAME("42602"),
DATATYPE_MISMATCH("42804"),
CANNOT_COERCE("42846"),
UNDEFINED_TABLE("42P01"),

OUT_OF_MEMORY("53200"),
OBJECT_NOT_IN_STATE("55000"),
@@ -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) {
}
}
}
}
@@ -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;
@@ -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();

0 comments on commit d96ed59

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