Permalink
Browse files

Added Trace writes for all ADO command execution

  • Loading branch information...
markrendle committed Mar 29, 2011
1 parent bc78653 commit 238752beace6e08f096d3f6cc0da2acf63e527ca
@@ -114,6 +114,7 @@ private int Execute(ICommandBuilder commandBuilder, IAdapterTransaction transact
private static int TryExecute(IDbCommand command)
{
+ command.WriteTrace();
try
{
return command.ExecuteNonQuery();
@@ -109,6 +109,7 @@ public AdoAdapterInserter(AdoAdapter adapter, IDbTransaction transaction)
private static IDictionary<string, object> TryExecuteSingletonQuery(IDbCommand command)
{
+ command.WriteTrace();
try
{
using (var reader = command.ExecuteReader())
@@ -147,6 +148,7 @@ internal int Execute(string sql, params object[] values)
private static int TryExecute(IDbCommand command)
{
+ command.WriteTrace();
try
{
return command.ExecuteNonQuery();
@@ -1,132 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Data;
-using System.Data.Common;
-
-namespace Simple.Data.Ado
-{
- class DataReaderObservableRunner : IDisposable
- {
- private static readonly object StaticSync = new object();
- private static readonly WeakCache<DataReaderObservableRunner> RunnerCache = new WeakCache<DataReaderObservableRunner>();
-
- private readonly IDbCommand _command;
- private readonly IObserver<IDictionary<string, object>> _observer;
- private IList<IDictionary<string, object>> _cache;
- private bool _disposed;
-
- protected DataReaderObservableRunner()
- {
-
- }
-
- private DataReaderObservableRunner(IDbCommand command, IObserver<IDictionary<string, object>> observer)
- {
- _command = command;
- _observer = observer;
- }
-
- public static DataReaderObservableRunner RunAsync(IDbCommand command, IObserver<IDictionary<string, object>> observer)
- {
- DataReaderObservableRunner instance = null;
-
- if (!RunnerCache.Contains(command))
- {
- instance = CreateInstance(command, observer);
- }
-
- if (instance == null) instance = new CachedRunner(RunnerCache.Get(command), observer);
- var asyncAction = new Action(instance.Run);
- asyncAction.BeginInvoke(asyncAction.EndInvoke, null);
- return instance;
- }
-
- private static DataReaderObservableRunner CreateInstance(IDbCommand command, IObserver<IDictionary<string, object>> observer)
- {
- lock (StaticSync)
- {
- if (!RunnerCache.Contains(command))
- {
- var instance = new DataReaderObservableRunner(command, observer);
- RunnerCache.Add(command, instance);
- return instance;
- }
- }
- return null;
- }
-
- protected virtual void Run()
- {
- try
- {
- RunQuery();
- _observer.OnCompleted();
- }
- catch (Exception ex)
- {
- _observer.OnError(ex);
- }
- }
-
- private void RunQuery()
- {
- if (_command.Connection.State == ConnectionState.Open)
- {
- RunReader();
- }
- else
- {
- using (_command.Connection)
- using (_command)
- {
- _command.Connection.Open();
- RunReader();
- }
- }
- }
-
- private void RunReader()
- {
- _cache = new List<IDictionary<string, object>>();
- using (var reader = _command.ExecuteReader())
- {
- while ((!_disposed) && reader.Read())
- {
- var row = reader.ToDictionary();
- _cache.Add(row);
- _observer.OnNext(row);
- }
- }
- }
-
- /// <summary>
- /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
- /// </summary>
- /// <filterpriority>2</filterpriority>
- public void Dispose()
- {
- _disposed = true;
- }
-
- private class CachedRunner : DataReaderObservableRunner
- {
- private readonly DataReaderObservableRunner _original;
- private readonly IObserver<IDictionary<string, object>> _newObserver;
-
- public CachedRunner(DataReaderObservableRunner original, IObserver<IDictionary<string, object>> observer)
- {
- _original = original;
- _newObserver = observer;
- }
-
- protected override void Run()
- {
- foreach (var row in _original._cache)
- {
- _newObserver.OnNext(row);
- }
- _newObserver.OnCompleted();
- }
- }
- }
-}
@@ -37,17 +37,6 @@ static class DbCommandExtensions
return new Dictionary<string, int>(keys, HomogenizedEqualityComparer.DefaultInstance);
}
- public static IEnumerable<IDictionary<string, object>> ToAsyncEnumerable(this IDbCommand command)
- {
- if (command.Connection == null) throw new InvalidOperationException("Command has no connection.");
- return ToObservable(command).ToEnumerable();
- }
-
- public static IObservable<IDictionary<string, object>> ToObservable(this IDbCommand command)
- {
- return new ObservableDataReader(command);
- }
-
public static IDbDataParameter AddParameter(this IDbCommand command, string name, object value)
{
var parameter = command.CreateParameter();
@@ -59,6 +48,7 @@ public static IDbDataParameter AddParameter(this IDbCommand command, string name
public static IDataReader ExecuteReaderWithExceptionWrap(this IDbCommand command)
{
+ command.WriteTrace();
try
{
return command.ExecuteReader();
@@ -1,24 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Data;
-using System.Data.Common;
-using System.Linq;
-using System.Reflection;
-
-namespace Simple.Data.Ado
-{
- public static class DbCommandObservableEx
- {
- public static IEnumerable<IDictionary<string, object>> ToAsyncEnumerable(this DbCommand command)
- {
- if (command.Connection.State != ConnectionState.Closed) throw new InvalidOperationException(MethodBase.GetCurrentMethod().Name + " must be called with a closed DbConnection.");
- return ToObservable(command).ToEnumerable();
- }
-
- public static IObservable<IDictionary<string,object>> ToObservable(this DbCommand command)
- {
- if (command.Connection.State != ConnectionState.Closed) throw new InvalidOperationException(MethodBase.GetCurrentMethod().Name + " must be called with a closed DbConnection.");
- return new ObservableDataReader(command);
- }
- }
-}
@@ -1,29 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Data;
-using System.Data.Common;
-
-namespace Simple.Data.Ado
-{
- class ObservableDataReader : IObservable<IDictionary<string, object>>
- {
- private readonly IDbCommand _command;
-
- public ObservableDataReader(IDbCommand command)
- {
- _command = command;
- }
-
- /// <summary>
- /// Notifies the provider that an observer is to receive notifications.
- /// </summary>
- /// <returns>
- /// The observer's interface that enables resources to be disposed.
- /// </returns>
- /// <param name="observer">The object that is to receive notifications.</param>
- public IDisposable Subscribe(IObserver<IDictionary<string, object>> observer)
- {
- return DataReaderObservableRunner.RunAsync(_command, observer);
- }
- }
-}
@@ -72,6 +72,7 @@ private static void RetrieveOutputParameterValues(Procedure procedure, IDbComman
public IEnumerable<ResultSet> ExecuteReader(IDbCommand command)
{
+ command.WriteTrace();
command.Connection.Open();
using (var reader = command.ExecuteReader())
{
@@ -88,7 +89,8 @@ public IEnumerable<ResultSet> ExecuteReader(IDbCommand command)
private static IEnumerable<ResultSet> ExecuteNonQuery(IDbCommand command)
{
- Trace.TraceInformation("ExecuteNonQuery");
+ command.WriteTrace();
+ Trace.TraceInformation("ExecuteNonQuery", "Simple.Data.SqlTest");
command.Connection.Open();
command.ExecuteNonQuery();
return Enumerable.Empty<ResultSet>();
@@ -63,10 +63,8 @@
<Compile Include="CommandHelper.cs" />
<Compile Include="DataParameterCollectionEx.cs" />
<Compile Include="DataReaderExtensions.cs" />
- <Compile Include="DataReaderObservableRunner.cs" />
<Compile Include="DataRecordExtensions.cs" />
<Compile Include="DbCommandExtensions.cs" />
- <Compile Include="DbCommandObservableEx.cs" />
<Compile Include="DbConnectionExtensions.cs" />
<Compile Include="DelegatingConnectionBase`1.cs" />
<Compile Include="DeleteHelper.cs" />
@@ -80,7 +78,6 @@
<Compile Include="ISchemaGetter.cs" />
<Compile Include="Joiner.cs" />
<Compile Include="JoinType.cs" />
- <Compile Include="ObservableDataReader.cs" />
<Compile Include="OptimizedDictionary.cs" />
<Compile Include="OptimizedDictionaryIndex.cs" />
<Compile Include="OptimizedDictionary`2.cs" />
@@ -110,6 +107,7 @@
<Compile Include="Schema\TableJoin.cs" />
<Compile Include="Schema\TableType.cs" />
<Compile Include="ObjectName.cs" />
+ <Compile Include="TraceHelper.cs" />
<Compile Include="TupleExtensions.cs" />
<Compile Include="UpdateHelper.cs" />
<Compile Include="WeakCache.cs" />
@@ -0,0 +1,38 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.Common;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+
+namespace Simple.Data.Ado
+{
+ public static class TraceHelper
+ {
+ public static void WriteTrace(this IDbCommand command)
+ {
+ if (Trace.Listeners.Count == 0) return;
+ try
+ {
+ var str = new StringBuilder();
+ str.AppendLine();
+ str.AppendLine(command.CommandType.ToString());
+ str.AppendLine(command.CommandText);
+ foreach (var parameter in command.Parameters.OfType<DbParameter>())
+ {
+ str.AppendFormat("{0} ({1}) = {2}", parameter.ParameterName, parameter.DbType, parameter.Value);
+ str.AppendLine();
+ }
+
+ Trace.WriteLine(str.ToString(), "Simple.Data.Ado");
+ }
+ catch (Exception)
+ {
+#if(DEBUG)
+ throw;
+#endif
+ }
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 238752b

Please sign in to comment.