Skip to content

Commit

Permalink
Made IDatabaseClient implement the IDisposable interface (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sem1712 authored Dec 12, 2024
1 parent a6435b1 commit 751f556
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Libsql.Client;

// Create a database client using the static factory method
var dbClient = await DatabaseClient.Create(opts => {
using var dbClient = await DatabaseClient.Create(opts => {
opts.Url = ":memory:";
});

Expand Down
8 changes: 7 additions & 1 deletion Libsql.Client.Tests/EmbeddedReplicaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Libsql.Client.Tests;

public class EmbeddedReplicaTests : IClassFixture<DatabaseContainer>
public class EmbeddedReplicaTests : IClassFixture<DatabaseContainer>, IDisposable
{
public IDatabaseClient DatabaseClient { get; }

Expand Down Expand Up @@ -38,4 +38,10 @@ public async Task CanCallSync()
{
await DatabaseClient.Sync();
}

public void Dispose()
{
DatabaseClient.Dispose();
GC.SuppressFinalize(this);
}
}
8 changes: 7 additions & 1 deletion Libsql.Client.Tests/PositionalArgumentTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Libsql.Client.Tests;

public class PositionalArgumentTests
public class PositionalArgumentTests : IDisposable
{
private readonly IDatabaseClient _db = DatabaseClient.Create().Result;

Expand Down Expand Up @@ -86,4 +86,10 @@ public async Task BindBlobParameter()

Assert.Equal(new byte[] { 1, 2, 3 }, blob.Value);
}

public void Dispose()
{
_db.Dispose();
GC.SuppressFinalize(this);
}
}
8 changes: 7 additions & 1 deletion Libsql.Client.Tests/RemoteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Libsql.Client.Tests;

public class RemoteTests : IClassFixture<DatabaseContainer>
public class RemoteTests : IClassFixture<DatabaseContainer>, IDisposable
{
public IDatabaseClient DatabaseClient { get; }

Expand All @@ -28,4 +28,10 @@ public async Task CanConnectAndQueryRemoteDatabase()
var value = Assert.IsType<Integer>(count);
Assert.Equal(3503, value.Value);
}

public void Dispose()
{
DatabaseClient.Dispose();
GC.SuppressFinalize(this);
}
}
8 changes: 7 additions & 1 deletion Libsql.Client.Tests/ResultSetTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Libsql.Client.Tests;

public class ResultSetTests
public class ResultSetTests : IDisposable
{
private readonly IDatabaseClient _db = DatabaseClient.Create().Result;

Expand Down Expand Up @@ -90,4 +90,10 @@ public async Task Changes_ReturnsExectedValue_WhenMultipleUpdates()

Assert.Equal(10ul, rs2.RowsAffected);
}

public void Dispose()
{
_db.Dispose();
GC.SuppressFinalize(this);
}
}
8 changes: 7 additions & 1 deletion Libsql.Client.Tests/RowsTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Libsql.Client.Tests;

public class RowsTests
public class RowsTests : IDisposable
{
private readonly IDatabaseClient _db = DatabaseClient.Create().Result;

Expand Down Expand Up @@ -38,4 +38,10 @@ public async Task Rows_CanPartiallyIterateTwice()
var expected = new []{new Integer(1), new Integer(2), new Integer(3)};
Assert.Equal(expected, secondArray.Select(x => x.First() as Integer).ToArray());
}

public void Dispose()
{
_db.Dispose();
GC.SuppressFinalize(this);
}
}
8 changes: 7 additions & 1 deletion Libsql.Client.Tests/SelectTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Libsql.Client.Tests;

public class SelectTests
public class SelectTests : IDisposable
{
private readonly IDatabaseClient _db = DatabaseClient.Create().Result;

Expand Down Expand Up @@ -118,4 +118,10 @@ public async Task SelectNullType()
var value = row.First();
Assert.IsType<Null>(value);
}

public void Dispose()
{
_db.Dispose();
GC.SuppressFinalize(this);
}
}
2 changes: 1 addition & 1 deletion Libsql.Client/DatabaseWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Libsql.Client
{
internal class DatabaseWrapper : IDatabaseClient, IDisposable
internal class DatabaseWrapper : IDatabaseClient
{
private libsql_database_t _db;
private libsql_connection_t _connection;
Expand Down
3 changes: 2 additions & 1 deletion Libsql.Client/IDatabaseClient.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Threading.Tasks;

namespace Libsql.Client
{
/// <summary>
/// Interface for a Libsql database client.
/// </summary>
public interface IDatabaseClient
public interface IDatabaseClient : IDisposable
{
/// <summary>
/// Executes the given SQL query and returns the result set.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ For an example, see the Demo project in the repository.

```csharp
// Create an in-memory database.
var dbClient = DatabaseClient.Create(opts => {
var dbClient = await DatabaseClient.Create(opts => {
opts.Url = ":memory:";
});
```
Expand Down Expand Up @@ -80,7 +80,7 @@ dbClient.Dispose();
or with a `using` statement:

```csharp
using (var dbClient = DatabaseClient.Create(opts => {
using (var dbClient = await DatabaseClient.Create(opts => {
opts.Url = ":memory:";
}))
{
Expand Down

0 comments on commit 751f556

Please sign in to comment.