Skip to content

Commit

Permalink
Bug Fix : Failure when executing SQL string MRCollective#7
Browse files Browse the repository at this point in the history
  • Loading branch information
BlairAllegroTech committed Jul 6, 2014
1 parent e77528b commit 313fd85
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 13 deletions.
16 changes: 16 additions & 0 deletions ReliableDbProvider.Tests/DbProviderTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ public void Insert_and_select_entity()
Assert.That(dbUser.Name, Is.EqualTo(user.Name));
}
}

[Test]
public void Insert_and_select_entity_with_custom_sql()
{
using (var context = GetContext())
using (var context2 = GetContext())
{
var user = new User { Name = "Name" };
context.Users.Add(user);
context.SaveChanges();

var dbUser = context2.Database.SqlQuery<User>("select * from [user] where Id = '" + user.Id + "'").FirstOrDefault();

Assert.That(dbUser.Name, Is.EqualTo(user.Name));
}
}

[Test]
public void Insert_and_select_multiple_entities()
Expand Down
1 change: 1 addition & 0 deletions ReliableDbProvider/ReliableDbProvider.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReliableDbProviderServices.cs" />
<Compile Include="ReliableSqlDbTransaction.cs" />
<Compile Include="ReliableSqlClientProvider.cs" />
<Compile Include="ReliableSqlCommand.cs">
<SubType>Component</SubType>
Expand Down
70 changes: 59 additions & 11 deletions ReliableDbProvider/ReliableSqlCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,40 @@ namespace ReliableDbProvider
/// </remarks>
public class ReliableSqlCommand : DbCommand, ICloneable
{
ReliableSqlDbConnection ReliableDbConnection;
ReliableSqlDbTransaction ReliableDbTransaction;

/// <summary>
/// The underlying <see cref="SqlCommand"/> being proxied.
/// </summary>
public SqlCommand Current { get; private set; }
SqlCommand Current { get; set; }

/// <summary>
/// The <see cref="ReliableSqlConnection"/> that has been assigned to the command via the Connection property.
/// </summary>
public ReliableSqlConnection ReliableConnection { get; set; }
ReliableSqlConnection ReliableConnection { get; set; }


/// <summary>
/// Constructs a <see cref="ReliableSqlCommand"/>.
/// Constructs a <see cref="ReliableSqlCommand"/>. with no associated connection
/// </summary>
public ReliableSqlCommand(SqlCommand commandToWrap)
internal ReliableSqlCommand(SqlCommand commandToWrap)
{
this.Current = commandToWrap;
System.Diagnostics.Debug.Assert(
Current.Connection == null,
"Expected Command connection to be uninitialised. This constructor creates a new command witn no associated connection!");

this.ReliableDbConnection = null;
this.ReliableConnection = null;
}

//Bug Fix: Failure when executing SQL string
public ReliableSqlCommand(ReliableSqlDbConnection connection, SqlCommand commandToWrap)
{
Current = commandToWrap;
this.Current = commandToWrap;
this.ReliableDbConnection = connection;
this.ReliableConnection = (connection==null) ? null : connection.ReliableConnection;
}

/// <summary>
Expand All @@ -48,19 +66,24 @@ public static explicit operator SqlCommand(ReliableSqlCommand command)
/// </summary>
protected override DbConnection DbConnection
{
get { return Current.Connection; }
get
{
return ReliableDbConnection;
}
set
{
if (value == null)
return;
ReliableConnection = ((ReliableSqlDbConnection)value).ReliableConnection;

ReliableDbConnection = ((ReliableSqlDbConnection)value);
ReliableConnection = ReliableDbConnection.ReliableConnection;
Current.Connection = ReliableConnection.Current;
}
}

public object Clone()
{
return new ReliableSqlCommand(Current.Clone());
return new ReliableSqlCommand(this.ReliableDbConnection, Current.Clone());
}

#region Wrapping code
Expand Down Expand Up @@ -109,13 +132,38 @@ public override int ExecuteNonQuery()

public override object ExecuteScalar()
{
return ReliableConnection.ExecuteCommand<int>(Current);
//Bug: In Entlib 5 this returns an IDataReader
//return ReliableConnection.ExecuteCommand<object>(Current);

return ReliableConnection.CommandRetryPolicy.ExecuteAction(() =>
{
if (Connection == null)
Connection = ReliableConnection.Open();
if (Connection.State != ConnectionState.Open)
Connection.Open();
return Current.ExecuteScalar();
});
}

protected override DbTransaction DbTransaction
{
get { return Current.Transaction; }
set { Current.Transaction = (SqlTransaction)value; }
get
{
return ReliableDbTransaction;
}
set
{
if (value == null)
{
ReliableDbTransaction = null;
Current.Transaction = null;
}
else
{
ReliableDbTransaction = (ReliableSqlDbTransaction)value;
Current.Transaction = ReliableDbTransaction.InnerTransaction;
}
}
}

public override string CommandText
Expand Down
6 changes: 4 additions & 2 deletions ReliableDbProvider/ReliableSqlDbConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLeve
{
if (ReliableConnection.State != ConnectionState.Open)
ReliableConnection.Open();
return (DbTransaction) ReliableConnection.BeginTransaction(isolationLevel);

var transaction = (SqlTransaction)ReliableConnection.BeginTransaction(isolationLevel);
return new ReliableSqlDbTransaction(this, transaction);
});
}

Expand Down Expand Up @@ -95,7 +97,7 @@ public override void ChangeDatabase(string databaseName)

protected override DbCommand CreateDbCommand()
{
return new ReliableSqlCommand(ReliableConnection.CreateCommand());
return new ReliableSqlCommand(this, ReliableConnection.CreateCommand());
}

public override void Open()
Expand Down
55 changes: 55 additions & 0 deletions ReliableDbProvider/ReliableSqlDbTransaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReliableDbProvider
{
/// <summary>
/// This class just wraps a Dbtransaction and prevents our SqlConnection from being leaked to the outside world
/// </summary>
public class ReliableSqlDbTransaction : DbTransaction
{
readonly ReliableSqlDbConnection innerConnection;
readonly SqlTransaction innerTransaction;

internal SqlTransaction InnerTransaction { get { return innerTransaction; } }

public ReliableSqlDbTransaction(ReliableSqlDbConnection connection, SqlTransaction transaction)
{
this.innerConnection = connection;
this.innerTransaction = transaction;
}
public override void Commit()
{
innerTransaction.Commit();
}

protected override DbConnection DbConnection
{
get { return innerConnection; }
}

public override System.Data.IsolationLevel IsolationLevel
{
get { return innerTransaction.IsolationLevel; }
}

public override void Rollback()
{
innerTransaction.Rollback();
}

protected override void Dispose(bool disposing)
{
innerTransaction.Dispose();
base.Dispose(disposing);
}

}
}

0 comments on commit 313fd85

Please sign in to comment.