diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/Parameter.java b/src/main/java/com/microsoft/sqlserver/jdbc/Parameter.java index 86edb5d8a..e0731eefe 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/Parameter.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/Parameter.java @@ -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 + ")"; @@ -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 + ")"; @@ -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; @@ -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; @@ -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 + ")"; @@ -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 + ")"; @@ -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 + ")"; @@ -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 + ")";