// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Data;
using System.Globalization;
using Core.Common.IO.Exceptions;
namespace Core.Common.IO.Readers
{
public static class IDataReaderExtensions
{
///
/// Reads a value at column from the database.
///
/// The expected type of value in the column with name .
/// The data reader to read a column of a certain type from.
/// The name of the column to read from.
/// The read value from the column with name .
/// Thrown when is not present in the read
/// data row.
/// Thrown when or
/// is null.
/// Thrown when the value in the column could not be converted
/// to type .
public static T Read(this IDataReader dataReader, string columnName)
{
if (dataReader == null)
{
throw new ArgumentNullException("dataReader");
}
if (columnName == null)
{
throw new ArgumentNullException("columnName");
}
var conversionType = typeof(T);
object value;
try
{
value = dataReader[columnName];
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException(string.Format("Column '{0}' not defined for data row.", columnName), "columnName");
}
try
{
return (T) Convert.ChangeType(value, conversionType);
}
catch (InvalidCastException)
{
throw new ConversionException(
string.Format(CultureInfo.CurrentCulture, "Value read from data reader ('{0}') could not be cast to desired type {1}.",
value,
conversionType));
}
catch (FormatException)
{
throw new ConversionException(
string.Format(CultureInfo.CurrentCulture, "Value read from data reader ('{0}') is an incorrect format to transform to type {1}.",
value,
conversionType));
}
catch (OverflowException)
{
throw new ConversionException(
string.Format(CultureInfo.CurrentCulture, "Value read from data reader ('{0}') was too large to convert to type {1}.",
value,
conversionType));
}
}
}
}