Skip to content

Commit 4b91826

Browse files
committed
Expose the SQLite VFS setting
1 parent 90d0799 commit 4b91826

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/Microsoft.Data.Sqlite.Core/SqliteConnectionInternal.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ public SqliteConnectionInternal(SqliteConnectionStringBuilder connectionOptions,
9393
}
9494
}
9595

96-
var rc = sqlite3_open_v2(filename, out _db, flags, vfs: null);
96+
var vfs = !string.IsNullOrWhiteSpace(connectionOptions.Vfs)
97+
? connectionOptions.Vfs
98+
: null;
99+
var rc = sqlite3_open_v2(filename, out _db, flags, vfs: vfs);
97100
SqliteException.ThrowExceptionForRC(rc, _db);
98101

99102
if (connectionOptions.Password.Length != 0)

src/Microsoft.Data.Sqlite.Core/SqliteConnectionStringBuilder.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class SqliteConnectionStringBuilder : DbConnectionStringBuilder
3131
private const string DefaultTimeoutKeyword = "Default Timeout";
3232
private const string CommandTimeoutKeyword = "Command Timeout";
3333
private const string PoolingKeyword = "Pooling";
34+
private const string VfsKeyword = "Vfs";
3435

3536
private enum Keywords
3637
{
@@ -41,7 +42,8 @@ private enum Keywords
4142
ForeignKeys,
4243
RecursiveTriggers,
4344
DefaultTimeout,
44-
Pooling
45+
Pooling,
46+
Vfs,
4547
}
4648

4749
private static readonly IReadOnlyList<string> _validKeywords;
@@ -55,10 +57,11 @@ private enum Keywords
5557
private bool _recursiveTriggers;
5658
private int _defaultTimeout = 30;
5759
private bool _pooling = true;
60+
private string? _vfs = string.Empty;
5861

5962
static SqliteConnectionStringBuilder()
6063
{
61-
var validKeywords = new string[8];
64+
var validKeywords = new string[9];
6265
validKeywords[(int)Keywords.DataSource] = DataSourceKeyword;
6366
validKeywords[(int)Keywords.Mode] = ModeKeyword;
6467
validKeywords[(int)Keywords.Cache] = CacheKeyword;
@@ -67,9 +70,10 @@ static SqliteConnectionStringBuilder()
6770
validKeywords[(int)Keywords.RecursiveTriggers] = RecursiveTriggersKeyword;
6871
validKeywords[(int)Keywords.DefaultTimeout] = DefaultTimeoutKeyword;
6972
validKeywords[(int)Keywords.Pooling] = PoolingKeyword;
73+
validKeywords[(int)Keywords.Vfs] = VfsKeyword;
7074
_validKeywords = validKeywords;
7175

72-
_keywords = new Dictionary<string, Keywords>(11, StringComparer.OrdinalIgnoreCase)
76+
_keywords = new Dictionary<string, Keywords>(12, StringComparer.OrdinalIgnoreCase)
7377
{
7478
[DataSourceKeyword] = Keywords.DataSource,
7579
[ModeKeyword] = Keywords.Mode,
@@ -79,6 +83,7 @@ static SqliteConnectionStringBuilder()
7983
[RecursiveTriggersKeyword] = Keywords.RecursiveTriggers,
8084
[DefaultTimeoutKeyword] = Keywords.DefaultTimeout,
8185
[PoolingKeyword] = Keywords.Pooling,
86+
[VfsKeyword] = Keywords.Vfs,
8287

8388
// aliases
8489
[FilenameKeyword] = Keywords.DataSource,
@@ -217,6 +222,17 @@ public bool Pooling
217222
set => base[PoolingKeyword] = _pooling = value;
218223
}
219224

225+
/// <summary>
226+
/// Gets or sets the SQLite VFS used by the connection.
227+
/// </summary>
228+
/// <value>The SQLite VFS used by the connection.</value>
229+
/// <seealso href="https://www.sqlite.org/vfs.html">SQLite VFS</seealso>
230+
public string? Vfs
231+
{
232+
get => _vfs;
233+
set => base[VfsKeyword] = _vfs = value;
234+
}
235+
220236
/// <summary>
221237
/// Gets or sets the value associated with the specified key.
222238
/// </summary>
@@ -269,6 +285,9 @@ public override object? this[string keyword]
269285
case Keywords.Pooling:
270286
Pooling = Convert.ToBoolean(value, CultureInfo.InvariantCulture);
271287
return;
288+
case Keywords.Vfs:
289+
Vfs = Convert.ToString(value, CultureInfo.InvariantCulture);;
290+
return;
272291

273292
default:
274293
Debug.Fail("Unexpected keyword: " + keyword);
@@ -458,6 +477,9 @@ private void Reset(Keywords index)
458477
case Keywords.Pooling:
459478
_pooling = true;
460479
return;
480+
case Keywords.Vfs:
481+
_vfs = string.Empty;
482+
return;
461483

462484
default:
463485
Debug.Fail("Unexpected keyword: " + index);

test/Microsoft.Data.Sqlite.Tests/SqliteConnectionStringBuilderTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public void Keys_works()
148148
var keys = (ICollection<string>)new SqliteConnectionStringBuilder().Keys;
149149

150150
Assert.True(keys.IsReadOnly);
151-
Assert.Equal(8, keys.Count);
151+
Assert.Equal(9, keys.Count);
152152
Assert.Contains("Data Source", keys);
153153
Assert.Contains("Mode", keys);
154154
Assert.Contains("Cache", keys);
@@ -157,6 +157,7 @@ public void Keys_works()
157157
Assert.Contains("Recursive Triggers", keys);
158158
Assert.Contains("Default Timeout", keys);
159159
Assert.Contains("Pooling", keys);
160+
Assert.Contains("Vfs", keys);
160161
}
161162

162163
[Fact]
@@ -165,7 +166,7 @@ public void Values_works()
165166
var values = (ICollection<object>)new SqliteConnectionStringBuilder().Values;
166167

167168
Assert.True(values.IsReadOnly);
168-
Assert.Equal(8, values.Count);
169+
Assert.Equal(9, values.Count);
169170
}
170171

171172
[Fact]

0 commit comments

Comments
 (0)