提交 78c9c1d6 authored 作者: Sergi Vladykin's avatar Sergi Vladykin

Support for setSchema/getSchema in JdbcConnection

上级 e635e003
......@@ -1649,6 +1649,11 @@ public class Session extends SessionWithState {
}
}
@Override
public boolean isRemote() {
return false;
}
/**
* Represents a savepoint (a position in a transaction to where one can roll
* back to).
......
......@@ -134,4 +134,10 @@ public interface SessionInterface extends Closeable {
*/
void addTemporaryLob(Value v);
/**
* Check if this session is remote or embedded.
*
* @return true if this session is remote
*/
boolean isRemote();
}
......@@ -853,4 +853,9 @@ public class SessionRemote extends SessionWithState implements DataHandler {
public CompareMode getCompareMode() {
return compareMode;
}
@Override
public boolean isRemote() {
return true;
}
}
......@@ -37,13 +37,16 @@ import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface;
import org.h2.engine.ConnectionInfo;
import org.h2.engine.Constants;
import org.h2.engine.Database;
import org.h2.engine.Mode;
import org.h2.engine.Session;
import org.h2.engine.SessionInterface;
import org.h2.engine.SessionRemote;
import org.h2.engine.SysProperties;
import org.h2.message.DbException;
import org.h2.message.TraceObject;
import org.h2.result.ResultInterface;
import org.h2.schema.Schema;
import org.h2.util.CloseWatcher;
import org.h2.util.JdbcUtils;
import org.h2.util.Utils;
......@@ -1888,21 +1891,48 @@ public class JdbcConnection extends TraceObject implements Connection, JdbcConne
}
/**
* [Not supported]
* Sets the given schema name to access. Current implementation is case sensitive,
* i.e. requires schema name to be passed in correct case.
*
* @param schema the schema
* @param schema the schema name
*/
@Override
public void setSchema(String schema) {
// not supported
public void setSchema(String schema) throws SQLException {
try {
if (isDebugEnabled()) {
debugCodeCall("setSchema", schema);
}
checkClosed();
if (session.isRemote()) {
throw DbException.getUnsupportedException("setSchema && remote session");
}
Database database = (Database) session.getDataHandler();
Schema s = database.getSchema(schema);
((Session) session).setCurrentSchema(s);
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* [Not supported]
* Retrieves this current schema name for this connection.
*
* @return current schema name
*/
@Override
public String getSchema() {
return null;
public String getSchema() throws SQLException {
try {
if (isDebugEnabled()) {
debugCodeCall("getSchema");
}
checkClosed();
if (session.isRemote()) {
throw DbException.getUnsupportedException("getSchema && remote session");
}
return ((Session) session).getCurrentSchemaName();
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
......
......@@ -5,6 +5,7 @@
*/
package org.h2.jdbc;
import java.sql.SQLException;
import java.util.concurrent.Executor;
/**
......@@ -12,13 +13,13 @@ import java.util.concurrent.Executor;
*/
public interface JdbcConnectionBackwardsCompat {
void setSchema(String schema);
void setSchema(String schema) throws SQLException;
String getSchema();
String getSchema() throws SQLException;
void abort(Executor executor);
void abort(Executor executor) throws SQLException;
void setNetworkTimeout(Executor executor, int milliseconds);
void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException;
int getNetworkTimeout();
int getNetworkTimeout() throws SQLException;
}
......@@ -5,11 +5,13 @@
*/
package org.h2.test.jdbc;
import org.h2.api.ErrorCode;
import org.h2.jdbc.JdbcConnectionBackwardsCompat;
import org.h2.test.TestBase;
import java.sql.Connection;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
......@@ -34,6 +36,7 @@ public class TestConnection extends TestBase {
testSetSupportedClientInfoProperties();
testSetUnsupportedClientInfoProperties();
testSetInternalProperty();
testSetGetSchema();
}
private void testSetInternalProperty() throws SQLException {
......@@ -85,4 +88,28 @@ public class TestConnection extends TestBase {
assertNull(conn.getClientInfo("UnknownProperty"));
}
private void testSetGetSchema() throws SQLException {
if (config.networked) {
return;
}
deleteDb("schemaSetGet");
Connection conn = getConnection("schemaSetGet");
Statement s = conn.createStatement();
s.executeUpdate("create schema my_test_schema");
s.executeUpdate("create table my_test_schema.my_test_table(id uuid, nave varchar)");
JdbcConnectionBackwardsCompat connx = (JdbcConnectionBackwardsCompat) conn;
assertEquals("PUBLIC", connx.getSchema());
assertThrows(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, s, "select * from my_test_table");
assertThrows(ErrorCode.SCHEMA_NOT_FOUND_1, connx).setSchema("my_test_table");
connx.setSchema("MY_TEST_SCHEMA");
assertEquals("MY_TEST_SCHEMA", connx.getSchema());
s.executeQuery("select * from my_test_table");
assertThrows(ErrorCode.SCHEMA_NOT_FOUND_1, connx).setSchema("NON_EXISTING_SCHEMA");
assertEquals("MY_TEST_SCHEMA", connx.getSchema());
s.executeUpdate("create schema \"otheR_schEma\"");
connx.setSchema("otheR_schEma");
assertEquals("otheR_schEma", connx.getSchema());
s.close();
conn.close();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论