Permalink
Browse files

fixed naming strategy issue in ReadExtension

  • Loading branch information...
1 parent 82a26c5 commit 8084d4ace509888695c1b710d1276becbc37aadc Thomas Grassauer committed Nov 21, 2012
Showing with 72 additions and 65 deletions.
  1. +72 −65 src/ServiceStack.OrmLite/Expressions/ReadExtensions.cs
@@ -2,37 +2,37 @@
using System.Linq;
using System.Collections.Generic;
using System.Data;
-using System.Linq.Expressions;
+using System.Linq.Expressions;
namespace ServiceStack.OrmLite
{
public static class ReadExtensions
- {
- public static SqlExpressionVisitor<T> CreateExpression<T>()
- {
- return OrmLiteConfig.DialectProvider.ExpressionVisitor<T>();
- }
-
- public static List<T> Select<T>(this IDbCommand dbCmd, Expression<Func<T, bool>> predicate)
- where T : new()
- {
- var ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<T>();
- string sql = ev.Where(predicate).ToSelectStatement();
- using (var reader = dbCmd.ExecReader(sql))
- {
- return ConvertToList<T>(reader);
- }
- }
-
- public static List<T> Select<T>(this IDbCommand dbCmd, Func<SqlExpressionVisitor<T>, SqlExpressionVisitor<T>> expression)
- where T : new()
- {
- var ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<T>();
- string sql = expression(ev).ToSelectStatement();
- using (var reader = dbCmd.ExecReader(sql))
- {
- return ConvertToList<T>(reader);
- }
+ {
+ public static SqlExpressionVisitor<T> CreateExpression<T>()
+ {
+ return OrmLiteConfig.DialectProvider.ExpressionVisitor<T>();
+ }
+
+ public static List<T> Select<T>(this IDbCommand dbCmd, Expression<Func<T, bool>> predicate)
+ where T : new()
+ {
+ var ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<T>();
+ string sql = ev.Where(predicate).ToSelectStatement();
+ using (var reader = dbCmd.ExecReader(sql))
+ {
+ return ConvertToList<T>(reader);
+ }
+ }
+
+ public static List<T> Select<T>(this IDbCommand dbCmd, Func<SqlExpressionVisitor<T>, SqlExpressionVisitor<T>> expression)
+ where T : new()
+ {
+ var ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<T>();
+ string sql = expression(ev).ToSelectStatement();
+ using (var reader = dbCmd.ExecReader(sql))
+ {
+ return ConvertToList<T>(reader);
+ }
}
public static List<T> Select<T>(this IDbCommand dbCmd, SqlExpressionVisitor<T> expression)
@@ -71,8 +71,8 @@ public static T FirstOrDefault<T>(this IDbCommand dbCmd, Expression<Func<T, bool
var ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<T>();
return FirstOrDefault(dbCmd, ev.Where(predicate).Limit(1));
- }
-
+ }
+
public static T FirstOrDefault<T>(this IDbCommand dbCmd, SqlExpressionVisitor<T> expression)
where T : new()
{
@@ -81,18 +81,18 @@ public static T FirstOrDefault<T>(this IDbCommand dbCmd, SqlExpressionVisitor<T>
{
return ConvertTo<T>(dbReader);
}
- }
-
- /// <summary>
- /// e.g. dbCmd.GetScalar&lt;MyClass, DateTime&gt;(myClass => Sql.Max(myClass.Timestamp));
- /// </summary>
- public static TKey GetScalar<T, TKey>(this IDbCommand dbCmd, Expression<Func<T, TKey>> field)
- where T : new()
- {
- var ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<T>();
- ev.Select(field);
- var sql = ev.ToSelectStatement();
- return dbCmd.GetScalar<TKey>(sql);
+ }
+
+ /// <summary>
+ /// e.g. dbCmd.GetScalar&lt;MyClass, DateTime&gt;(myClass => Sql.Max(myClass.Timestamp));
+ /// </summary>
+ public static TKey GetScalar<T, TKey>(this IDbCommand dbCmd, Expression<Func<T, TKey>> field)
+ where T : new()
+ {
+ var ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<T>();
+ ev.Select(field);
+ var sql = ev.ToSelectStatement();
+ return dbCmd.GetScalar<TKey>(sql);
}
/// <summary>
@@ -118,16 +118,20 @@ private static T ConvertTo<T>(IDataReader dataReader)
{
if (dataReader.Read())
{
- var row = new T();
+ var row = new T();
+
+ var namingStrategy = OrmLiteConfig.DialectProvider.NamingStrategy;
+
for (int i = 0; i<dataReader.FieldCount; i++)
- {
- //Bug: Not respecting the dialect's naming strategy
- var fieldDef = fieldDefs.FirstOrDefault(
- x => x.FieldName.ToUpper() == dataReader.GetName(i).ToUpper());
-
- if (fieldDef == null) continue;
+ {
+ var fieldDef = fieldDefs.FirstOrDefault(
+ x =>
+ namingStrategy.GetColumnName(x.FieldName).ToUpper() ==
+ dataReader.GetName(i).ToUpper());
+
+ if (fieldDef == null) continue;
var value = dataReader.GetValue(i);
- fieldDef.SetValue(row, value);
+ fieldDef.SetValue(row, value);
}
return row;
@@ -139,7 +143,7 @@ private static T ConvertTo<T>(IDataReader dataReader)
private static List<T> ConvertToList<T>(IDataReader dataReader)
where T : new()
{
- var fieldDefs = ModelDefinition<T>.Definition.FieldDefinitionsArray;
+ var fieldDefs = ModelDefinition<T>.Definition.FieldDefinitionsArray;
var fieldDefCache = new Dictionary<int, FieldDefinition>();
var to = new List<T>();
@@ -149,42 +153,45 @@ private static List<T> ConvertToList<T>(IDataReader dataReader)
{
var row = new T();
+ var namingStrategy = OrmLiteConfig.DialectProvider.NamingStrategy;
+
for (int i = 0; i<dataReader.FieldCount; i++)
{
- FieldDefinition fieldDef;
+ FieldDefinition fieldDef;
if (!fieldDefCache.TryGetValue(i, out fieldDef))
- {
- //Bug: Not respecting the dialect's naming strategy
- fieldDef = fieldDefs.FirstOrDefault(
- x => x.FieldName.ToUpper() == dataReader.GetName(i).ToUpper());
+ {
+ fieldDef = fieldDefs.FirstOrDefault(
+ x =>
+ namingStrategy.GetColumnName(x.FieldName).ToUpper() ==
+ dataReader.GetName(i).ToUpper());
fieldDefCache[i] = fieldDef;
- }
-
- if (fieldDef == null) continue;
-
+ }
+
+ if (fieldDef == null) continue;
+
var value = dataReader.GetValue(i);
- fieldDef.SetValue(row, value);
+ fieldDef.SetValue(row, value);
}
to.Add(row);
}
}
return to;
- }
-
+ }
+
/*
private static T PopulateWithSqlReader<T>( T objWithProperties, IDataReader dataReader, FieldDefinition[] fieldDefs)
{
foreach (var fieldDef in fieldDefs)
{
try{
// NOTE: this is a nasty ineffeciency here when we're calling this for multiple rows!
- // we should only call GetOrdinal once per column per result set
+ // we should only call GetOrdinal once per column per result set
// and one could only wish for a -1 return instead of an IndexOutOfRangeException...
// how to get -1 ?
//If the index of the named field is not found, an IndexOutOfRangeException is thrown.
//http://msdn.microsoft.com/en-us/library/system.data.idatarecord.getordinal(v=vs.100).aspx
- var index = dataReader.GetColumnIndex(fieldDef.FieldName);
+ var index = dataReader.GetColumnIndex(fieldDef.FieldName);
var value = dataReader.GetValue(index);
fieldDef.SetValue(objWithProperties, value);
}
@@ -196,7 +203,7 @@ private static T PopulateWithSqlReader<T>( T objWithProperties, IDataReader data
return objWithProperties;
}
- */
+ */
// First FirstOrDefault // Use LIMIT to retrive only one row ! someone did it
}

0 comments on commit 8084d4a

Please sign in to comment.