feat: Add connectrion string handling to database connection builders

Allows override of default database connection parameters by providing explicit connection strings for SQLite, MySQL, and PostgreSQL, improving configuration flexibility and setup robustness. Includes refactoring path handling and directory creation for SQLite files.
This commit is contained in:
Sakura Akeno Isayeki 2025-05-23 14:39:34 +02:00
parent f2e88cbedc
commit 2fa07303f7
No known key found for this signature in database
GPG key ID: BAB781B71FD2E7E6

View file

@ -1,4 +1,5 @@
using System.Data; using System;
using System.Data;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
@ -53,22 +54,39 @@ public sealed class DbBuilder
private SqliteConnection BuildSqliteConnection() private SqliteConnection BuildSqliteConnection()
{ {
string dbFilePath = Path.Combine(_savePath, _config.Settings.SqliteDBPath); try
if (Path.GetDirectoryName(dbFilePath) is not { } dbDirPath)
{ {
throw new DirectoryNotFoundException($"The SQLite database path '{dbFilePath}' could not be found."); // Handle first the connection string, if specified.
if (_config.Settings.SqliteConnectionString is not (null or ""))
{
// Use factory to build the string, the path may be relative.
SqliteConnectionStringBuilder builder = new(_config.Settings.SqliteConnectionString);
builder.DataSource = GetDbFile(builder.DataSource).FullName;
return new(builder.ConnectionString);
}
// Fallback to SqliteDBPath setting.
string dbFilePath = GetDbFile(_config.Settings.SqliteDBPath).FullName;
return new($"Data Source={dbFilePath};");
}
catch (SqliteException e)
{
ServerApi.LogWriter.PluginWriteLine(_caller, e.ToString(), TraceLevel.Error);
throw new("Sqlite not setup correctly", e);
} }
Directory.CreateDirectory(dbDirPath);
return new($"Data Source={dbFilePath}");
} }
private MySqlConnection BuildMySqlConnection() private MySqlConnection BuildMySqlConnection()
{ {
try try
{ {
// If specified, use the connection string instead of other parameters.
if (_config.Settings.MySqlConnectionString is not (null or ""))
{
MySqlConnectionStringBuilder builder = new(_config.Settings.MySqlConnectionString);
return new(builder.ToString());
}
string[] hostport = _config.Settings.MySqlHost.Split(':'); string[] hostport = _config.Settings.MySqlHost.Split(':');
MySqlConnectionStringBuilder connStrBuilder = new() MySqlConnectionStringBuilder connStrBuilder = new()
@ -93,6 +111,13 @@ public sealed class DbBuilder
{ {
try try
{ {
// If specified, use the connection string instead of other parameters.
if (_config.Settings.PostgresConnectionString is not (null or ""))
{
NpgsqlConnectionStringBuilder builder = new(_config.Settings.PostgresConnectionString);
return new(builder.ToString());
}
string[] hostport = _config.Settings.PostgresHost.Split(':'); string[] hostport = _config.Settings.PostgresHost.Split(':');
NpgsqlConnectionStringBuilder connStrBuilder = new() NpgsqlConnectionStringBuilder connStrBuilder = new()
@ -112,4 +137,21 @@ public sealed class DbBuilder
throw new("Postgres not setup correctly", e); throw new("Postgres not setup correctly", e);
} }
} }
private FileInfo GetDbFile(string path)
{
FileInfo dbFile = new(Path.IsPathRooted(path) ? path : Path.Combine(_savePath, path));
if (dbFile.Directory is not { } dbDir)
{
throw new DirectoryNotFoundException($"The SQLite database path '{path}' could not be found.");
}
if (!dbDir.Exists)
{
dbDir.Create();
}
return dbFile;
}
} }