Permalink
Browse files

Adding support for commands with an existing/default/inherited transa…

…ction.
  • Loading branch information...
1 parent c7dcf4f commit 63062d96aeeb009e15ee176c73e8b34b1eb75e12 @brannon committed Apr 18, 2012
Showing with 122 additions and 7 deletions.
  1. +2 −1 Dapper/SqlMapper.cs
  2. +120 −6 Tests/Tests.cs
View
@@ -1372,7 +1372,8 @@ private static IDbCommand SetupCommand(IDbConnection cnn, IDbTransaction transac
var cmd = cnn.CreateCommand();
var bindByName = GetBindByName(cmd.GetType());
if (bindByName != null) bindByName(cmd, true);
- cmd.Transaction = transaction;
+ if (transaction != null)
+ cmd.Transaction = transaction;
cmd.CommandText = sql;
if (commandTimeout.HasValue)
cmd.CommandTimeout = commandTimeout.Value;
View
@@ -769,12 +769,6 @@ public void TestEnumWeirdness()
connection.Query<TestEnumClass>("select null as [EnumEnum]").First().EnumEnum.IsEqualTo(null);
connection.Query<TestEnumClass>("select cast(1 as tinyint) as [EnumEnum]").First().EnumEnum.IsEqualTo(TestEnum.Bla);
}
- void Foo()
- {
- string s = "Bla";
- var obj = new TestEnumClassNoNull();
- obj.EnumEnum = (TestEnum)Enum.Parse(typeof(TestEnum), s, true);
- }
public void TestEnumStrings()
{
connection.Query<TestEnumClassNoNull>("select 'BLA' as [EnumEnum]").First().EnumEnum.IsEqualTo(TestEnum.Bla);
@@ -1393,6 +1387,126 @@ public enum ShortEnum : short
Zero = 0, One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6
}
+ public void TestTransactionCommit()
+ {
+ try
+ {
+ connection.Execute("create table #TransactionTest ([ID] int, [Value] varchar(32));");
+
+ using (var transaction = connection.BeginTransaction())
+ {
+ connection.Execute("insert into #TransactionTest ([ID], [Value]) values (1, 'ABC');", transaction: transaction);
+
+ transaction.Commit();
+ }
+
+ connection.Query<int>("select count(*) from #TransactionTest;").Single().IsEqualTo(1);
+ }
+ finally
+ {
+ connection.Execute("drop table #TransactionTest;");
+ }
+ }
+
+ public void TestTransactionRollback()
+ {
+ connection.Execute("create table #TransactionTest ([ID] int, [Value] varchar(32));");
+
+ try
+ {
+ using (var transaction = connection.BeginTransaction())
+ {
+ connection.Execute("insert into #TransactionTest ([ID], [Value]) values (1, 'ABC');", transaction: transaction);
+
+ transaction.Rollback();
+ }
+
+ connection.Query<int>("select count(*) from #TransactionTest;").Single().IsEqualTo(0);
+ }
+ finally
+ {
+ connection.Execute("drop table #TransactionTest;");
+ }
+ }
+
+ public void TestCommandWithInheritedTransaction()
+ {
+ connection.Execute("create table #TransactionTest ([ID] int, [Value] varchar(32));");
+
+ try
+ {
+ using (var transaction = connection.BeginTransaction())
+ {
+ var transactedConnection = new TransactedConnection(connection, transaction);
+
+ transactedConnection.Execute("insert into #TransactionTest ([ID], [Value]) values (1, 'ABC');");
+
+ transaction.Rollback();
+ }
+
+ connection.Query<int>("select count(*) from #TransactionTest;").Single().IsEqualTo(0);
+ }
+ finally
+ {
+ connection.Execute("drop table #TransactionTest;");
+ }
+ }
+
+ class TransactedConnection : IDbConnection
+ {
+ IDbConnection _conn;
+ IDbTransaction _tran;
+
+ public TransactedConnection(IDbConnection conn, IDbTransaction tran)
+ {
+ _conn = conn;
+ _tran = tran;
+ }
+
+ public string ConnectionString { get { return _conn.ConnectionString; } set { _conn.ConnectionString = value; } }
+ public int ConnectionTimeout { get { return _conn.ConnectionTimeout; } }
+ public string Database { get { return _conn.Database; } }
+ public ConnectionState State { get { return _conn.State; } }
+
+ public IDbTransaction BeginTransaction(IsolationLevel il)
+ {
+ throw new NotImplementedException();
+ }
+
+ public IDbTransaction BeginTransaction()
+ {
+ return _tran;
+ }
+
+ public void ChangeDatabase(string databaseName)
+ {
+ _conn.ChangeDatabase(databaseName);
+ }
+
+ public void Close()
+ {
+ _conn.Close();
+ }
+
+ public IDbCommand CreateCommand()
+ {
+ // The command inherits the "current" transaction.
+ var command = _conn.CreateCommand();
+ command.Transaction = _tran;
+ return command;
+ }
+
+ public void Dispose()
+ {
+ _conn.Dispose();
+ }
+
+ public void Open()
+ {
+ _conn.Open();
+ }
+ }
+
#if POSTGRESQL
class Cat

0 comments on commit 63062d9

Please sign in to comment.