Implement user login and registration; update database connection management to be per use not a singleton by adjusting service lifetimes in DI.
This commit is contained in:
@@ -2,52 +2,67 @@ using Microsoft.Data.Sqlite;
|
||||
|
||||
namespace DataBase {
|
||||
|
||||
public class DbDriver : IDisposable {
|
||||
public class DbDriver {
|
||||
|
||||
static SqliteConnection? singletonConnector = null;
|
||||
static bool Initilized = false;
|
||||
|
||||
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();
|
||||
if (!Initilized) {
|
||||
// Open a connection with auto dispose when done
|
||||
using (SqliteConnection sqlite = new SqliteConnection("Data Source=test.db")) {
|
||||
// Open the connection
|
||||
sqlite.Open();
|
||||
// Create the key value store if not exist
|
||||
using var command = sqlite.CreateCommand();
|
||||
command.CommandText = """
|
||||
CREATE TABLE IF NOT EXISTS KeyValuePair (
|
||||
key TEXT NOT NULL,
|
||||
value TEXT,
|
||||
PRIMARY KEY(key)
|
||||
);
|
||||
""";
|
||||
// Run the command
|
||||
command.ExecuteReader();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return Values from keys
|
||||
public string Get(string Key) {
|
||||
if (singletonConnector != null) {
|
||||
using var command = singletonConnector.CreateCommand();
|
||||
// Open a connection with auto dispose when done
|
||||
using (SqliteConnection sqlite = new SqliteConnection("Data Source=test.db")) {
|
||||
// Open the connection
|
||||
sqlite.Open();
|
||||
// Create the key value store if not exist
|
||||
using var command = sqlite.CreateCommand();
|
||||
command.CommandText = """
|
||||
SELECT value
|
||||
FROM KeyValuePair
|
||||
Where key = $key;
|
||||
""";
|
||||
// Add the parameter to prevent sql injection
|
||||
command.Parameters.AddWithValue("$key", Key);
|
||||
command.ExecuteReader();
|
||||
// Run the command
|
||||
using var reader = command.ExecuteReader();
|
||||
// Read off the result if exists
|
||||
if (reader.Read()) {
|
||||
// Return the results
|
||||
return reader.GetString(0);
|
||||
}
|
||||
// Return something if nothing existed
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// Set a key value pair in the store
|
||||
public void Set(string Key, string Value) {
|
||||
if (singletonConnector != null) {
|
||||
using var command = singletonConnector.CreateCommand();
|
||||
// Open a connection with auto dispose when done
|
||||
using (SqliteConnection sqlite = new SqliteConnection("Data Source=test.db")) {
|
||||
// Open the connection
|
||||
sqlite.Open();
|
||||
// Create the key value store if not exist
|
||||
using var command = sqlite.CreateCommand();
|
||||
command.CommandText = """
|
||||
INSERT INTO KeyValuePair
|
||||
(key,value)
|
||||
@@ -57,24 +72,14 @@ namespace DataBase {
|
||||
DO UPDATE
|
||||
SET value = excluded.value;
|
||||
""";
|
||||
// Add the parameters to prevent sql injection
|
||||
command.Parameters.AddWithValue("$key", Key);
|
||||
command.Parameters.AddWithValue("$value", Value);
|
||||
// Process the command
|
||||
command.ExecuteReader();
|
||||
}
|
||||
}
|
||||
|
||||
// Deconstructor
|
||||
~DbDriver() {
|
||||
Dispose();
|
||||
}
|
||||
|
||||
// Deconstructor
|
||||
public void Dispose() {
|
||||
if (singletonConnector != null) {
|
||||
singletonConnector.Close();
|
||||
singletonConnector = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user