Skip to content

Commit

Permalink
Delete a DB file if it doesn't have any records
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Jul 26, 2024
1 parent c4cd464 commit 03e9370
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 29 deletions.
1 change: 0 additions & 1 deletion JL.Core/Dicts/DictUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using JL.Core.Network;
using JL.Core.Utilities;
using JL.Core.WordClass;
using Microsoft.Data.Sqlite;

namespace JL.Core.Dicts;

Expand Down
28 changes: 12 additions & 16 deletions JL.Core/Dicts/DictUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -935,9 +939,7 @@ public static async Task LoadDictionaries()
if (File.Exists(dbPath))
{
DBUtils.SendOptimizePragmaToAllDBs();
SqliteConnection.ClearAllPools();
File.Delete(dbPath);
DBUtils.DeleteDB(dbPath);
}
}
}));
Expand Down Expand Up @@ -1022,9 +1024,7 @@ public static async Task LoadDictionaries()
if (File.Exists(dbPath))
{
DBUtils.SendOptimizePragmaToAllDBs();
SqliteConnection.ClearAllPools();
File.Delete(dbPath);
DBUtils.DeleteDB(dbPath);
}
}
}));
Expand Down Expand Up @@ -1205,9 +1205,7 @@ dict.Type is DictType.CustomNameDictionary
if (File.Exists(dbPath))
{
DBUtils.SendOptimizePragmaToAllDBs();
SqliteConnection.ClearAllPools();
File.Delete(dbPath);
DBUtils.DeleteDB(dbPath);
}
}
}));
Expand Down Expand Up @@ -1293,9 +1291,7 @@ dict.Type is DictType.CustomNameDictionary
if (File.Exists(dbPath))
{
DBUtils.SendOptimizePragmaToAllDBs();
SqliteConnection.ClearAllPools();
File.Delete(dbPath);
DBUtils.DeleteDB(dbPath);
}
}
}));
Expand Down
20 changes: 10 additions & 10 deletions JL.Core/Freqs/FreqUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -141,9 +145,7 @@ public static async Task LoadFrequencies()
if (File.Exists(dbPath))
{
DBUtils.SendOptimizePragmaToAllDBs();
SqliteConnection.ClearAllPools();
File.Delete(dbPath);
DBUtils.DeleteDB(dbPath);
}
}
}));
Expand Down Expand Up @@ -241,9 +243,7 @@ public static async Task LoadFrequencies()
if (File.Exists(dbPath))
{
DBUtils.SendOptimizePragmaToAllDBs();
SqliteConnection.ClearAllPools();
File.Delete(dbPath);
DBUtils.DeleteDB(dbPath);
}
}
}));
Expand Down
18 changes: 18 additions & 0 deletions JL.Core/Utilities/DBUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion JL.Windows/GUI/ManageDictionariesWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion JL.Windows/GUI/ManageFrequenciesWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using JL.Core.Freqs;
using JL.Core.Utilities;
using JL.Windows.Utilities;
using Microsoft.Data.Sqlite;

namespace JL.Windows.GUI;

Expand Down

0 comments on commit 03e9370

Please sign in to comment.