From 03e93709a07e21f42b373339fc800eafec61a74f Mon Sep 17 00:00:00 2001 From: rampaa Date: Fri, 26 Jul 2024 17:56:52 +0300 Subject: [PATCH] Delete a DB file if it doesn't have any records --- JL.Core/Dicts/DictUpdater.cs | 1 - JL.Core/Dicts/DictUtils.cs | 28 ++++++++----------- JL.Core/Freqs/FreqUtils.cs | 20 ++++++------- JL.Core/Utilities/DBUtils.cs | 18 ++++++++++++ .../GUI/ManageDictionariesWindow.xaml.cs | 1 - .../GUI/ManageFrequenciesWindow.xaml.cs | 1 - 6 files changed, 40 insertions(+), 29 deletions(-) diff --git a/JL.Core/Dicts/DictUpdater.cs b/JL.Core/Dicts/DictUpdater.cs index e0f47998..f509dbe5 100644 --- a/JL.Core/Dicts/DictUpdater.cs +++ b/JL.Core/Dicts/DictUpdater.cs @@ -7,7 +7,6 @@ using JL.Core.Network; using JL.Core.Utilities; using JL.Core.WordClass; -using Microsoft.Data.Sqlite; namespace JL.Core.Dicts; diff --git a/JL.Core/Dicts/DictUtils.cs b/JL.Core/Dicts/DictUtils.cs index 9636d666..0872d33b 100644 --- a/JL.Core/Dicts/DictUtils.cs +++ b/JL.Core/Dicts/DictUtils.cs @@ -566,14 +566,18 @@ public static async Task LoadDictionaries() if (dbJournalExists) { - DBUtils.SendOptimizePragmaToAllDBs(); - SqliteConnection.ClearAllPools(); - File.Delete(dbJournalPath); if (dbExists) { - File.Delete(dbPath); + DBUtils.DeleteDB(dbPath); dbExists = false; } + + File.Delete(dbJournalPath); + } + else if (dbExists && !DBUtils.RecordExists(dbPath)) + { + DBUtils.DeleteDB(dbPath); + dbExists = false; } bool loadFromDB; @@ -935,9 +939,7 @@ public static async Task LoadDictionaries() if (File.Exists(dbPath)) { - DBUtils.SendOptimizePragmaToAllDBs(); - SqliteConnection.ClearAllPools(); - File.Delete(dbPath); + DBUtils.DeleteDB(dbPath); } } })); @@ -1022,9 +1024,7 @@ public static async Task LoadDictionaries() if (File.Exists(dbPath)) { - DBUtils.SendOptimizePragmaToAllDBs(); - SqliteConnection.ClearAllPools(); - File.Delete(dbPath); + DBUtils.DeleteDB(dbPath); } } })); @@ -1205,9 +1205,7 @@ dict.Type is DictType.CustomNameDictionary if (File.Exists(dbPath)) { - DBUtils.SendOptimizePragmaToAllDBs(); - SqliteConnection.ClearAllPools(); - File.Delete(dbPath); + DBUtils.DeleteDB(dbPath); } } })); @@ -1293,9 +1291,7 @@ dict.Type is DictType.CustomNameDictionary if (File.Exists(dbPath)) { - DBUtils.SendOptimizePragmaToAllDBs(); - SqliteConnection.ClearAllPools(); - File.Delete(dbPath); + DBUtils.DeleteDB(dbPath); } } })); diff --git a/JL.Core/Freqs/FreqUtils.cs b/JL.Core/Freqs/FreqUtils.cs index 8407e7c9..6ea32160 100644 --- a/JL.Core/Freqs/FreqUtils.cs +++ b/JL.Core/Freqs/FreqUtils.cs @@ -66,14 +66,18 @@ public static async Task LoadFrequencies() if (dbJournalExists) { - DBUtils.SendOptimizePragmaToAllDBs(); - SqliteConnection.ClearAllPools(); - File.Delete(dbJournalPath); if (dbExists) { - File.Delete(dbPath); + DBUtils.DeleteDB(dbPath); dbExists = false; } + + File.Delete(dbJournalPath); + } + else if (dbExists && !DBUtils.RecordExists(dbPath)) + { + DBUtils.DeleteDB(dbPath); + dbExists = false; } bool loadFromDB; @@ -141,9 +145,7 @@ public static async Task LoadFrequencies() if (File.Exists(dbPath)) { - DBUtils.SendOptimizePragmaToAllDBs(); - SqliteConnection.ClearAllPools(); - File.Delete(dbPath); + DBUtils.DeleteDB(dbPath); } } })); @@ -241,9 +243,7 @@ public static async Task LoadFrequencies() if (File.Exists(dbPath)) { - DBUtils.SendOptimizePragmaToAllDBs(); - SqliteConnection.ClearAllPools(); - File.Delete(dbPath); + DBUtils.DeleteDB(dbPath); } } })); diff --git a/JL.Core/Utilities/DBUtils.cs b/JL.Core/Utilities/DBUtils.cs index 9a6a8fbd..26763b3b 100644 --- a/JL.Core/Utilities/DBUtils.cs +++ b/JL.Core/Utilities/DBUtils.cs @@ -176,6 +176,24 @@ internal static SqliteConnection CreateReadWriteDBConnection(string path) return connection; } + internal static bool RecordExists(string dbPath) + { + using SqliteConnection connection = CreateReadOnlyDBConnection(dbPath); + using SqliteCommand command = connection.CreateCommand(); + + command.CommandText = + """ + SELECT EXISTS + ( + SELECT 1 + FROM record + LIMIT 1 + ); + """; + + return Convert.ToBoolean(command.ExecuteScalar()!, CultureInfo.InvariantCulture); + } + //public static string GetSqliteVersion() //{ // using SqliteConnection connection = new(); diff --git a/JL.Windows/GUI/ManageDictionariesWindow.xaml.cs b/JL.Windows/GUI/ManageDictionariesWindow.xaml.cs index 5a021a3f..cee676fe 100644 --- a/JL.Windows/GUI/ManageDictionariesWindow.xaml.cs +++ b/JL.Windows/GUI/ManageDictionariesWindow.xaml.cs @@ -11,7 +11,6 @@ using JL.Core.Dicts; using JL.Core.Utilities; using JL.Windows.Utilities; -using Microsoft.Data.Sqlite; using Button = System.Windows.Controls.Button; using CheckBox = System.Windows.Controls.CheckBox; using Cursors = System.Windows.Input.Cursors; diff --git a/JL.Windows/GUI/ManageFrequenciesWindow.xaml.cs b/JL.Windows/GUI/ManageFrequenciesWindow.xaml.cs index 92cf7b71..230fb60b 100644 --- a/JL.Windows/GUI/ManageFrequenciesWindow.xaml.cs +++ b/JL.Windows/GUI/ManageFrequenciesWindow.xaml.cs @@ -9,7 +9,6 @@ using JL.Core.Freqs; using JL.Core.Utilities; using JL.Windows.Utilities; -using Microsoft.Data.Sqlite; namespace JL.Windows.GUI;