Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON datatype support #2558

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ else if (jdbcType.isBinary()) {

// Update of Unicode SSType from textual JDBCType: Use Unicode.
if ((SSType.NCHAR == ssType || SSType.NVARCHAR == ssType || SSType.NVARCHARMAX == ssType
|| SSType.NTEXT == ssType || SSType.XML == ssType) &&
|| SSType.NTEXT == ssType || SSType.XML == ssType || SSType.JSON == ssType) &&

(JDBCType.CHAR == jdbcType || JDBCType.VARCHAR == jdbcType || JDBCType.LONGVARCHAR == jdbcType
|| JDBCType.CLOB == jdbcType)) {
Expand Down
37 changes: 26 additions & 11 deletions src/main/java/com/microsoft/sqlserver/jdbc/DataTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ enum TDSType {
NTEXT(0x63), // 99
UDT(0xF0), // -16
XML(0xF1), // -15
JSON(0xF4), // -12

// LONGLEN types
SQL_VARIANT(0x62); // 98
Expand Down Expand Up @@ -148,7 +149,8 @@ enum SSType {
XML(Category.XML, "xml", JDBCType.LONGNVARCHAR),
TIMESTAMP(Category.TIMESTAMP, "timestamp", JDBCType.BINARY),
GEOMETRY(Category.UDT, "geometry", JDBCType.GEOMETRY),
GEOGRAPHY(Category.UDT, "geography", JDBCType.GEOGRAPHY);
GEOGRAPHY(Category.UDT, "geography", JDBCType.GEOGRAPHY),
JSON(Category.JSON, "json", JDBCType.LONGNVARCHAR);

final Category category;
private final String name;
Expand Down Expand Up @@ -204,7 +206,8 @@ enum Category {
TIMESTAMP,
UDT,
SQL_VARIANT,
XML;
XML,
JSON;

private static final Category[] VALUES = values();
}
Expand Down Expand Up @@ -266,7 +269,12 @@ enum GetterConversion {

SQL_VARIANT(SSType.Category.SQL_VARIANT, EnumSet.of(JDBCType.Category.CHARACTER, JDBCType.Category.SQL_VARIANT,
JDBCType.Category.NUMERIC, JDBCType.Category.DATE, JDBCType.Category.TIME, JDBCType.Category.BINARY,
JDBCType.Category.TIMESTAMP, JDBCType.Category.NCHARACTER, JDBCType.Category.GUID));
JDBCType.Category.TIMESTAMP, JDBCType.Category.NCHARACTER, JDBCType.Category.GUID)),

JSON(SSType.Category.JSON, EnumSet.of(JDBCType.Category.CHARACTER, JDBCType.Category.LONG_CHARACTER,
JDBCType.Category.CLOB, JDBCType.Category.NCHARACTER, JDBCType.Category.LONG_NCHARACTER,
JDBCType.Category.NCLOB, JDBCType.Category.BINARY, JDBCType.Category.LONG_BINARY,
JDBCType.Category.BLOB, JDBCType.Category.JSON));

private final SSType.Category from;
private final EnumSet<JDBCType.Category> to;
Expand Down Expand Up @@ -450,6 +458,7 @@ JDBCType getJDBCType(SSType ssType, JDBCType jdbcTypeFromApp) {
case NVARCHAR:
case NVARCHARMAX:
case NTEXT:
case JSON:
jdbcType = JDBCType.LONGVARCHAR;
break;

Expand Down Expand Up @@ -673,8 +682,9 @@ enum JDBCType {
SQL_VARIANT(Category.SQL_VARIANT, microsoft.sql.Types.SQL_VARIANT, Object.class.getName()),
GEOMETRY(Category.GEOMETRY, microsoft.sql.Types.GEOMETRY, Object.class.getName()),
GEOGRAPHY(Category.GEOGRAPHY, microsoft.sql.Types.GEOGRAPHY, Object.class.getName()),
LOCALDATETIME(Category.TIMESTAMP, java.sql.Types.TIMESTAMP, LocalDateTime.class.getName());

LOCALDATETIME(Category.TIMESTAMP, java.sql.Types.TIMESTAMP, LocalDateTime.class.getName()),
JSON(Category.JSON, microsoft.sql.Types.JSON, Object.class.getName());

final Category category;
private final int intValue;
private final String className;
Expand Down Expand Up @@ -722,7 +732,8 @@ enum Category {
GUID,
SQL_VARIANT,
GEOMETRY,
GEOGRAPHY;
GEOGRAPHY,
JSON;

private static final Category[] VALUES = values();
}
Expand All @@ -733,7 +744,7 @@ enum SetterConversion {
JDBCType.Category.TIME, JDBCType.Category.TIMESTAMP, JDBCType.Category.DATETIMEOFFSET,
JDBCType.Category.CHARACTER, JDBCType.Category.LONG_CHARACTER, JDBCType.Category.NCHARACTER,
JDBCType.Category.LONG_NCHARACTER, JDBCType.Category.BINARY, JDBCType.Category.LONG_BINARY,
JDBCType.Category.GUID, JDBCType.Category.SQL_VARIANT)),
JDBCType.Category.GUID, JDBCType.Category.SQL_VARIANT, JDBCType.Category.JSON)),

LONG_CHARACTER(JDBCType.Category.LONG_CHARACTER, EnumSet.of(JDBCType.Category.CHARACTER,
JDBCType.Category.LONG_CHARACTER, JDBCType.Category.NCHARACTER, JDBCType.Category.LONG_NCHARACTER,
Expand Down Expand Up @@ -795,7 +806,8 @@ enum SetterConversion {

GEOMETRY(JDBCType.Category.GEOMETRY, EnumSet.of(JDBCType.Category.GEOMETRY)),

GEOGRAPHY(JDBCType.Category.GEOGRAPHY, EnumSet.of(JDBCType.Category.GEOGRAPHY));
GEOGRAPHY(JDBCType.Category.GEOGRAPHY, EnumSet.of(JDBCType.Category.GEOGRAPHY)),
JSON(JDBCType.Category.JSON, EnumSet.of(JDBCType.Category.JSON));

private final JDBCType.Category from;
private final EnumSet<JDBCType.Category> to;
Expand Down Expand Up @@ -832,7 +844,7 @@ enum UpdaterConversion {
SSType.Category.DATETIMEOFFSET, SSType.Category.CHARACTER, SSType.Category.LONG_CHARACTER,
SSType.Category.NCHARACTER, SSType.Category.LONG_NCHARACTER, SSType.Category.XML,
SSType.Category.BINARY, SSType.Category.LONG_BINARY, SSType.Category.UDT, SSType.Category.GUID,
SSType.Category.TIMESTAMP, SSType.Category.SQL_VARIANT)),
SSType.Category.TIMESTAMP, SSType.Category.SQL_VARIANT, SSType.Category.JSON)),

LONG_CHARACTER(JDBCType.Category.LONG_CHARACTER, EnumSet.of(SSType.Category.CHARACTER,
SSType.Category.LONG_CHARACTER, SSType.Category.NCHARACTER, SSType.Category.LONG_NCHARACTER,
Expand Down Expand Up @@ -895,7 +907,9 @@ enum UpdaterConversion {
SSType.Category.DATETIMEOFFSET, SSType.Category.CHARACTER, SSType.Category.LONG_CHARACTER,
SSType.Category.NCHARACTER, SSType.Category.LONG_NCHARACTER)),

SQL_VARIANT(JDBCType.Category.SQL_VARIANT, EnumSet.of(SSType.Category.SQL_VARIANT));
SQL_VARIANT(JDBCType.Category.SQL_VARIANT, EnumSet.of(SSType.Category.SQL_VARIANT)),

JSON(JDBCType.Category.JSON, EnumSet.of(SSType.Category.JSON));

private final JDBCType.Category from;
private final EnumSet<SSType.Category> to;
Expand Down Expand Up @@ -970,7 +984,7 @@ boolean isBinary() {
* @return true if the JDBC type is textual
*/
private final static EnumSet<Category> textualCategories = EnumSet.of(Category.CHARACTER, Category.LONG_CHARACTER,
Category.CLOB, Category.NCHARACTER, Category.LONG_NCHARACTER, Category.NCLOB);
Category.CLOB, Category.NCHARACTER, Category.LONG_NCHARACTER, Category.NCLOB, Category.JSON); //FIXME: JSON is textual?

boolean isTextual() {
return textualCategories.contains(category);
Expand All @@ -997,6 +1011,7 @@ int asJavaSqlType() {
return java.sql.Types.CHAR;
case NVARCHAR:
case SQLXML:
case JSON:
return java.sql.Types.VARCHAR;
case LONGNVARCHAR:
return java.sql.Types.LONGVARCHAR;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ final class TDS {
static final byte TDS_FEATURE_EXT_AZURESQLDNSCACHING = 0x0B;
static final byte TDS_FEATURE_EXT_SESSIONRECOVERY = 0x01;

// JSON support
static final byte TDS_FEATURE_EXT_JSONSUPPORT = 0x0D;
static final byte JSONSUPPORT_NOT_SUPPORTED = 0x00;
static final byte MAX_JSONSUPPORT_VERSION = 0x01;

static final int TDS_TVP = 0xF3;
static final int TVP_ROW = 0x01;
static final int TVP_NULL_TOKEN = 0xFFFF;
Expand Down Expand Up @@ -237,6 +242,9 @@ static final String getTokenName(int tdsTokenType) {
return "TDS_FEATURE_EXT_AZURESQLDNSCACHING (0x0B)";
case TDS_FEATURE_EXT_SESSIONRECOVERY:
return "TDS_FEATURE_EXT_SESSIONRECOVERY (0x01)";
case TDS_FEATURE_EXT_JSONSUPPORT:
return "TDS_FEATURE_EXT_JSONSUPPORT (0x0D)";

default:
return "unknown token (0x" + Integer.toHexString(tdsTokenType).toUpperCase() + ")";
}
Expand Down Expand Up @@ -5241,6 +5249,7 @@ private void writeInternalTVPRowValues(JDBCType jdbcType, String currentColumnSt
case LONGVARCHAR:
case LONGNVARCHAR:
case SQLXML:
case JSON:
isShortValue = (2L * columnPair.getValue().precision) <= DataTypes.SHORT_VARTYPE_MAX_BYTES;
isNull = (null == currentColumnStringValue);
dataLength = isNull ? 0 : currentColumnStringValue.length() * 2;
Expand Down Expand Up @@ -5476,6 +5485,7 @@ void writeTVPColumnMetaData(TVP value) throws SQLServerException {
case LONGVARCHAR:
case LONGNVARCHAR:
case SQLXML:
case JSON:
writeByte(TDSType.NVARCHAR.byteValue());
isShortValue = (2L * pair.getValue().precision) <= DataTypes.SHORT_VARTYPE_MAX_BYTES;
// Use PLP encoding on Yukon and later with long values
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,9 @@ private void setTypeDefinition(DTV dtv) {
case SQLXML:
param.typeDefinition = SSType.XML.toString();
break;

case JSON:
param.typeDefinition = SSType.JSON.toString();
break;
case TVP:
// definition should contain the TVP name and the keyword READONLY
String schema = param.schemaName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,16 @@ byte getServerSupportedDataClassificationVersion() {
return serverSupportedDataClassificationVersion;
}

/** whether server supports JSON */
private boolean serverSupportsJSON = false;

/** server supported JSON version */
private byte serverSupportedJSONVersion = TDS.JSONSUPPORT_NOT_SUPPORTED;

boolean getServerSupportsJSON() {
return serverSupportsJSON;
}

/** Boolean that indicates whether LOB objects created by this connection should be loaded into memory */
private boolean delayLoadingLobs = SQLServerDriverBooleanProperty.DELAY_LOADING_LOBS.getDefaultValue();

Expand Down Expand Up @@ -5340,6 +5350,17 @@ int writeDNSCacheFeatureRequest(boolean write, /* if false just calculates the l
return len;
}

int writeJSONSupportFeatureRequest(boolean write, /* if false just calculates the length */
TDSWriter tdsWriter) throws SQLServerException {
int len = 6; // 1byte = featureID, 4bytes = featureData length, 1 bytes = Version
if (write) {
tdsWriter.writeByte(TDS.TDS_FEATURE_EXT_JSONSUPPORT);
tdsWriter.writeInt(1);
tdsWriter.writeByte(TDS.MAX_JSONSUPPORT_VERSION);
}
return len;
}

int writeIdleConnectionResiliencyRequest(boolean write, TDSWriter tdsWriter) throws SQLServerException {
SessionStateTable ssTable = sessionRecovery.getSessionStateTable();
int len = 1;
Expand Down Expand Up @@ -6469,6 +6490,24 @@ private void onFeatureExtAck(byte featureId, byte[] data) throws SQLServerExcept
sessionRecovery.setConnectionRecoveryPossible(true);
break;
}

case TDS.TDS_FEATURE_EXT_JSONSUPPORT: {
if (connectionlogger.isLoggable(Level.FINER)) {
connectionlogger.fine(toString() + " Received feature extension acknowledgement for JSON Support.");
}

if (1 != data.length) {
throw new SQLServerException(SQLServerException.getErrString("R_unknownJSONSupportValue"), null);
}

serverSupportedJSONVersion = data[0];
if (0 == serverSupportedJSONVersion || serverSupportedJSONVersion > TDS.MAX_JSONSUPPORT_VERSION) {
throw new SQLServerException(SQLServerException.getErrString("R_InvalidJSONVersionNumber"), null);
}
serverSupportsJSON = true;
break;
}

default: {
// Unknown feature ack
throw new SQLServerException(SQLServerException.getErrString("R_UnknownFeatureAck"), null);
Expand Down Expand Up @@ -6768,6 +6807,9 @@ final boolean complete(LogonCommand logonCommand, TDSReader tdsReader) throws SQ

len = len + writeDNSCacheFeatureRequest(false, tdsWriter);

// request JSON support
len += writeJSONSupportFeatureRequest(false, tdsWriter);

len = len + 1; // add 1 to length because of FeatureEx terminator

// Idle Connection Resiliency is requested
Expand Down Expand Up @@ -6964,6 +7006,7 @@ final boolean complete(LogonCommand logonCommand, TDSReader tdsReader) throws SQ
writeDataClassificationFeatureRequest(true, tdsWriter);
writeUTF8SupportFeatureRequest(true, tdsWriter);
writeDNSCacheFeatureRequest(true, tdsWriter);
writeJSONSupportFeatureRequest(true, tdsWriter);

// Idle Connection Resiliency is requested
if (connectRetryCount > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ private void internalAddrow(JDBCType jdbcType, Object val, Object[] rowValues,
case LONGVARCHAR:
case LONGNVARCHAR:
case SQLXML:
case JSON:
if (val instanceof UUID)
val = val.toString();
nValueLen = (2 * ((String) val).length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ private void parseQueryMeta(ResultSet rsQueryMeta) throws SQLServerException {
qm.precision = 8;
} else if (SSType.XML == ssType) {
qm.precision = SQLServerDatabaseMetaData.MAXLOBSIZE / 2;
} else if (SSType.JSON == ssType) {
qm.precision = SQLServerDatabaseMetaData.MAXLOBSIZE / 2;
}

qm.parameterTypeName = ssType.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ protected Object[][] getContents() {
{"R_StreamingDataTypeAE", "Data of length greater than {0} is not supported in encrypted {1} column."},
{"R_AE_NotSupportedByServer", "SQL Server in use does not support column encryption."},
{"R_InvalidAEVersionNumber", "Received invalid version number \"{0}\" for Always Encrypted."}, // From server
{"R_InvalidJSONVersionNumber", "Received invalid version number \"{0}\" for JSON."},
{"R_NullEncryptedColumnEncryptionKey", "Internal error. Encrypted column encryption key cannot be null."},
{"R_EmptyEncryptedColumnEncryptionKey", "Internal error. Empty encrypted column encryption key specified."},
{"R_InvalidMasterKeyDetails", "Invalid master key details specified."},
Expand Down Expand Up @@ -470,6 +471,7 @@ protected Object[][] getContents() {
{"R_InvalidDataClsVersionNumber", "Invalid version number {0} for Data Classification."}, // From Server
{"R_unknownUTF8SupportValue", "Unknown value for UTF8 support."},
{"R_unknownAzureSQLDNSCachingValue", "Unknown value for Azure SQL DNS Caching."},
{"R_unknownJSONSupportValue", "Unknown value for JSON support."},
{"R_illegalWKT", "Illegal Well-Known text. Please make sure Well-Known text is valid."},
{"R_illegalTypeForGeometry", "{0} is not supported for Geometry."},
{"R_illegalWKTposition", "Illegal character in Well-Known text at position {0}."},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public boolean isSearchable(int column) throws SQLServerException {
case NTEXT:
case UDT:
case XML:
case JSON:
return false;

default:
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/dtv.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ abstract class DTVExecuteOp {
abstract void execute(DTV dtv, TVP tvpValue) throws SQLServerException;

abstract void execute(DTV dtv, SqlVariant sqlVariantValue) throws SQLServerException;

}


Expand Down Expand Up @@ -1518,6 +1519,7 @@ final void executeOp(DTVExecuteOp op) throws SQLServerException {
case VARCHAR:
case LONGVARCHAR:
case CLOB:
case JSON:
op.execute(this, (byte[]) null);
break;

Expand Down Expand Up @@ -2989,6 +2991,25 @@ public void apply(TypeInfo typeInfo, TDSReader tdsReader) throws SQLServerExcept
typeInfo.maxLength = tdsReader.readInt();
typeInfo.ssType = SSType.SQL_VARIANT;
}
}),

JSON(TDSType.JSON, new Strategy() {
/**
* Sets the fields of typeInfo to the correct values
*
* @param typeInfo
* the TypeInfo whos values are being corrected
* @param tdsReader
* the TDSReader used to set the fields of typeInfo to the correct values
* @throws SQLServerException
* when an error occurs
*/
public void apply(TypeInfo typeInfo, TDSReader tdsReader) throws SQLServerException {
typeInfo.ssLenType = SSLenType.PARTLENTYPE;
typeInfo.ssType = SSType.JSON;
typeInfo.displaySize = typeInfo.precision = Integer.MAX_VALUE / 2;
typeInfo.charset = Encoding.UTF8.charset();
}
});

private final TDSType tdsType;
Expand Down Expand Up @@ -3737,6 +3758,7 @@ Object getValue(DTV dtv, JDBCType jdbcType, int scale, InputStreamGetterArgs str
case VARBINARYMAX:
case VARCHARMAX:
case NVARCHARMAX:
case JSON:
case UDT: {
convertedValue = DDC.convertStreamToObject(
PLPInputStream.makeStream(tdsReader, streamGetterArgs, this), typeInfo, jdbcType,
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/microsoft/sql/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,10 @@ private Types() {
* Microsoft SQL type GEOGRAPHY.
*/
public static final int GEOGRAPHY = -158;

/**
* The constant in the Java programming language, sometimes referred to as a type code, that identifies the
* Microsoft SQL type JSON.
*/
public static final int JSON = -159;
}
Loading
Loading