From 10b9d60a3f1d67c10e377831dd50846a8dc1decb Mon Sep 17 00:00:00 2001 From: rampaa Date: Thu, 19 Dec 2024 20:50:46 +0300 Subject: [PATCH] Fix profile stats not working correctly for the default profile --- JL.Core/Config/ConfigDBManager.cs | 32 ++++++++++++++----- JL.Core/Config/ProfileDBUtils.cs | 51 ++++++++++++++++++++++++++----- JL.Core/Config/ProfileUtils.cs | 2 ++ JL.Core/Config/StatsDBUtils.cs | 8 ++--- 4 files changed, 74 insertions(+), 19 deletions(-) diff --git a/JL.Core/Config/ConfigDBManager.cs b/JL.Core/Config/ConfigDBManager.cs index d1a83c17..7347bef2 100644 --- a/JL.Core/Config/ConfigDBManager.cs +++ b/JL.Core/Config/ConfigDBManager.cs @@ -27,17 +27,18 @@ UPDATE setting public static void CreateDB() { - if (File.Exists(s_configsPath)) + bool dbExists = File.Exists(s_configsPath); + if (dbExists) { if (File.Exists($"{s_configsPath}-journal")) { RestoreDatabase(); } - - return; } - - _ = Directory.CreateDirectory(Utils.ConfigPath); + else + { + _ = Directory.CreateDirectory(Utils.ConfigPath); + } using SqliteConnection connection = DBUtils.CreateDBConnection(s_configsPath); using SqliteCommand command = connection.CreateCommand(); @@ -68,8 +69,25 @@ FOREIGN KEY (profile_id) REFERENCES profile (id) ON DELETE CASCADE """; _ = command.ExecuteNonQuery(); - ProfileDBUtils.InsertDefaultProfile(connection); - StatsDBUtils.InsertStats(connection, Stats.LifetimeStats, ProfileUtils.DefaultProfileId); + bool globalProfileExists = ProfileDBUtils.ProfileExists(ProfileUtils.GlobalProfileId); + bool defaultProfileExists = ProfileDBUtils.ProfileExists(ProfileUtils.DefaultProfileId); + + if (!globalProfileExists) + { + if (defaultProfileExists) + { + ProfileUtils.CurrentProfileId = ProfileDBUtils.GetCurrentProfileIdFromDB(connection, ProfileUtils.DefaultProfileId); + Stats.LifetimeStats = StatsDBUtils.GetStatsFromDB(connection, ProfileUtils.DefaultProfileId)!; + } + + ProfileDBUtils.InsertGlobalProfile(connection); + StatsDBUtils.InsertStats(connection, Stats.LifetimeStats, ProfileUtils.GlobalProfileId); + } + + if (!defaultProfileExists) + { + ProfileDBUtils.InsertDefaultProfile(connection); + } } private static void RestoreDatabase() diff --git a/JL.Core/Config/ProfileDBUtils.cs b/JL.Core/Config/ProfileDBUtils.cs index cb0207ff..6cc868d6 100644 --- a/JL.Core/Config/ProfileDBUtils.cs +++ b/JL.Core/Config/ProfileDBUtils.cs @@ -19,6 +19,22 @@ INSERT INTO profile (name) _ = command.ExecuteNonQuery(); } + internal static void InsertGlobalProfile(SqliteConnection connection) + { + using SqliteCommand command = connection.CreateCommand(); + command.CommandText = + """ + INSERT INTO profile (id, name) + VALUES (@id, @name); + """; + + _ = command.Parameters.AddWithValue("@id", ProfileUtils.GlobalProfileId); + _ = command.Parameters.AddWithValue("@name", ProfileUtils.GlobalProfileName); + _ = command.ExecuteNonQuery(); + + ConfigDBManager.InsertSetting(connection, nameof(ProfileUtils.CurrentProfileId), ProfileUtils.CurrentProfileId.ToString(CultureInfo.InvariantCulture), ProfileUtils.GlobalProfileId); + } + internal static void InsertDefaultProfile(SqliteConnection connection) { using SqliteCommand command = connection.CreateCommand(); @@ -31,20 +47,20 @@ INSERT INTO profile (id, name) _ = command.Parameters.AddWithValue("@id", ProfileUtils.DefaultProfileId); _ = command.Parameters.AddWithValue("@name", ProfileUtils.DefaultProfileName); _ = command.ExecuteNonQuery(); - - ConfigDBManager.InsertSetting(connection, nameof(ProfileUtils.CurrentProfileId), ProfileUtils.DefaultProfileId.ToString(CultureInfo.InvariantCulture), ProfileUtils.DefaultProfileId); } - private static int GetCurrentProfileIdFromDB(SqliteConnection connection) + internal static int GetCurrentProfileIdFromDB(SqliteConnection connection, int profileId) { using SqliteCommand command = connection.CreateCommand(); +#pragma warning disable CA2100 // Review SQL queries for security vulnerabilities command.CommandText = $""" SELECT value FROM setting - WHERE profile_id = 1 AND name = '{nameof(ProfileUtils.CurrentProfileId)}'; + WHERE profile_id = {profileId} AND name = '{nameof(ProfileUtils.CurrentProfileId)}'; """; +#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities return Convert.ToInt32(command.ExecuteScalar()!, CultureInfo.InvariantCulture); } @@ -79,7 +95,8 @@ public static List GetProfileNames(SqliteConnection connection) command.CommandText = """ SELECT id, name - FROM profile; + FROM profile + WHERE id != 0; """; using SqliteDataReader dataReader = command.ExecuteReader(); @@ -91,6 +108,25 @@ public static List GetProfileNames(SqliteConnection connection) return profiles; } + internal static bool ProfileExists(int profileId) + { + using SqliteConnection connection = ConfigDBManager.CreateReadOnlyDBConnection(); + using SqliteCommand command = connection.CreateCommand(); + + command.CommandText = + """ + SELECT EXISTS + ( + SELECT 1 + FROM profile + WHERE id = @profileId + ); + """; + + _ = command.Parameters.AddWithValue("@profileId", profileId); + return Convert.ToBoolean(command.ExecuteScalar()!, CultureInfo.InvariantCulture); + } + public static bool ProfileExists(string profileName) { using SqliteConnection connection = ConfigDBManager.CreateReadOnlyDBConnection(); @@ -107,7 +143,6 @@ FROM profile """; _ = command.Parameters.AddWithValue("@name", profileName); - return Convert.ToBoolean(command.ExecuteScalar()!, CultureInfo.InvariantCulture); } @@ -143,12 +178,12 @@ DELETE FROM profile public static void UpdateCurrentProfile(SqliteConnection connection) { - ConfigDBManager.UpdateSetting(connection, nameof(ProfileUtils.CurrentProfileId), ProfileUtils.CurrentProfileId.ToString(CultureInfo.InvariantCulture), ProfileUtils.DefaultProfileId); + ConfigDBManager.UpdateSetting(connection, nameof(ProfileUtils.CurrentProfileId), ProfileUtils.CurrentProfileId.ToString(CultureInfo.InvariantCulture), ProfileUtils.GlobalProfileId); } public static void SetCurrentProfileFromDB(SqliteConnection connection) { - ProfileUtils.CurrentProfileId = GetCurrentProfileIdFromDB(connection); + ProfileUtils.CurrentProfileId = GetCurrentProfileIdFromDB(connection, ProfileUtils.GlobalProfileId); ProfileUtils.CurrentProfileName = GetProfileName(connection, ProfileUtils.CurrentProfileId); } } diff --git a/JL.Core/Config/ProfileUtils.cs b/JL.Core/Config/ProfileUtils.cs index 5cafed2a..bdbb1c00 100644 --- a/JL.Core/Config/ProfileUtils.cs +++ b/JL.Core/Config/ProfileUtils.cs @@ -4,6 +4,8 @@ namespace JL.Core.Config; public static class ProfileUtils { + internal const int GlobalProfileId = 0; + internal const string GlobalProfileName = "JLGlobal"; internal const int DefaultProfileId = 1; public const string DefaultProfileName = "Default"; public static readonly string ProfileFolderPath = Path.Join(Utils.ApplicationPath, "Profiles"); diff --git a/JL.Core/Config/StatsDBUtils.cs b/JL.Core/Config/StatsDBUtils.cs index 677ec3c7..3d0a7758 100644 --- a/JL.Core/Config/StatsDBUtils.cs +++ b/JL.Core/Config/StatsDBUtils.cs @@ -12,7 +12,7 @@ public static void InsertStats(SqliteConnection connection, Stats stats, int pro InsertStats(connection, JsonSerializer.Serialize(stats, Utils.s_jsoNotIgnoringNullWithEnumConverterAndIndentation), profileId); } - public static void InsertStats(SqliteConnection connection, string stats, int profileId) + private static void InsertStats(SqliteConnection connection, string stats, int profileId) { using SqliteCommand command = connection.CreateCommand(); command.CommandText = @@ -31,7 +31,7 @@ private static void UpdateStats(SqliteConnection connection, Stats stats, int pr UpdateStats(connection, JsonSerializer.Serialize(stats, Utils.s_jsoNotIgnoringNullWithEnumConverterAndIndentation), profileId); } - public static void UpdateStats(SqliteConnection connection, string stats, int profileId) + private static void UpdateStats(SqliteConnection connection, string stats, int profileId) { using SqliteCommand command = connection.CreateCommand(); command.CommandText = @@ -73,7 +73,7 @@ public static void UpdateLifetimeStats() public static void UpdateLifetimeStats(SqliteConnection connection) { - UpdateStats(connection, Stats.LifetimeStats, ProfileUtils.DefaultProfileId); + UpdateStats(connection, Stats.LifetimeStats, ProfileUtils.GlobalProfileId); } public static void UpdateProfileLifetimeStats() @@ -89,7 +89,7 @@ public static void UpdateProfileLifetimeStats(SqliteConnection connection) public static void SetStatsFromDB(SqliteConnection connection) { - Stats.LifetimeStats = GetStatsFromDB(connection, ProfileUtils.DefaultProfileId)!; + Stats.LifetimeStats = GetStatsFromDB(connection, ProfileUtils.GlobalProfileId)!; Stats.ProfileLifetimeStats = GetStatsFromDB(connection, ProfileUtils.CurrentProfileId)!; } }