From f2e88cbedc3cb6985c39958e2d4563c041776c08 Mon Sep 17 00:00:00 2001 From: Sakura Akeno Isayeki Date: Fri, 23 May 2025 13:47:35 +0200 Subject: [PATCH 1/3] feat: Add connection strings parameters for databases Allows specifying complete connection strings for SQLite, MySQL, and Postgres, overriding individual host and credential properties when specified, to provide more flexible database configuration. --- TShockAPI/Configuration/TShockConfig.cs | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/TShockAPI/Configuration/TShockConfig.cs b/TShockAPI/Configuration/TShockConfig.cs index 64fbf77c..63fb4cca 100644 --- a/TShockAPI/Configuration/TShockConfig.cs +++ b/TShockAPI/Configuration/TShockConfig.cs @@ -532,10 +532,31 @@ namespace TShockAPI.Configuration [Description("The type of database to use when storing data (either \"sqlite\", \"mysql\" or \"postgres\").")] public string StorageType = "sqlite"; + /// + /// The connection string to use when connecting to a SQLite database. + /// + /// This property will override the property, if used. + [Description("The connection string to use when connecting to a SQLite database. This property will override the SqliteDBPath property, if used.")] + public string SqliteConnectionString = ""; + /// The path of sqlite db. [Description("The path of sqlite db.")] public string SqliteDBPath = "tshock.sqlite"; + /// + /// The connection string to use when connecting to a MySQL database. + /// + /// + /// This property will override the + /// , + /// , + /// , + /// and properties, if used. + /// + [Description("The connection string to use when connecting to a MySQL database. " + + "This property will override the MySqlHost, MySqlDbName, MySqlUsername and MySqlPassword properties, if used.")] + public string MySqlConnectionString = ""; + /// The MySQL hostname and port to direct connections to. [Description("The MySQL hostname and port to direct connections to.")] public string MySqlHost = "localhost:3306"; @@ -552,6 +573,20 @@ namespace TShockAPI.Configuration [Description("The password used when connecting to a MySQL database.")] public string MySqlPassword = ""; + /// + /// The connection string to use when connecting to a Postgres database. + /// + /// + /// This property will override the + /// , + /// , + /// , + /// and properties, if used. + /// + [Description("The connection string to use when connecting to a Postgres database. " + + "This property will override the PostgresHost, PostgresDbName, PostgresUsername and PostgresPassword properties, if used.")] + public string PostgresConnectionString = ""; + ///The Postgres hostname and port to direct connections to. [Description("The Postgres hostname and port to direct connections to.")] public string PostgresHost = ""; From 2fa07303f723b08f05583d5ec21d8f32479382f3 Mon Sep 17 00:00:00 2001 From: Sakura Akeno Isayeki Date: Fri, 23 May 2025 14:39:34 +0200 Subject: [PATCH 2/3] 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. --- TShockAPI/DB/DbBuilder.cs | 60 +++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/TShockAPI/DB/DbBuilder.cs b/TShockAPI/DB/DbBuilder.cs index dc6e20df..98aedcdf 100644 --- a/TShockAPI/DB/DbBuilder.cs +++ b/TShockAPI/DB/DbBuilder.cs @@ -1,4 +1,5 @@ -using System.Data; +using System; +using System.Data; using System.Diagnostics; using System.IO; using Microsoft.Data.Sqlite; @@ -53,22 +54,39 @@ public sealed class DbBuilder private SqliteConnection BuildSqliteConnection() { - string dbFilePath = Path.Combine(_savePath, _config.Settings.SqliteDBPath); - - if (Path.GetDirectoryName(dbFilePath) is not { } dbDirPath) + try { - 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() { 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(':'); MySqlConnectionStringBuilder connStrBuilder = new() @@ -93,6 +111,13 @@ public sealed class DbBuilder { 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(':'); NpgsqlConnectionStringBuilder connStrBuilder = new() @@ -112,4 +137,21 @@ public sealed class DbBuilder 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; + } } From d0e5c84a794876d0596810a21fd9946a4281b22a Mon Sep 17 00:00:00 2001 From: Sakura Akeno Isayeki Date: Fri, 23 May 2025 15:07:11 +0200 Subject: [PATCH 3/3] docs: Add example links for SQLite, MySQL, & Postgres connection strings Provides links to example connection string formats for SQLite, MySQL, and Postgres to assist users in configuring database connections more effectively. --- TShockAPI/Configuration/TShockConfig.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/TShockAPI/Configuration/TShockConfig.cs b/TShockAPI/Configuration/TShockConfig.cs index 63fb4cca..83dd6e4e 100644 --- a/TShockAPI/Configuration/TShockConfig.cs +++ b/TShockAPI/Configuration/TShockConfig.cs @@ -536,6 +536,7 @@ namespace TShockAPI.Configuration /// The connection string to use when connecting to a SQLite database. /// /// This property will override the property, if used. + /// Example SQLite connection strings (connectionstrings.com) [Description("The connection string to use when connecting to a SQLite database. This property will override the SqliteDBPath property, if used.")] public string SqliteConnectionString = ""; @@ -553,6 +554,9 @@ namespace TShockAPI.Configuration /// , /// and properties, if used. /// + /// + /// Example MySQL connection strings (connectionstrings.com) + /// [Description("The connection string to use when connecting to a MySQL database. " + "This property will override the MySqlHost, MySqlDbName, MySqlUsername and MySqlPassword properties, if used.")] public string MySqlConnectionString = ""; @@ -583,6 +587,7 @@ namespace TShockAPI.Configuration /// , /// and properties, if used. /// + /// Example Npgsql connection strings (connectionstrings.com) [Description("The connection string to use when connecting to a Postgres database. " + "This property will override the PostgresHost, PostgresDbName, PostgresUsername and PostgresPassword properties, if used.")] public string PostgresConnectionString = "";