Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions MiniMods/DataReader/DataReaderMinimod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;
using System.Data;

namespace Minimod.DataReader
{
/// <summary>
/// <h1>Minimod.DataReader, Version 0.0.1, Copyright © Uwe Zimmermann, 2012</h1>
/// <para>A minimod with some extension methods for <see cref="IDataReader"/>.</para>
/// </summary>
/// <remarks>
/// Licensed under the Apache License, Version 2.0; you may not use this file except in compliance with the License.
/// http://www.apache.org/licenses/LICENSE-2.0
/// </remarks>
public static class DataReaderMinimod
{
/// <summary>
/// Gets the string value of the specified field. If the value is <see cref="DBNull.Value"/> it returns <c>null</c>.
/// </summary>
/// <param name="self">An <see cref="IDataReader"/>.</param>
/// <param name="name">The name of the field.</param>
/// <returns>A string containing the value of the specified field or <c>null</c> if the value is <see cref="DBNull.Value"/>.</returns>
/// <exception cref="ArgumentNullException">When parameter <paramref name="self"/> is <c>null</c>.</exception>
public static string GetStringOrNull(this IDataReader self, string name)
{
if (self == null)
{
throw new ArgumentNullException("self");
}

var ordinal = self.GetOrdinal(name);

return self.IsDBNull(ordinal) ? null : self.GetString(ordinal);
}

/// <summary>
/// Gets the string value of the specified field. If the value is <see cref="DBNull.Value"/> it returns <c>null</c>.
/// </summary>
/// <param name="self">An <see cref="IDataReader"/>.</param>
/// <param name="fieldIndex">The index of the field.</param>
/// <returns>A string containing the value of the specified field or <c>null</c> if the value is <see cref="DBNull.Value"/>.</returns>
/// <exception cref="ArgumentNullException">When parameter <paramref name="self"/> is <c>null</c>.</exception>
public static string GetStringOrNull(this IDataReader self, int fieldIndex)
{
if (self == null)
{
throw new ArgumentNullException("self");
}

return self.IsDBNull(fieldIndex) ? null : self.GetString(fieldIndex);
}

/// <summary>
/// Gets the value of the specified field as <c>Nullable</c> of <typeparamref name="TResult"/>. If the value is <see cref="DBNull.Value"/>
/// it returns <c>null</c>.
/// </summary>
/// <typeparam name="TResult">The type to return. It have to be a struct.</typeparam>
/// <param name="self">An <see cref="IDataReader"/>.</param>
/// <param name="fieldName">The name of the field.</param>
/// <returns>The value of the specified field or <c>null</c> if the value is <see cref="DBNull.Value"/>.</returns>
/// <exception cref="ArgumentNullException">When parameter <paramref name="self"/> is <c>null</c>.</exception>
public static TResult? GetNullableValue<TResult>(this IDataReader self, string fieldName) where TResult : struct
{
if (self == null)
{
throw new ArgumentNullException("self");
}

var ordinal = self.GetOrdinal(fieldName);

return self.IsDBNull(ordinal) ? null : (TResult?)self.GetValue(ordinal);
}

/// <summary>
/// Gets the value of the specified field as <c>Nullable</c> of <typeparamref name="TResult"/>. If the value is <see cref="DBNull.Value"/>
/// it returns <c>null</c>.
/// </summary>
/// <typeparam name="TResult">The type to return. It have to be a struct.</typeparam>
/// <param name="self">An <see cref="IDataReader"/>.</param>
/// <param name="fieldIndex">The index of the field.</param>
/// <returns>The value of the specified field or <c>null</c> if the value is <see cref="DBNull.Value"/>.</returns>
/// <exception cref="ArgumentNullException">When parameter <paramref name="self"/> is <c>null</c>.</exception>
public static TResult? GetNullableValue<TResult>(this IDataReader self, int fieldIndex) where TResult : struct
{
if (self == null)
{
throw new ArgumentNullException("self");
}

return self.IsDBNull(fieldIndex) ? null : (TResult?)self.GetValue(fieldIndex);
}

/// <summary>
/// Gets the value of the specified field as <typeparamref name="TResult"/>.
/// </summary>
/// <typeparam name="TResult">The type to return. It have to be a struct.</typeparam>
/// <param name="self">An <see cref="IDataReader"/>.</param>
/// <param name="fieldName">The name of the field.</param>
/// <returns>The value of the specified field or <c>null</c> if the value is <see cref="DBNull.Value"/>.</returns>
/// <exception cref="ArgumentNullException">When parameter <paramref name="self"/> is <c>null</c>.</exception>
public static TResult GetValue<TResult>(this IDataReader self, string fieldName) where TResult : struct
{
if (self == null)
{
throw new ArgumentNullException("self");
}

var ordinal = self.GetOrdinal(fieldName);

return (TResult)self.GetValue(ordinal);
}

/// <summary>
/// Gets the value of the specified field as <typeparamref name="TResult"/>.
/// </summary>
/// <typeparam name="TResult">The type to return. It have to be a struct.</typeparam>
/// <param name="self">An <see cref="IDataReader"/>.</param>
/// <param name="fieldIndex">The index of the field.</param>
/// <returns>The value of the specified field or <c>null</c> if the value is <see cref="DBNull.Value"/>.</returns>
/// <exception cref="ArgumentNullException">When parameter <paramref name="self"/> is <c>null</c>.</exception>
public static TResult GetValue<TResult>(this IDataReader self, int fieldIndex) where TResult : struct
{
if (self == null)
{
throw new ArgumentNullException("self");
}

return (TResult)self.GetValue(fieldIndex);
}
}
}
18 changes: 18 additions & 0 deletions MiniMods/DataReader/DataReaderMinimod.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Minimod.DataReader</id>
<version>0.0.1</version>
<authors>Uwe Zimmermann</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0</licenseUrl>
<tags>minimod</tags>
<summary>
A minimod with some extension methods for <see cref="IDataReader"/>.
</summary>
<description xml:space="preserve">A minimod with some extension methods for <see cref="IDataReader"/>.</description>
</metadata>
<files>
<file src="DataReaderMinimod.cs" target="content\Minimods\Minimod.DataReader.Generated.cs" />
</files>
</package>
3 changes: 3 additions & 0 deletions MiniMods/Minimod.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Interactive">
<HintPath>..\packages\Ix_Experimental-Main.1.1.10823\lib\Net4\System.Interactive.dll</HintPath>
</Reference>
Expand All @@ -96,6 +97,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Continuation\ContinuationMinimod.cs" />
<Compile Include="DataReader\DataReaderMinimod.cs" />
<Compile Include="HttpMessageStream\HttpContext.cs" />
<Compile Include="HttpMessageStream\HttpMessageProcessorMinimod.cs" />
<Compile Include="HttpMessageStream\HttpMessageStreamMinimod.cs" />
Expand Down Expand Up @@ -160,6 +162,7 @@
</ItemGroup>
<ItemGroup>
<None Include="Continuation\ContinuationMinimod.nuspec" />
<None Include="DataReader\DataReaderMinimod.nuspec" />
<None Include="HttpMessageStream\HttpMessageStreamMinimod.nuspec" />
<None Include="MessageProcessor\MessageProcessorMinimod.nuspec" />
<None Include="FluentNullOrEmpty\FluentNullOrEmptyMinimod.nuspec" />
Expand Down