From 257d6e1ba389cbb1c792fdadc572d6247ca95c82 Mon Sep 17 00:00:00 2001 From: Afsaneh Rafighi Date: Mon, 18 Dec 2017 13:43:36 -0800 Subject: [PATCH 1/7] use wrapper for sharding APIs --- .../jdbc/ISQLServerConnection43.java | 20 ++++++++++ .../jdbc/ISQLServerDataSource43.java | 12 ++++++ .../sqlserver/jdbc/SQLServerConnection.java | 25 ------------- .../sqlserver/jdbc/SQLServerConnection43.java | 37 +++++++++++++++++++ .../sqlserver/jdbc/SQLServerDataSource.java | 19 ++++------ .../sqlserver/jdbc/SQLServerDataSource43.java | 24 ++++++++++++ .../sqlserver/jdbc/SQLServerDriver.java | 7 +++- .../sqlserver/jdbc/SQLServerXAConnection.java | 8 +++- .../com/microsoft/sqlserver/jdbc/Util.java | 22 +++++++++++ 9 files changed, 135 insertions(+), 39 deletions(-) create mode 100644 src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerConnection43.java create mode 100644 src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerDataSource43.java create mode 100644 src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection43.java create mode 100644 src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource43.java diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerConnection43.java b/src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerConnection43.java new file mode 100644 index 000000000..13695e703 --- /dev/null +++ b/src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerConnection43.java @@ -0,0 +1,20 @@ +package com.microsoft.sqlserver.jdbc; + +import java.sql.SQLFeatureNotSupportedException; +import java.sql.ShardingKey; + +public interface ISQLServerConnection43 extends ISQLServerConnection { + + public void setShardingKey(ShardingKey shardingKey) throws SQLFeatureNotSupportedException; + + public void setShardingKey(ShardingKey shardingKey, + ShardingKey superShardingKey) throws SQLFeatureNotSupportedException; + + public boolean setShardingKeyIfValid(ShardingKey shardingKey, + int timeout) throws SQLFeatureNotSupportedException; + + public boolean setShardingKeyIfValid(ShardingKey shardingKey, + ShardingKey superShardingKey, + int timeout) throws SQLFeatureNotSupportedException; + +} diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerDataSource43.java b/src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerDataSource43.java new file mode 100644 index 000000000..47cbe003c --- /dev/null +++ b/src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerDataSource43.java @@ -0,0 +1,12 @@ +package com.microsoft.sqlserver.jdbc; + +import java.sql.ConnectionBuilder; +import java.sql.SQLException; +import java.sql.ShardingKeyBuilder; + +public interface ISQLServerDataSource43 extends ISQLServerDataSource { + + public ShardingKeyBuilder createShardingKeyBuilder() throws SQLException; + + public ConnectionBuilder createConnectionBuilder() throws SQLException; +} diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java index 34e72b207..3bc6e8c66 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java @@ -33,7 +33,6 @@ import java.sql.SQLWarning; import java.sql.SQLXML; import java.sql.Savepoint; -import java.sql.ShardingKey; import java.sql.Statement; import java.sql.Struct; import java.text.MessageFormat; @@ -5259,30 +5258,6 @@ public void endRequest() throws SQLFeatureNotSupportedException { throw new SQLFeatureNotSupportedException("endRequest not implemented"); } - public void setShardingKey(ShardingKey shardingKey) throws SQLFeatureNotSupportedException { - DriverJDBCVersion.checkSupportsJDBC43(); - throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); - } - - public void setShardingKey(ShardingKey shardingKey, - ShardingKey superShardingKey) throws SQLFeatureNotSupportedException { - DriverJDBCVersion.checkSupportsJDBC43(); - throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); - } - - public boolean setShardingKeyIfValid(ShardingKey shardingKey, - int timeout) throws SQLFeatureNotSupportedException { - DriverJDBCVersion.checkSupportsJDBC43(); - throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); - } - - public boolean setShardingKeyIfValid(ShardingKey shardingKey, - ShardingKey superShardingKey, - int timeout) throws SQLFeatureNotSupportedException { - DriverJDBCVersion.checkSupportsJDBC43(); - throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); - } - /** * Replace JDBC syntax parameter markets '?' with SQL Server paramter markers @p1, @p2 etc... * diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection43.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection43.java new file mode 100644 index 000000000..44e348d72 --- /dev/null +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection43.java @@ -0,0 +1,37 @@ +package com.microsoft.sqlserver.jdbc; + +import java.sql.SQLFeatureNotSupportedException; +import java.sql.ShardingKey; + +public class SQLServerConnection43 extends SQLServerConnection implements ISQLServerConnection43 { + + SQLServerConnection43(String parentInfo) throws SQLServerException { + super(parentInfo); + // TODO Auto-generated constructor stub + } + + public void setShardingKey(ShardingKey shardingKey) throws SQLFeatureNotSupportedException { + DriverJDBCVersion.checkSupportsJDBC43(); + throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); + } + + public void setShardingKey(ShardingKey shardingKey, + ShardingKey superShardingKey) throws SQLFeatureNotSupportedException { + DriverJDBCVersion.checkSupportsJDBC43(); + throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); + } + + public boolean setShardingKeyIfValid(ShardingKey shardingKey, + int timeout) throws SQLFeatureNotSupportedException { + DriverJDBCVersion.checkSupportsJDBC43(); + throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); + } + + public boolean setShardingKeyIfValid(ShardingKey shardingKey, + ShardingKey superShardingKey, + int timeout) throws SQLFeatureNotSupportedException { + DriverJDBCVersion.checkSupportsJDBC43(); + throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); + } + +} diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java index cfb1081cb..ebe4fee35 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java @@ -10,10 +10,8 @@ import java.io.PrintWriter; import java.sql.Connection; -import java.sql.ConnectionBuilder; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; -import java.sql.ShardingKeyBuilder; import java.util.Enumeration; import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; @@ -1005,7 +1003,13 @@ SQLServerConnection getConnectionInternal(String username, // Create new connection and connect. if (dsLogger.isLoggable(Level.FINER)) dsLogger.finer(toString() + " Begin create new connection."); - SQLServerConnection result = new SQLServerConnection(toString()); + SQLServerConnection result = null; + if (Util.use43Wrapper()) { + result = new SQLServerConnection43(toString()); + } + else { + result = new SQLServerConnection(toString()); + } result.connect(mergedProps, pooledConnection); if (dsLogger.isLoggable(Level.FINER)) dsLogger.finer(toString() + " End create new connection " + result.toString()); @@ -1107,15 +1111,6 @@ public T unwrap(Class iface) throws SQLException { return t; } - public ShardingKeyBuilder createShardingKeyBuilder() throws SQLException { - DriverJDBCVersion.checkSupportsJDBC43(); - throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); - } - - public ConnectionBuilder createConnectionBuilder() throws SQLException { - DriverJDBCVersion.checkSupportsJDBC43(); - throw new SQLFeatureNotSupportedException("createConnectionBuilder not implemented"); - } // Returns unique id for each DataSource instance. private static int nextDataSourceID() { diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource43.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource43.java new file mode 100644 index 000000000..7d4621e87 --- /dev/null +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource43.java @@ -0,0 +1,24 @@ +package com.microsoft.sqlserver.jdbc; + +import java.sql.ConnectionBuilder; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.ShardingKeyBuilder; + +public class SQLServerDataSource43 extends SQLServerDataSource implements ISQLServerDataSource43{ + + public SQLServerDataSource43 () throws SQLServerException { + super(); + } + + public ShardingKeyBuilder createShardingKeyBuilder() throws SQLException { + DriverJDBCVersion.checkSupportsJDBC43(); + throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); + } + + public ConnectionBuilder createConnectionBuilder() throws SQLException { + DriverJDBCVersion.checkSupportsJDBC43(); + throw new SQLFeatureNotSupportedException("createConnectionBuilder not implemented"); + } + +} diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDriver.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDriver.java index 1063f903e..d7480d5e1 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDriver.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDriver.java @@ -612,7 +612,12 @@ static String getPropertyOnlyName(String name, // Merge connectProperties (from URL) and supplied properties from user. Properties connectProperties = parseAndMergeProperties(Url, suppliedProperties); if (connectProperties != null) { - result = new SQLServerConnection(toString()); + if (Util.use43Wrapper()) { + result = new SQLServerConnection43(toString()); + } + else { + result = new SQLServerConnection(toString()); + } result.connect(connectProperties, null); } loggerExternal.exiting(getClassNameLogging(), "connect", result); diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java index c1f8d69fd..424fe52ce 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java @@ -44,7 +44,13 @@ public final class SQLServerXAConnection extends SQLServerPooledConnection imple if (xaLogger.isLoggable(Level.FINER)) xaLogger.finer("Creating an internal control connection for" + toString()); - physicalControlConnection = new SQLServerConnection(toString()); + physicalControlConnection = null; + if (Util.use43Wrapper()) { + physicalControlConnection = new SQLServerConnection43(toString()); + } + else { + physicalControlConnection = new SQLServerConnection(toString()); + } physicalControlConnection.connect(controlConnectionProperties, null); if (xaLogger.isLoggable(Level.FINER)) xaLogger.finer("Created an internal control connection" + physicalControlConnection.toString() + " for " + toString() diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/Util.java b/src/main/java/com/microsoft/sqlserver/jdbc/Util.java index c1d6d81dc..15d5b81d9 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/Util.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/Util.java @@ -1008,6 +1008,28 @@ static synchronized boolean checkIfNeedNewAccessToken(SQLServerConnection connec static boolean use42Wrapper() { return use42Wrapper; } + + static final boolean use43Wrapper; + + static { + boolean supportJDBC43 = true; + try { + DriverJDBCVersion.checkSupportsJDBC43(); + } + catch (UnsupportedOperationException e) { + supportJDBC43 = false; + } + + double jvmVersion = Double.parseDouble(Util.SYSTEM_SPEC_VERSION); + + use43Wrapper = supportJDBC43 && (9 <= jvmVersion); + } + + // if driver is for JDBC 43 and jvm version is 9 or higher, then always return as SQLServerConnection43, + // otherwise return SQLServerConnection + static boolean use43Wrapper() { + return use43Wrapper; + } } final class SQLIdentifier { From 6a833eb99d744343c59bfe70912c614ef8ca678a Mon Sep 17 00:00:00 2001 From: Afsaneh Rafighi Date: Mon, 18 Dec 2017 14:40:57 -0800 Subject: [PATCH 2/7] fix another issue with Java 9 regarding bulkcopy connection check --- .../java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java | 3 ++- .../java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java b/src/main/java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java index 2667d5af0..f15c2846e 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java @@ -11,6 +11,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.Reader; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; @@ -255,7 +256,7 @@ private boolean encodeChars() throws IOException { // The raw character buffer may now have characters available for encoding. // Flip the buffer back to be ready for get (charset encode) operations. - rawChars.flip(); + ((Buffer)rawChars).flip(); } // If the raw character buffer remains empty, despite our efforts to (re)populate it, diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java index a383e1946..50f711c29 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java @@ -327,7 +327,7 @@ public void run() { public SQLServerBulkCopy(Connection connection) throws SQLServerException { loggerExternal.entering(loggerClassName, "SQLServerBulkCopy", connection); - if (null == connection || !connection.getClass().equals(SQLServerConnection.class)) { + if (null == connection || !(connection instanceof SQLServerConnection)) { SQLServerException.makeFromDriverError(null, null, SQLServerException.getErrString("R_invalidDestConnection"), null, false); } From 4b28d7084a8e405efa0a2fb81d0c2f294222ea84 Mon Sep 17 00:00:00 2001 From: Afsaneh Rafighi Date: Mon, 18 Dec 2017 18:20:01 -0800 Subject: [PATCH 3/7] added another fix --- .../java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java b/src/main/java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java index f15c2846e..e30dd640c 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java @@ -183,7 +183,7 @@ private boolean encodeChars() throws IOException { } else { // Flip the buffer to be ready for put (reader read) operations. - rawChars.clear(); + ((Buffer)rawChars).clear(); } // Try to fill up the raw character buffer by reading available characters From 19d8a5157e31c0eeeabd6fa9e2afccb87717ff91 Mon Sep 17 00:00:00 2001 From: Afsaneh Rafighi Date: Mon, 18 Dec 2017 18:31:40 -0800 Subject: [PATCH 4/7] added cast to Buffer --- .../com/microsoft/sqlserver/jdbc/ReaderInputStream.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java b/src/main/java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java index e30dd640c..1a2513816 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java @@ -195,7 +195,7 @@ private boolean encodeChars() throws IOException { // - the reader throws any kind of Exception (driver throws an IOException) // - the reader violates its interface contract (driver throws an IOException) while (rawChars.hasRemaining()) { - int lastPosition = rawChars.position(); + int lastPosition = ((Buffer)rawChars).position(); int charsRead = 0; // Try reading from the app-supplied Reader @@ -219,7 +219,7 @@ private boolean encodeChars() throws IOException { if (-1 == charsRead) { // If the reader violates its interface contract then throw an exception. - if (rawChars.position() != lastPosition) + if (((Buffer)rawChars).position() != lastPosition) throw new IOException(SQLServerException.getErrString("R_streamReadReturnedInvalidValue")); // Check that the reader has returned exactly the amount of data we expect @@ -229,7 +229,7 @@ private boolean encodeChars() throws IOException { } // If there are no characters left to encode then we're done. - if (0 == rawChars.position()) { + if (0 == ((Buffer)rawChars).position()) { rawChars = null; atEndOfStream = true; return false; @@ -242,7 +242,7 @@ private boolean encodeChars() throws IOException { assert charsRead > 0; // If the reader violates its interface contract then throw an exception. - if (charsRead != rawChars.position() - lastPosition) + if (charsRead != ((Buffer)rawChars).position() - lastPosition) throw new IOException(SQLServerException.getErrString("R_streamReadReturnedInvalidValue")); // Check that the reader isn't trying to return more data than we expect From 99868e6d31e7b78232e18e50a23c12d9fb3177fe Mon Sep 17 00:00:00 2001 From: Afsaneh Rafighi Date: Tue, 19 Dec 2017 16:12:47 -0800 Subject: [PATCH 5/7] change Base64.getEncoder().encodeToString() --- .../jdbc/ISQLServerDataSource43.java | 12 ---------- .../SQLServerAeadAes256CbcHmac256Factory.java | 2 +- ...umnEncryptionCertificateStoreProvider.java | 3 ++- .../sqlserver/jdbc/SQLServerDataSource43.java | 24 ------------------- .../jdbc/SQLServerSymmetricKeyCache.java | 2 +- 5 files changed, 4 insertions(+), 39 deletions(-) delete mode 100644 src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerDataSource43.java delete mode 100644 src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource43.java diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerDataSource43.java b/src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerDataSource43.java deleted file mode 100644 index 47cbe003c..000000000 --- a/src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerDataSource43.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.microsoft.sqlserver.jdbc; - -import java.sql.ConnectionBuilder; -import java.sql.SQLException; -import java.sql.ShardingKeyBuilder; - -public interface ISQLServerDataSource43 extends ISQLServerDataSource { - - public ShardingKeyBuilder createShardingKeyBuilder() throws SQLException; - - public ConnectionBuilder createConnectionBuilder() throws SQLException; -} diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerAeadAes256CbcHmac256Factory.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerAeadAes256CbcHmac256Factory.java index 1c3e1cd7e..3a170da2d 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerAeadAes256CbcHmac256Factory.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerAeadAes256CbcHmac256Factory.java @@ -37,7 +37,7 @@ SQLServerEncryptionAlgorithm create(SQLServerSymmetricKey columnEncryptionKey, } StringBuilder factoryKeyBuilder = new StringBuilder(); - factoryKeyBuilder.append(Base64.getEncoder().encode(new String(columnEncryptionKey.getRootKey(), UTF_8).getBytes())); + factoryKeyBuilder.append(Base64.getEncoder().encodeToString(new String(columnEncryptionKey.getRootKey(), UTF_8).getBytes())); factoryKeyBuilder.append(":"); factoryKeyBuilder.append(encryptionType); diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerColumnEncryptionCertificateStoreProvider.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerColumnEncryptionCertificateStoreProvider.java index d4cddff78..5f8bc8001 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerColumnEncryptionCertificateStoreProvider.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerColumnEncryptionCertificateStoreProvider.java @@ -22,6 +22,7 @@ import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.text.MessageFormat; +import java.util.Base64; import java.util.Enumeration; import java.util.Locale; @@ -139,7 +140,7 @@ private String getThumbPrint(X509Certificate cert) throws NoSuchAlgorithmExcepti byte[] der = cert.getEncoded(); md.update(der); byte[] digest = md.digest(); - return Util.bytesToHexString(digest, digest.length); + return Base64.getEncoder().encodeToString(digest); } private CertificateDetails getCertificateByThumbprint(String storeLocation, diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource43.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource43.java deleted file mode 100644 index 7d4621e87..000000000 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource43.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.microsoft.sqlserver.jdbc; - -import java.sql.ConnectionBuilder; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.sql.ShardingKeyBuilder; - -public class SQLServerDataSource43 extends SQLServerDataSource implements ISQLServerDataSource43{ - - public SQLServerDataSource43 () throws SQLServerException { - super(); - } - - public ShardingKeyBuilder createShardingKeyBuilder() throws SQLException { - DriverJDBCVersion.checkSupportsJDBC43(); - throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); - } - - public ConnectionBuilder createConnectionBuilder() throws SQLException { - DriverJDBCVersion.checkSupportsJDBC43(); - throw new SQLFeatureNotSupportedException("createConnectionBuilder not implemented"); - } - -} diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSymmetricKeyCache.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSymmetricKeyCache.java index 819293f7b..04f9335ff 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSymmetricKeyCache.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSymmetricKeyCache.java @@ -98,7 +98,7 @@ SQLServerSymmetricKey getKey(EncryptionKeyInfo keyInfo, String keyLookupValue; keyLookupValuebuffer.append(":"); - keyLookupValuebuffer.append(Base64.getEncoder().encode((new String(keyInfo.encryptedKey, UTF_8)).getBytes())); + keyLookupValuebuffer.append(Base64.getEncoder().encodeToString((new String(keyInfo.encryptedKey, UTF_8)).getBytes())); keyLookupValuebuffer.append(":"); keyLookupValuebuffer.append(keyInfo.keyStoreName); From b9fc8015a6344cf71de756cfceb3a5d4c402acbc Mon Sep 17 00:00:00 2001 From: Afsaneh Rafighi Date: Wed, 20 Dec 2017 14:14:50 -0800 Subject: [PATCH 6/7] Make sure the right wrapper is used for 4.2 and above. --- .../sqlserver/jdbc/SQLServerConnection.java | 17 +++++++++++------ .../sqlserver/jdbc/SQLServerStatement.java | 3 ++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java index 3bc6e8c66..af9d3c329 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java @@ -3186,8 +3186,9 @@ public PreparedStatement prepareStatement(String sql, checkClosed(); PreparedStatement st; - - if (Util.use42Wrapper()) { + + // Make sure SQLServerPreparedStatement42 is used for 4.2 and above. + if (Util.use42Wrapper() || Util.use43Wrapper()) { st = new SQLServerPreparedStatement42(this, sql, resultSetType, resultSetConcurrency, SQLServerStatementColumnEncryptionSetting.UseConnectionSetting); } @@ -3211,7 +3212,8 @@ private PreparedStatement prepareStatement(String sql, PreparedStatement st; - if (Util.use42Wrapper()) { + // Make sure SQLServerPreparedStatement42 is used for 4.2 and above. + if (Util.use42Wrapper() || Util.use43Wrapper()) { st = new SQLServerPreparedStatement42(this, sql, resultSetType, resultSetConcurrency, stmtColEncSetting); } else { @@ -3232,7 +3234,8 @@ public CallableStatement prepareCall(String sql, CallableStatement st; - if (Util.use42Wrapper()) { + // Make sure SQLServerCallableStatement42 is used for 4.2 and above. + if (Util.use42Wrapper() || Util.use43Wrapper()) { st = new SQLServerCallableStatement42(this, sql, resultSetType, resultSetConcurrency, SQLServerStatementColumnEncryptionSetting.UseConnectionSetting); } @@ -4655,7 +4658,8 @@ public PreparedStatement prepareStatement(java.lang.String sql, PreparedStatement st; - if (Util.use42Wrapper()) { + // Make sure SQLServerPreparedStatement42 is used for 4.2 and above. + if (Util.use42Wrapper() || Util.use43Wrapper()) { st = new SQLServerPreparedStatement42(this, sql, nType, nConcur, stmtColEncSetting); } else { @@ -4690,7 +4694,8 @@ public CallableStatement prepareCall(String sql, CallableStatement st; - if (Util.use42Wrapper()) { + // Make sure SQLServerCallableStatement42 is used for 4.2 and above + if (Util.use42Wrapper() || Util.use43Wrapper()) { st = new SQLServerCallableStatement42(this, sql, nType, nConcur, stmtColEncSetiing); } else { diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java index 9385fd165..696cfa6c5 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java @@ -1569,7 +1569,8 @@ boolean onInfo(TDSReader tdsReader) throws SQLServerException { // Not an error. Is it a result set? else if (nextResult.isResultSet()) { - if (Util.use42Wrapper()) { + // Make sure SQLServerResultSet42 is used for 4.2 and above + if (Util.use42Wrapper() || Util.use43Wrapper()) { resultSet = new SQLServerResultSet42(this); } else { From 8faa9123f22ce92c5255e5e69ab6820ff48d6846 Mon Sep 17 00:00:00 2001 From: Afsaneh Rafighi Date: Wed, 20 Dec 2017 14:59:20 -0800 Subject: [PATCH 7/7] make SQLServerConnection43 throw SQLServerException for new Unsupported APIs --- .../jdbc/ISQLServerConnection43.java | 9 ++-- .../sqlserver/jdbc/SQLServerConnection43.java | 17 +++---- .../microsoft/sqlserver/jdbc/JDBC43Test.java | 51 +++++++++++++++++++ 3 files changed, 63 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerConnection43.java b/src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerConnection43.java index 13695e703..1689cde87 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerConnection43.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerConnection43.java @@ -1,20 +1,19 @@ package com.microsoft.sqlserver.jdbc; -import java.sql.SQLFeatureNotSupportedException; import java.sql.ShardingKey; public interface ISQLServerConnection43 extends ISQLServerConnection { - public void setShardingKey(ShardingKey shardingKey) throws SQLFeatureNotSupportedException; + public void setShardingKey(ShardingKey shardingKey) throws SQLServerException; public void setShardingKey(ShardingKey shardingKey, - ShardingKey superShardingKey) throws SQLFeatureNotSupportedException; + ShardingKey superShardingKey) throws SQLServerException; public boolean setShardingKeyIfValid(ShardingKey shardingKey, - int timeout) throws SQLFeatureNotSupportedException; + int timeout) throws SQLServerException; public boolean setShardingKeyIfValid(ShardingKey shardingKey, ShardingKey superShardingKey, - int timeout) throws SQLFeatureNotSupportedException; + int timeout) throws SQLServerException; } diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection43.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection43.java index 44e348d72..217bcfc72 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection43.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection43.java @@ -7,31 +7,30 @@ public class SQLServerConnection43 extends SQLServerConnection implements ISQLSe SQLServerConnection43(String parentInfo) throws SQLServerException { super(parentInfo); - // TODO Auto-generated constructor stub } - public void setShardingKey(ShardingKey shardingKey) throws SQLFeatureNotSupportedException { + public void setShardingKey(ShardingKey shardingKey) throws SQLServerException { DriverJDBCVersion.checkSupportsJDBC43(); - throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); + throw new SQLServerException("setShardingKey not implemented", new SQLFeatureNotSupportedException("setShardingKey not implemented")); } public void setShardingKey(ShardingKey shardingKey, - ShardingKey superShardingKey) throws SQLFeatureNotSupportedException { + ShardingKey superShardingKey) throws SQLServerException { DriverJDBCVersion.checkSupportsJDBC43(); - throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); + throw new SQLServerException("setShardingKey not implemented", new SQLFeatureNotSupportedException("setShardingKey not implemented")) ; } public boolean setShardingKeyIfValid(ShardingKey shardingKey, - int timeout) throws SQLFeatureNotSupportedException { + int timeout) throws SQLServerException { DriverJDBCVersion.checkSupportsJDBC43(); - throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); + throw new SQLServerException("setShardingKeyIfValid not implemented", new SQLFeatureNotSupportedException("setShardingKeyIfValid not implemented")); } public boolean setShardingKeyIfValid(ShardingKey shardingKey, ShardingKey superShardingKey, - int timeout) throws SQLFeatureNotSupportedException { + int timeout) throws SQLServerException { DriverJDBCVersion.checkSupportsJDBC43(); - throw new SQLFeatureNotSupportedException("createShardingKeyBuilder not implemented"); + throw new SQLServerException("setShardingKeyIfValid not implemented", new SQLFeatureNotSupportedException("setShardingKeyIfValid not implemented")); } } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/JDBC43Test.java b/src/test/java/com/microsoft/sqlserver/jdbc/JDBC43Test.java index b4ac96682..6873bbffe 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/JDBC43Test.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/JDBC43Test.java @@ -29,6 +29,7 @@ import com.microsoft.sqlserver.testframework.util.Util; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assumptions.assumeTrue; /** @@ -139,6 +140,56 @@ public void connectionPoolDataSourceTest() throws TestAbortedException, SQLExcep assert (e.getMessage().contains("not implemented")); } } + + /** + * Tests that we are throwing the unsupported exception for setShardingKeyIfValid() + * @throws SQLException + * @throws TestAbortedException + * @since 1.9 + */ + @Test + public void setShardingKeyIfValidTest() throws TestAbortedException, SQLException { + assumeTrue(Util.supportJDBC43(connection)); + SQLServerConnection connection43 = (SQLServerConnection43) DriverManager.getConnection(connectionString); + try { + connection43.setShardingKeyIfValid(shardingKey, 10); + } + catch (SQLException e) { + assert (e.getMessage().contains("not implemented")); + } + try { + connection43.setShardingKeyIfValid(shardingKey, superShardingKey, 10); + } + catch (SQLException e) { + assert (e.getMessage().contains("not implemented")); + } + + } + + /** + * Tests that we are throwing the unsupported exception for setShardingKey() + * @throws SQLException + * @throws TestAbortedException + * @since 1.9 + */ + @Test + public void setShardingKeyTest() throws TestAbortedException, SQLException { + assumeTrue(Util.supportJDBC43(connection)); + SQLServerConnection connection43 = (SQLServerConnection43) DriverManager.getConnection(connectionString); + try { + connection43.setShardingKey(shardingKey); + } + catch (SQLException e) { + assert (e.getMessage().contains("not implemented")); + } + try { + connection43.setShardingKey(shardingKey, superShardingKey); + } + catch (SQLException e) { + assert (e.getMessage().contains("not implemented")); + } + + } /** * Tests the stream drivers() methods in java.sql.DriverManager