Add individual SQL options

This commit is contained in:
Noi 2020-10-05 15:52:29 -07:00
parent 2f0fe8641a
commit fc9d611ba6
2 changed files with 19 additions and 10 deletions

View file

@ -1,5 +1,6 @@
using BirthdayBot.Data; using BirthdayBot.Data;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Npgsql;
using System; using System;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
@ -48,10 +49,23 @@ namespace BirthdayBot
DBotsToken = null; DBotsToken = null;
} }
var sqlcs = jc["SqlConnectionString"]?.Value<string>(); var sqlhost = jc["SqlHost"]?.Value<string>() ?? "localhost"; // Default to localhost
if (string.IsNullOrWhiteSpace(sqlcs)) var sqluser = jc["SqlUsername"]?.Value<string>();
throw new Exception("'SqlConnectionString' must be specified."); var sqlpass = jc["SqlPassword"]?.Value<string>();
Database.DBConnectionString = sqlcs; if (string.IsNullOrWhiteSpace(sqluser) || string.IsNullOrWhiteSpace(sqlpass))
throw new Exception("'SqlUsername', 'SqlPassword' must be specified.");
var csb = new NpgsqlConnectionStringBuilder()
{
Host = sqlhost,
Username = sqluser,
Password = sqlpass,
// Note: Npgsql connection pooling settings are defined (hardcoded) here.
MinPoolSize = 3,
MaxPoolSize = 20
};
var sqldb = jc["SqlDatabase"]?.Value<string>();
if (sqldb != null) csb.Database = sqldb; // Optional database setting
Database.DBConnectionString = csb.ToString();
int? sc = jc["ShardCount"]?.Value<int>(); int? sc = jc["ShardCount"]?.Value<int>();
if (!sc.HasValue) ShardCount = 1; if (!sc.HasValue) ShardCount = 1;

View file

@ -9,12 +9,7 @@ namespace BirthdayBot.Data
/// </summary> /// </summary>
internal static class Database internal static class Database
{ {
private static string _connString; public static string DBConnectionString { get; set; }
public static string DBConnectionString
{
get => _connString;
set => _connString = "Minimum Pool Size=5;Maximum Pool Size=50;Connection Idle Lifetime=30;" + value;
}
public static async Task<NpgsqlConnection> OpenConnectionAsync() public static async Task<NpgsqlConnection> OpenConnectionAsync()
{ {