Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Creating DataReader.

  • Loading branch information...
commit b200378a53ea48794fe8b149a8bf6d01f109d15e 1 parent f3fb4c1
@wagnerandrade authored
View
12 Thunderstruck/DataContext.cs
@@ -118,7 +118,8 @@ public object GetValue(string query, object queryParams = null)
/// <returns>The value of first column of the first row of the type specified on T.</returns>
public T GetValue<T>(string query, object queryParams = null)
{
- return DataHelpers.CastTo<T>(GetValue(query, queryParams));
+ var result = GetValue(query, queryParams);
+ return DataReader.CastTo<T>(result);
}
/// <summary>
@@ -129,7 +130,8 @@ public T GetValue<T>(string query, object queryParams = null)
/// <returns>The values of first column.</returns>
public T[] GetValues<T>(string query, object queryParams = null)
{
- return DataHelpers.DataReaderToPrimaryArray<T>(Query(query, queryParams));
+ var data = Query(query, queryParams);
+ return new DataReader(data).ToArray<T>();
}
/// <summary>
@@ -141,7 +143,8 @@ public T[] GetValues<T>(string query, object queryParams = null)
/// <returns>All row of query result in array of specified type.</returns>
public T[] All<T>(string query, object queryParams = null) where T : new()
{
- return DataHelpers.DataReaderToObjectArray<T>(Query(query, queryParams));
+ var data = Query(query, queryParams);
+ return new DataReader(data).ToObjectArray<T>();
}
/// <summary>
@@ -153,7 +156,8 @@ public T[] All<T>(string query, object queryParams = null) where T : new()
/// <returns>First row of query result in specified type.</returns>
public T First<T>(string query, object queryParams = null) where T : new()
{
- return DataHelpers.DataReaderToObjectArray<T>(Query(query, queryParams)).FirstOrDefault();
+ var data = Query(query, queryParams);
+ return new DataReader(data).ToObjectArray<T>().FirstOrDefault();
}
/// <summary>
View
41 Thunderstruck/Runtime/DataHelpers.cs → Thunderstruck/Runtime/DataReader.cs
@@ -1,22 +1,29 @@
using System;
using System.Collections.Generic;
-using System.Data;
using System.Linq;
-using System.Reflection;
+using System.Text;
+using System.Data;
namespace Thunderstruck.Runtime
{
- internal static class DataHelpers
+ public class DataReader
{
- internal static T[] DataReaderToObjectArray<T>(IDataReader reader) where T : new()
+ private IDataReader _dataReader;
+
+ public DataReader(IDataReader dataReader)
+ {
+ _dataReader = dataReader;
+ }
+
+ public T[] ToObjectArray<T>() where T : new()
{
try
{
var list = new List<T>();
var properties = typeof(T).GetProperties();
- var readerFields = GetDataReaderFields(reader);
+ var readerFields = GetFields();
- while (reader.Read())
+ while (_dataReader.Read())
{
var item = new T();
@@ -28,7 +35,7 @@ internal static T[] DataReaderToObjectArray<T>(IDataReader reader) where T : new
try
{
var propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
- object safeValue = (reader[field] == null || reader[field] is DBNull) ? null : Convert.ChangeType(reader[field], propertyType);
+ object safeValue = (_dataReader[field] == null || _dataReader[field] is DBNull) ? null : Convert.ChangeType(_dataReader[field], propertyType);
property.SetValue(item, safeValue, null);
}
catch (FormatException err)
@@ -47,37 +54,35 @@ internal static T[] DataReaderToObjectArray<T>(IDataReader reader) where T : new
}
finally
{
- reader.Close();
+ _dataReader.Close();
}
}
- internal static T[] DataReaderToPrimaryArray<T>(IDataReader reader)
+ public T[] ToArray<T>()
{
try
{
var list = new List<T>();
- while (reader.Read())
+ while (_dataReader.Read())
{
- list.Add(CastTo<T>(reader[0]));
+ list.Add(CastTo<T>(_dataReader[0]));
}
return list.ToArray();
}
finally
{
- reader.Close();
+ _dataReader.Close();
}
}
- internal static string[] GetDataReaderFields(IDataReader reader)
+ public string[] GetFields()
{
- var fields = new String[reader.FieldCount];
-
- for (int i = 0; i < reader.FieldCount; i++) fields[i] = reader.GetName(i);
-
+ var fields = new String[_dataReader.FieldCount];
+ for (int i = 0; i < _dataReader.FieldCount; i++) fields[i] = _dataReader.GetName(i);
return fields;
}
- internal static T CastTo<T>(object value)
+ public static T CastTo<T>(object value)
{
if (value is DBNull) return default(T);
else return (T) Convert.ChangeType(value, typeof(T));
View
2  Thunderstruck/Thunderstruck.csproj
@@ -50,10 +50,10 @@
<Compile Include="Provider\DefaultProvider.cs" />
<Compile Include="Provider\SqlProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
- <Compile Include="Runtime\DataHelpers.cs" />
<Compile Include="Runtime\ParametersBinder.cs" />
<Compile Include="Runtime\ProviderBuilder.cs" />
<Compile Include="Runtime\DataRuntimeObject.cs" />
+ <Compile Include="Runtime\DataReader.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Please sign in to comment.
Something went wrong with that request. Please try again.