Skip to content

Commit

Permalink
Unescape/unquote result of getSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
alexismeneses committed Nov 22, 2014
1 parent 169c6df commit f97294a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
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.Map.Entry;
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 @@ -27,6 +29,11 @@ abstract class AbstractJdbc4Connection extends org.postgresql.jdbc3g.AbstractJdb
{
private static final SQLPermission SQL_PERMISSION_ABORT = new SQLPermission("callAbort");

/**
* 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 @@ -278,15 +285,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
27 changes: 23 additions & 4 deletions org/postgresql/test/jdbc4/jdbc41/SchemaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected void setUp() throws Exception
stmt.execute("CREATE SCHEMA \"schema 3\"");
stmt.execute("CREATE SCHEMA \"schema \"\"4\"");
stmt.execute("CREATE SCHEMA \"schema '5\"");
stmt.execute("CREATE SCHEMA \"schema ,6\"");
stmt.execute("CREATE SCHEMA \"UpperCase\"");
TestUtil.createTable(_conn, "schema1.table1", "id integer");
TestUtil.createTable(_conn, "schema2.table2", "id integer");
Expand All @@ -49,6 +50,7 @@ protected void tearDown() throws SQLException
stmt.execute("DROP SCHEMA \"schema 3\" CASCADE");
stmt.execute("DROP SCHEMA \"schema \"\"4\" CASCADE");
stmt.execute("DROP SCHEMA \"schema '5\" CASCADE");
stmt.execute("DROP SCHEMA \"schema ,6\"");
stmt.execute("DROP SCHEMA \"UpperCase\" CASCADE");
TestUtil.closeDB(_conn);
}
Expand All @@ -63,13 +65,13 @@ public void testGetSetSchema() throws SQLException
_conn.setSchema("schema2");
assertEquals("schema2", _conn.getSchema());
_conn.setSchema("schema 3");
assertEquals("\"schema 3\"", _conn.getSchema());
assertEquals("schema 3", _conn.getSchema());
_conn.setSchema("schema \"4");
assertEquals("\"schema \"\"4\"", _conn.getSchema());
assertEquals("schema \"4", _conn.getSchema());
_conn.setSchema("schema '5");
assertEquals("\"schema '5\"", _conn.getSchema());
assertEquals("schema '5", _conn.getSchema());
_conn.setSchema("UpperCase");
assertEquals("\"UpperCase\"", _conn.getSchema());
assertEquals("UpperCase", _conn.getSchema());
}

/**
Expand Down Expand Up @@ -162,6 +164,23 @@ public void testMultipleSearchPath() throws SQLException
}
}
assertEquals("schema1", _conn.getSchema());

stmt = _conn.createStatement();
try
{
stmt.execute("SET search_path TO \"schema ,6\",schema2");
}
finally
{
try
{
stmt.close();
}
catch (SQLException e)
{
}
}
assertEquals("schema ,6", _conn.getSchema());
}

public void testSchemaInProperties() throws Exception
Expand Down

0 comments on commit f97294a

Please sign in to comment.