Sqlite Database setup and dataset retrieval

This commit is contained in:
Chris Bell 2025-09-09 00:28:44 -05:00
parent 5a5b17bf20
commit 27ec8e3a01
8 changed files with 200 additions and 6 deletions

View File

@ -1,3 +1,3 @@
{
"godotTools.editorPath.godot4": "/home/chris/Godot/Godot_v4.4.1-stable_mono_linux_x86_64/Godot_v4.4.1-stable_mono_linux.x86_64"
"godotTools.editorPath.godot4": "/data/OtherApps/godot/Godot_v4.4.1-stable_mono_linux_x86_64/Godot_v4.4.1-stable_mono_linux.x86_64"
}

View File

@ -1,4 +1,4 @@
[gd_resource type="Resource" script_class="ApplicationSettingsResource" load_steps=8 format=3 uid="uid://dwhgu0ywrt618"]
[gd_resource type="Resource" script_class="ApplicationSettingsResource" load_steps=7 format=3 uid="uid://dwhgu0ywrt618"]
[ext_resource type="Script" uid="uid://ddh7o0nfsuo4k" path="res://src/scripts/custom_resources/ApplicationSettingsResource.cs" id="1_3gmkh"]
[ext_resource type="PackedScene" uid="uid://djj22j6g14exe" path="res://src/scenes/main/MainUI.tscn" id="1_688px"]
@ -6,10 +6,9 @@
[ext_resource type="PackedScene" uid="uid://dkrls6nkk3fk4" path="res://src/scenes/main/content_pages/library_page.tscn" id="3_tfier"]
[ext_resource type="PackedScene" uid="uid://cymkrj587gxm5" path="res://src/scenes/main/content_pages/datasets_page.tscn" id="4_jyu5u"]
[ext_resource type="PackedScene" uid="uid://d2fmlv2jifbvd" path="res://src/scenes/main/content_pages/characters_page.tscn" id="5_biv5l"]
[ext_resource type="PackedScene" uid="uid://cqop2rx4uybvs" path="res://src/scenes/main/content_pages/character_templates_page.tscn" id="6_y30hn"]
[resource]
script = ExtResource("1_3gmkh")
MainUiScene = ExtResource("1_688px")
Pages = Array[PackedScene]([ExtResource("2_tfier"), ExtResource("3_tfier"), ExtResource("4_jyu5u"), ExtResource("5_biv5l"), ExtResource("6_y30hn")])
Pages = Array[PackedScene]([ExtResource("2_tfier"), ExtResource("3_tfier"), ExtResource("4_jyu5u"), ExtResource("5_biv5l")])
metadata/_custom_type_script = "uid://ddh7o0nfsuo4k"

View File

@ -4,6 +4,8 @@
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CogwheelLib" Version="1.3.2" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.0-preview.7.25380.108" />
<PackageReference Include="SessionZero.SzfLib" Version="0.1.0" />
</ItemGroup>
</Project>

View File

@ -13,11 +13,14 @@ config_version=5
config/name="SessionZero"
config/tags=PackedStringArray("c#")
run/main_scene="uid://bv1ceq4dnkl7l"
config/use_custom_user_dir=true
config/features=PackedStringArray("4.4", "C#", "GL Compatibility")
boot_splash/bg_color=Color(0.101961, 0.12549, 0.180392, 1)
boot_splash/fullsize=false
boot_splash/image="uid://qnpvlqg85kx4"
config/icon="res://resources/images/icon.svg"
config/icon="uid://08eaio0bj4c7"
config/macos_native_icon="uid://08eaio0bj4c7"
config/windows_native_icon="res://resources/images/icon.svgs"
[autoload]

View File

