Skip to content

Commit

Permalink
Fix for issue when attempting to insert an empty or null value on enc…
Browse files Browse the repository at this point in the history
…rypted column (#632)

* Workaround for #624

* Apply the fix to only variable length types

* Adding comments
  • Loading branch information
ulvii authored Mar 7, 2018
1 parent db0f41a commit f0df377
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/main/java/com/microsoft/sqlserver/jdbc/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,9 @@ private void setTypeDefinition(DTV dtv) {
* specific type info, otherwise generic type info can be used as before.
*/
if (0 == valueLength) {
param.typeDefinition = "varbinary";
// Workaround for the issue when inserting empty string and null into encrypted columns
param.typeDefinition = "varbinary(1)";
valueLength++;
}
else {
param.typeDefinition = "varbinary(" + valueLength + ")";
Expand Down Expand Up @@ -721,7 +723,9 @@ private void setTypeDefinition(DTV dtv) {
* specific type info, otherwise generic type info can be used as before.
*/
if (0 == valueLength) {
param.typeDefinition = "varchar";
// Workaround for the issue when inserting empty string and null into encrypted columns
param.typeDefinition = "varchar(1)";
valueLength++;
}
else {
param.typeDefinition = "varchar(" + valueLength + ")";
Expand All @@ -745,7 +749,9 @@ private void setTypeDefinition(DTV dtv) {
if ((null != jdbcTypeSetByUser) && ((jdbcTypeSetByUser == JDBCType.VARCHAR) || (jdbcTypeSetByUser == JDBCType.CHAR)
|| (jdbcTypeSetByUser == JDBCType.LONGVARCHAR))) {
if (0 == valueLength) {
param.typeDefinition = "varchar";
// Workaround for the issue when inserting empty string and null into encrypted columns
param.typeDefinition = "varchar(1)";
valueLength++;
}
else if (DataTypes.SHORT_VARTYPE_MAX_BYTES < valueLength) {
param.typeDefinition = VARCHAR_MAX;
Expand All @@ -761,7 +767,9 @@ else if (DataTypes.SHORT_VARTYPE_MAX_BYTES < valueLength) {
else if ((null != jdbcTypeSetByUser)
&& (jdbcTypeSetByUser == JDBCType.NVARCHAR || jdbcTypeSetByUser == JDBCType.LONGNVARCHAR)) {
if (0 == valueLength) {
param.typeDefinition = "nvarchar";
// Workaround for the issue when inserting empty string and null into encrypted columns
param.typeDefinition = "nvarchar(1)";
valueLength++;
}
else if (DataTypes.SHORT_VARTYPE_MAX_CHARS < valueLength) {
param.typeDefinition = NVARCHAR_MAX;
Expand All @@ -776,7 +784,9 @@ else if (DataTypes.SHORT_VARTYPE_MAX_CHARS < valueLength) {
}
else { // used if setNull() is called with java.sql.Types.NCHAR
if (0 == valueLength) {
param.typeDefinition = "nvarchar";
// Workaround for the issue when inserting empty string and null into encrypted columns
param.typeDefinition = "nvarchar(1)";
valueLength++;
}
else {
param.typeDefinition = "nvarchar(" + valueLength + ")";
Expand Down Expand Up @@ -813,7 +823,9 @@ else if (DataTypes.SHORT_VARTYPE_MAX_CHARS < valueLength) {
if ((null != jdbcTypeSetByUser) && ((jdbcTypeSetByUser == JDBCType.VARCHAR) || (jdbcTypeSetByUser == JDBCType.CHAR)
|| (JDBCType.LONGVARCHAR == jdbcTypeSetByUser))) {
if (0 == valueLength) {
param.typeDefinition = "varchar";
// Workaround for the issue when inserting empty string and null into encrypted columns
param.typeDefinition = "varchar(1)";
valueLength++;
}
else {
param.typeDefinition = "varchar(" + valueLength + ")";
Expand All @@ -830,7 +842,9 @@ else if (DataTypes.SHORT_VARTYPE_MAX_CHARS < valueLength) {
else if ((null != jdbcTypeSetByUser) && ((jdbcTypeSetByUser == JDBCType.NVARCHAR) || (jdbcTypeSetByUser == JDBCType.NCHAR)
|| (JDBCType.LONGNVARCHAR == jdbcTypeSetByUser))) {
if (0 == valueLength) {
param.typeDefinition = "nvarchar";
// Workaround for the issue when inserting empty string and null into encrypted columns
param.typeDefinition = "nvarchar(1)";
valueLength++;
}
else {
param.typeDefinition = "nvarchar(" + valueLength + ")";
Expand All @@ -846,7 +860,9 @@ else if ((null != jdbcTypeSetByUser) && ((jdbcTypeSetByUser == JDBCType.NVARCHAR
}
else { // used if setNull() is called with java.sql.Types.NCHAR
if (0 == valueLength) {
param.typeDefinition = "nvarchar";
// Workaround for the issue when inserting empty string and null into encrypted columns
param.typeDefinition = "nvarchar(1)";
valueLength++;
}
else {
param.typeDefinition = "nvarchar(" + valueLength + ")";
Expand Down

0 comments on commit f0df377

Please sign in to comment.