Skip to content

Commit

Permalink
Fix profile stats not working correctly for the default profile
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Dec 19, 2024
1 parent dd23c16 commit 10b9d60
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 19 deletions.
32 changes: 25 additions & 7 deletions JL.Core/Config/ConfigDBManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand Down
51 changes: 43 additions & 8 deletions JL.Core/Config/ProfileDBUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down Expand Up @@ -79,7 +95,8 @@ public static List<string> GetProfileNames(SqliteConnection connection)
command.CommandText =
"""
SELECT id, name
FROM profile;
FROM profile
WHERE id != 0;
""";

using SqliteDataReader dataReader = command.ExecuteReader();
Expand All @@ -91,6 +108,25 @@ public static List<string> 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();
Expand All @@ -107,7 +143,6 @@ FROM profile
""";

_ = command.Parameters.AddWithValue("@name", profileName);

return Convert.ToBoolean(command.ExecuteScalar()!, CultureInfo.InvariantCulture);
}

Expand Down Expand Up @@ -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);
}
}
2 changes: 2 additions & 0 deletions JL.Core/Config/ProfileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
8 changes: 4 additions & 4 deletions JL.Core/Config/StatsDBUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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 =
Expand Down Expand Up @@ -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()
Expand All @@ -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)!;
}
}

0 comments on commit 10b9d60

Please sign in to comment.