postgresql-jdbc.git / summary / log / commit / refs
commit d2c1ef7c15171d7cd080e4451254dd02706b0c5d
Author: Alexis Meneses <alexismeneses@users.noreply.github.com>
Date: Wed May 21 14:03:22 2014 +0000
Addition of a connection property to set default schema (aka search path).
Better compatibility for setSchema (supports older backend versions, and take null to reset to the default)
doc/pgjdbc.xml | 15 +++++++++--
org/postgresql/Driver.java.in | 4 ++-
org/postgresql/ds/common/BaseDataSource.java | 19 +++++++++++++-
org/postgresql/jdbc2/AbstractJdbc2Connection.java | 28 +++++++++++++++++++-
org/postgresql/jdbc4/AbstractJdbc4Connection.java | 14 ----------
org/postgresql/test/jdbc4/jdbc41/SchemaTest.java | 32 +++++++++++++++++++++++
6 files changed, 93 insertions(+), 19 deletions(-)
diff --git a/doc/pgjdbc.xml b/doc/pgjdbc.xml
index f67555f6..2b79b4bf 100644
--- a/doc/pgjdbc.xml
+++ b/doc/pgjdbc.xml
@@ -628,8 +628,8 @@ openssl pkcs8 -topk8 -in client.key -out client.pk8 -outform DER -v1 PBE-SHA1-3D
<term><varname>charSet</varname> = <type>String</type></term>
<listitem>
<para>
- The character set to use for data sent to the database or recieved
- from the database. This property is only relevent for server
+ The character set to use for data sent to the database or received
+ from the database. This property is only relevant for server
versions less than or equal to 7.2. The 7.3 release was the first
with multibyte support compiled by default and the driver uses
its character set translation facilities instead of trying to do
@@ -846,6 +846,17 @@ openssl pkcs8 -topk8 -in client.key -out client.pk8 -outform DER -v1 PBE-SHA1-3D
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><varname>currentSchema</varname></term>
+ <listitem>
+ <para>
+ Specify the schema to be set in the search-path. This schema will be used
+ to resolve unqualified object names used in statements over this connection.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
</para>
diff --git a/org/postgresql/Driver.java.in b/org/postgresql/Driver.java.in
index 5aca4060..8170bb67 100644
--- a/org/postgresql/Driver.java.in
+++ b/org/postgresql/Driver.java.in
@@ -484,7 +484,9 @@ public class Driver implements java.sql.Driver
{ "jaasApplicationName", Boolean.FALSE,
"Specifies the name of the JAAS system or application login configuration." },
{ "disableColumnSanitiser", Boolean.FALSE,
- "Enable optimization that disables column name sanitiser." }
+ "Enable optimization that disables column name sanitiser." },
+ { "currentSchema", Boolean.FALSE,
+ "Specify the schema to set in the search_path to resolve unqualified object names." }
};
/**
diff --git a/org/postgresql/ds/common/BaseDataSource.java b/org/postgresql/ds/common/BaseDataSource.java
index c06cc0ab..b86ee2be 100644
--- a/org/postgresql/ds/common/BaseDataSource.java
+++ b/org/postgresql/ds/common/BaseDataSource.java
@@ -67,6 +67,7 @@ public abstract class BaseDataSource implements Referenceable
private String stringType=null;
private boolean logLevelSet = false;
private boolean disableColumnSanitiser = false;
+ private String currentSchema;
/**
* Gets a connection to the PostgreSQL database. The database is identified by the
@@ -517,6 +518,16 @@ public abstract class BaseDataSource implements Referenceable
this.disableColumnSanitiser = disableColumnSanitiser;
}
+ public String getCurrentSchema()
+ {
+ return currentSchema;
+ }
+
+ public void setCurrentSchema(String currentSchema)
+ {
+ this.currentSchema = currentSchema;
+ }
+
/**
* Generates a DriverManager URL from the other properties supplied.
*/
@@ -573,7 +584,10 @@ public abstract class BaseDataSource implements Referenceable
sb.append("&binaryTransferDisable=").append(binaryTransferDisable);
}
sb.append("&disableColumnSanitiser=").append(disableColumnSanitiser);
-
+
+ if (currentSchema != null) {
+ sb.append("¤tSchema=").append(currentSchema);
+ }
return sb.toString();
}
@@ -602,6 +616,7 @@ public abstract class BaseDataSource implements Referenceable
stringType = p.getProperty("stringtype");
binaryTransfer = Boolean.parseBoolean(p.getProperty("binaryTransfer"));
disableColumnSanitiser = Boolean.parseBoolean(p.getProperty("disableColumnSanitiser"));
+ currentSchema = p.getProperty("currentSchema");
}
/**
@@ -703,6 +718,7 @@ public abstract class BaseDataSource implements Referenceable
out.writeObject(binaryTransferDisable);
out.writeBoolean(logLevelSet);
out.writeBoolean(disableColumnSanitiser);
+ out.writeObject(currentSchema);
}
protected void readBaseObject(ObjectInputStream in) throws IOException, ClassNotFoundException
@@ -731,6 +747,7 @@ public abstract class BaseDataSource implements Referenceable
binaryTransferDisable = (String)in.readObject();
logLevelSet = in.readBoolean();
disableColumnSanitiser = in.readBoolean();
+ currentSchema = (String)in.readObject();
}
public void initializeFrom(BaseDataSource source) throws IOException, ClassNotFoundException {
diff --git a/org/postgresql/jdbc2/AbstractJdbc2Connection.java b/org/postgresql/jdbc2/AbstractJdbc2Connection.java
index 11178c9a..3cdd53bc 100644
--- a/org/postgresql/jdbc2/AbstractJdbc2Connection.java
+++ b/org/postgresql/jdbc2/AbstractJdbc2Connection.java
@@ -19,7 +19,6 @@ import org.postgresql.PGNotification;
import org.postgresql.fastpath.Fastpath;
import org.postgresql.largeobject.LargeObjectManager;
import org.postgresql.util.*;
-import org.postgresql.util.HostSpec;
import org.postgresql.copy.*;
/**
@@ -270,6 +269,12 @@ public abstract class AbstractJdbc2Connection implements BaseConnection
}
this.disableColumnSanitiser = Boolean.valueOf(info.getProperty(""
+ "disableColumnSanitiser", Boolean.FALSE.toString()));
+
+ String currentSchema = info.getProperty("currentSchema");
+ if (currentSchema != null)
+ {
+ setSchema(currentSchema);
+ }
}
private Set<Integer> getOidSet(String oidList) throws PSQLException {
@@ -1274,6 +1279,27 @@ public abstract class AbstractJdbc2Connection implements BaseConnection
this.disableColumnSanitiser = disableColumnSanitiser;
}
+ public void setSchema(String schema) throws SQLException
+ {
+ checkClosed();
+ Statement stmt = createStatement();
+ try
+ {
+ if (schema != null)
+ {
+ stmt.executeUpdate("SET SESSION search_path TO '" + schema + "'");
+ }
+ else
+ {
+ stmt.executeUpdate("SET SESSION search_path TO DEFAULT");
+ }
+ }
+ finally
+ {
+ stmt.close();
+ }
+ }
+
protected void abort()
{
protoConnection.abort();
diff --git a/org/postgresql/jdbc4/AbstractJdbc4Connection.java b/org/postgresql/jdbc4/AbstractJdbc4Connection.java
index 8968ab2d..1508179f 100644
--- a/org/postgresql/jdbc4/AbstractJdbc4Connection.java
+++ b/org/postgresql/jdbc4/AbstractJdbc4Connection.java
@@ -252,20 +252,6 @@ abstract class AbstractJdbc4Connection extends org.postgresql.jdbc3g.AbstractJdb
throw new SQLException("Cannot unwrap to " + iface.getName());
}
- public void setSchema(String schema) throws SQLException
- {
- checkClosed();
- Statement stmt = createStatement();
- try
- {
- stmt.executeUpdate("SET SESSION SCHEMA '" + schema + "'");
- }
- finally
- {
- stmt.close();
- }
- }
-
public String getSchema() throws SQLException
{
checkClosed();
diff --git a/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java b/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java
index 5a52b985..657c5089 100644
--- a/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java
+++ b/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java
@@ -8,7 +8,11 @@
package org.postgresql.test.jdbc4.jdbc41;
import java.sql.*;
+
+import java.util.Properties;
+
import junit.framework.TestCase;
+
import org.postgresql.test.TestUtil;
public class SchemaTest extends TestCase
@@ -33,6 +37,7 @@ public class SchemaTest extends TestCase
protected void tearDown() throws SQLException
{
+ _conn.setSchema(null);
Statement stmt = _conn.createStatement();
stmt.execute("DROP SCHEMA schema1 CASCADE");
stmt.execute("DROP SCHEMA schema2 CASCADE");
@@ -129,4 +134,31 @@ public class SchemaTest extends TestCase
assertEquals("schema1", _conn.getSchema());
}
+ public void testSchemaInProperties() throws Exception
+ {
+ Properties properties = new Properties();
+ properties.setProperty("currentSchema", "schema1");
+ Connection conn = TestUtil.openDB(properties);
+ try
+ {
+ assertEquals("schema1", conn.getSchema());
+
+ Statement stmt = conn.createStatement();
+ stmt.executeQuery(TestUtil.selectSQL("table1", "*"));
+ stmt.executeQuery(TestUtil.selectSQL("schema2.table2", "*"));
+ try
+ {
+ stmt.executeQuery(TestUtil.selectSQL("table2", "*"));
+ fail("Objects of schema2 should not be visible without prefix");
+ }
+ catch (SQLException e)
+ {
+ // expected
+ }
+ }
+ finally
+ {
+ TestUtil.closeDB(conn);
+ }
+ }
}
[parent: 588c03c6fccc]