toParamValue function

ParamValue toParamValue(
  1. Object? value
)

Converts a single object to a ParamValue instance.

Handles the common implicit Dart types. For SqlDataType / typedParam mappings, use the infrastructure protocol helpers (also re-exported from package:odbc_fast/odbc_fast.dart).

Implementation

ParamValue toParamValue(Object? value) {
  if (value == null) return const ParamValueNull();
  if (value is ParamValue) return value;

  if (value is int) {
    if (value >= -0x80000000 && value <= 0x7FFFFFFF) {
      return ParamValueInt32(value);
    }
    return ParamValueInt64(value);
  }

  if (value is String) return ParamValueString(value);
  if (value is List<int>) return ParamValueBinary(value);

  if (value is bool) {
    return ParamValueInt32(value ? 1 : 0);
  }
  if (value is double) {
    if (value.isNaN) {
      throw ArgumentError(
        'Double value is NaN. Cannot convert to decimal. '
        'Use explicit ParamValue with desired representation.',
      );
    }
    if (value.isInfinite) {
      final label = value.isNegative ? '-Infinity' : 'Infinity';
      throw ArgumentError(
        'Double value is $label. Cannot convert to decimal. '
        'Use explicit ParamValue with desired representation.',
      );
    }
    return ParamValueDecimal(value.toStringAsFixed(_defaultDecimalScale));
  }
  if (value is DateTime) {
    return ParamValueString(_toValidatedUtcIso8601(value));
  }

  throw ArgumentError(_unsupportedParameterTypeMessage(value));
}