From 1ed35b5f8b73eba5af692faad2ece30c83f62e47 Mon Sep 17 00:00:00 2001 From: v-xiangs Date: Fri, 24 Mar 2017 16:30:41 -0700 Subject: [PATCH 1/6] retrieve paramater encryption metadata if they are not retrieved yet --- .../sqlserver/jdbc/SQLServerPreparedStatement.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java index 5bb74adeb..1690fcf10 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java @@ -415,7 +415,13 @@ final void doExecutePreparedStatement(PrepStmtExecCmd command) throws SQLServerE boolean hasNewTypeDefinitions = buildPreparedStrings(inOutParam, false); if ((Util.shouldHonorAEForParameters(stmtColumnEncriptionSetting, connection)) && (0 < inOutParam.length) && !isInternalEncryptionQuery) { - getParameterEncryptionMetadata(inOutParam); + + // retrieve paramater encryption metadata if they are not retrieved yet + if (0 < inOutParam.length) { + if (null == inOutParam[0].getCryptoMetadata()) { + getParameterEncryptionMetadata(inOutParam); + } + } // maxRows is set to 0 when retreving encryption metadata, // need to set it back From bc8b7a42b87cbf6ed72a0907a592465eb39eb580 Mon Sep 17 00:00:00 2001 From: v-xiangs Date: Fri, 24 Mar 2017 16:42:23 -0700 Subject: [PATCH 2/6] remove length check since it's already done --- .../sqlserver/jdbc/SQLServerPreparedStatement.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java index 1690fcf10..ef37ef9c0 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java @@ -417,10 +417,8 @@ final void doExecutePreparedStatement(PrepStmtExecCmd command) throws SQLServerE if ((Util.shouldHonorAEForParameters(stmtColumnEncriptionSetting, connection)) && (0 < inOutParam.length) && !isInternalEncryptionQuery) { // retrieve paramater encryption metadata if they are not retrieved yet - if (0 < inOutParam.length) { - if (null == inOutParam[0].getCryptoMetadata()) { - getParameterEncryptionMetadata(inOutParam); - } + if (null == inOutParam[0].getCryptoMetadata()) { + getParameterEncryptionMetadata(inOutParam); } // maxRows is set to 0 when retreving encryption metadata, From 67e4306f046087b64d2d875ccd27a14f61b00c78 Mon Sep 17 00:00:00 2001 From: v-xiangs Date: Mon, 27 Mar 2017 12:50:25 -0700 Subject: [PATCH 3/6] instead of checking the first parameter, using a flag --- .../sqlserver/jdbc/SQLServerPreparedStatement.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java index ef37ef9c0..e7af2df4d 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java @@ -89,6 +89,11 @@ public class SQLServerPreparedStatement extends SQLServerStatement implements IS /** Flag set to true when statement execution is expected to return the prepared statement handle */ private boolean expectPrepStmtHandle = false; + + /** + * Flag set to true when all encryption metadata of inOutParam is retrieved + */ + private boolean encryptionMetadataISRetrieved = false; // Internal function used in tracing String getClassNameInternal() { @@ -202,6 +207,7 @@ final void closeInternal() { * @param sql */ /* L0 */ final void initParams(String sql) { + encryptionMetadataISRetrieved = false; int nParams = 0; // Figure out the expected number of parameters by counting the @@ -219,6 +225,7 @@ final void closeInternal() { /* L0 */ public final void clearParameters() throws SQLServerException { loggerExternal.entering(getClassNameLogging(), "clearParameters"); checkClosed(); + encryptionMetadataISRetrieved = false; int i; if (inOutParam == null) return; @@ -417,8 +424,9 @@ final void doExecutePreparedStatement(PrepStmtExecCmd command) throws SQLServerE if ((Util.shouldHonorAEForParameters(stmtColumnEncriptionSetting, connection)) && (0 < inOutParam.length) && !isInternalEncryptionQuery) { // retrieve paramater encryption metadata if they are not retrieved yet - if (null == inOutParam[0].getCryptoMetadata()) { + if (!encryptionMetadataISRetrieved) { getParameterEncryptionMetadata(inOutParam); + encryptionMetadataISRetrieved = true; } // maxRows is set to 0 when retreving encryption metadata, From 96b8f42ba9eeedbb91102fdf83bf24d58d39d8e5 Mon Sep 17 00:00:00 2001 From: v-xiangs Date: Mon, 27 Mar 2017 13:24:52 -0700 Subject: [PATCH 4/6] fix a typo --- .../sqlserver/jdbc/SQLServerPreparedStatement.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java index e7af2df4d..39d7ae14b 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java @@ -93,7 +93,7 @@ public class SQLServerPreparedStatement extends SQLServerStatement implements IS /** * Flag set to true when all encryption metadata of inOutParam is retrieved */ - private boolean encryptionMetadataISRetrieved = false; + private boolean encryptionMetadataIsRetrieved = false; // Internal function used in tracing String getClassNameInternal() { @@ -207,7 +207,7 @@ final void closeInternal() { * @param sql */ /* L0 */ final void initParams(String sql) { - encryptionMetadataISRetrieved = false; + encryptionMetadataIsRetrieved = false; int nParams = 0; // Figure out the expected number of parameters by counting the @@ -225,7 +225,7 @@ final void closeInternal() { /* L0 */ public final void clearParameters() throws SQLServerException { loggerExternal.entering(getClassNameLogging(), "clearParameters"); checkClosed(); - encryptionMetadataISRetrieved = false; + encryptionMetadataIsRetrieved = false; int i; if (inOutParam == null) return; @@ -424,9 +424,9 @@ final void doExecutePreparedStatement(PrepStmtExecCmd command) throws SQLServerE if ((Util.shouldHonorAEForParameters(stmtColumnEncriptionSetting, connection)) && (0 < inOutParam.length) && !isInternalEncryptionQuery) { // retrieve paramater encryption metadata if they are not retrieved yet - if (!encryptionMetadataISRetrieved) { + if (!encryptionMetadataIsRetrieved) { getParameterEncryptionMetadata(inOutParam); - encryptionMetadataISRetrieved = true; + encryptionMetadataIsRetrieved = true; } // maxRows is set to 0 when retreving encryption metadata, From bbb5e250c4523bdf4fa2b18c899983068aaa0154 Mon Sep 17 00:00:00 2001 From: v-xiangs Date: Tue, 28 Mar 2017 14:33:59 -0700 Subject: [PATCH 5/6] only call buildPreparedStrings for the first prepared statewment --- .../jdbc/SQLServerPreparedStatement.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java index 39d7ae14b..cee70a0a4 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java @@ -420,21 +420,25 @@ final void doExecutePreparedStatement(PrepStmtExecCmd command) throws SQLServerE loggerExternal.finer(toString() + " ActivityId: " + ActivityCorrelator.getNext().toString()); } - boolean hasNewTypeDefinitions = buildPreparedStrings(inOutParam, false); + boolean hasNewTypeDefinitions = true; + if (!encryptionMetadataIsRetrieved) { + hasNewTypeDefinitions = buildPreparedStrings(inOutParam, false); + } + if ((Util.shouldHonorAEForParameters(stmtColumnEncriptionSetting, connection)) && (0 < inOutParam.length) && !isInternalEncryptionQuery) { - + // retrieve paramater encryption metadata if they are not retrieved yet if (!encryptionMetadataIsRetrieved) { getParameterEncryptionMetadata(inOutParam); encryptionMetadataIsRetrieved = true; - } - // maxRows is set to 0 when retreving encryption metadata, - // need to set it back - setMaxRowsAndMaxFieldSize(); + // maxRows is set to 0 when retreving encryption metadata, + // need to set it back + setMaxRowsAndMaxFieldSize(); - // fix an issue when inserting unicode into non-encrypted nchar column using setString() and AE is on on Connection - buildPreparedStrings(inOutParam, true); + // fix an issue when inserting unicode into non-encrypted nchar column using setString() and AE is on on Connection + buildPreparedStrings(inOutParam, true); + } } // Start the request and detach the response reader so that we can From 277b50ea91b761c0055a99cc56edce59e21871fb Mon Sep 17 00:00:00 2001 From: v-xiangs Date: Tue, 28 Mar 2017 17:31:31 -0700 Subject: [PATCH 6/6] update hasNewTypeDefinitions when setter type changes --- .../sqlserver/jdbc/SQLServerPreparedStatement.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java index cee70a0a4..d3e19f04b 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java @@ -424,7 +424,7 @@ final void doExecutePreparedStatement(PrepStmtExecCmd command) throws SQLServerE if (!encryptionMetadataIsRetrieved) { hasNewTypeDefinitions = buildPreparedStrings(inOutParam, false); } - + if ((Util.shouldHonorAEForParameters(stmtColumnEncriptionSetting, connection)) && (0 < inOutParam.length) && !isInternalEncryptionQuery) { // retrieve paramater encryption metadata if they are not retrieved yet @@ -435,10 +435,10 @@ final void doExecutePreparedStatement(PrepStmtExecCmd command) throws SQLServerE // maxRows is set to 0 when retreving encryption metadata, // need to set it back setMaxRowsAndMaxFieldSize(); - - // fix an issue when inserting unicode into non-encrypted nchar column using setString() and AE is on on Connection - buildPreparedStrings(inOutParam, true); } + + // fix an issue when inserting unicode into non-encrypted nchar column using setString() and AE is on on Connection + hasNewTypeDefinitions = buildPreparedStrings(inOutParam, true); } // Start the request and detach the response reader so that we can