提交 a655cd87 authored 作者: Noel Grandin's avatar Noel Grandin

some cleanups for the ClientInfo pull

- allocate clientInfo on demand
- move getMode() from JdbcDatabaseMetaData to JdbcConnection so we only
need to fetch it once per connection
- add minimal support for JdbcDatabaseMetaData#getClientInfoProperties
上级 460a7531
......@@ -21,6 +21,8 @@ Change Log
<h2>Next Version (unreleased)</h2>
<ul>
<li>Added support for Connection.setClientInfo() in compatibility modes for DB2, Postgresql, Oracle and MySQL.
</li>
<li>Issue #249: Clarify license declaration in Maven POM xml
</li>
<li>Fix NPE in querying spatial data through a sub-select.
......@@ -52,9 +54,6 @@ table with an LOB column.
when concurrently writing into MVStore.
</li>
<ul>
<li>Added support for Connection.setClientInfo() in compatibility modes for DB2, Postgresql, Oracle and MySQL.
</li>
</ul>
<h2>Version 1.4.191 Beta (2016-01-21)</h2>
<ul>
......
......@@ -27,6 +27,16 @@ import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
/*## Java 1.7 ##
import java.util.concurrent.Executor;
//*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;
import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface;
import org.h2.engine.ConnectionInfo;
......@@ -47,17 +57,6 @@ import org.h2.value.ValueInt;
import org.h2.value.ValueNull;
import org.h2.value.ValueString;
/*## Java 1.7 ##
import java.util.concurrent.Executor;
//*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;
/**
* <p>
* Represents a connection (session) to a database.
......@@ -94,6 +93,7 @@ public class JdbcConnection extends TraceObject implements Connection {
private int queryTimeoutCache = -1;
private Map<String, String> clientInfo;
private String mode;
/**
* INTERNAL
......@@ -128,7 +128,6 @@ public class JdbcConnection extends TraceObject implements Connection {
this.url = ci.getURL();
closeOld();
watcher = CloseWatcher.register(this, session, keepOpenStackTrace);
this.clientInfo = new HashMap<String, String>();
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -152,7 +151,9 @@ public class JdbcConnection extends TraceObject implements Connection {
this.getReadOnly = clone.getReadOnly;
this.rollback = clone.rollback;
this.watcher = null;
this.clientInfo = new HashMap<String, String>(clone.clientInfo);
if (clone.clientInfo != null) {
this.clientInfo = new HashMap<String, String>(clone.clientInfo);
}
}
/**
......@@ -1705,9 +1706,13 @@ public class JdbcConnection extends TraceObject implements Connection {
throw new SQLClientInfoException("Property name '" + name + " is used internally by H2.",
Collections.<String, ClientInfoStatus> emptyMap());
}
Pattern clientInfoNameRegEx = getMode().supportedClientInfoPropertiesRegEx;
Pattern clientInfoNameRegEx = Mode.getInstance(getMode()).supportedClientInfoPropertiesRegEx;
if (clientInfoNameRegEx != null && clientInfoNameRegEx.matcher(name).matches()) {
if (clientInfo == null) {
clientInfo = new HashMap<String, String>();
}
clientInfo.put(name, value);
} else {
throw new SQLClientInfoException("Client info name '" + name + "' not supported.",
......@@ -1718,14 +1723,10 @@ public class JdbcConnection extends TraceObject implements Connection {
}
}
private boolean isInternalProperty(String name) {
private static boolean isInternalProperty(String name) {
return NUM_SERVERS.equals(name) || name.startsWith(PREFIX_SERVER);
}
private Mode getMode() throws SQLException {
return Mode.getInstance(((JdbcDatabaseMetaData) getMetaData()).getMode());
}
private static SQLClientInfoException convertToClientInfoException(
SQLException x) {
if (x instanceof SQLClientInfoException) {
......@@ -1750,7 +1751,11 @@ public class JdbcConnection extends TraceObject implements Connection {
debugCode("setClientInfo(properties);");
}
checkClosed();
clientInfo.clear();
if (clientInfo == null) {
clientInfo = new HashMap<String, String>();
} else {
clientInfo.clear();
}
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
setClientInfo((String) entry.getKey(), (String) entry.getValue());
}
......@@ -1774,8 +1779,10 @@ public class JdbcConnection extends TraceObject implements Connection {
ArrayList<String> serverList = session.getClusterServers();
Properties p = new Properties();
for (Map.Entry<String, String> entry : clientInfo.entrySet()) {
p.setProperty(entry.getKey(), entry.getValue());
if (clientInfo != null) {
for (Map.Entry<String, String> entry : clientInfo.entrySet()) {
p.setProperty(entry.getKey(), entry.getValue());
}
}
p.setProperty(NUM_SERVERS, String.valueOf(serverList.size()));
......@@ -1994,4 +2001,17 @@ public class JdbcConnection extends TraceObject implements Connection {
trace.setLevel(level);
}
String getMode() throws SQLException {
if (mode == null) {
PreparedStatement prep = prepareStatement(
"SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS WHERE NAME=?");
prep.setString(1, "MODE");
ResultSet rs = prep.executeQuery();
rs.next();
mode = rs.getString(1);
prep.close();
}
return mode;
}
}
......@@ -11,8 +11,8 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.RowIdLifetime;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;
import org.h2.engine.Constants;
import org.h2.engine.SysProperties;
import org.h2.message.DbException;
......@@ -29,7 +29,6 @@ public class JdbcDatabaseMetaData extends TraceObject implements
DatabaseMetaData {
private final JdbcConnection conn;
private String mode;
JdbcDatabaseMetaData(JdbcConnection conn, Trace trace, int id) {
setTrace(trace, TraceObject.DATABASE_META_DATA, id);
......@@ -2452,7 +2451,7 @@ public class JdbcDatabaseMetaData extends TraceObject implements
@Override
public boolean supportsMixedCaseQuotedIdentifiers() throws SQLException {
debugCodeCall("supportsMixedCaseQuotedIdentifiers");
String m = getMode();
String m = conn.getMode();
if (m.equals("MySQL")) {
return false;
}
......@@ -2468,7 +2467,7 @@ public class JdbcDatabaseMetaData extends TraceObject implements
@Override
public boolean storesUpperCaseIdentifiers() throws SQLException {
debugCodeCall("storesUpperCaseIdentifiers");
String m = getMode();
String m = conn.getMode();
if (m.equals("MySQL")) {
return false;
}
......@@ -2484,7 +2483,7 @@ public class JdbcDatabaseMetaData extends TraceObject implements
@Override
public boolean storesLowerCaseIdentifiers() throws SQLException {
debugCodeCall("storesLowerCaseIdentifiers");
String m = getMode();
String m = conn.getMode();
if (m.equals("MySQL")) {
return true;
}
......@@ -2512,7 +2511,7 @@ public class JdbcDatabaseMetaData extends TraceObject implements
@Override
public boolean storesUpperCaseQuotedIdentifiers() throws SQLException {
debugCodeCall("storesUpperCaseQuotedIdentifiers");
String m = getMode();
String m = conn.getMode();
if (m.equals("MySQL")) {
return true;
}
......@@ -2528,7 +2527,7 @@ public class JdbcDatabaseMetaData extends TraceObject implements
@Override
public boolean storesLowerCaseQuotedIdentifiers() throws SQLException {
debugCodeCall("storesLowerCaseQuotedIdentifiers");
String m = getMode();
String m = conn.getMode();
if (m.equals("MySQL")) {
return true;
}
......@@ -2544,7 +2543,7 @@ public class JdbcDatabaseMetaData extends TraceObject implements
@Override
public boolean storesMixedCaseQuotedIdentifiers() throws SQLException {
debugCodeCall("storesMixedCaseQuotedIdentifiers");
String m = getMode();
String m = conn.getMode();
if (m.equals("MySQL")) {
return false;
}
......@@ -3074,14 +3073,16 @@ public class JdbcDatabaseMetaData extends TraceObject implements
return false;
}
/**
* [Not supported] Returns the client info properties.
*/
@Override
public ResultSet getClientInfoProperties() throws SQLException {
Properties clientInfo = conn.getClientInfo();
// we don't have any client properties, so return an empty result set
return new SimpleResultSet();
SimpleResultSet result = new SimpleResultSet();
result.addColumn("Name", Types.VARCHAR, 0, 0);
result.addColumn("Value", Types.VARCHAR, 0, 0);
for (Object key : clientInfo.keySet()) {
result.addRow(key, clientInfo.get(key));
}
return result;
}
/**
......@@ -3166,17 +3167,4 @@ public class JdbcDatabaseMetaData extends TraceObject implements
return getTraceObjectName() + ": " + conn;
}
String getMode() throws SQLException {
if (mode == null) {
PreparedStatement prep = conn.prepareStatement(
"SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS WHERE NAME=?");
prep.setString(1, "MODE");
ResultSet rs = prep.executeQuery();
rs.next();
mode = rs.getString(1);
prep.close();
}
return mode;
}
}
......@@ -13,7 +13,6 @@ import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.test.TestBase;
......@@ -1201,6 +1200,7 @@ public class TestMetaData extends TestBase {
assertNull(conn.getClientInfo("xxx"));
DatabaseMetaData meta = conn.getMetaData();
ResultSet rs = meta.getClientInfoProperties();
assertTrue(rs.next());
assertFalse(rs.next());
conn.close();
deleteDb("metaData");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论