diff --git a/Dotnet/Program.cs b/Dotnet/Program.cs index be2988c2..99e2a69e 100644 --- a/Dotnet/Program.cs +++ b/Dotnet/Program.cs @@ -7,12 +7,12 @@ using NLog; using NLog.Targets; using System; -using System.Data.SQLite; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text.Json; using System.Threading; using System.Windows.Forms; +using Microsoft.Data.Sqlite; namespace VRCX { @@ -172,7 +172,7 @@ namespace VRCX #region Handle Database Error - catch (SQLiteException e) + catch (SqliteException e) { logger.Fatal(e, "Unhandled SQLite Exception, closing."); var messageBoxResult = MessageBox.Show( diff --git a/Dotnet/SQLite.cs b/Dotnet/SQLite.cs index 32a69747..dfba1f2e 100644 --- a/Dotnet/SQLite.cs +++ b/Dotnet/SQLite.cs @@ -1,18 +1,17 @@ +#nullable enable using System; using System.Collections.Generic; -using System.Data.SQLite; -using System.IO; -using System.Text.Json.Nodes; using System.Threading; using System.Text.Json; +using Microsoft.Data.Sqlite; namespace VRCX { public class SQLite { public static SQLite Instance; - private readonly ReaderWriterLockSlim m_ConnectionLock; - private SQLiteConnection m_Connection; + private readonly ReaderWriterLockSlim _connectionLock; + private SqliteConnection _connection; static SQLite() { @@ -21,7 +20,7 @@ namespace VRCX public SQLite() { - m_ConnectionLock = new ReaderWriterLockSlim(); + _connectionLock = new ReaderWriterLockSlim(); } public void Init() @@ -34,18 +33,24 @@ namespace VRCX if (!string.IsNullOrEmpty(jsonDataSource)) dataSource = jsonDataSource; - m_Connection = new SQLiteConnection($"Data Source=\"{dataSource}\";Version=3;PRAGMA locking_mode=NORMAL;PRAGMA busy_timeout=5000;PRAGMA journal_mode=WAL;PRAGMA optimize=0x10002;", true); - - m_Connection.Open(); + _connection = new SqliteConnection($"Data Source=\"{dataSource}\";Mode=ReadWriteCreate;Cache=Shared;"); + _connection.Open(); + using var command = _connection.CreateCommand(); + command.CommandText = @"PRAGMA locking_mode=NORMAL; + PRAGMA busy_timeout=5000; + PRAGMA journal_mode=WAL; + PRAGMA optimize=0x10002;"; + command.ExecuteNonQuery(); } public void Exit() { - m_Connection.Close(); - m_Connection.Dispose(); + _connection.Close(); + _connection.Dispose(); } - public string ExecuteJson(string sql, IDictionary args = null) + // for Electron + public string ExecuteJson(string sql, IDictionary? args = null) { var result = Execute(sql, args); if (result.Item1 != null) @@ -63,17 +68,17 @@ namespace VRCX }); } - public Tuple Execute(string sql, IDictionary args = null) + public Tuple Execute(string sql, IDictionary? args = null) { - m_ConnectionLock.EnterReadLock(); + _connectionLock.EnterReadLock(); try { - using var command = new SQLiteCommand(sql, m_Connection); + using var command = new SqliteCommand(sql, _connection); if (args != null) { foreach (var arg in args) { - command.Parameters.Add(new SQLiteParameter(arg.Key, arg.Value)); + command.Parameters.Add(new SqliteParameter(arg.Key, arg.Value)); } } @@ -88,37 +93,37 @@ namespace VRCX } result.Add(values); } - return new Tuple(null, result.ToArray()); + return new Tuple(null, result.ToArray()); } catch (Exception ex) { - return new Tuple(ex.Message, null); + return new Tuple(ex.Message, null); } finally { - m_ConnectionLock.ExitReadLock(); + _connectionLock.ExitReadLock(); } } - public int ExecuteNonQuery(string sql, IDictionary args = null) + public int ExecuteNonQuery(string sql, IDictionary? args = null) { int result = -1; - m_ConnectionLock.EnterWriteLock(); + _connectionLock.EnterWriteLock(); try { - using var command = new SQLiteCommand(sql, m_Connection); + using var command = new SqliteCommand(sql, _connection); if (args != null) { foreach (var arg in args) { - command.Parameters.Add(new SQLiteParameter(arg.Key, arg.Value)); + command.Parameters.Add(new SqliteParameter(arg.Key, arg.Value)); } } result = command.ExecuteNonQuery(); } finally { - m_ConnectionLock.ExitWriteLock(); + _connectionLock.ExitWriteLock(); } return result; diff --git a/Dotnet/ScreenshotMetadata/ScreenshotMetadataDatabase.cs b/Dotnet/ScreenshotMetadata/ScreenshotMetadataDatabase.cs index af76c5e0..8e991807 100644 --- a/Dotnet/ScreenshotMetadata/ScreenshotMetadataDatabase.cs +++ b/Dotnet/ScreenshotMetadata/ScreenshotMetadataDatabase.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; -using System.Data.SQLite; +using Microsoft.Data.Sqlite; namespace VRCX { @@ -16,20 +16,27 @@ namespace VRCX // Couldn't be me... oh wait internal class ScreenshotMetadataDatabase { - private readonly SQLiteConnection _sqlite; + private readonly SqliteConnection _sqlite; public ScreenshotMetadataDatabase(string databaseLocation) { - _sqlite = new SQLiteConnection($"Data Source=\"{databaseLocation}\";Version=3;PRAGMA locking_mode=NORMAL;PRAGMA busy_timeout=5000;PRAGMA journal_mode=WAL;PRAGMA optimize=0x10002;", true); + _sqlite = new SqliteConnection($"Data Source=\"{databaseLocation}\";Mode=ReadWriteCreate;Cache=Shared;"); _sqlite.Open(); - using var cmd = new SQLiteCommand(_sqlite); - cmd.CommandText = @"CREATE TABLE IF NOT EXISTS cache ( + using var command = _sqlite.CreateCommand(); + command.CommandText = @"PRAGMA locking_mode=NORMAL; + PRAGMA busy_timeout=5000; + PRAGMA journal_mode=WAL; + PRAGMA optimize=0x10002;"; + command.ExecuteNonQuery(); + + var createCommand = _sqlite.CreateCommand(); + createCommand.CommandText = @"CREATE TABLE IF NOT EXISTS cache ( id INTEGER PRIMARY KEY AUTOINCREMENT, file_path TEXT NOT NULL UNIQUE, metadata TEXT, cached_at INTEGER NOT NULL );"; - cmd.ExecuteNonQuery(); + createCommand.ExecuteNonQuery(); } public void AddMetadataCache(string filePath, string metadata) @@ -46,7 +53,7 @@ namespace VRCX CachedAt = DateTimeOffset.Now }; const string sql = "INSERT OR REPLACE INTO cache (file_path, metadata, cached_at) VALUES (@FilePath, @Metadata, @CachedAt);"; - using var command = new SQLiteCommand(sql, _sqlite); + using var command = new SqliteCommand(sql, _sqlite); command.Parameters.AddWithValue("@FilePath", cache.FilePath); command.Parameters.AddWithValue("@Metadata", cache.Metadata); command.Parameters.AddWithValue("@CachedAt", cache.CachedAt.Ticks); @@ -56,12 +63,12 @@ namespace VRCX public void BulkAddMetadataCache(IEnumerable cache) { using var transaction = _sqlite.BeginTransaction(); - using var command = new SQLiteCommand(_sqlite); + using var command = _sqlite.CreateCommand(); const string sql = "INSERT OR REPLACE INTO cache (file_path, metadata, cached_at) VALUES (@FilePath, @Metadata, @CachedAt);"; command.CommandText = sql; - var filePathParam = command.Parameters.Add("@FilePath", System.Data.DbType.String); - var metadataParam = command.Parameters.Add("@Metadata", System.Data.DbType.String); - var cachedAtParam = command.Parameters.Add("@CachedAt", System.Data.DbType.Int64); + var filePathParam = command.Parameters.Add("@FilePath", SqliteType.Text); + var metadataParam = command.Parameters.Add("@Metadata", SqliteType.Text); + var cachedAtParam = command.Parameters.Add("@CachedAt", SqliteType.Integer); foreach (var item in cache) { var isFileCached = IsFileCached(item.FilePath); @@ -79,7 +86,8 @@ namespace VRCX public int IsFileCached(string filePath) { const string sql = "SELECT id FROM cache WHERE file_path = @FilePath;"; - using var command = new SQLiteCommand(sql, _sqlite); + using var command = _sqlite.CreateCommand(); + command.CommandText = sql; command.Parameters.AddWithValue("@FilePath", filePath); using var reader = command.ExecuteReader(); var result = new List(); @@ -97,7 +105,8 @@ namespace VRCX public string? GetMetadata(string filePath) { const string sql = "SELECT id, file_path, metadata, cached_at FROM cache WHERE file_path = @FilePath;"; - using var command = new SQLiteCommand(sql, _sqlite); + using var command = _sqlite.CreateCommand(); + command.CommandText = sql; command.Parameters.AddWithValue("@FilePath", filePath); using var reader = command.ExecuteReader(); var result = new List(); @@ -121,7 +130,8 @@ namespace VRCX public string? GetMetadataById(int id) { const string sql = "SELECT id, file_path, metadata, cached_at FROM cache WHERE id = @Id;"; - using var command = new SQLiteCommand(sql, _sqlite); + using var command = _sqlite.CreateCommand(); + command.CommandText = sql; command.Parameters.AddWithValue("@Id", id); using var reader = command.ExecuteReader(); var result = new List(); diff --git a/Dotnet/VRCX-Cef.csproj b/Dotnet/VRCX-Cef.csproj index b4ffaaf8..9fa22b89 100644 --- a/Dotnet/VRCX-Cef.csproj +++ b/Dotnet/VRCX-Cef.csproj @@ -91,6 +91,7 @@ + @@ -98,8 +99,6 @@ - - diff --git a/Dotnet/VRCX-Electron.csproj b/Dotnet/VRCX-Electron.csproj index f37cc34b..d63c021e 100644 --- a/Dotnet/VRCX-Electron.csproj +++ b/Dotnet/VRCX-Electron.csproj @@ -87,6 +87,7 @@ + @@ -94,8 +95,6 @@ - -