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
|
|
@ -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