314 lines
11 KiB
C#
Executable File
314 lines
11 KiB
C#
Executable File
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Opsoft.NetCore.O3App.Shared.Data
|
|
{
|
|
[ComVisible(false)]
|
|
internal class OPDbCommand : DbCommand
|
|
{
|
|
public delegate void ExecutingEvent(string dbName, string sql);
|
|
|
|
public delegate void ExecutedEvent(string dbName, string sql);
|
|
|
|
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
|
private OPDbConnection innerConnection;
|
|
|
|
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
|
private OPDbTransaction innerTransaction;
|
|
|
|
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
|
private readonly DbCommand innerDbCommand;
|
|
|
|
private List<ExecutingEvent> ExecutingEventList;
|
|
|
|
private List<ExecutedEvent> ExecutedEventList;
|
|
|
|
public override string CommandText
|
|
{
|
|
get
|
|
{
|
|
return innerDbCommand.CommandText;
|
|
}
|
|
set
|
|
{
|
|
innerDbCommand.CommandText = value;
|
|
}
|
|
}
|
|
|
|
public override int CommandTimeout
|
|
{
|
|
get
|
|
{
|
|
return innerDbCommand.CommandTimeout;
|
|
}
|
|
set
|
|
{
|
|
innerDbCommand.CommandTimeout = value;
|
|
}
|
|
}
|
|
|
|
public override CommandType CommandType
|
|
{
|
|
get
|
|
{
|
|
return innerDbCommand.CommandType;
|
|
}
|
|
set
|
|
{
|
|
innerDbCommand.CommandType = value;
|
|
}
|
|
}
|
|
|
|
public override bool DesignTimeVisible
|
|
{
|
|
get
|
|
{
|
|
return innerDbCommand.DesignTimeVisible;
|
|
}
|
|
set
|
|
{
|
|
innerDbCommand.DesignTimeVisible = value;
|
|
}
|
|
}
|
|
|
|
public override UpdateRowSource UpdatedRowSource
|
|
{
|
|
get
|
|
{
|
|
return innerDbCommand.UpdatedRowSource;
|
|
}
|
|
set
|
|
{
|
|
innerDbCommand.UpdatedRowSource = value;
|
|
}
|
|
}
|
|
|
|
protected override DbParameterCollection DbParameterCollection => innerDbCommand.Parameters;
|
|
|
|
protected override DbConnection DbConnection
|
|
{
|
|
get
|
|
{
|
|
return innerConnection;
|
|
}
|
|
set
|
|
{
|
|
innerConnection = value as OPDbConnection;
|
|
if (innerConnection == null)
|
|
{
|
|
innerDbCommand.Connection = value;
|
|
}
|
|
else
|
|
{
|
|
innerDbCommand.Connection = innerConnection.GetConnection();
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override DbTransaction DbTransaction
|
|
{
|
|
get
|
|
{
|
|
return innerTransaction;
|
|
}
|
|
set
|
|
{
|
|
innerTransaction = value as OPDbTransaction;
|
|
if (innerTransaction == null)
|
|
{
|
|
innerDbCommand.Transaction = value;
|
|
}
|
|
else
|
|
{
|
|
innerDbCommand.Transaction = innerTransaction.GetTransaction();
|
|
}
|
|
}
|
|
}
|
|
|
|
public event ExecutingEvent Executing;
|
|
|
|
public event ExecutedEvent Executed;
|
|
|
|
public OPDbCommand(DbCommand command, OPDbConnection connection)
|
|
: this(command, connection, checkWhere: false)
|
|
{
|
|
}
|
|
|
|
public OPDbCommand(DbCommand command, OPDbConnection connection, bool checkWhere = false)
|
|
{
|
|
innerDbCommand = command;
|
|
innerConnection = connection;
|
|
ExecutingEventList = new List<ExecutingEvent>();
|
|
ExecutedEventList = new List<ExecutedEvent>();
|
|
if (checkWhere)
|
|
{
|
|
ExecutingEventList.Add(ExtensionLoader.Instance.GetService<IDbCommandInterceptor>("Opsoft.Data.Audit").Executing);
|
|
}
|
|
foreach (ExecutingEvent executingEvent in ExecutingEventList)
|
|
{
|
|
Executing += executingEvent;
|
|
}
|
|
foreach (ExecutedEvent executedEvent in ExecutedEventList)
|
|
{
|
|
Executed += executedEvent;
|
|
}
|
|
}
|
|
|
|
public override void Cancel()
|
|
{
|
|
innerDbCommand.Cancel();
|
|
}
|
|
|
|
public override void Prepare()
|
|
{
|
|
innerDbCommand.Prepare();
|
|
}
|
|
|
|
protected override DbParameter CreateDbParameter()
|
|
{
|
|
return innerDbCommand.CreateParameter();
|
|
}
|
|
|
|
public override int ExecuteNonQuery()
|
|
{
|
|
Guid operationId = innerConnection.Listener.BeforeExecuteDbCommand(innerConnection.Provider, this, "ExecuteNonQuery");
|
|
try
|
|
{
|
|
this.Executing?.Invoke(innerConnection.Database, innerDbCommand.CommandText);
|
|
return innerDbCommand.ExecuteNonQuery();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
innerConnection.Listener.ExecuteDbCommandError(innerConnection.Provider, operationId, this, ex, "ExecuteNonQuery");
|
|
//Log.Error(ex, ex.Message);
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
innerConnection.Listener.AfterExecuteDbCommand(innerConnection.Provider, operationId, this, "ExecuteNonQuery");
|
|
}
|
|
}
|
|
|
|
public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
|
|
{
|
|
Guid operationId = innerConnection.Listener.BeforeExecuteDbCommand(innerConnection.Provider, this, "ExecuteNonQueryAsync");
|
|
try
|
|
{
|
|
this.Executing?.Invoke(innerConnection.Database, innerDbCommand.CommandText);
|
|
return await innerDbCommand.ExecuteNonQueryAsync(cancellationToken);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
innerConnection.Listener.ExecuteDbCommandError(innerConnection.Provider, operationId, this, ex, "ExecuteNonQueryAsync");
|
|
//Log.Error(ex, ex.Message);
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
innerConnection.Listener.AfterExecuteDbCommand(innerConnection.Provider, operationId, this, "ExecuteNonQueryAsync");
|
|
}
|
|
}
|
|
|
|
public override object ExecuteScalar()
|
|
{
|
|
Guid operationId = innerConnection.Listener.BeforeExecuteDbCommand(innerConnection.Provider, this, "ExecuteScalar");
|
|
try
|
|
{
|
|
this.Executing?.Invoke(innerConnection.Database, innerDbCommand.CommandText);
|
|
return innerDbCommand.ExecuteScalar();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
innerConnection.Listener.ExecuteDbCommandError(innerConnection.Provider, operationId, this, ex, "ExecuteScalar");
|
|
//Log.Error(ex, ex.Message);
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
innerConnection.Listener.AfterExecuteDbCommand(innerConnection.Provider, operationId, this, "ExecuteScalar");
|
|
}
|
|
}
|
|
|
|
public override async Task<object> ExecuteScalarAsync(CancellationToken cancellationToken)
|
|
{
|
|
Guid operationId = innerConnection.Listener.BeforeExecuteDbCommand(innerConnection.Provider, this, "ExecuteScalarAsync");
|
|
try
|
|
{
|
|
this.Executing?.Invoke(innerConnection.Database, innerDbCommand.CommandText);
|
|
return await innerDbCommand.ExecuteScalarAsync(cancellationToken);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
innerConnection.Listener.ExecuteDbCommandError(innerConnection.Provider, operationId, this, ex, "ExecuteScalarAsync");
|
|
//Log.Error(ex, ex.Message);
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
innerConnection.Listener.AfterExecuteDbCommand(innerConnection.Provider, operationId, this, "ExecuteScalarAsync");
|
|
}
|
|
}
|
|
|
|
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
|
|
{
|
|
Guid operationId = innerConnection.Listener.BeforeExecuteDbCommand(innerConnection.Provider, this, "ExecuteDbDataReader");
|
|
try
|
|
{
|
|
this.Executing?.Invoke(innerConnection.Database, innerDbCommand.CommandText);
|
|
return innerDbCommand.ExecuteReader(behavior);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
innerConnection.Listener.ExecuteDbCommandError(innerConnection.Provider, operationId, this, ex, "ExecuteDbDataReader");
|
|
//Log.Error(ex, ex.Message);
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
innerConnection.Listener.AfterExecuteDbCommand(innerConnection.Provider, operationId, this, "ExecuteDbDataReader");
|
|
}
|
|
}
|
|
|
|
protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
|
|
{
|
|
Guid operationId = innerConnection.Listener.BeforeExecuteDbCommand(innerConnection.Provider, this, "ExecuteDbDataReaderAsync");
|
|
try
|
|
{
|
|
this.Executing?.Invoke(innerConnection.Database, innerDbCommand.CommandText);
|
|
return await innerDbCommand.ExecuteReaderAsync(behavior, cancellationToken);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
innerConnection.Listener.ExecuteDbCommandError(innerConnection.Provider, operationId, this, ex, "ExecuteDbDataReaderAsync");
|
|
//Log.Error(ex, ex.Message);
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
innerConnection.Listener.AfterExecuteDbCommand(innerConnection.Provider, operationId, this, "ExecuteDbDataReaderAsync");
|
|
}
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
foreach (ExecutingEvent executingEvent in ExecutingEventList)
|
|
{
|
|
Executing -= executingEvent;
|
|
}
|
|
foreach (ExecutedEvent executedEvent in ExecutedEventList)
|
|
{
|
|
Executed -= executedEvent;
|
|
}
|
|
innerDbCommand.Dispose();
|
|
base.Dispose(disposing);
|
|
}
|
|
}
|
|
|
|
}
|