Permalink
Browse files

No commit message

  • Loading branch information...
1 parent 9cb558a commit 675087206b3b82341c833725fa7cc776992c84ac @Ivony committed Oct 16, 2012
View
40 Sources/Ivony.Data/EntityExtensions.cs
@@ -52,6 +52,15 @@ public static T Entity<T>( this DbUtility dbUtility, IDbExpression expression )
/// <returns>实体</returns>
public static T ToEntity<T>( this DataRow dataItem ) where T : new()
{
+
+ if ( dataItem.Table.Columns.Count == 1 )
+ {
+ var value = dataItem[0];
+
+ if ( value is T )
+ return (T) value;
+ }
+
var method = CreateEntityConverter<T>();
var entity = new T();
method( dataItem, entity );
@@ -138,7 +147,7 @@ private static string GetFieldname( PropertyInfo p )
private static object sync = new object();
- private static Dictionary<PropertyInfo, object[]> _propertyAttributesCache = new Dictionary<PropertyInfo,object[]>();
+ private static Dictionary<PropertyInfo, object[]> _propertyAttributesCache = new Dictionary<PropertyInfo, object[]>();
private static object[] GetAttributes( PropertyInfo p )
{
@@ -197,4 +206,33 @@ public class NonFieldAttribute : Attribute
{
}
+
+ /// <summary>
+ /// 指定类型所应使用的实体转换器
+ /// </summary>
+ [AttributeUsage( AttributeTargets.Class, Inherited = false )]
+ public class EntityConvertAttribute : Attribute
+ {
+
+ private Type _convertType;
+
+ public EntityConvertAttribute( Type convertType )
+ {
+ _convertType = convertType;
+ }
+
+ public IEntityConverter<T> CreateConverter<T>()
+ {
+ return (IEntityConverter<T>) Activator.CreateInstance( _convertType );
+ }
+
+ }
+
+ public interface IEntityConverter<T>
+ {
+ T Convert( DataRow dataItem );
+ }
+
+
+
}
View
15 Sources/Ivony.Data/ExcelDbUtility.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data
+{
+ public class ExcelDbUtility : DbUtility
+ {
+ protected override IDbExpressionParser GetExpressionParser()
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
View
337 Sources/Ivony.Data/Expressions/TemplateExpression.cs
@@ -1,164 +1,173 @@
-using System;
-using System.Collections.Generic;
-using System.Data;
-using System.Linq;
-using System.Text;
-using System.Text.RegularExpressions;
-
-namespace Ivony.Data.Expressions
-{
-
-
- /// <summary>
- /// 模板解析上下文
- /// </summary>
- public abstract class TemplateParseContext
- {
-
- public abstract string CreateParameterExpression( ParameterExpression parameter );
-
- }
-
- /// <summary>
- /// 定义可以作为模板的一部分被嵌入模板的表达式
- /// </summary>
- public interface ITemplatePartialExpression : IDbExpression
- {
-
- string Parse( TemplateParseContext context );
-
- }
-
- /// <summary>
- /// 模板表达式
- /// </summary>
- public class TemplateExpression : ITemplatePartialExpression
- {
- /// <summary>
- /// 创建 TemplateExpression 对象
- /// </summary>
- /// <param name="template">模版</param>
- /// <param name="parameters">参数列表</param>
- public TemplateExpression( string template, params object[] parameters )
- {
-
- Template = template.Replace( "{...}", ParseParameterListSymbol( parameters.Length ) );
-
- Parameters = parameters.Select( item =>
- {
- var partial = item as ITemplatePartialExpression;
- if ( partial == null )
- partial = new ParameterExpression( item );
- return partial;
- }).ToArray();
- }
-
- /// <summary>
- /// 模版
- /// </summary>
- public string Template
- {
- get;
- private set;
- }
-
- /// <summary>
- /// 参数列表
- /// </summary>
- public ITemplatePartialExpression[] Parameters
- {
- get;
- private set;
- }
-
- /// <summary>
- /// 解析模版表达式
- /// </summary>
- /// <param name="context">模版解析上下文</param>
- /// <returns>需要嵌入在模版中的形式</returns>
- public string Parse( TemplateParseContext context )
- {
- return FormatRegexNum.Replace( Template, delegate( Match match )
- {
- string format = match.Groups["format"].ToString();
- int index = int.Parse( match.Groups["index"].ToString() );
-
- if ( index >= Parameters.Length )
- throw new IndexOutOfRangeException();
-
- ITemplatePartialExpression partial = Parameters[index];
-
- return partial.Parse( context );
- }
- );
- }
-
-
- /// <summary>
- /// 处理参数列表表达式“{...}”
- /// </summary>
- /// <param name="amount">参数个数</param>
- /// <returns></returns>
- private static string ParseParameterListSymbol( int amount )
- {
- StringBuilder builder = new StringBuilder();
-
- bool begin = true;
- for ( int i = 0; i < amount; i++ )
- {
- if ( !begin )
- builder.Append( " , " );
- builder.Append( "{" + i + "}" );
- begin = false;
- }
-
- return builder.ToString();
- }
-
-
- internal static readonly Regex FormatRegexNum = new Regex( @"\{(?<index>[0-9]*)(,(?<alignment>[0-9]+[a-zA-Z]*))?(\:(?<format>[^{}]*))?\}", RegexOptions.Compiled );
- }
-
- /// <summary>
- /// 参数表达式
- /// </summary>
- public class ParameterExpression : ITemplatePartialExpression
- {
- /// <summary>
- /// 创建 ParameterExpression 实例
- /// </summary>
- /// <param name="value">参数值</param>
- public ParameterExpression( object value )
- {
- Value = value;
- }
-
- /// <summary>
- /// 参数值
- /// </summary>
- public object Value
- {
- get;
- private set;
- }
-
- /// <summary>
- /// 数据类型,可选
- /// </summary>
- public DbType? DbType
- {
- get;
- private set;
- }
-
- /// <summary>
- /// 解析成参数表达式
- /// </summary>
- /// <param name="context">模版解析上下文</param>
- /// <returns>需要嵌入在模版中的形式</returns>
- public string Parse( TemplateParseContext context )
- {
- return context.CreateParameterExpression( this );
- }
- }
-}
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+
+namespace Ivony.Data.Expressions
+{
+
+
+ /// <summary>
+ /// 模板解析上下文
+ /// </summary>
+ public abstract class TemplateParseContext
+ {
+
+ /// <summary>
+ /// 创建参数 SQL 表达式
+ /// </summary>
+ /// <param name="parameter">参数表达式</param>
+ /// <returns>该参数在 SQL 语句中引用的形式</returns>
+ public abstract string CreateParameterExpression( ParameterExpression parameter );
+
+ }
+
+ /// <summary>
+ /// 定义可以作为模板的一部分被嵌入模板的表达式
+ /// </summary>
+ public interface ITemplatePartialExpression : IDbExpression
+ {
+ /// <summary>
+ /// 解析模版并提供嵌入的 SQL 表达式
+ /// </summary>
+ /// <param name="context">模版解析上下文</param>
+ /// <returns></returns>
+ string Parse( TemplateParseContext context );
+
+ }
+
+ /// <summary>
+ /// 模板表达式
+ /// </summary>
+ public class TemplateExpression : ITemplatePartialExpression
+ {
+ /// <summary>
+ /// 创建 TemplateExpression 对象
+ /// </summary>
+ /// <param name="template">模版</param>
+ /// <param name="parameters">参数列表</param>
+ public TemplateExpression( string template, params object[] parameters )
+ {
+
+ Template = template.Replace( "{...}", ParseParameterListSymbol( parameters.Length ) );
+
+ Parameters = parameters.Select( item =>
+ {
+ var partial = item as ITemplatePartialExpression;
+ if ( partial == null )
+ partial = new ParameterExpression( item );
+ return partial;
+ }).ToArray();
+ }
+
+ /// <summary>
+ /// 模版
+ /// </summary>
+ public string Template
+ {
+ get;
+ private set;
+ }
+
+ /// <summary>
+ /// 参数列表
+ /// </summary>
+ public ITemplatePartialExpression[] Parameters
+ {
+ get;
+ private set;
+ }
+
+ /// <summary>
+ /// 解析模版表达式
+ /// </summary>
+ /// <param name="context">模版解析上下文</param>
+ /// <returns>需要嵌入在模版中的形式</returns>
+ public string Parse( TemplateParseContext context )
+ {
+ return FormatRegexNum.Replace( Template, delegate( Match match )
+ {
+ string format = match.Groups["format"].ToString();
+ int index = int.Parse( match.Groups["index"].ToString() );
+
+ if ( index >= Parameters.Length )
+ throw new IndexOutOfRangeException();
+
+ ITemplatePartialExpression partial = Parameters[index];
+
+ return partial.Parse( context );
+ }
+ );
+ }
+
+
+ /// <summary>
+ /// 处理参数列表表达式“{...}”
+ /// </summary>
+ /// <param name="amount">参数个数</param>
+ /// <returns></returns>
+ private static string ParseParameterListSymbol( int amount )
+ {
+ StringBuilder builder = new StringBuilder();
+
+ bool begin = true;
+ for ( int i = 0; i < amount; i++ )
+ {
+ if ( !begin )
+ builder.Append( " , " );
+ builder.Append( "{" + i + "}" );
+ begin = false;
+ }
+
+ return builder.ToString();
+ }
+
+
+ internal static readonly Regex FormatRegexNum = new Regex( @"\{(?<index>[0-9]*)(,(?<alignment>[0-9]+[a-zA-Z]*))?(\:(?<format>[^{}]*))?\}", RegexOptions.Compiled );
+ }
+
+ /// <summary>
+ /// 参数表达式
+ /// </summary>
+ public class ParameterExpression : ITemplatePartialExpression
+ {
+ /// <summary>
+ /// 创建 ParameterExpression 实例
+ /// </summary>
+ /// <param name="value">参数值</param>
+ public ParameterExpression( object value )
+ {
+ Value = value;
+ }
+
+ /// <summary>
+ /// 参数值
+ /// </summary>
+ public object Value
+ {
+ get;
+ private set;
+ }
+
+ /// <summary>
+ /// 数据类型,可选
+ /// </summary>
+ public DbType? DbType
+ {
+ get;
+ private set;
+ }
+
+ /// <summary>
+ /// 解析成参数表达式
+ /// </summary>
+ /// <param name="context">模版解析上下文</param>
+ /// <returns>需要嵌入在模版中的形式</returns>
+ public string Parse( TemplateParseContext context )
+ {
+ return context.CreateParameterExpression( this );
+ }
+ }
+}
View
1 Sources/Ivony.Data/Ivony.Data.csproj
@@ -56,6 +56,7 @@
<Compile Include="DataSetExtensions.cs" />
<Compile Include="DbUtility.cs" />
<Compile Include="EntityExtensions.cs" />
+ <Compile Include="ExcelDbUtility.cs" />
<Compile Include="Expressions\StoredProcedureExpression.cs" />
<Compile Include="Expressions\TableExpression.cs" />
<Compile Include="Expressions\TemplateExpression.cs" />

0 comments on commit 6750872

Please sign in to comment.