Skip to content

Commit

Permalink
Merge branch 'dev' into data-classification
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra authored Jun 22, 2018
2 parents dd14563 + abc9851 commit c62fbcf
Show file tree
Hide file tree
Showing 29 changed files with 639 additions and 153 deletions.
27 changes: 18 additions & 9 deletions src/main/java/com/microsoft/sqlserver/jdbc/Geography.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ public class Geography extends SQLServerSpatialDatatype {

/**
* Private constructor used for creating a Geography object from WKT and srid.
* @throws SQLServerException
*/
private Geography(String WellKnownText, int srid) {
private Geography(String WellKnownText, int srid) throws SQLServerException {
this.wkt = WellKnownText;
this.srid = srid;

try {
parseWKTForSerialization(this, currentWktPos, -1, false);
}
catch (StringIndexOutOfBoundsException e) {
throw new IllegalArgumentException("Reached unexpected end of WKT. Please make sure WKT is valid.");
String strError = SQLServerException.getErrString("R_illegalWKT");
throw new SQLServerException(strError, null, 0, null);
}

serializeToWkb(false);
Expand All @@ -33,8 +35,9 @@ private Geography(String WellKnownText, int srid) {

/**
* Private constructor used for creating a Geography object from WKB.
* @throws SQLServerException
*/
private Geography(byte[] wkb) {
private Geography(byte[] wkb) throws SQLServerException {
this.wkb = wkb;
buffer = ByteBuffer.wrap(wkb);
buffer.order(ByteOrder.LITTLE_ENDIAN);
Expand Down Expand Up @@ -62,8 +65,9 @@ public Geography() {
* @param wkt WKT
* @param srid SRID
* @return Geography instance
* @throws SQLServerException
*/
public static Geography STGeomFromText(String wkt, int srid) {
public static Geography STGeomFromText(String wkt, int srid) throws SQLServerException {
return new Geography(wkt, srid);
}

Expand All @@ -73,8 +77,9 @@ public static Geography STGeomFromText(String wkt, int srid) {
*
* @param wkb WKB
* @return Geography instance
* @throws SQLServerException
*/
public static Geography STGeomFromWKB(byte[] wkb) {
public static Geography STGeomFromWKB(byte[] wkb) throws SQLServerException {
return new Geography(wkb);
}

Expand All @@ -83,8 +88,9 @@ public static Geography STGeomFromWKB(byte[] wkb) {
*
* @param wkb WKB
* @return Geography instance
* @throws SQLServerException
*/
public static Geography deserialize(byte[] wkb) {
public static Geography deserialize(byte[] wkb) throws SQLServerException {
return new Geography(wkb);
}

Expand All @@ -94,8 +100,9 @@ public static Geography deserialize(byte[] wkb) {
*
* @param wkt WKt
* @return Geography instance
* @throws SQLServerException
*/
public static Geography parse(String wkt) {
public static Geography parse(String wkt) throws SQLServerException {
return new Geography(wkt, 4326);
}

Expand All @@ -106,8 +113,9 @@ public static Geography parse(String wkt) {
* @param y y coordinate
* @param srid SRID
* @return Geography instance
* @throws SQLServerException
*/
public static Geography point(double x, double y, int srid) {
public static Geography point(double x, double y, int srid) throws SQLServerException {
return new Geography("POINT (" + x + " " + y + ")", srid);
}

Expand All @@ -116,8 +124,9 @@ public static Geography point(double x, double y, int srid) {
* Geography instance. This text will not contain any Z (elevation) or M (measure) values carried by the instance.
*
* @return the WKT representation without the Z and M values.
* @throws SQLServerException
*/
public String STAsText() {
public String STAsText() throws SQLServerException {
if (null == wktNoZM) {
buffer = ByteBuffer.wrap(wkb);
buffer.order(ByteOrder.LITTLE_ENDIAN);
Expand Down
27 changes: 18 additions & 9 deletions src/main/java/com/microsoft/sqlserver/jdbc/Geometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ public class Geometry extends SQLServerSpatialDatatype {

/**
* Private constructor used for creating a Geometry object from WKT and srid.
* @throws SQLServerException
*/
private Geometry(String WellKnownText, int srid) {
private Geometry(String WellKnownText, int srid) throws SQLServerException {
this.wkt = WellKnownText;
this.srid = srid;

try {
parseWKTForSerialization(this, currentWktPos, -1, false);
}
catch (StringIndexOutOfBoundsException e) {
throw new IllegalArgumentException("Reached unexpected end of WKT. Please make sure WKT is valid.");
String strError = SQLServerException.getErrString("R_illegalWKT");
throw new SQLServerException(strError, null, 0, null);
}

serializeToWkb(false);
Expand All @@ -33,8 +35,9 @@ private Geometry(String WellKnownText, int srid) {

/**
* Private constructor used for creating a Geometry object from WKB.
* @throws SQLServerException
*/
private Geometry(byte[] wkb) {
private Geometry(byte[] wkb) throws SQLServerException {
this.wkb = wkb;
buffer = ByteBuffer.wrap(wkb);
buffer.order(ByteOrder.LITTLE_ENDIAN);
Expand Down Expand Up @@ -62,8 +65,9 @@ public Geometry() {
* @param wkt WKT
* @param srid SRID
* @return Geometry instance
* @throws SQLServerException
*/
public static Geometry STGeomFromText(String wkt, int srid) {
public static Geometry STGeomFromText(String wkt, int srid) throws SQLServerException {
return new Geometry(wkt, srid);
}

Expand All @@ -73,8 +77,9 @@ public static Geometry STGeomFromText(String wkt, int srid) {
*
* @param wkb WKB
* @return Geometry instance
* @throws SQLServerException
*/
public static Geometry STGeomFromWKB(byte[] wkb) {
public static Geometry STGeomFromWKB(byte[] wkb) throws SQLServerException {
return new Geometry(wkb);
}

Expand All @@ -83,8 +88,9 @@ public static Geometry STGeomFromWKB(byte[] wkb) {
*
* @param wkb WKB
* @return Geometry instance
* @throws SQLServerException
*/
public static Geometry deserialize(byte[] wkb) {
public static Geometry deserialize(byte[] wkb) throws SQLServerException {
return new Geometry(wkb);
}

Expand All @@ -94,8 +100,9 @@ public static Geometry deserialize(byte[] wkb) {
*
* @param wkt WKT
* @return Geometry instance
* @throws SQLServerException
*/
public static Geometry parse(String wkt) {
public static Geometry parse(String wkt) throws SQLServerException {
return new Geometry(wkt, 0);
}

Expand All @@ -106,8 +113,9 @@ public static Geometry parse(String wkt) {
* @param y y coordinate
* @param srid SRID
* @return Geometry instance
* @throws SQLServerException
*/
public static Geometry point(double x, double y, int srid) {
public static Geometry point(double x, double y, int srid) throws SQLServerException {
return new Geometry("POINT (" + x + " " + y + ")", srid);
}

Expand All @@ -116,8 +124,9 @@ public static Geometry point(double x, double y, int srid) {
* Geometry instance. This text will not contain any Z (elevation) or M (measure) values carried by the instance.
*
* @return the WKT representation without the Z and M values.
* @throws SQLServerException
*/
public String STAsText() {
public String STAsText() throws SQLServerException {
if (null == wktNoZM) {
buffer = ByteBuffer.wrap(wkb);
buffer.order(ByteOrder.LITTLE_ENDIAN);
Expand Down
62 changes: 15 additions & 47 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ final class TDS {
static final int AES_256_CBC = 1;
static final int AEAD_AES_256_CBC_HMAC_SHA256 = 2;
static final int AE_METADATA = 0x08;

static final byte TDS_FEATURE_EXT_UTF8SUPPORT = 0x0A;

static final int TDS_TVP = 0xF3;
static final int TVP_ROW = 0x01;
Expand Down Expand Up @@ -192,6 +194,8 @@ static final String getTokenName(int tdsTokenType) {
return "TDS_FEDAUTHINFO (0xEE)";
case TDS_FEATURE_EXT_DATACLASSIFICATION:
return "TDS_FEATURE_EXT_DATACLASSIFICATION (0x09)";
case TDS_FEATURE_EXT_UTF8SUPPORT:
return "TDS_FEATURE_EXT_UTF8SUPPORT (0x0A)";
default:
return "unknown token (0x" + Integer.toHexString(tdsTokenType).toUpperCase() + ")";
}
Expand Down Expand Up @@ -2344,58 +2348,22 @@ else if (!useTnir) {
conn.terminate(SQLServerException.DRIVER_ERROR_UNSUPPORTED_CONFIG, errorStr);
}

if (inetAddrs.length == 1) {
// Single address so do not start any threads
return getConnectedSocket(inetAddrs[0], portNumber, timeoutInMilliSeconds);
}
timeoutInMilliSeconds = Math.max(timeoutInMilliSeconds, minTimeoutForParallelConnections);
if (Util.isIBM()) {
timeoutInMilliSeconds = Math.max(timeoutInMilliSeconds, minTimeoutForParallelConnections);
if (logger.isLoggable(Level.FINER)) {
logger.finer(this.toString() + "Using Java NIO with timeout:" + timeoutInMilliSeconds);
}
findSocketUsingJavaNIO(inetAddrs, portNumber, timeoutInMilliSeconds);
}
else {
LinkedList<InetAddress> inet4Addrs = new LinkedList<>();
LinkedList<InetAddress> inet6Addrs = new LinkedList<>();

for (InetAddress inetAddr : inetAddrs) {
if (inetAddr instanceof Inet4Address) {
inet4Addrs.add((Inet4Address) inetAddr);
}
else {
assert inetAddr instanceof Inet6Address : "Unexpected IP address " + inetAddr.toString();
inet6Addrs.add((Inet6Address) inetAddr);
}
}

// use half timeout only if both IPv4 and IPv6 addresses are present
int timeoutForEachIPAddressType;
if ((!inet4Addrs.isEmpty()) && (!inet6Addrs.isEmpty())) {
timeoutForEachIPAddressType = Math.max(timeoutInMilliSeconds / 2, minTimeoutForParallelConnections);
}
else
timeoutForEachIPAddressType = Math.max(timeoutInMilliSeconds, minTimeoutForParallelConnections);

if (!inet4Addrs.isEmpty()) {
if (logger.isLoggable(Level.FINER)) {
logger.finer(this.toString() + "Using Java Threading with timeout:" + timeoutForEachIPAddressType);
}

findSocketUsingThreading(inet4Addrs, portNumber, timeoutForEachIPAddressType);
}

if (!result.equals(Result.SUCCESS)) {
// try threading logic
if (!inet6Addrs.isEmpty()) {
// do not start any threads if there is only one ipv6 address
if (inet6Addrs.size() == 1) {
return getConnectedSocket(inet6Addrs.get(0), portNumber, timeoutForEachIPAddressType);
}

if (logger.isLoggable(Level.FINER)) {
logger.finer(this.toString() + "Using Threading with timeout:" + timeoutForEachIPAddressType);
}

findSocketUsingThreading(inet6Addrs, portNumber, timeoutForEachIPAddressType);
}
if (logger.isLoggable(Level.FINER)) {
logger.finer(this.toString() + "Using Threading with timeout:" + timeoutInMilliSeconds);
}
findSocketUsingThreading(inetAddrs, portNumber, timeoutInMilliSeconds);
}

// If the thread continued execution due to timeout, the result may not be known.
Expand Down Expand Up @@ -2648,20 +2616,20 @@ private Socket getConnectedSocket(InetSocketAddress addr,
return selectedSocket;
}

private void findSocketUsingThreading(LinkedList<InetAddress> inetAddrs,
private void findSocketUsingThreading(InetAddress[] inetAddrs,
int portNumber,
int timeoutInMilliSeconds) throws IOException, InterruptedException {
assert timeoutInMilliSeconds != 0 : "The timeout cannot be zero";

assert inetAddrs.isEmpty() == false : "Number of inetAddresses should not be zero in this function";
assert inetAddrs.length != 0 : "Number of inetAddresses should not be zero in this function";

LinkedList<Socket> sockets = new LinkedList<>();
LinkedList<SocketConnector> socketConnectors = new LinkedList<>();

try {

// create a socket, inetSocketAddress and a corresponding socketConnector per inetAddress
noOfSpawnedThreads = inetAddrs.size();
noOfSpawnedThreads = inetAddrs.length;
for (InetAddress inetAddress : inetAddrs) {
Socket s = new Socket();
sockets.add(s);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Microsoft JDBC Driver for SQL Server
*
* Copyright(c) Microsoft Corporation All rights reserved.
*
* This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/

package com.microsoft.sqlserver.jdbc;

import java.sql.SQLException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Microsoft JDBC Driver for SQL Server
*
* Copyright(c) Microsoft Corporation All rights reserved.
*
* This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/

package com.microsoft.sqlserver.jdbc;

public enum InternalSpatialDatatype {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/KerbCallback.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Microsoft JDBC Driver for SQL Server
*
* Copyright(c) Microsoft Corporation All rights reserved.
*
* This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/

package com.microsoft.sqlserver.jdbc;

import java.io.IOException;
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLCollation.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ final class SQLCollation implements java.io.Serializable
private int langID() { return info & 0x0000FFFF; }
private final int sortId; // 5th byte of TDS collation.
private final Encoding encoding;
private static final int UTF8_IN_TDSCOLLATION = 0x4000000;

// Utility methods for getting details of this collation's encoding
final Charset getCharset() throws SQLServerException { return encoding.charset(); }
Expand Down Expand Up @@ -77,8 +78,13 @@ int getCollationSortID() {
*/
info = tdsReader.readInt(); // 4 bytes, contains: LCID ColFlags Version
sortId = tdsReader.readUnsignedByte(); // 1 byte, contains: SortId
// For a SortId==0 collation, the LCID bits correspond to a LocaleId
encoding = (0 == sortId) ? encodingFromLCID() : encodingFromSortId();
if (UTF8_IN_TDSCOLLATION == (info & UTF8_IN_TDSCOLLATION)) {
encoding = Encoding.UTF8;
}
else {
// For a SortId==0 collation, the LCID bits correspond to a LocaleId
encoding = (0 == sortId) ? encodingFromLCID() : encodingFromSortId();
}
}

/**
Expand Down Expand Up @@ -549,6 +555,7 @@ private Encoding encodingFromSortId() throws UnsupportedEncodingException {
enum Encoding
{
UNICODE ("UTF-16LE", true, false),
UTF8 ("UTF-8", true, false),
CP437 ("Cp437", false, false),
CP850 ("Cp850", false, false),
CP874 ("MS874", true, true),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Microsoft JDBC Driver for SQL Server
*
* Copyright(c) Microsoft Corporation All rights reserved.
*
* This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/

package com.microsoft.sqlserver.jdbc;

import java.io.IOException;
Expand Down
Loading

0 comments on commit c62fbcf

Please sign in to comment.