Skip to content
Browse files

No commit message

  • Loading branch information...
1 parent 3b49e9e commit f6dac728371c0bbdc2d8094e6481ae9661960645 @Ivony committed
View
36 Sources/Ivony.Data/AccessDbUtility.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Linq;
+using System.Text;
+
+namespace Ivony.Data
+{
+ public class AccessDbUtility : DbUtility
+ {
+ protected override IDataAdapter CreateDataAdapter( System.Data.IDbCommand selectCommand )
+ {
+ throw new NotImplementedException();
+ }
+
+ protected override IDbExpressionParser GetExpressionParser()
+ {
+ throw new NotImplementedException();
+ }
+
+
+ private class AccessExpressionParser : IDbExpressionParser
+ {
+
+ public IDbCommand Parse( IDbExpression expression )
+ {
+
+ throw new NotImplementedException();
+
+ }
+
+ }
+
+
+ }
+}
View
211 Sources/Ivony.Data/DbUtility.cs
@@ -15,22 +15,7 @@ public abstract class DbUtility
/// <summary>
- /// 由派生类实现,创建IDataAdapter对象
- /// </summary>
- /// <param name="selectCommand">查询命令</param>
- protected abstract IDataAdapter CreateDataAdapter( IDbCommand selectCommand );
-
- /// <summary>
- /// 由派生类实现,创建IDataParameter对象
- /// </summary>
- /// <param name="name">参数名</param>
- /// <param name="value">参数值</param>
- protected abstract IDataParameter CreateParameter( string name, object value );
-
-
-
- /// <summary>
- /// 获取DbExpressionParser实例,用于分析Sql表达式
+ /// 获取DbExpressionParser实例,用于分析数据查询表达式
/// </summary>
/// <returns>DbExpressionParser实例</returns>
protected abstract IDbExpressionParser GetExpressionParser();
@@ -38,7 +23,7 @@ public abstract class DbUtility
/// <summary>
/// 创建 DbCommand 对象
/// </summary>
- /// <param name="expression">命令表达式</param>
+ /// <param name="expression">查询表达式</param>
/// <returns>DbCommand 对象</returns>
protected virtual IDbCommand CreateCommand( IDbExpression expression )
{
@@ -49,30 +34,20 @@ protected virtual IDbCommand CreateCommand( IDbExpression expression )
/// <summary>
- /// 填充DataSet,并将填充的最后一个表返回
- /// </summary>
- /// <param name="template">查询字符串模板</param>
- /// <param name="parameters">查询字符串参数</param>
- /// <returns></returns>
- public DataTable Data( IDbExpression expression )
- {
- return Data( null, null, expression );
- }
-
- /// <summary>
- /// 填充DataSet,并将填充的最后一个表返回
+ /// 执行无结果的查询
/// </summary>
- /// <param name="dataSet">需要被填充的数据集</param>
- /// <param name="tableName">将最后一个表设置为什么名字</param>
/// <param name="expression">查询表达式</param>
- /// <returns></returns>
- public virtual DataTable Data( DataSet dataSet, string tableName, IDbExpression expression )
+ /// <returns>影响的行数</returns>
+ public virtual int ExecuteNonQuery( IDbExpression expression )
{
-
IDbCommand command = CreateCommand( expression );
try
{
- return Data( CreateDataAdapter( command ), dataSet, tableName );
+ OnCommandExecuting( this, command );
+ var result = ExecuteNonQuery( command );
+ OnCommandExecuted( this, command );
+
+ return result;
}
catch ( DbException e )
{
@@ -82,16 +57,20 @@ public virtual DataTable Data( DataSet dataSet, string tableName, IDbExpression
}
/// <summary>
- /// 执行无结果的查询
+ /// 执行查询,并返回首行首列
/// </summary>
/// <param name="expression">查询表达式</param>
- /// <returns>影响的行数</returns>
- public virtual int NonQuery( IDbExpression expression )
+ /// <returns>查询结果的首行首列</returns>
+ public virtual object ExecuteScalar( IDbExpression expression )
{
IDbCommand command = CreateCommand( expression );
try
{
- return NonQuery( command );
+ OnCommandExecuting( this, command );
+ var result = ExecuteScalar( command );
+ OnCommandExecuted( this, command );
+
+ return result;
}
catch ( DbException e )
{
@@ -101,16 +80,22 @@ public virtual int NonQuery( IDbExpression expression )
}
/// <summary>
- /// 执行查询,并返回首行首列
+ /// 执行查询,并返回首行
/// </summary>
/// <param name="expression">查询表达式</param>
- /// <returns>查询结果的首行首列</returns>
- public virtual object Scalar( IDbExpression expression )
+ /// <returns>查询结果的首行</returns>
+ public virtual DataRow ExecuteFirstRow( IDbExpression expression )
{
+
IDbCommand command = CreateCommand( expression );
+
try
{
- return Scalar( command );
+ OnCommandExecuting( this, command );
+ var result = ExecuteFirstRow( command );
+ OnCommandExecuted( this, command );
+
+ return result;
}
catch ( DbException e )
{
@@ -120,18 +105,21 @@ public virtual object Scalar( IDbExpression expression )
}
/// <summary>
- /// 执行查询,并返回首行
+ /// 执行查询,并返回第一个结果集
/// </summary>
/// <param name="expression">查询表达式</param>
- /// <returns></returns>
- public virtual DataRow FirstRow( IDbExpression expression )
+ /// <returns>第一个结果集</returns>
+ public virtual DataTable ExecuteData( IDbExpression expression )
{
- IDbCommand command = CreateCommand( expression );
-
+ var command = CreateCommand( expression );
try
{
- return FirstRow( command );
+ OnCommandExecuting( this, command );
+ var result = ExecuteData( command );
+ OnCommandExecuted( this, command );
+
+ return result;
}
catch ( DbException e )
{
@@ -142,35 +130,15 @@ public virtual DataRow FirstRow( IDbExpression expression )
- /// <summary>
- /// 协助填充DataSet,并将填充的最后一个表返回
- /// </summary>
- /// <param name="adapter">用来填充数据的适配器</param>
- /// <param name="dataSet">需要被填充的数据集</param>
- /// <param name="tableName">将最后一个表设置为什么名字</param>
- /// <returns></returns>
- public static DataTable Data( IDataAdapter adapter, DataSet dataSet, string tableName )
- {
- if ( dataSet == null )
- dataSet = new DataSet();
-
- adapter.Fill( dataSet );
- DataTable dataTable = dataSet.Tables[dataSet.Tables.Count - 1];
- if ( tableName != null )
- dataTable.TableName = tableName;
-
- return dataTable;
- }
-
/// <summary>
- /// 协助执行无结果的查询
+ /// 执行无结果的查询
/// </summary>
/// <param name="command">查询命令</param>
/// <returns>受影响的行数。</returns>
- public static int NonQuery( IDbCommand command )
+ public static int ExecuteNonQuery( IDbCommand command )
{
if ( command.Connection == null )
throw new InvalidOperationException( "数据库连接尚未初始化" );
@@ -196,7 +164,7 @@ public static int NonQuery( IDbCommand command )
/// </summary>
/// <param name="command">查询命令</param>
/// <returns></returns>
- public static object Scalar( IDbCommand command )
+ public static object ExecuteScalar( IDbCommand command )
{
if ( command.Connection == null )
throw new InvalidOperationException( "数据库连接尚未初始化" );
@@ -222,7 +190,7 @@ public static object Scalar( IDbCommand command )
/// </summary>
/// <param name="command">查询命令</param>
/// <returns></returns>
- public static DataRow FirstRow( IDbCommand command )
+ public static DataRow ExecuteFirstRow( IDbCommand command )
{
if ( command.Connection == null )
throw new InvalidOperationException( "数据库连接尚未初始化" );
@@ -232,7 +200,7 @@ public static DataRow FirstRow( IDbCommand command )
try
{
command.Connection.Open();
- return ExecuteSingleRowPrivate( command );
+ return ExecuteFirstRowPrivate( command );
}
finally
{
@@ -241,11 +209,11 @@ public static DataRow FirstRow( IDbCommand command )
}
else
{
- return ExecuteSingleRowPrivate( command );
+ return ExecuteFirstRowPrivate( command );
}
}
- private static DataRow ExecuteSingleRowPrivate( IDbCommand command )
+ private static DataRow ExecuteFirstRowPrivate( IDbCommand command )
{
using ( IDataReader reader = command.ExecuteReader( CommandBehavior.SingleRow | CommandBehavior.SingleResult ) )
{
@@ -260,6 +228,44 @@ private static DataRow ExecuteSingleRowPrivate( IDbCommand command )
}
}
+ /// <summary>
+ /// 执行查询,并返回第一个结果集
+ /// </summary>
+ /// <param name="command">查询命令</param>
+ /// <returns>第一个结果集</returns>
+ public static DataTable ExecuteData( IDbCommand command )
+ {
+ if ( command.Connection == null )
+ throw new InvalidOperationException( "数据库连接尚未初始化" );
+
+ if ( command.Connection.State == ConnectionState.Closed )
+ {
+ try
+ {
+ command.Connection.Open();
+ return ExecuteDataPrivate( command );
+ }
+ finally
+ {
+ command.Connection.Close();
+ }
+ }
+ else
+ return ExecuteDataPrivate( command );
+ }
+
+ private static DataTable ExecuteDataPrivate( IDbCommand command )
+ {
+ using ( IDataReader reader = command.ExecuteReader( CommandBehavior.SingleRow | CommandBehavior.SingleResult ) )
+ {
+ DataTable table = new DataTable();
+
+ table.Load( reader );
+
+ return table;
+ }
+ }
+
/// <summary>
@@ -304,7 +310,7 @@ public static IDataReader ExecuteReader( IDbCommand command )
/// <summary>
- /// 当发生错误时
+ /// 当发生错误时引发此事件
/// </summary>
public event EventHandler<DbErrorEventArgs> Error;
@@ -325,6 +331,57 @@ protected void OnError( object sender, DbException exception, IDbCommand command
/// <summary>
+ /// 当执行某个命令前引发此事件
+ /// </summary>
+ public event EventHandler<DbCommandEventArgs> CommandExecuting;
+
+ protected void OnCommandExecuting( object sender, IDbCommand command )
+ {
+ if ( CommandExecuting != null )
+ CommandExecuting( sender, new DbCommandEventArgs( command ) );
+ }
+
+ /// <summary>
+ /// 当成功执行某个命令后引发此事件
+ /// </summary>
+ public event EventHandler<DbCommandEventArgs> CommandExecuted;
+
+ protected void OnCommandExecuted( object sender, IDbCommand command )
+ {
+ if ( CommandExecuted != null )
+ CommandExecuted( sender, new DbCommandEventArgs( command ) );
+ }
+
+
+
+
+ /// <summary>
+ /// 为数据库操作事件提供事件参数
+ /// </summary>
+ public class DbCommandEventArgs : EventArgs
+ {
+ /// <summary>
+ /// 创建 DbCommandEventArgs 对象
+ /// </summary>
+ /// <param name="command">执行的查询命令</param>
+ public DbCommandEventArgs( IDbCommand command )
+ {
+ Command = command;
+ }
+
+ /// <summary>
+ /// 获取执行的查询命令对象
+ /// </summary>
+ public IDbCommand Command
+ {
+ get;
+ private set;
+ }
+
+ }
+
+
+ /// <summary>
/// 为 Error 事件提供参数
/// </summary>
public class DbErrorEventArgs : EventArgs
View
4 Sources/Ivony.Data/EntityExtensions.cs
@@ -25,7 +25,7 @@ public static class EntityExtensions
/// <returns>实体集</returns>
public static T[] Entities<T>( this DbUtility dbUtility, IDbExpression expression ) where T : new()
{
- var data = dbUtility.Data( expression );
+ var data = dbUtility.ExecuteData( expression );
return data.Rows.Cast<DataRow>().Select( dataItem => dataItem.ToEntity<T>() ).ToArray();
}
@@ -39,7 +39,7 @@ public static T[] Entities<T>( this DbUtility dbUtility, IDbExpression expressio
/// <returns>实体</returns>
public static T Entity<T>( this DbUtility dbUtility, IDbExpression expression ) where T : new()
{
- var dataItem = dbUtility.FirstRow( expression );
+ var dataItem = dbUtility.ExecuteFirstRow( expression );
return dataItem.ToEntity<T>();
}
View
1 Sources/Ivony.Data/Ivony.Data.csproj
@@ -52,6 +52,7 @@
<Reference Include="System.XML" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="AccessDbUtility.cs" />
<Compile Include="DbUtility.cs" />
<Compile Include="EntityExtensions.cs" />
<Compile Include="Expressions\StoredProcedureExpression.cs" />
View
7 Sources/Ivony.Data/SqlDbUtility.cs
@@ -73,13 +73,6 @@ protected override IDataAdapter CreateDataAdapter( IDbCommand selectCommand )
return new SqlDataAdapter( (SqlCommand) selectCommand );
}
- protected override IDataParameter CreateParameter( string name, object value )
- {
- return new SqlParameter( name, value ?? DBNull.Value );
- }
-
-
-
/// <summary>
/// 创建一个事务
/// </summary>
View
8 Sources/Ivony.Data/TemplateExtensions.cs
@@ -23,7 +23,7 @@ public static class TemplateExtensions
/// <returns></returns>
public static DataTable Data( this DbUtility dbUtility, string template, params object[] parameters )
{
- return dbUtility.Data( Template( template, parameters ) );
+ return dbUtility.ExecuteData( Template( template, parameters ) );
}
/// <summary>
@@ -48,7 +48,7 @@ public static DataTable Data( this DbUtility dbUtility, DataSet dataSet, string
/// <returns></returns>
public static int NonQuery( this DbUtility dbUtility, string commandText, params object[] parameters )
{
- return dbUtility.NonQuery( Template( commandText, parameters ) );
+ return dbUtility.ExecuteNonQuery( Template( commandText, parameters ) );
}
/// <summary>
@@ -59,7 +59,7 @@ public static int NonQuery( this DbUtility dbUtility, string commandText, params
/// <returns></returns>
public static object Scalar( this DbUtility dbUtility, string commandText, params object[] parameters )
{
- return dbUtility.Scalar( Template( commandText, parameters ) );
+ return dbUtility.ExecuteScalar( Template( commandText, parameters ) );
}
/// <summary>
@@ -70,7 +70,7 @@ public static object Scalar( this DbUtility dbUtility, string commandText, param
/// <returns></returns>
public static DataRow FirstRow( this DbUtility dbUtility, string commandText, params object[] parameters )
{
- return dbUtility.FirstRow( Template( commandText, parameters ) );
+ return dbUtility.ExecuteFirstRow( Template( commandText, parameters ) );
}
/// <summary>

0 comments on commit f6dac72

Please sign in to comment.
Something went wrong with that request. Please try again.