@ -1,11 +1,16 @@
using Godot;
using Microsoft.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.IO;
namespace SessionZeroApp;
public partial class AppManager : Node
{
public static AppManager Instance { get; private set; }
public ApplicationSettingsResource AppSettings { get; private set; }
@ -17,10 +22,13 @@ public partial class AppManager : Node
private PackedScene _mainUiScene;
public override void _Ready()
{
Instance = this;
LocalDatabaseHelper.SetupLocalDatabase();
try
{
AppSettings = ResourceLoader.Load<ApplicationSettingsResource>("res://AppSettings.tres");
@ -68,6 +76,8 @@ public partial class AppManager : Node
LoadPage("Home");
}
public void LoadPage(string pageName)
{
if (!Pages.TryGetValue(pageName, out ContentPageBase value))

View File

@ -0,0 +1,177 @@
using System;
using System.Collections.Generic;
using System.IO;
using Godot;
using Microsoft.Data.Sqlite;
using SessionZero.SzfLib.File;
using SessionZero.SzfLib.Helpers;
using SessionZero.SzfLib.Objects;
using SessionZero.SzfLib.Parser;
namespace SessionZeroApp;
public static class LocalDatabaseHelper
{
public const string DATA_PATH = "user://data";
public const string LOCAL_DATABASE_FILE_PATH = "user://data/SessionZerolocal.db";
public static string DataDir = ProjectSettings.GlobalizePath(DATA_PATH);
public static string LocalDatabaseFilePath = ProjectSettings.GlobalizePath(LOCAL_DATABASE_FILE_PATH);
public static List<SzfObject> SzfObjects { get; private set; } = new();
public static void SetupLocalDatabase()
{
if (!Directory.Exists(DataDir))
{
GD.Print($"SZ WARNING: Local Data Directory does not exist, creating it at {DataDir}");
Directory.CreateDirectory(DataDir);
}
if (!File.Exists(LocalDatabaseFilePath))
{
GD.Print($"SZ WARNING: Local Database file does not exist, creating a new one at {LocalDatabaseFilePath}");
try
{
using var dbConnection = new SqliteConnection($"Data Source={LocalDatabaseFilePath}");
dbConnection.Open();
SetupLocalDbTables(dbConnection);
GD.Print($"SZ INFO: Local Database file created at {LocalDatabaseFilePath}");
}
catch (SqliteException e)
{
GD.PrintErr($"SZ ERROR: Could not connect local database; {e}");
return;
}
}
GD.Print($"SZ INFO: Local Database verified at {LocalDatabaseFilePath}");
}
private static void SetupLocalDbTables(SqliteConnection dbConnection)
{
var sql = @"CREATE TABLE szf_data(
name TEXT NOT NULL,
id TEXT NOT NULL,
version FLOAT NOT NULL,
type TEXT NOT NULL,
path TEXT NOT NULL,
PRIMARY KEY (name, id, version)
);";
try
{
using var command = new SqliteCommand(sql, dbConnection);
command.ExecuteNonQuery();
GD.Print($"SZ INFO: Local database tables initialized");
}
catch (SqliteException e)
{
GD.PrintErr($"SZ ERROR: Could not set up local database tables; {e}");
return;
}
}
public static ISzfObject? GetSzfObject(string name, string guid, float version)
{
var sql = @"SELECT path FROM szf_data
WHERE name = @name AND guid = @guid AND version = @version;";
string selectedPath = "";
try
{
using var connection = new SqliteConnection($"Data Source={LocalDatabaseFilePath}");
connection.Open();
using var command = new SqliteCommand(sql, connection);
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@guid", guid);
command.Parameters.AddWithValue("@version", version);
using var reader = command.ExecuteReader();
if (reader.HasRows)
{
while(reader.Read())
{
selectedPath = reader.GetString(0);
}
}
}
catch (SqliteException e)
{
GD.PrintErr(e);
return null;
}
try
{
var content = File.ReadAllText(selectedPath);
SzfParser parser = new();
var szfObject = parser.Parse(content);
return szfObject;
}
catch (Exception e)
{
GD.PrintErr(e);
return null;
}
}
public static List<SzfDataset> GetAllDatasets()
{
var sql = @"SELECT path FROM szf_data
WHERE type = @type";
List<SzfDataset> datasets = new();
try
{
using var connection = new SqliteConnection($"Data Source={LocalDatabaseFilePath}");
connection.Open();
using var command = new SqliteCommand(sql, connection);
command.Parameters.AddWithValue("@type", "dataset");
using var reader = command.ExecuteReader();
if (reader.HasRows)
{
while(reader.Read())
{
var path = reader.GetString(0);
try
{
var content = File.ReadAllText(path);
SzfParser parser = new();
var szfObject = parser.Parse(content) as SzfDataset;
datasets.Add(szfObject);
}
catch (Exception e)
{
GD.PrintErr(e);
}
}
}
}
catch (SqliteException e)
{
GD.PrintErr(e);
}
GD.Print($"Retrieving {datasets.Count} datasets");
return datasets;
}
}

View File

@ -0,0 +1 @@
uid://d2hdpv3n022wx

View File

@ -21,7 +21,9 @@ public partial class DatasetsContentPage : ContentPageBase
{
SetupConnections();
Datasets.AddRange(LocalDatabaseHelper.GetAllDatasets());
GD.Print(Datasets[0].GetMetadataField("Name"));
}
private void SetupConnections()