Skip to content

Commit

Permalink
Added writeRPCJSON to support JSON in out param
Browse files Browse the repository at this point in the history
  • Loading branch information
Divang Sharma committed Jan 16, 2025
1 parent 42842e4 commit 4858245
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 53 deletions.
60 changes: 16 additions & 44 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4865,7 +4865,7 @@ void writeRPCStringUnicode(String sValue) throws SQLServerException {
}

void writeRPCJSON(String sValue) throws SQLServerException {
writeRPCJSON(null, sValue, false, null);
writeRPCJSON(null, sValue, true, null);
}
/**
* Writes a string value as Unicode for RPC
Expand Down Expand Up @@ -4933,50 +4933,22 @@ void writeRPCJSON(String sName, String sValue, boolean bOut,
SQLCollation collation) throws SQLServerException {
boolean bValueNull = (sValue == null);
int nValueLen = bValueNull ? 0 : (2 * sValue.length());
// Textual RPC requires a collation. If none is provided, as is the case when
// the SSType is non-textual, then use the database collation by default.
// if (null == collation)
// collation = con.getDatabaseCollation();

/*
* Use PLP encoding if either OUT params were specified or if the user query exceeds
* DataTypes.SHORT_VARTYPE_MAX_BYTES
*/
if (nValueLen > DataTypes.SHORT_VARTYPE_MAX_BYTES || bOut) {
writeRPCNameValType(sName, bOut, TDSType.JSON);

writeVMaxHeader(nValueLen, // Length
bValueNull, // Is null?
collation);

// Send the data.
if (!bValueNull) {
if (nValueLen > 0) {
writeInt(nValueLen);
writeString(sValue);
}

// Send the terminator PLP chunk.
writeInt(0);
}
} else { // non-PLP type
// Write maximum length of data
writeRPCNameValType(sName, bOut, TDSType.JSON);
writeShort((short) DataTypes.UNKNOWN_STREAM_LENGTH);

// collation.writeCollation(this);

// Data and length
if (bValueNull) {
writeShort((short) -1); // actual len
} else {
// Write actual length of data
writeShort((short) nValueLen);

// If length is zero, we're done.
if (0 != nValueLen)
writeString(sValue); // data

writeRPCNameValType(sName, bOut, TDSType.JSON);

writeVMaxHeader(nValueLen, // Length
bValueNull, // Is null?
null); // For JSON test code POC, JSON does not have collation value

// Send the data.
if (!bValueNull) {
if (nValueLen > 0) {
writeInt(nValueLen);
writeString(sValue);
}

// Send the terminator PLP chunk.
writeInt(0);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ private void buildExecSQLParams(TDSWriter tdsWriter) throws SQLServerException {

// <formal parameter defn> IN
if (preparedTypeDefinitions.length() > 0)
tdsWriter.writeRPCStringUnicode(preparedTypeDefinitions);
tdsWriter.writeRPCStringUnicode(preparedTypeDefinitions);
}

private void buildServerCursorExecParams(TDSWriter tdsWriter) throws SQLServerException {
Expand Down
29 changes: 21 additions & 8 deletions src/main/java/com/microsoft/sqlserver/jdbc/dtv.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,22 @@ void execute(DTV dtv, String strValue) throws SQLServerException {
if (dtv.getJdbcType() == JDBCType.GUID) {
tdsWriter.writeRPCUUID(name, UUID.fromString(strValue), isOutParam);
}
else if (dtv.getJdbcType() == JDBCType.JSON) {
tdsWriter.writeRPCJSON(name, strValue, isOutParam, collation);
}
// else if (dtv.getJdbcType() == JDBCType.JSON) {
// /* If you enble the following code, you will get the following exception:
// * FINE: *** SQLException: com.microsoft.sqlserver.jdbc.SQLServerException: The incoming
// * tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect.
// * Parameter 3 (""): JSON data type is not supported in TDS on the server side.
// */
// tdsWriter.writeRPCJSON(name, strValue, isOutParam, collation);
// }
else {
/*
* For JSON type, if we use below method then we get the below error:
* FINE: *** SQLException: com.microsoft.sqlserver.jdbc.SQLServerException: Implicit conversion
* from data type json to nvarchar(max) is not allowed. Use the CONVERT function to run this query.
* Msg 257, Level 16, State 3, Implicit conversion from data type
* json to nvarchar(max) is not allowed.
*/
tdsWriter.writeRPCStringUnicode(name, strValue, isOutParam, collation);
}
}
Expand Down Expand Up @@ -1531,8 +1543,8 @@ final void executeOp(DTVExecuteOp op) throws SQLServerException {
case LONGVARCHAR:
case CLOB:
//case JSON:
op.execute(this, (byte[]) null);
break;
// op.execute(this, (byte[]) null);
// break;

case GUID:
if (null != cryptoMeta)
Expand Down Expand Up @@ -1613,6 +1625,7 @@ final void executeOp(DTVExecuteOp op) throws SQLServerException {

switch (javaType) {
case STRING:
case JSON:
if (JDBCType.GUID == jdbcType) {
if (null != cryptoMeta) {
if (value instanceof String) {
Expand Down Expand Up @@ -1900,9 +1913,9 @@ else if ((JDBCType.VARCHAR == jdbcTypeSetByUser) || (JDBCType.CHAR == jdbcTypeSe
op.execute(this, (SQLServerSQLXML) value);
break;

case JSON:
op.execute(this, (SQLServerSQLJSON) value);
break;
// case JSON:
// op.execute(this, (SQLServerSQLJSON) value);
// break;

default:
assert false : "Unexpected JavaType: " + javaType;
Expand Down

0 comments on commit 4858245

Please sign in to comment.