You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafodion.apache.org by se...@apache.org on 2018/12/21 01:31:07 UTC
[1/2] trafodion git commit: [TRAFODION-3250] optimize get/set schema
Repository: trafodion
Updated Branches:
refs/heads/master dd184e01d -> 923f0a919
[TRAFODION-3250] optimize get/set schema
Project: http://git-wip-us.apache.org/repos/asf/trafodion/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafodion/commit/018f5efb
Tree: http://git-wip-us.apache.org/repos/asf/trafodion/tree/018f5efb
Diff: http://git-wip-us.apache.org/repos/asf/trafodion/diff/018f5efb
Branch: refs/heads/master
Commit: 018f5efbd234ca7b1fe1fadada21c58db1d64c3e
Parents: 7e94e79
Author: Aven <sh...@esgyn.cn>
Authored: Fri Dec 21 01:02:16 2018 +0800
Committer: Aven <sh...@esgyn.cn>
Committed: Fri Dec 21 01:02:16 2018 +0800
----------------------------------------------------------------------
.../jdbc/t4/CONNECTION_CONTEXT_def.java | 10 +-
.../trafodion/jdbc/t4/InterfaceConnection.java | 38 +++++--
.../trafodion/jdbc/t4/InterfaceStatement.java | 29 ++++-
.../jdbc/t4/OUT_CONNECTION_CONTEXT_def.java | 10 +-
.../org/trafodion/jdbc/t4/TrafT4Connection.java | 113 ++++++-------------
5 files changed, 110 insertions(+), 90 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/trafodion/blob/018f5efb/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/CONNECTION_CONTEXT_def.java
----------------------------------------------------------------------
diff --git a/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/CONNECTION_CONTEXT_def.java b/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/CONNECTION_CONTEXT_def.java
index 9f5c0ed..f8e9701 100644
--- a/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/CONNECTION_CONTEXT_def.java
+++ b/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/CONNECTION_CONTEXT_def.java
@@ -27,7 +27,7 @@ import java.nio.charset.UnsupportedCharsetException;
class CONNECTION_CONTEXT_def {
String datasource = "";
String catalog = "";
- String schema = "";
+ private String schema = "";
String location = "";
String userRole = "";
@@ -147,4 +147,12 @@ class CONNECTION_CONTEXT_def {
clientVersionList.insertIntoByteArray(buf);
}
+
+ public String getSchema() {
+ return schema;
+ }
+
+ public void setSchema(String schema) {
+ this.schema = schema;
+ }
}
http://git-wip-us.apache.org/repos/asf/trafodion/blob/018f5efb/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InterfaceConnection.java
----------------------------------------------------------------------
diff --git a/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InterfaceConnection.java b/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InterfaceConnection.java
index 6cd0601..d23d615 100644
--- a/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InterfaceConnection.java
+++ b/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InterfaceConnection.java
@@ -96,6 +96,7 @@ class InterfaceConnection {
static final short SQL_ATTR_ACCESS_MODE = 101;
static final short SQL_ATTR_AUTOCOMMIT = 102;
static final short SQL_TXN_ISOLATION = 108;
+ static final short SET_SCHEMA = 1001; // this value is follow server side definition
// spj proxy syntax support
static final short SPJ_ENABLE_PROXY = 1040;
@@ -217,7 +218,7 @@ class InterfaceConnection {
private CONNECTION_CONTEXT_def getInContext(T4Properties t4props) {
inContext = new CONNECTION_CONTEXT_def();
inContext.catalog = t4props.getCatalog();
- inContext.schema = t4props.getSchema();
+ inContext.setSchema(t4props.getSchema());
inContext.datasource = t4props.getServerDataSource();
inContext.userRole = t4props.getRoleName();
inContext.cpuToUse = t4props.getCpuToUse();
@@ -417,13 +418,34 @@ class InterfaceConnection {
return userDesc.userName;
}
- String getSchema() {
- if (outContext != null) {
- return outContext.schema;
- } else {
- return inContext.schema;
- }
- }
+ String getSchema() {
+ if (outContext != null) {
+ return outContext.getSchema();
+ } else {
+ return inContext.getSchema();
+ }
+ }
+
+ void setSchemaDirect(String schema) {
+ outContext.setSchema(schema);
+ }
+ void setSchema(TrafT4Connection conn, String schema) throws SQLException {
+ if (t4props_.t4Logger_.isLoggable(Level.FINEST) == true) {
+ Object p[] = T4LoggingUtilities.makeParams(conn.props_, schema);
+ String temp = "Setting connection schema = " + schema;
+ t4props_.t4Logger_.logp(Level.FINEST, "InterfaceConnection", "setSchema", temp, p);
+ }
+ if (schema == null || schema.length() == 0) {
+ return;
+ }
+ setConnectionAttr(conn, SET_SCHEMA, 0, schema);
+ setSchemaDirect(schema);
+ if (t4props_.t4Logger_.isLoggable(Level.FINEST) == true) {
+ Object p[] = T4LoggingUtilities.makeParams(conn.props_, schema);
+ String temp = "Setting connection schema = " + schema + " is done.";
+ t4props_.t4Logger_.logp(Level.FINEST, "InterfaceConnection", "setSchema", temp, p);
+ }
+ }
void setLocale(Locale locale) {
this.locale = locale;
http://git-wip-us.apache.org/repos/asf/trafodion/blob/018f5efb/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InterfaceStatement.java
----------------------------------------------------------------------
diff --git a/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InterfaceStatement.java b/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InterfaceStatement.java
index f5958de..d9d7c2c 100644
--- a/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InterfaceStatement.java
+++ b/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InterfaceStatement.java
@@ -33,6 +33,8 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import java.util.logging.Level;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
class InterfaceStatement {
InterfaceConnection ic_;
@@ -1403,8 +1405,9 @@ class InterfaceStatement {
else if (er.returnCode == TRANSPORT.SQL_SUCCESS || er.returnCode == TRANSPORT.SQL_SUCCESS_WITH_INFO
|| er.returnCode == TRANSPORT.NO_DATA_FOUND) {
Arrays.fill(stmt.batchRowCount_, -2); // fill with success
- if (er.errorList != null) // if we had errors with valid rowIds,
- // update the array
+
+ // if we had errors with valid rowIds, update the array
+ if (er.errorList != null)
{
for (int i = 0; i < er.errorList.length; i++) {
int row = er.errorList[i].rowId - 1;
@@ -1414,7 +1417,15 @@ class InterfaceStatement {
}
}
}
-
+
+ // Sometimes users may set schema through stmt.exec("set schema xx") instead of
+ // conn.setSchema("xx"), so there need to update local schema when it success
+ if (this.sqlQueryType_ == TRANSPORT.SQL_SET_SCHEMA) {
+ String schema = extractSchema(sqlString);
+ System.out.println("extract schema : "+schema);
+ ic_.setSchemaDirect(schema);
+ }
+
//set the statement mode as the command succeeded
if (sqlStmtType_ == TRANSPORT.TYPE_QS_OPEN) {
this.ic_.setMode(InterfaceConnection.MODE_WMS);
@@ -1492,7 +1503,17 @@ class InterfaceStatement {
}
}
- protected void setTransactionStatus(TrafT4Connection conn, String sql) {
+ private String extractSchema(String sqlString) {
+ String schemaRegex = "(SET)\\s+(SCHEMA)\\s+([a-zA-Z0-9]+\\s*\\.)\\s*([a-zA-Z0-9]+)\\s*";
+ Pattern pattern = Pattern.compile(schemaRegex);
+ Matcher m = pattern.matcher(sqlString.toUpperCase());
+ while (m.find()) {
+ return m.group(m.groupCount());
+ }
+ return "";
+ }
+
+ protected void setTransactionStatus(TrafT4Connection conn, String sql) {
short tranStatus = getTransactionStatus(sql);
if(tranStatus == TRANSPORT.TYPE_BEGIN_TRANSACTION){
conn.setBeginTransaction(true);
http://git-wip-us.apache.org/repos/asf/trafodion/blob/018f5efb/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/OUT_CONNECTION_CONTEXT_def.java
----------------------------------------------------------------------
diff --git a/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/OUT_CONNECTION_CONTEXT_def.java b/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/OUT_CONNECTION_CONTEXT_def.java
index b7b07dc..5ad2aae 100644
--- a/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/OUT_CONNECTION_CONTEXT_def.java
+++ b/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/OUT_CONNECTION_CONTEXT_def.java
@@ -38,7 +38,7 @@ class OUT_CONNECTION_CONTEXT_def {
String computerName;
String catalog;
- String schema;
+ private String schema;
int optionFlags1;
int optionFlags2;
@@ -94,4 +94,12 @@ class OUT_CONNECTION_CONTEXT_def {
}
}
}
+
+ public String getSchema() {
+ return schema;
+ }
+
+ public void setSchema(String schema) {
+ this.schema = schema;
+ }
}
http://git-wip-us.apache.org/repos/asf/trafodion/blob/018f5efb/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/TrafT4Connection.java
----------------------------------------------------------------------
diff --git a/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/TrafT4Connection.java b/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/TrafT4Connection.java
index ce7c980..bae5a37 100644
--- a/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/TrafT4Connection.java
+++ b/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/TrafT4Connection.java
@@ -258,35 +258,16 @@ public class TrafT4Connection extends PreparedStatementManager implements java.s
return ic_.getCatalog();
}
- public String getSchema() throws SQLException {
- if (props_.t4Logger_.isLoggable(Level.FINE) == true) {
- Object p[] = T4LoggingUtilities.makeParams(props_);
- props_.t4Logger_.logp(Level.FINE, "TrafT4Connection", "getSchema", "", p);
- }
-
- Statement s = null;
- ResultSet rs = null;
- String sch = null;
-
- try {
- s = this.createStatement();
- rs = s.executeQuery("SHOWCONTROL DEFAULT SCHEMA, match full, no header");
- rs.next();
- sch = rs.getString(1);
- if(sch.charAt(0) != '\"' && sch.indexOf('.') != -1) {
- sch = sch.substring(sch.indexOf('.') + 1);
- }
- }catch(SQLException e) {
- sch = ic_.getSchema();
- }finally {
- if(rs != null)
- rs.close();
- if(s != null)
- s.close();
- }
-
- return sch;
- }
+ public String getSchema() throws SQLException {
+ if (props_.t4Logger_.isLoggable(Level.FINE) == true) {
+ Object p[] = T4LoggingUtilities.makeParams(props_);
+ props_.t4Logger_.logp(Level.FINE, "TrafT4Connection", "getSchema", "", p);
+ }
+
+ validateConnection();
+
+ return ic_.getSchema();
+ }
public int getHoldability() throws SQLException {
if (props_.t4Logger_.isLoggable(Level.FINE) == true) {
@@ -1922,54 +1903,34 @@ public class TrafT4Connection extends PreparedStatementManager implements java.s
return null;
}
- public void setSchema(String schema) throws SQLException {
- if (props_.t4Logger_.isLoggable(Level.FINE) == true) {
- Object p[] = T4LoggingUtilities.makeParams(props_, schema);
- props_.t4Logger_.logp(Level.FINE, "TrafT4Connection", "setSchema", "", p);
- }
- if (props_.getLogWriter() != null) {
- LogRecord lr = new LogRecord(Level.FINE, "");
- Object p[] = T4LoggingUtilities.makeParams(props_, schema);
- lr.setParameters(p);
- lr.setSourceClassName("TrafT4Connection");
- lr.setSourceMethodName("setSchema");
- T4LogFormatter lf = new T4LogFormatter();
- String temp = lf.format(lr);
- props_.getLogWriter().println(temp);
+ public void setSchema(String schema) throws SQLException {
+ if (props_.t4Logger_.isLoggable(Level.FINE) == true) {
+ Object p[] = T4LoggingUtilities.makeParams(props_, schema);
+ props_.t4Logger_.logp(Level.FINE, "TrafT4Connection", "setSchema", "", p);
+ }
+ if (props_.getLogWriter() != null) {
+ LogRecord lr = new LogRecord(Level.FINE, "");
+ Object p[] = T4LoggingUtilities.makeParams(props_, schema);
+ lr.setParameters(p);
+ lr.setSourceClassName("TrafT4Connection");
+ lr.setSourceMethodName("setSchema");
+ T4LogFormatter lf = new T4LogFormatter();
+ String temp = lf.format(lr);
+ props_.getLogWriter().println(temp);
+ }
+ clearWarnings();
+ if (_isClosed() == true) {
+ throw TrafT4Messages.createSQLException(props_, null, "invalid_connection", null);
+ }
+ if (schema != null) {
+ try {
+ ic_.setSchema(this, schema);
+ } catch (TrafT4Exception se) {
+ performConnectionErrorChecks(se);
+ throw se;
}
- clearWarnings();
- if (_isClosed() == true) {
- throw TrafT4Messages.createSQLException(props_, null, "invalid_connection", null);
- }
- if (schema != null && !"".equals(schema)) {
- Statement stmt = null;
- try {
- stmt = createStatement();
- stmt.execute("set schema " + schema);
- } catch (TrafT4Exception se) {
- performConnectionErrorChecks(se);
- throw se;
- } finally {
- if (stmt != null) {
- try {
- stmt.close();
- } catch (Exception e) {
- if (props_.getLogWriter() != null) {
- LogRecord lr = new LogRecord(Level.WARNING, "");
- Object p[] = T4LoggingUtilities.makeParams(props_, e.getMessage());
- lr.setParameters(p);
- lr.setSourceClassName("TrafT4Connection");
- lr.setSourceMethodName("setSchema");
- T4LogFormatter lf = new T4LogFormatter();
- String temp = lf.format(lr);
- props_.getLogWriter().println(temp);
- }
- }
- }
- }
- }
-
- }
+ }
+ }
public void abort(Executor executor) throws SQLException {
if (ic_.getT4Connection().getInputOutput() != null) {
[2/2] trafodion git commit: Merge PR 1764 optimize get/set schema
Posted by se...@apache.org.
Merge PR 1764 optimize get/set schema
Project: http://git-wip-us.apache.org/repos/asf/trafodion/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafodion/commit/923f0a91
Tree: http://git-wip-us.apache.org/repos/asf/trafodion/tree/923f0a91
Diff: http://git-wip-us.apache.org/repos/asf/trafodion/diff/923f0a91
Branch: refs/heads/master
Commit: 923f0a9197f545b5f7a8762ed9fd23430adcd9a2
Parents: dd184e0 018f5ef
Author: selvaganesang <se...@apache.org>
Authored: Fri Dec 21 01:29:37 2018 +0000
Committer: selvaganesang <se...@apache.org>
Committed: Fri Dec 21 01:29:37 2018 +0000
----------------------------------------------------------------------
.../jdbc/t4/CONNECTION_CONTEXT_def.java | 10 +-
.../trafodion/jdbc/t4/InterfaceConnection.java | 38 +++++--
.../trafodion/jdbc/t4/InterfaceStatement.java | 29 ++++-
.../jdbc/t4/OUT_CONNECTION_CONTEXT_def.java | 10 +-
.../org/trafodion/jdbc/t4/TrafT4Connection.java | 113 ++++++-------------
5 files changed, 110 insertions(+), 90 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/trafodion/blob/923f0a91/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InterfaceConnection.java
----------------------------------------------------------------------