Path: redb-route/redb.Route.Sql/Mapping/ScalarMapper.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Sql/Mapping/ScalarMapper.cs
----
using System.Data.Common;
namespace redb.Route.Sql.Mapping;
///
/// Maps the first column of the current row to a scalar value.
/// A is null for a nullable or reference and an error for a value type;
/// other values convert only when that is lossless and unambiguous, as does.
///
/// Scalar value type.
public sealed class ScalarMapper : ISqlRowMapper
{
///
/// The value cannot be read as without loss or guessing.
public T? Map(DbDataReader reader)
{
if (reader.IsDBNull(0))
{
if (!SqlValueConverter.AcceptsNull(typeof(T)))
throw new SqlRowMappingException(
$"Column '{reader.GetName(0)}' is NULL and cannot be read as {SqlValueConverter.TypeName(typeof(T))}: " +
"use a nullable type.");
return default;
}
var value = reader.GetValue(0);
if (!SqlValueConverter.TryConvert(value, typeof(T), out var converted, out var reason))
throw new SqlRowMappingException(
$"Column '{reader.GetName(0)}' ({value.GetType().Name}) cannot be read as {SqlValueConverter.TypeName(typeof(T))}: {reason}.");
return (T?)converted;
}
}