Skip to content

Commit

Permalink
Added JDBC4 schema support
Browse files Browse the repository at this point in the history
  • Loading branch information
alexismeneses committed Apr 30, 2014
1 parent 1fd09e4 commit ea55458
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 2 deletions.
46 changes: 44 additions & 2 deletions org/postgresql/jdbc4/AbstractJdbc4Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,54 @@ public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedE

public void setSchema(String schema) throws SQLException
{
throw org.postgresql.Driver.notImplemented(this.getClass(), "setSchema(String)");
checkClosed();
Statement stmt = createStatement();
try
{
stmt.executeUpdate("SET SESSION SCHEMA '" + schema + "'");
}
finally
{
stmt.close();
}
}

public String getSchema() throws SQLException
{
throw org.postgresql.Driver.notImplemented(this.getClass(), "getSchema()");
checkClosed();
String searchPath;
Statement stmt = createStatement();
try
{
ResultSet rs = stmt.executeQuery( "SHOW search_path");
try
{
if (!rs.next())
{
return null;
}
searchPath = rs.getString(1);
}
finally
{
rs.close();
}
}
finally
{
stmt.close();
}

// 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);
}
}

public void abort(Executor executor) throws SQLException
Expand Down
1 change: 1 addition & 0 deletions org/postgresql/test/jdbc4/Jdbc4TestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static TestSuite suite() throws Exception
suite.addTestSuite(WrapperTest.class);
suite.addTestSuite(BinaryTest.class);
suite.addTestSuite(IsValidTest.class);
suite.addTestSuite(SchemaTest.class);

Connection connection = TestUtil.openDB();
try
Expand Down
132 changes: 132 additions & 0 deletions org/postgresql/test/jdbc4/SchemaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*-------------------------------------------------------------------------
*
* Copyright (c) 2010-2014, PostgreSQL Global Development Group
*
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc4;

import java.sql.*;
import junit.framework.TestCase;
import org.postgresql.test.TestUtil;

public class SchemaTest extends TestCase
{

private Connection _conn;

public SchemaTest(String name)
{
super(name);
}

protected void setUp() throws Exception
{
_conn = TestUtil.openDB();
Statement stmt = _conn.createStatement();
stmt.execute("CREATE SCHEMA schema1");
stmt.execute("CREATE SCHEMA schema2");
TestUtil.createTable(_conn, "schema1.table1", "id integer");
TestUtil.createTable(_conn, "schema2.table2", "id integer");
}

protected void tearDown() throws SQLException
{
Statement stmt = _conn.createStatement();
stmt.execute("DROP SCHEMA schema1 CASCADE");
stmt.execute("DROP SCHEMA schema2 CASCADE");
TestUtil.closeDB(_conn);
}

/**
* Test that what you set is what you get
*/
public void testGetSetSchema() throws SQLException
{
_conn.setSchema("schema1");
assertEquals("schema1", _conn.getSchema());
_conn.setSchema("schema2");
assertEquals("schema2", _conn.getSchema());
}

/**
* Test that setting the schema allows to access objects of this schema
* without prefix, hide objects from other schemas but doesn't prevent
* to prefix-access to them.
*/
public void testUsingSchema() throws SQLException
{
Statement stmt = _conn.createStatement();
try
{
try
{
_conn.setSchema("schema1");
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
}

_conn.setSchema("schema2");
stmt.executeQuery(TestUtil.selectSQL("table2", "*"));
stmt.executeQuery(TestUtil.selectSQL("schema1.table1", "*"));
try
{
stmt.executeQuery(TestUtil.selectSQL("table1", "*"));
fail("Objects of schema1 should not be visible without prefix");
}
catch (SQLException e)
{
// expected
}
}
catch (SQLException e)
{
fail("Could not find expected schema elements: " + e.getMessage());
}
}
finally
{
try
{
stmt.close();
}
catch (SQLException e)
{
}
}
}

/**
* Test that get schema returns the schema with the highest priority
* in the search path
*/
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)
{
}
}
assertEquals("schema1", _conn.getSchema());
}

}

0 comments on commit ea55458

Please sign in to comment.