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

[backpatch] Fixes on get/set/current-schema (9.3 branch) #232

Merged
merged 5 commits into from
Dec 18, 2014
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
9 changes: 9 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@
</and>
</condition>

<condition property="jdbc41tests">
<and>
<isset property="jdbc41" />
<isset property="junit" />
</and>
</condition>

<condition property="jdbc.version" value="jdbc3">
<isset property="jdbc3any" />
</condition>
Expand Down Expand Up @@ -440,6 +447,7 @@
<include name="${package}/test/**" />

<exclude name="${package}/test/jdbc4/**" unless="jdbc4tests" />
<exclude name="${package}/test/jdbc4/jdbc41/**" unless="jdbc41tests" />
<exclude name="${package}/test/ssl/**" unless="jdbc4tests" />

<classpath>
Expand Down Expand Up @@ -484,6 +492,7 @@
<test name="org.postgresql.test.xa.XATestSuite" outfile="${testResultsDir}/xa"/>
<test name="org.postgresql.test.extensions.ExtensionsSuite" outfile="${testResultsDir}/extensions"/>
<test name="org.postgresql.test.jdbc4.Jdbc4TestSuite" if="jdbc4tests" outfile="${testResultsDir}/jdbc4"/>
<test name="org.postgresql.test.jdbc4.jdbc41.Jdbc41TestSuite" if="jdbc41tests" outfile="${testResultsDir}/jdbc41"/>
<test name="org.postgresql.test.ssl.SslTestSuite" if="jdbc4tests" outfile="${testResultsDir}/ssl"/>
</junit>
</target>
Expand Down
9 changes: 9 additions & 0 deletions org/postgresql/core/v2/ConnectionFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,5 +490,14 @@ else if (dbEncoding != null)
sb.append("'");
SetupQueryRunner.run(protoConnection, sb.toString(), false);
}

String currentSchema = info.getProperty("currentSchema");
if (currentSchema != null)
{
StringBuffer sb = new StringBuffer("SET search_path = '");
Utils.appendEscapedLiteral(sb, appName, protoConnection.getStandardConformingStrings());
sb.append("'");
SetupQueryRunner.run(protoConnection, sb.toString(), false);
}
}
}
27 changes: 17 additions & 10 deletions org/postgresql/core/v3/ConnectionFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
*/
package org.postgresql.core.v3;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.TimeZone;

import java.sql.SQLException;
import java.io.IOException;
import java.net.ConnectException;
Expand Down Expand Up @@ -165,15 +166,21 @@ else if ("require".equals(sslmode) || "verify-ca".equals(sslmode) || "verify-ful
logger.info("Receive Buffer Size is " + newStream.getSocket().getReceiveBufferSize());
logger.info("Send Buffer Size is " + newStream.getSocket().getSendBufferSize());

// Construct and send a startup packet.
String[][] params = {
{ "user", user },
{ "database", database },
{ "client_encoding", "UTF8" },
{ "DateStyle", "ISO" },
{ "extra_float_digits", "2" },
{ "TimeZone", createPostgresTimeZone() },
};
List<String[]> paramList = new ArrayList<String[]>();
paramList.add(new String[] {"user", user});
paramList.add(new String[] {"database", database});
paramList.add(new String[] {"client_encoding", "UTF8"});
paramList.add(new String[] {"DateStyle", "ISO"});
paramList.add(new String[] {"TimeZone", createPostgresTimeZone()});
paramList.add(new String[] {"extra_float_digits", "2"});

String currentSchema = info.getProperty("currentSchema");
if (currentSchema != null)
{
paramList.add(new String[] {"search_path", currentSchema});
}

String[][] params = paramList.toArray(new String[][]{});

sendStartupPacket(newStream, params, logger);

Expand Down
12 changes: 5 additions & 7 deletions org/postgresql/jdbc2/AbstractJdbc2Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,6 @@ else if (stringType.equalsIgnoreCase("varchar"))
}
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 {
Expand Down Expand Up @@ -1287,7 +1281,11 @@ public void setSchema(String schema) throws SQLException
{
if (schema != null)
{
stmt.executeUpdate("SET SESSION search_path TO '" + schema + "'");
StringBuffer sb = new StringBuffer();
sb.append("SET SESSION search_path TO '");
Utils.appendEscapedLiteral(sb, schema, protoConnection.getStandardConformingStrings());
sb.append("'");
stmt.executeUpdate(sb.toString());
}
else
{
Expand Down
27 changes: 22 additions & 5 deletions org/postgresql/jdbc4/AbstractJdbc4Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.Iterator;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.postgresql.core.Oid;
import org.postgresql.core.Utils;
Expand All @@ -25,6 +27,11 @@

abstract class AbstractJdbc4Connection extends org.postgresql.jdbc3g.AbstractJdbc3gConnection
{
/**
* Pattern used to unquote the result of {@link #getSchema()}
*/
private static final Pattern PATTERN_GET_SCHEMA = Pattern.compile("^\\\"(.*)\\\"(?!\\\")");

private final Properties _clientInfo;

public AbstractJdbc4Connection(HostSpec[] hostSpecs, String user, String database, Properties info, String url) throws SQLException {
Expand Down Expand Up @@ -255,15 +262,25 @@ public String getSchema() throws SQLException
stmt.close();
}

// keep only the first schema of the search path if there are many
int commaIndex = searchPath.indexOf(',');
if (commaIndex == -1)
if (searchPath.startsWith("\""))
{
return searchPath;
// unquote the result if it's a quoted string
Matcher matcher = PATTERN_GET_SCHEMA.matcher(searchPath);
matcher.find();
return matcher.group(1).replaceAll("\"\"", "\"");
}
else
{
return searchPath.substring(0, commaIndex);
// keep only the first schema of the search path if there are many
int commaIndex = searchPath.indexOf(',');
if (commaIndex == -1)
{
return searchPath;
}
else
{
return searchPath.substring(0, commaIndex);
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions org/postgresql/test/jdbc4/jdbc41/Jdbc41TestSuite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*-------------------------------------------------------------------------
*
* Copyright (c) 2007-2014, PostgreSQL Global Development Group
*
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc4.jdbc41;

import junit.framework.TestSuite;

/*
* Executes all known tests for JDBC4.1
*/
public class Jdbc41TestSuite extends TestSuite
{

/*
* The main entry point for JUnit
*/
public static TestSuite suite() throws Exception
{
Class.forName("org.postgresql.Driver");
TestSuite suite = new TestSuite();

suite.addTestSuite(SchemaTest.class);

return suite;
}

}