Create DbDriver and DI the driver + the python interface

This commit is contained in:
2026-02-16 12:06:40 -08:00
parent e26fd7a3fc
commit 63a38297c7
3 changed files with 96 additions and 13 deletions
+80
View File
@@ -0,0 +1,80 @@
using Microsoft.Data.Sqlite;
namespace DataBase {
public class DbDriver : IDisposable {
static SqliteConnection? singletonConnector = null;
public DbDriver() {
// Load in the datastore if not already loaded
if (singletonConnector == null) {
singletonConnector = new SqliteConnection("Data Source=test.db");
singletonConnector.Open();
// Create the key value store if not exist
using var command = singletonConnector.CreateCommand();
command.CommandText = """
CREATE TABLE IF NOT EXISTS KeyValuePair (
key TEXT NOT NULL,
value TEXT,
PRIMARY KEY(key)
);
""";
command.ExecuteReader();
}
}
// Return Values from keys
public string Get(string Key) {
if (singletonConnector != null) {
using var command = singletonConnector.CreateCommand();
command.CommandText = """
SELECT value
FROM KeyValuePair
Where key = $key;
""";
command.Parameters.AddWithValue("$key", Key);
command.ExecuteReader();
using var reader = command.ExecuteReader();
if (reader.Read()) {
return reader.GetString(0);
}
}
return "";
}
// Set a key value pair in the store
public void Set(string Key, string Value) {
if (singletonConnector != null) {
using var command = singletonConnector.CreateCommand();
command.CommandText = """
INSERT INTO KeyValuePair
(key,value)
VALUES
($key, $value)
ON CONFLICT(key)
DO UPDATE
SET value = excluded.value;
""";
command.Parameters.AddWithValue("$key", Key);
command.Parameters.AddWithValue("$value", Value);
command.ExecuteReader();
}
}
// Deconstructor
~DbDriver() {
Dispose();
}
// Deconstructor
public void Dispose() {
if (singletonConnector != null) {
singletonConnector.Close();
singletonConnector = null;
}
}
}
}
+15 -13
View File
@@ -1,11 +1,24 @@
using WebServer.Components; using WebServer.Components;
using PythonInterop; using PythonInterop;
using DataBase;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Load the module in globally and use correct path for local or docker runners
#if DEBUG
AIModule interopModule = new AIModule(PythonPathBase: "/usr/", PythonVersion: "python3.11");
#else
AIModule interopModule = new AIModule();
#endif
// Add services to the container. // Add services to the container.
builder.Services.AddRazorComponents() builder.Services.AddRazorComponents().AddInteractiveServerComponents();
.AddInteractiveServerComponents();
// Insert the DB driver for Dependency Injection
builder.Services.AddSingleton<DbDriver>();
// Insert the python modlue for Dependency Injection
builder.Services.AddSingleton(interopModule);
var app = builder.Build(); var app = builder.Build();
@@ -16,17 +29,6 @@ if (!app.Environment.IsDevelopment()) {
app.UseHsts(); app.UseHsts();
} }
// Load the module in globally and use correct path for local or docker runners
#if DEBUG
AIModule interopModule = new AIModule(PythonPathBase: "/usr/", PythonVersion: "python3.11");
#else
AIModule interopModule = new AIModule();
#endif
// Run this for testing purposes
interopModule.TestAI();
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseAntiforgery(); app.UseAntiforgery();
+1
View File
@@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.3" />
<PackageReference Include="pythonnet" Version="3.0.5" /> <PackageReference Include="pythonnet" Version="3.0.5" />
</ItemGroup> </ItemGroup>