feat(db): add Postgres support to configuration
Extends database configuration to support Postgres in addition to existing SQLite and MySQL options. Includes new settings for Postgres host, database name, username, and password. Implements a connection builder for Postgres, ensuring proper error handling when connecting. Updates dependency to include Npgsql for Postgres connectivity.
This commit is contained in:
parent
084411f847
commit
69b98980f1
3 changed files with 45 additions and 2 deletions
|
|
@ -529,7 +529,7 @@ namespace TShockAPI.Configuration
|
|||
#region MySQL Settings
|
||||
|
||||
/// <summary>The type of database to use when storing data (either "sqlite" or "mysql").</summary>
|
||||
[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";
|
||||
|
||||
/// <summary>The path of sqlite db.</summary>
|
||||
|
|
@ -552,6 +552,22 @@ namespace TShockAPI.Configuration
|
|||
[Description("The password used when connecting to a MySQL database.")]
|
||||
public string MySqlPassword = "";
|
||||
|
||||
///<summary>The Postgres hostname and port to direct connections to.</summary>
|
||||
[Description("The Postgres hostname and port to direct connections to.")]
|
||||
public string PostgresHost = "";
|
||||
|
||||
/// <summary>The database name to connect to when using Postgres as the database type.</summary>
|
||||
[Description("The database name to connect to when using Postgres as the database type.")]
|
||||
public string PostgresDbName = "";
|
||||
|
||||
/// <summary>The username used when connecting to a Postgres database.</summary>
|
||||
[Description("The username used when connecting to a Postgres database.")]
|
||||
public string PostgresUsername = "";
|
||||
|
||||
/// <summary>The password used when connecting to a Postgres database.</summary>
|
||||
[Description("The password used when connecting to a Postgres database.")]
|
||||
public string PostgresPassword = "";
|
||||
|
||||
/// <summary>Whether or not to save logs to the SQL database instead of a text file.</summary>
|
||||
[Description("Whether or not to save logs to the SQL database instead of a text file.\nDefault = false.")]
|
||||
public bool UseSqlLogs = false;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue