Skip to content

Commit 7b1faf0

Browse files
fix [#31]: Add explicit 'file:' URL handling and update documentation (#32)
* fix [#31]: add explicit 'file:' case and update URL docs - Added explicit handling for URLs prefixed with 'file:' in DatabaseWrapper. - Updated DatabaseClientOptions.Url documentation with examples for: - In-memory database - Local file database - Connection string * update README: add remote database examples and clarify connection options
1 parent ddc6119 commit 7b1faf0

4 files changed

Lines changed: 94 additions & 1 deletion

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace Libsql.Client.Tests;
2+
3+
public class DatabaseClientTests
4+
{
5+
[Theory]
6+
[InlineData("file:testdb.sqlite")]
7+
[InlineData(":memory:")]
8+
public async Task Create_WithValidPaths_ShouldCreateDatabaseClient(string path)
9+
{
10+
var client = await DatabaseClient.Create(path);
11+
12+
Assert.NotNull(client);
13+
Assert.IsAssignableFrom<IDatabaseClient>(client);
14+
}
15+
16+
[Fact]
17+
public async Task Create_WithLocalPath_ShouldCreateDatabaseClient()
18+
{
19+
var filePath = Path.GetFullPath("testdb.sqlite");
20+
21+
var client = await DatabaseClient.Create(filePath);
22+
23+
Assert.NotNull(client);
24+
Assert.IsAssignableFrom<IDatabaseClient>(client);
25+
}
26+
27+
[Fact]
28+
public async Task Create_WithNullPath_ShouldThrowArgumentNullException()
29+
{
30+
await Assert.ThrowsAsync<ArgumentNullException>(() => DatabaseClient.Create((string)null!));
31+
}
32+
}

Libsql.Client/DatabaseClientOptions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,21 @@ private DatabaseClientOptions(string url, string authToken = null, string replic
1818
/// <summary>
1919
/// Gets or sets the URL of the database server.
2020
/// </summary>
21-
/// <remarks>Default: <c>""</c>. <c>""</c> or <c>":memory:"</c> will create an in-memory database.</remarks>
21+
/// <remarks>
22+
/// Supported values:
23+
/// <list type="bullet">
24+
/// <item>
25+
/// <description><c>":memory:"</c> or <c>""</c> - Creates an in-memory database.</description>
26+
/// </item>
27+
/// <item>
28+
/// <description>Local file path (e.g. <c>file:localdb.sqlite</c> or <c>C:\data\localdbs.sqlite</c>) - Creates or opens a file-based database.</description>
29+
/// </item>
30+
/// <item>
31+
/// <description>Remote URL (e.g. <c>http://example.com/db</c>) - For remote database support.</description>
32+
/// </item>
33+
/// </list>
34+
/// Default: <c>""</c>.
35+
/// </remarks>
2236
public string Url { get; set; }
2337

2438
/// <summary>

Libsql.Client/DatabaseWrapper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public unsafe DatabaseWrapper(DatabaseClientOptions options)
3737
case "wss":
3838
_type = _options.ReplicaPath != null ? DatabaseType.EmbeddedReplica : DatabaseType.Remote;
3939
break;
40+
case "file":
41+
_type = DatabaseType.File;
42+
break;
4043
default:
4144
throw new InvalidOperationException($"Unsupported scheme: {uri.Scheme}");
4245
}

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,57 @@ For an example, see the Demo project in the repository.
2929

3030
### Creating a Database
3131

32+
You can open a database in different locations:
33+
34+
1. In-Memory Database
3235
```csharp
3336
// Create an in-memory database.
3437
var dbClient = await DatabaseClient.Create(opts => {
3538
opts.Url = ":memory:";
3639
});
3740
```
3841

42+
2. From a File
43+
```csharp
44+
// Creates or opens a database stored in a local file.
45+
var dbClient = await DatabaseClient.Create(opts => {
46+
opts.Url = "file:./mydb.sqlite";
47+
});
48+
```
49+
50+
3. From a Connection String
51+
```csharp
52+
// Opens a database using a full connection string.
53+
var dbClient = await DatabaseClient.Create(opts => {
54+
opts.Url = "https://my-remote-db.example.com";
55+
});
56+
```
57+
58+
#### Provide Additional Connection Options
59+
#### Remote Databases
60+
For remote libSQL databases, use client options for authentication and secure connections:
61+
62+
```csharp
63+
// Connect to a remote database with authentication and HTTPS enabled
64+
var dbClient = await DatabaseClient.Create(opts =>
65+
{
66+
opts.Url = "https://my-remote-db.example.com";
67+
opts.AuthToken = "MY_TOKEN";
68+
opts.UseHttps = true;
69+
});
70+
```
71+
72+
#### Local File Databases
73+
For local files, you can pass standard query parameters via the file URI:
74+
75+
```csharp
76+
// Open a local database with query parameters
77+
var dbClient = await DatabaseClient.Create(opts =>
78+
{
79+
opts.Url = "file:mydb.sqlite?mode=rwc&cache=shared";
80+
});
81+
```
82+
3983
### Executing SQL Statements
4084

4185
Using direct queries

0 commit comments

Comments
 (0)