Skip to content

Commit 4858245

Browse files
author
Divang Sharma
committed
Added writeRPCJSON to support JSON in out param
1 parent 42842e4 commit 4858245

File tree

3 files changed

+38
-53
lines changed

3 files changed

+38
-53
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4865,7 +4865,7 @@ void writeRPCStringUnicode(String sValue) throws SQLServerException {
48654865
}
48664866

48674867
void writeRPCJSON(String sValue) throws SQLServerException {
4868-
writeRPCJSON(null, sValue, false, null);
4868+
writeRPCJSON(null, sValue, true, null);
48694869
}
48704870
/**
48714871
* Writes a string value as Unicode for RPC
@@ -4933,50 +4933,22 @@ void writeRPCJSON(String sName, String sValue, boolean bOut,
49334933
SQLCollation collation) throws SQLServerException {
49344934
boolean bValueNull = (sValue == null);
49354935
int nValueLen = bValueNull ? 0 : (2 * sValue.length());
4936-
// Textual RPC requires a collation. If none is provided, as is the case when
4937-
// the SSType is non-textual, then use the database collation by default.
4938-
// if (null == collation)
4939-
// collation = con.getDatabaseCollation();
4940-
4941-
/*
4942-
* Use PLP encoding if either OUT params were specified or if the user query exceeds
4943-
* DataTypes.SHORT_VARTYPE_MAX_BYTES
4944-
*/
4945-
if (nValueLen > DataTypes.SHORT_VARTYPE_MAX_BYTES || bOut) {
4946-
writeRPCNameValType(sName, bOut, TDSType.JSON);
4947-
4948-
writeVMaxHeader(nValueLen, // Length
4949-
bValueNull, // Is null?
4950-
collation);
4951-
4952-
// Send the data.
4953-
if (!bValueNull) {
4954-
if (nValueLen > 0) {
4955-
writeInt(nValueLen);
4956-
writeString(sValue);
4957-
}
4958-
4959-
// Send the terminator PLP chunk.
4960-
writeInt(0);
4961-
}
4962-
} else { // non-PLP type
4963-
// Write maximum length of data
4964-
writeRPCNameValType(sName, bOut, TDSType.JSON);
4965-
writeShort((short) DataTypes.UNKNOWN_STREAM_LENGTH);
4966-
4967-
// collation.writeCollation(this);
4968-
4969-
// Data and length
4970-
if (bValueNull) {
4971-
writeShort((short) -1); // actual len
4972-
} else {
4973-
// Write actual length of data
4974-
writeShort((short) nValueLen);
4975-
4976-
// If length is zero, we're done.
4977-
if (0 != nValueLen)
4978-
writeString(sValue); // data
4936+
4937+
writeRPCNameValType(sName, bOut, TDSType.JSON);
4938+
4939+
writeVMaxHeader(nValueLen, // Length
4940+
bValueNull, // Is null?
4941+
null); // For JSON test code POC, JSON does not have collation value
4942+
4943+
// Send the data.
4944+
if (!bValueNull) {
4945+
if (nValueLen > 0) {
4946+
writeInt(nValueLen);
4947+
writeString(sValue);
49794948
}
4949+
4950+
// Send the terminator PLP chunk.
4951+
writeInt(0);
49804952
}
49814953
}
49824954

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ private void buildExecSQLParams(TDSWriter tdsWriter) throws SQLServerException {
919919

920920
// <formal parameter defn> IN
921921
if (preparedTypeDefinitions.length() > 0)
922-
tdsWriter.writeRPCStringUnicode(preparedTypeDefinitions);
922+
tdsWriter.writeRPCStringUnicode(preparedTypeDefinitions);
923923
}
924924

925925
private void buildServerCursorExecParams(TDSWriter tdsWriter) throws SQLServerException {

src/main/java/com/microsoft/sqlserver/jdbc/dtv.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,22 @@ void execute(DTV dtv, String strValue) throws SQLServerException {
298298
if (dtv.getJdbcType() == JDBCType.GUID) {
299299
tdsWriter.writeRPCUUID(name, UUID.fromString(strValue), isOutParam);
300300
}
301-
else if (dtv.getJdbcType() == JDBCType.JSON) {
302-
tdsWriter.writeRPCJSON(name, strValue, isOutParam, collation);
303-
}
301+
// else if (dtv.getJdbcType() == JDBCType.JSON) {
302+
// /* If you enble the following code, you will get the following exception:
303+
// * FINE: *** SQLException: com.microsoft.sqlserver.jdbc.SQLServerException: The incoming
304+
// * tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect.
305+
// * Parameter 3 (""): JSON data type is not supported in TDS on the server side.
306+
// */
307+
// tdsWriter.writeRPCJSON(name, strValue, isOutParam, collation);
308+
// }
304309
else {
310+
/*
311+
* For JSON type, if we use below method then we get the below error:
312+
* FINE: *** SQLException: com.microsoft.sqlserver.jdbc.SQLServerException: Implicit conversion
313+
* from data type json to nvarchar(max) is not allowed. Use the CONVERT function to run this query.
314+
* Msg 257, Level 16, State 3, Implicit conversion from data type
315+
* json to nvarchar(max) is not allowed.
316+
*/
305317
tdsWriter.writeRPCStringUnicode(name, strValue, isOutParam, collation);
306318
}
307319
}
@@ -1531,8 +1543,8 @@ final void executeOp(DTVExecuteOp op) throws SQLServerException {
15311543
case LONGVARCHAR:
15321544
case CLOB:
15331545
//case JSON:
1534-
op.execute(this, (byte[]) null);
1535-
break;
1546+
// op.execute(this, (byte[]) null);
1547+
// break;
15361548

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

16141626
switch (javaType) {
16151627
case STRING:
1628+
case JSON:
16161629
if (JDBCType.GUID == jdbcType) {
16171630
if (null != cryptoMeta) {
16181631
if (value instanceof String) {
@@ -1900,9 +1913,9 @@ else if ((JDBCType.VARCHAR == jdbcTypeSetByUser) || (JDBCType.CHAR == jdbcTypeSe
19001913
op.execute(this, (SQLServerSQLXML) value);
19011914
break;
19021915

1903-
case JSON:
1904-
op.execute(this, (SQLServerSQLJSON) value);
1905-
break;
1916+
// case JSON:
1917+
// op.execute(this, (SQLServerSQLJSON) value);
1918+
// break;
19061919

19071920
default:
19081921
assert false : "Unexpected JavaType: " + javaType;

0 commit comments

Comments
 (0)