decode<T> static method

T decode<T>(
  1. MssqlRow row,
  2. int index, {
  3. required String projection,
  4. required String column,
  5. MssqlExpression? expression,
  6. bool required = false,
})

Reads index as T, naming name on a conversion failure.

Implementation

static T decode<T>(
  MssqlRow row,
  int index, {
  required String projection,
  required String column,
  MssqlExpression? expression,
  bool required = false,
}) {
  final raw = row.at(index);
  if (raw == null) {
    if (required || !_isNullable<T>()) {
      throw StateError(
        'Projection $projection column $column was NULL. '
        'The descriptor marks it non-null; fix the SQL or the override.',
      );
    }
    return null as T;
  }
  final type = expression == null ? null : MssqlSqlType.of(expression);
  if (type != null) {
    try {
      final decoded = MssqlTypeCodecs.forColumn(type).decode(raw);
      if (decoded is T) return decoded;
    } on Object catch (error) {
      throw StateError(
        'Projection $projection column $column could not decode '
        '${raw.runtimeType} as $T: $error',
      );
    }
  }
  if (raw is T) return raw as T;
  throw StateError(
    'Projection $projection column $column contains '
    '${raw.runtimeType}, not $T.',
  );
}