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