Change Database to be a static class

This commit is contained in:
Noi 2020-07-16 11:55:26 -07:00
parent 0f05e3f0a8
commit f71552c46d
2 changed files with 13 additions and 24 deletions

View file

@ -52,7 +52,7 @@ namespace BirthdayBot
var sqlcs = jc["SqlConnectionString"]?.Value<string>();
if (string.IsNullOrWhiteSpace(sqlcs))
throw new Exception("'SqlConnectionString' must be specified.");
DatabaseSettings = new Database(sqlcs);
Database.DBConnectionString = sqlcs;
int? sc = jc["ShardCount"]?.Value<int>();
if (!sc.HasValue) ShardCount = 1;

View file

@ -1,42 +1,31 @@
using Npgsql;
using System;
using System.Threading.Tasks;
namespace BirthdayBot.Data
{
/// <summary>
/// Some database abstractions.
/// Database access and some abstractions.
/// </summary>
class Database
internal static class Database
{
/*
* Database storage in this project, explained:
* Each guild gets a row in the settings table. This table is referred to when doing most things.
* Within each guild, each known user gets a row in the users table with specific information specified.
* Users can override certain settings in global, such as time zone.
*/
public static string DBConnectionString { get; set; }
private string DBConnectionString { get; }
public Database(string connString)
{
DBConnectionString = connString;
// Database initialization happens here as well.
SetupTables();
}
public async Task<NpgsqlConnection> OpenConnectionAsync()
public static async Task<NpgsqlConnection> OpenConnectionAsync()
{
if (DBConnectionString == null) throw new Exception("Database connection string not set");
var db = new NpgsqlConnection(DBConnectionString);
await db.OpenAsync();
return db;
}
private void SetupTables()
public static async Task DoInitialDatabaseSetupAsync()
{
using var db = OpenConnectionAsync().GetAwaiter().GetResult();
GuildStateInformation.SetUpDatabaseTable(db); // Note: Call this first. (Foreign reference constraints.)
GuildUserSettings.SetUpDatabaseTable(db);
using var db = await OpenConnectionAsync();
// Refer to the methods being called for information on how the database is set up.
await GuildConfiguration.DatabaseSetupAsync(db); // Note: Call this first. (Foreign reference constraints.)
await GuildUserConfiguration.DatabaseSetupAsync(db);
}
}
}