diff --git a/TShockAPI/Configuration/TShockConfig.cs b/TShockAPI/Configuration/TShockConfig.cs index c91466fe..0a9e1a1a 100644 --- a/TShockAPI/Configuration/TShockConfig.cs +++ b/TShockAPI/Configuration/TShockConfig.cs @@ -529,7 +529,7 @@ namespace TShockAPI.Configuration #region MySQL Settings /// The type of database to use when storing data (either "sqlite" or "mysql"). - [Description("The type of database to use when storing data (either \"sqlite\" or \"mysql\").")] + [Description("The type of database to use when storing data (either \"sqlite\", \"mysql\" or \"postgres\").")] public string StorageType = "sqlite"; /// The path of sqlite db. @@ -552,6 +552,22 @@ namespace TShockAPI.Configuration [Description("The password used when connecting to a MySQL database.")] public string MySqlPassword = ""; + ///The Postgres hostname and port to direct connections to. + [Description("The Postgres hostname and port to direct connections to.")] + public string PostgresHost = ""; + + /// The database name to connect to when using Postgres as the database type. + [Description("The database name to connect to when using Postgres as the database type.")] + public string PostgresDbName = ""; + + /// The username used when connecting to a Postgres database. + [Description("The username used when connecting to a Postgres database.")] + public string PostgresUsername = ""; + + /// The password used when connecting to a Postgres database. + [Description("The password used when connecting to a Postgres database.")] + public string PostgresPassword = ""; + /// Whether or not to save logs to the SQL database instead of a text file. [Description("Whether or not to save logs to the SQL database instead of a text file.\nDefault = false.")] public bool UseSqlLogs = false; diff --git a/TShockAPI/DB/DbBuilder.cs b/TShockAPI/DB/DbBuilder.cs index d02f993b..dc6e20df 100644 --- a/TShockAPI/DB/DbBuilder.cs +++ b/TShockAPI/DB/DbBuilder.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.IO; using Microsoft.Data.Sqlite; using MySql.Data.MySqlClient; +using Npgsql; using TerrariaApi.Server; using TShockAPI.Configuration; @@ -45,6 +46,7 @@ public sealed class DbBuilder { "sqlite" => BuildSqliteConnection(), "mysql" => BuildMySqlConnection(), + "postgres" => BuildPostgresConnection(), _ => throw new("Invalid storage type") }; } @@ -86,4 +88,28 @@ public sealed class DbBuilder throw new("MySql not setup correctly", e); } } + + private NpgsqlConnection BuildPostgresConnection() + { + try + { + string[] hostport = _config.Settings.PostgresHost.Split(':'); + + NpgsqlConnectionStringBuilder connStrBuilder = new() + { + Host = hostport[0], + Port = hostport.Length > 1 ? int.Parse(hostport[1]) : 5432, + Database = _config.Settings.PostgresDbName, + Username = _config.Settings.PostgresUsername, + Password = _config.Settings.PostgresPassword + }; + + return new(connStrBuilder.ToString()); + } + catch (NpgsqlException e) + { + ServerApi.LogWriter.PluginWriteLine(_caller, e.ToString(), TraceLevel.Error); + throw new("Postgres not setup correctly", e); + } + } } diff --git a/TShockLauncher/TShockLauncher.csproj b/TShockLauncher/TShockLauncher.csproj index fbe428bb..3c04ae52 100644 --- a/TShockLauncher/TShockLauncher.csproj +++ b/TShockLauncher/TShockLauncher.csproj @@ -33,7 +33,8 @@ - + +