Skip to content

Commit

Permalink
Merge pull request #221 from ylorph/state_change_event_handlers
Browse files Browse the repository at this point in the history
Added support for  DbConnection.StateChange event handler
  • Loading branch information
Giorgi authored Oct 1, 2024
2 parents 4f88227 + 9512a3d commit 9c65872
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions DuckDB.NET.Data/DuckDBConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public partial class DuckDBConnection : DbConnection
private DuckDBConnectionString? parsedConnection;
private ConnectionReference? connectionReference;
private bool inMemoryDuplication = false;

private static readonly StateChangeEventArgs FromClosedToOpenEventArgs = new(ConnectionState.Closed, ConnectionState.Open);
private static readonly StateChangeEventArgs FromOpenToClosedEventArgs = new(ConnectionState.Open, ConnectionState.Closed);

#region Protected Properties

Expand Down Expand Up @@ -107,6 +110,7 @@ public override void Open()
: connectionManager.GetConnectionReference(ParsedConnection);

connectionState = ConnectionState.Open;
OnStateChange(FromClosedToOpenEventArgs);
}

protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
Expand Down Expand Up @@ -185,6 +189,7 @@ protected override void Dispose(bool disposing)
connectionManager.ReturnConnectionReference(connectionReference);
}
connectionState = ConnectionState.Closed;
OnStateChange(FromOpenToClosedEventArgs);
}
}

Expand Down
42 changes: 42 additions & 0 deletions DuckDB.NET.Test/DuckDBConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,46 @@ public void ConnectionStringSetThreadsAndAccessModeOption(string accessMode, int
value.Should().Be(threads);
}
}

[Fact]
public void ConnectionStateHandlerIsCalledOnOpen()
{
using var dbInfo = DisposableFile.GenerateInTemp("db");
using var connection = new DuckDBConnection(dbInfo.ConnectionString);
var handlerCalled = false;
connection.StateChange += Assert;
connection.Open();
connection.StateChange -= Assert; // otherwise the dispose/close will trigger the assert

handlerCalled.Should().BeTrue();
return;

void Assert(object sender, StateChangeEventArgs args)
{
args.OriginalState.Should().Be(ConnectionState.Closed);
args.CurrentState.Should().Be(ConnectionState.Open);
handlerCalled = true;
}
}

[Fact]
public async Task ConnectionStateHandlerIsCalledOnClose()
{
using var dbInfo = DisposableFile.GenerateInTemp("db");
await using var connection = new DuckDBConnection(dbInfo.ConnectionString);
var handlerCalled = false;
await connection.OpenAsync();
connection.StateChange += Assert;
await connection.CloseAsync();

handlerCalled.Should().BeTrue();
return;

void Assert(object sender, StateChangeEventArgs args)
{
args.OriginalState.Should().Be(ConnectionState.Open);
args.CurrentState.Should().Be(ConnectionState.Closed);
handlerCalled = true;
}
}
}

0 comments on commit 9c65872

Please sign in to comment.