NIFI-5819: Support SQLServer sql_variant type

This closes #6699.

Signed-off-by: Peter Turcsanyi <turcsanyi@apache.org>
This commit is contained in:
Matthew Burgess 2022-11-21 18:35:35 -05:00 committed by Peter Turcsanyi
parent 2ad33eea80
commit 5f1d93f977
2 changed files with 13 additions and 0 deletions

View File

@ -650,6 +650,11 @@ public class JdbcCommon {
builder.name(columnName).type().unionOf().nullBuilder().endNull().and().bytesType().endUnion().noDefault();
break;
case -150: // SQLServer may return -150 from the driver even though it's really -156 (sql_variant), treat as a union since we don't know what the values will actually be
case -156:
builder.name(columnName).type().unionOf().nullBuilder().endNull().and().stringType().and().intType().and().longType().and().booleanType().and().bytesType().and()
.doubleType().and().floatType().endUnion().noDefault();
break;
default:
throw new IllegalArgumentException("createSchema: Unknown SQL type " + meta.getColumnType(i) + " / " + meta.getColumnTypeName(i)

View File

@ -736,12 +736,20 @@ public class PutDatabaseRecord extends AbstractProcessor {
}
} else {
sqlType = column.getDataType();
// SQLServer returns -150 for sql_variant from DatabaseMetaData though the server expects -156 when setting a sql_variant parameter
if (sqlType == -150) {
sqlType = -156;
}
}
// Convert (if necessary) from field data type to column data type
if (fieldSqlType != sqlType) {
try {
DataType targetDataType = DataTypeUtils.getDataTypeFromSQLTypeValue(sqlType);
// If sqlType is unsupported, fall back to the fieldSqlType instead
if (targetDataType == null) {
targetDataType = DataTypeUtils.getDataTypeFromSQLTypeValue(fieldSqlType);
}
if (targetDataType != null) {
if (sqlType == Types.BLOB || sqlType == Types.BINARY) {
if (currentValue instanceof Object[]) {