Skip to content

Commit 751f556

Browse files
authored
Made IDatabaseClient implement the IDisposable interface (#30)
1 parent a6435b1 commit 751f556

10 files changed

Lines changed: 48 additions & 11 deletions

File tree

Demo/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Libsql.Client;
22

33
// Create a database client using the static factory method
4-
var dbClient = await DatabaseClient.Create(opts => {
4+
using var dbClient = await DatabaseClient.Create(opts => {
55
opts.Url = ":memory:";
66
});
77

Libsql.Client.Tests/EmbeddedReplicaTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Libsql.Client.Tests;
44

5-
public class EmbeddedReplicaTests : IClassFixture<DatabaseContainer>
5+
public class EmbeddedReplicaTests : IClassFixture<DatabaseContainer>, IDisposable
66
{
77
public IDatabaseClient DatabaseClient { get; }
88

@@ -38,4 +38,10 @@ public async Task CanCallSync()
3838
{
3939
await DatabaseClient.Sync();
4040
}
41+
42+
public void Dispose()
43+
{
44+
DatabaseClient.Dispose();
45+
GC.SuppressFinalize(this);
46+
}
4147
}

Libsql.Client.Tests/PositionalArgumentTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Libsql.Client.Tests;
22

3-
public class PositionalArgumentTests
3+
public class PositionalArgumentTests : IDisposable
44
{
55
private readonly IDatabaseClient _db = DatabaseClient.Create().Result;
66

@@ -86,4 +86,10 @@ public async Task BindBlobParameter()
8686

8787
Assert.Equal(new byte[] { 1, 2, 3 }, blob.Value);
8888
}
89+
90+
public void Dispose()
91+
{
92+
_db.Dispose();
93+
GC.SuppressFinalize(this);
94+
}
8995
}

Libsql.Client.Tests/RemoteTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Libsql.Client.Tests;
44

5-
public class RemoteTests : IClassFixture<DatabaseContainer>
5+
public class RemoteTests : IClassFixture<DatabaseContainer>, IDisposable
66
{
77
public IDatabaseClient DatabaseClient { get; }
88

@@ -28,4 +28,10 @@ public async Task CanConnectAndQueryRemoteDatabase()
2828
var value = Assert.IsType<Integer>(count);
2929
Assert.Equal(3503, value.Value);
3030
}
31+
32+
public void Dispose()
33+
{
34+
DatabaseClient.Dispose();
35+
GC.SuppressFinalize(this);
36+
}
3137
}

Libsql.Client.Tests/ResultSetTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Libsql.Client.Tests;
22

3-
public class ResultSetTests
3+
public class ResultSetTests : IDisposable
44
{
55
private readonly IDatabaseClient _db = DatabaseClient.Create().Result;
66

@@ -90,4 +90,10 @@ public async Task Changes_ReturnsExectedValue_WhenMultipleUpdates()
9090

9191
Assert.Equal(10ul, rs2.RowsAffected);
9292
}
93+
94+
public void Dispose()
95+
{
96+
_db.Dispose();
97+
GC.SuppressFinalize(this);
98+
}
9399
}

Libsql.Client.Tests/RowsTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Libsql.Client.Tests;
22

3-
public class RowsTests
3+
public class RowsTests : IDisposable
44
{
55
private readonly IDatabaseClient _db = DatabaseClient.Create().Result;
66

@@ -38,4 +38,10 @@ public async Task Rows_CanPartiallyIterateTwice()
3838
var expected = new []{new Integer(1), new Integer(2), new Integer(3)};
3939
Assert.Equal(expected, secondArray.Select(x => x.First() as Integer).ToArray());
4040
}
41+
42+
public void Dispose()
43+
{
44+
_db.Dispose();
45+
GC.SuppressFinalize(this);
46+
}
4147
}

Libsql.Client.Tests/SelectTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Libsql.Client.Tests;
22

3-
public class SelectTests
3+
public class SelectTests : IDisposable
44
{
55
private readonly IDatabaseClient _db = DatabaseClient.Create().Result;
66

@@ -118,4 +118,10 @@ public async Task SelectNullType()
118118
var value = row.First();
119119
Assert.IsType<Null>(value);
120120
}
121+
122+
public void Dispose()
123+
{
124+
_db.Dispose();
125+
GC.SuppressFinalize(this);
126+
}
121127
}

Libsql.Client/DatabaseWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Libsql.Client
88
{
9-
internal class DatabaseWrapper : IDatabaseClient, IDisposable
9+
internal class DatabaseWrapper : IDatabaseClient
1010
{
1111
private libsql_database_t _db;
1212
private libsql_connection_t _connection;

Libsql.Client/IDatabaseClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
using System;
12
using System.Threading.Tasks;
23

34
namespace Libsql.Client
45
{
56
/// <summary>
67
/// Interface for a Libsql database client.
78
/// </summary>
8-
public interface IDatabaseClient
9+
public interface IDatabaseClient : IDisposable
910
{
1011
/// <summary>
1112
/// Executes the given SQL query and returns the result set.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ For an example, see the Demo project in the repository.
3131

3232
```csharp
3333
// Create an in-memory database.
34-
var dbClient = DatabaseClient.Create(opts => {
34+
var dbClient = await DatabaseClient.Create(opts => {
3535
opts.Url = ":memory:";
3636
});
3737
```
@@ -80,7 +80,7 @@ dbClient.Dispose();
8080
or with a `using` statement:
8181

8282
```csharp
83-
using (var dbClient = DatabaseClient.Create(opts => {
83+
using (var dbClient = await DatabaseClient.Create(opts => {
8484
opts.Url = ":memory:";
8585
}))
8686
{

0 commit comments

Comments
 (0)