Skip to content

Commit

Permalink
Merge pull request #10 from MRCollective/fix-for-7
Browse files Browse the repository at this point in the history
Fix #7
  • Loading branch information
robdmoore committed Jul 20, 2014
2 parents e77528b + 53d1392 commit 49d03fb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 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
40 changes: 27 additions & 13 deletions ReliableDbProvider/ReliableSqlCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure;

namespace ReliableDbProvider
Expand All @@ -18,21 +19,30 @@ public class ReliableSqlCommand : DbCommand, ICloneable
/// <summary>
/// The underlying <see cref="SqlCommand"/> being proxied.
/// </summary>
public SqlCommand Current { get; private set; }
private SqlCommand Current { get; set; }

/// <summary>
/// The <see cref="ReliableSqlConnection"/> that has been assigned to the command via the Connection property.
/// The <see cref="ReliableSqlDbConnection"/> wrapper that has been assigned to the command via the Connection property or the ctor.
/// </summary>
public ReliableSqlConnection ReliableConnection { get; set; }
private ReliableSqlDbConnection 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)
{
Debug.Assert(commandToWrap.Connection == null, "Expected Command connection to be uninitialised. This constructor creates a new command with no associated connection.");
Current = commandToWrap;
}

public ReliableSqlCommand(ReliableSqlDbConnection connection, SqlCommand commandToWrap)
{
Current = commandToWrap;
ReliableConnection = connection;
if (connection != null)
Current.Connection = ReliableConnection.ReliableConnection.Current;
}

/// <summary>
/// Explicit type-casting between a <see cref="ReliableSqlCommand"/> and a <see cref="SqlCommand"/>.
/// </summary>
Expand All @@ -48,19 +58,23 @@ public static explicit operator SqlCommand(ReliableSqlCommand command)
/// </summary>
protected override DbConnection DbConnection
{
get { return Current.Connection; }
get
{
return ReliableConnection;
}
set
{
if (value == null)
return;
ReliableConnection = ((ReliableSqlDbConnection)value).ReliableConnection;
Current.Connection = ReliableConnection.Current;

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

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

#region Wrapping code
Expand Down Expand Up @@ -93,9 +107,9 @@ protected override DbParameter CreateDbParameter()

protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
{
return ReliableConnection.CommandRetryPolicy.ExecuteAction(() => {
return ReliableConnection.ReliableConnection.CommandRetryPolicy.ExecuteAction(() => {
if (Connection == null)
Connection = ReliableConnection.Open();
Connection = ReliableConnection.ReliableConnection.Open();
if (Connection.State != ConnectionState.Open)
Connection.Open();
return Current.ExecuteReader(behavior);
Expand All @@ -104,12 +118,12 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)

public override int ExecuteNonQuery()
{
return ReliableConnection.ExecuteCommand(Current);
return ReliableConnection.ReliableConnection.ExecuteCommand(Current);
}

public override object ExecuteScalar()
{
return ReliableConnection.ExecuteCommand<int>(Current);
return ReliableConnection.ReliableConnection.ExecuteCommand<int>(Current);
}

protected override DbTransaction DbTransaction
Expand Down
2 changes: 1 addition & 1 deletion ReliableDbProvider/ReliableSqlDbConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,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

2 comments on commit 49d03fb

@MRCollectiveCI
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity ReliableDbProvider :: 1. Continuous Integration Build 24 is now running

@MRCollectiveCI
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity ReliableDbProvider :: 1. Continuous Integration Build 1.0.0+16 outcome was SUCCESS
Summary: Tests passed: 61 Build time: 0:0:0

Please sign in to comment.