Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.microsoft.sqlserver.jdbc;

import java.sql.ShardingKey;

public interface ISQLServerConnection43 extends ISQLServerConnection {

public void setShardingKey(ShardingKey shardingKey) throws SQLServerException;

public void setShardingKey(ShardingKey shardingKey,
ShardingKey superShardingKey) throws SQLServerException;

public boolean setShardingKeyIfValid(ShardingKey shardingKey,
int timeout) throws SQLServerException;

public boolean setShardingKeyIfValid(ShardingKey shardingKey,
ShardingKey superShardingKey,
int timeout) throws SQLServerException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -182,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
Expand All @@ -194,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
Expand All @@ -218,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
Expand All @@ -228,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;
Expand All @@ -241,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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
42 changes: 11 additions & 31 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -3187,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);
}
Expand All @@ -3212,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 {
Expand All @@ -3233,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);
}
Expand Down Expand Up @@ -4656,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 {
Expand Down Expand Up @@ -4691,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 {
Expand Down Expand Up @@ -5259,30 +5263,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...
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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);
}

public void setShardingKey(ShardingKey shardingKey) throws SQLServerException {
DriverJDBCVersion.checkSupportsJDBC43();
throw new SQLServerException("setShardingKey not implemented", new SQLFeatureNotSupportedException("setShardingKey not implemented"));
}

public void setShardingKey(ShardingKey shardingKey,
ShardingKey superShardingKey) throws SQLServerException {
DriverJDBCVersion.checkSupportsJDBC43();
throw new SQLServerException("setShardingKey not implemented", new SQLFeatureNotSupportedException("setShardingKey not implemented")) ;
}

public boolean setShardingKeyIfValid(ShardingKey shardingKey,
int timeout) throws SQLServerException {
DriverJDBCVersion.checkSupportsJDBC43();
throw new SQLServerException("setShardingKeyIfValid not implemented", new SQLFeatureNotSupportedException("setShardingKeyIfValid not implemented"));
}

public boolean setShardingKeyIfValid(ShardingKey shardingKey,
ShardingKey superShardingKey,
int timeout) throws SQLServerException {
DriverJDBCVersion.checkSupportsJDBC43();
throw new SQLServerException("setShardingKeyIfValid not implemented", new SQLFeatureNotSupportedException("setShardingKeyIfValid not implemented"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -1107,15 +1111,6 @@ public <T> T unwrap(Class<T> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading