Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Trace writes for all ADO command execution

  • Loading branch information...
commit 238752beace6e08f096d3f6cc0da2acf63e527ca 1 parent bc78653
@markrendle markrendle authored
View
1  Simple.Data.Ado/AdoAdapter.cs
@@ -114,6 +114,7 @@ private int Execute(ICommandBuilder commandBuilder, IAdapterTransaction transact
private static int TryExecute(IDbCommand command)
{
+ command.WriteTrace();
try
{
return command.ExecuteNonQuery();
View
2  Simple.Data.Ado/AdoAdapterInserter.cs
@@ -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();
View
132 Simple.Data.Ado/DataReaderObservableRunner.cs
@@ -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();
- }
- }
- }
-}
View
12 Simple.Data.Ado/DbCommandExtensions.cs
@@ -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();
View
24 Simple.Data.Ado/DbCommandObservableEx.cs
@@ -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);
- }
- }
-}
View
29 Simple.Data.Ado/ObservableDataReader.cs
@@ -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);
- }
- }
-}
View
4 Simple.Data.Ado/ProcedureExecutor.cs
@@ -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>();
View
4 Simple.Data.Ado/Simple.Data.Ado.csproj
@@ -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" />
View
38 Simple.Data.Ado/TraceHelper.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
+ }
+ }
+ }
+}
View
105 Simple.Data.SqlTest/ObservableDataReaderTest.cs
@@ -1,105 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Data.SqlClient;
-using System.Linq;
-using System.Text;
-using System.Threading;
-using NUnit.Framework;
-using Simple.Data.Ado;
-using Simple.Data.Extensions;
-
-namespace Simple.Data.SqlTest
-{
- class ObservableDataReaderTest
- {
- private static SqlConnection MakeConnection()
- {
- return new SqlConnection(Properties.Settings.Default.ConnectionString);
- }
-
- private static SqlCommand MakeCommand()
- {
- return new SqlCommand("select * from Users", MakeConnection());
- }
-
- private static List<IDictionary<string, object>> _canonicalResults;
- private static readonly object Sync = new object();
-
- private static List<IDictionary<string, object>> CanonicalResults
- {
- get
- {
- CreateCanonicalResultsIfNull();
-
- return _canonicalResults;
- }
- }
-
- private static void CreateCanonicalResultsIfNull()
- {
- if (_canonicalResults == null)
- {
- lock (Sync)
- {
- if (_canonicalResults == null)
- {
- CreateCanonicalResults();
- }
- }
- }
- }
-
- private static void CreateCanonicalResults()
- {
- _canonicalResults = new List<IDictionary<string, object>>();
- var cmd = MakeCommand();
- using (cmd.Connection)
- using (cmd)
- {
- cmd.Connection.Open();
- using (var reader = cmd.ExecuteReader())
- {
- while (reader.Read())
- {
- _canonicalResults.Add(reader.ToDictionary());
- }
- }
- }
- }
-
- // ReSharper disable InconsistentNaming
-
- [Test]
- [ExpectedException(typeof(InvalidOperationException))]
- public void ToObservable_With_Open_Connection_Should_Throw_Exception()
- {
- var cn = MakeConnection();
- var cmd = cn.CreateCommand();
- cn.Open();
- cmd.ToObservable();
- }
-
- [Test]
- public void ToAsyncEnumerable_Should_Work_After_Pause()
- {
- var users = MakeCommand().ToAsyncEnumerable();
- Thread.Sleep(100);
- foreach (var user in users)
- {
- Assert.IsTrue(user.ContainsKey("Id".Homogenize()));
- }
-
- }
-
- [Test]
- public void EnumeratorShouldRunOnOriginalThread()
- {
- var threadId = Thread.CurrentThread.ManagedThreadId;
- foreach (var x in MakeCommand().ToAsyncEnumerable())
- {
- Assert.AreEqual(threadId, Thread.CurrentThread.ManagedThreadId);
- }
- }
- // ReSharper restore InconsistentNaming
- }
-}
View
1  Simple.Data.SqlTest/Simple.Data.SqlTest.csproj
@@ -60,7 +60,6 @@
<Compile Include="DatabaseOpenerTests.cs" />
<Compile Include="InsertTests.cs" />
<Compile Include="NaturalJoinTest.cs" />
- <Compile Include="ObservableDataReaderTest.cs" />
<Compile Include="ProcedureTest.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
View
8 Simple.Data.SqlTest/TestTraceListener.cs
@@ -9,6 +9,14 @@ namespace Simple.Data.SqlTest
public class TestTraceListener : TraceListener
{
private readonly StringBuilder _builder = new StringBuilder();
+
+ public override void Write(string message, string category)
+ {
+ if (category.Equals("Simple.Data.SqlTest"))
+ {
+ Write(message);
+ }
+ }
/// <summary>
/// When overridden in a derived class, writes the specified message to the listener you create in the derived class.
/// </summary>
Please sign in to comment.
Something went wrong with that request. Please try again.