postgresql-jdbc.git / summary / log / commit / refs

commit    ffda4293a2d96890d2ea7b4cba77f54df11f941c
Author:   Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
Date:     Tue Jul 21 22:02:38 2015 +0000

    fix: use current_schema() for Connection#getSchema
    
    Previously the driver used "show search_path", however it might result into non-evaluated '$user' value
    
    fixes #356


org/postgresql/jdbc2/AbstractJdbc2Connection.java | 25 -------- org/postgresql/jdbc4/AbstractJdbc4Connection.java | 47 +++++++-------- org/postgresql/test/jdbc4/jdbc41/SchemaTest.java | 69 +++++++++++++---------- 3 files changed, 60 insertions(+), 81 deletions(-) diff --git a/org/postgresql/jdbc2/AbstractJdbc2Connection.java b/org/postgresql/jdbc2/AbstractJdbc2Connection.java index 15fe2576..0e63e03b 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2Connection.java +++ b/org/postgresql/jdbc2/AbstractJdbc2Connection.java @@ -1323,31 +1323,6 @@ public abstract class AbstractJdbc2Connection implements BaseConnection this.disableColumnSanitiser = disableColumnSanitiser; } - public void setSchema(String schema) throws SQLException - { - checkClosed(); - Statement stmt = createStatement(); - try - { - if (schema != null) - { - StringBuilder sb = new StringBuilder(); - sb.append("SET SESSION search_path TO '"); - Utils.escapeLiteral(sb, schema, protoConnection.getStandardConformingStrings()); - sb.append("'"); - stmt.executeUpdate(sb.toString()); - } - 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 fb7f72c7..0c176f31 100644 --- a/org/postgresql/jdbc4/AbstractJdbc4Connection.java +++ b/org/postgresql/jdbc4/AbstractJdbc4Connection.java @@ -25,8 +25,6 @@ import java.util.Map; 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.PGProperty; import org.postgresql.core.Oid; @@ -43,11 +41,6 @@ public abstract class AbstractJdbc4Connection extends org.postgresql.jdbc3g.Abst { 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 { @@ -277,18 +270,17 @@ public abstract class AbstractJdbc4Connection extends org.postgresql.jdbc3g.Abst public String getSchema() throws SQLException { checkClosed(); - String searchPath; Statement stmt = createStatement(); try { - ResultSet rs = stmt.executeQuery( "SHOW search_path"); + ResultSet rs = stmt.executeQuery( "select current_schema()"); try { if (!rs.next()) { - return null; + return null; // Is it ever possible? } - searchPath = rs.getString(1); + return rs.getString(1); } finally { @@ -299,27 +291,30 @@ public abstract class AbstractJdbc4Connection extends org.postgresql.jdbc3g.Abst { stmt.close(); } + } - if (searchPath.startsWith("\"")) - { - // unquote the result if it's a quoted string - Matcher matcher = PATTERN_GET_SCHEMA.matcher(searchPath); - matcher.find(); - return matcher.group(1).replaceAll("\"\"", "\""); - } - else + public void setSchema(String schema) throws SQLException + { + checkClosed(); + Statement stmt = createStatement(); + try { - // keep only the first schema of the search path if there are many - int commaIndex = searchPath.indexOf(','); - if (commaIndex == -1) + if (schema == null) { - return searchPath; - } - else + stmt.executeUpdate("SET SESSION search_path TO DEFAULT"); + } else { - return searchPath.substring(0, commaIndex); + StringBuilder sb = new StringBuilder(); + sb.append("SET SESSION search_path TO '"); + Utils.escapeLiteral(sb, schema, getStandardConformingStrings()); + sb.append("'"); + stmt.executeUpdate(sb.toString()); } } + finally + { + stmt.close(); + } } public void abort(Executor executor) throws SQLException diff --git a/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java b/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java index dea4679b..158f5124 100644 --- a/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java +++ b/org/postgresql/test/jdbc4/jdbc41/SchemaTest.java @@ -20,6 +20,8 @@ public class SchemaTest extends TestCase private Connection _conn; + private boolean dropUserSchema; + public SchemaTest(String name) { super(name); @@ -29,6 +31,12 @@ public class SchemaTest extends TestCase { _conn = TestUtil.openDB(); Statement stmt = _conn.createStatement(); + try { + stmt.execute("CREATE SCHEMA " + TestUtil.getUser()); + dropUserSchema = true; + } catch (SQLException e) { + /* assume schema existed */ + } stmt.execute("CREATE SCHEMA schema1"); stmt.execute("CREATE SCHEMA schema2"); stmt.execute("CREATE SCHEMA \"schema 3\""); @@ -45,6 +53,9 @@ public class SchemaTest extends TestCase { _conn.setSchema(null); Statement stmt = _conn.createStatement(); + if (dropUserSchema) { + stmt.execute("DROP SCHEMA " + TestUtil.getUser() + " CASCADE"); + } stmt.execute("DROP SCHEMA schema1 CASCADE"); stmt.execute("DROP SCHEMA schema2 CASCADE"); stmt.execute("DROP SCHEMA \"schema 3\" CASCADE"); @@ -148,38 +159,10 @@ public class SchemaTest extends TestCase */ public void testMultipleSearchPath() throws SQLException { - Statement stmt = _conn.createStatement(); - try - { - stmt.execute("SET search_path TO schema1,schema2"); - } - finally - { - try - { - stmt.close(); - } - catch (SQLException e) - { - } - } + execute("SET search_path TO schema1,schema2"); assertEquals("schema1", _conn.getSchema()); - stmt = _conn.createStatement(); - try - { - stmt.execute("SET search_path TO \"schema ,6\",schema2"); - } - finally - { - try - { - stmt.close(); - } - catch (SQLException e) - { - } - } + execute("SET search_path TO \"schema ,6\",schema2"); assertEquals("schema ,6", _conn.getSchema()); } @@ -210,4 +193,30 @@ public class SchemaTest extends TestCase TestUtil.closeDB(conn); } } + + public void testSchemaPath$User() throws Exception + { + execute("SET search_path TO \"$user\",public,schema2"); + assertEquals(TestUtil.getUser(), _conn.getSchema()); + } + + private void execute(String sql) throws SQLException + { + Statement stmt = _conn.createStatement(); + try + { + stmt.execute(sql); + } + finally + { + try + { + stmt.close(); + } + catch (SQLException e) + { + } + } + } + } [parent: 543ffd0cc012]