Skip to content

Commit 702bf6f

Browse files
committed
Check and set multithread on first instance
1 parent f579e11 commit 702bf6f

3 files changed

Lines changed: 26 additions & 16 deletions

File tree

src/SQLite.Net/Interop/ConfigOption.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace SQLite.Net2
2525
{
2626
public enum ConfigOption
2727
{
28+
Unknown = 0,
2829
SingleThread = 1,
2930
MultiThread = 2,
3031
Serialized = 3

src/SQLite.Net/Interop/SQLiteApi.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,17 @@ public Result Close(IDbHandle db)
5050

5151
public Result Initialize()
5252
{
53-
throw new NotImplementedException("");
54-
//return (Result)raw.sqlite3_initialize();
53+
return (Result)raw.sqlite3_initialize();
5554
}
5655

5756
public Result Shutdown()
5857
{
59-
throw new NotImplementedException("");
60-
//return (Result)raw.sqlite3_shutdown();
58+
return (Result)raw.sqlite3_shutdown();
6159
}
6260

6361
public Result Config(ConfigOption option)
6462
{
65-
throw new NotImplementedException("");
66-
//return (Result)raw.sqlite3_config(option);
63+
return (Result)raw.sqlite3_config((int)option);
6764
}
6865

6966
public Result BusyTimeout(IDbHandle db, int milliseconds)
@@ -277,11 +274,11 @@ public int BackupPagecount(IDbBackupHandle handle)
277274
return raw.sqlite3_backup_pagecount(internalBackup.DbBackupPtr);
278275
}
279276

280-
public int Sleep(int millis)
281-
{
282-
throw new NotImplementedException("Sleep is not implemented");
283-
//return raw.sqlite3_sleep(millis);
284-
}
277+
// public int Sleep(int millis)
278+
// {
279+
// throw new NotImplementedException("Sleep is not implemented");
280+
// //return raw.sqlite3_sleep(millis);
281+
// }
285282

286283
private struct DbBackupHandle : IDbBackupHandle
287284
{
@@ -292,10 +289,8 @@ public DbBackupHandle(sqlite3_backup dbBackupPtr) : this()
292289
DbBackupPtr = dbBackupPtr;
293290
}
294291

295-
public bool Equals(IDbBackupHandle other)
296-
{
297-
return other is DbBackupHandle && DbBackupPtr == ((DbBackupHandle)other).DbBackupPtr;
298-
}
292+
public bool Equals(IDbBackupHandle other)
293+
=> other is DbBackupHandle handle && DbBackupPtr == handle.DbBackupPtr;
299294
}
300295

301296
#endregion

src/SQLite.Net/SQLiteConnection.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ static SQLiteConnection()
9191
public virtual SQLiteConnection Clone()
9292
=> new (DatabasePath, databaseOpenFlags, StoreDateTimeAsTicks, Serializer, _tableMappings, ExtraTypeMappings, Resolver, encryptionKey);
9393

94+
private static ConfigOption firstConfigOption;
95+
9496
/// <summary>
9597
/// Constructs a new SQLiteConnection and opens a SQLite database specified by databasePath.
9698
/// </summary>
@@ -110,13 +112,25 @@ public virtual SQLiteConnection Clone()
110112
/// <param name="extraTypeMappings">Any extra type mappings that you wish to use for overriding the default for creating column definitions for SQLite DDL in the class Orm (snake in Swedish).</param>
111113
/// <param name="resolver">A contract resovler for resolving interfaces to concreate types during object creation</param>
112114
/// <param name="encryptionKey">When using SQL CIPHER, automatically sets the key (you won't need to override Clone() in this case)</param>
115+
/// <param name="configOption">Mode in which to open the db</param>
113116
public SQLiteConnection(string databasePath, SQLiteOpenFlags openFlags = SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create, bool storeDateTimeAsTicks = true, IBlobSerializer? serializer = null, IDictionary<string, TableMapping>? tableMappings = null,
114-
IDictionary<Type, string>? extraTypeMappings = null, IContractResolver? resolver = null, string? encryptionKey = null)
117+
IDictionary<Type, string>? extraTypeMappings = null, IContractResolver? resolver = null, string? encryptionKey = null, ConfigOption configOption = ConfigOption.MultiThread)
115118
{
116119
if (string.IsNullOrEmpty(databasePath))
117120
throw new ArgumentException("Must be specified", nameof(databasePath));
118121
DatabasePath = databasePath;
119122

123+
if (firstConfigOption == ConfigOption.Unknown)
124+
{
125+
firstConfigOption = configOption;
126+
127+
if (configOption > ConfigOption.SingleThread && sqlite.Threadsafe() == 0)
128+
throw new ArgumentException("SQlite is not compiled with multithread and config option is set to multithread", nameof(configOption));
129+
130+
sqlite.Config(configOption);
131+
sqlite.Initialize();
132+
}
133+
120134
var r = sqlite.Open(DatabasePath, out var handle, (int) openFlags, null);
121135
if (r != Result.OK)
122136
throw new SQLiteException(r, $"Could not open database file: {DatabasePath} ({r})");

0 commit comments

Comments
 (0)