mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-24 17:34:13 +00:00
Change Database to be a static class
This commit is contained in:
parent
0f05e3f0a8
commit
f71552c46d
2 changed files with 13 additions and 24 deletions
|
@ -52,7 +52,7 @@ namespace BirthdayBot
|
||||||
var sqlcs = jc["SqlConnectionString"]?.Value<string>();
|
var sqlcs = jc["SqlConnectionString"]?.Value<string>();
|
||||||
if (string.IsNullOrWhiteSpace(sqlcs))
|
if (string.IsNullOrWhiteSpace(sqlcs))
|
||||||
throw new Exception("'SqlConnectionString' must be specified.");
|
throw new Exception("'SqlConnectionString' must be specified.");
|
||||||
DatabaseSettings = new Database(sqlcs);
|
Database.DBConnectionString = sqlcs;
|
||||||
|
|
||||||
int? sc = jc["ShardCount"]?.Value<int>();
|
int? sc = jc["ShardCount"]?.Value<int>();
|
||||||
if (!sc.HasValue) ShardCount = 1;
|
if (!sc.HasValue) ShardCount = 1;
|
||||||
|
|
|
@ -1,42 +1,31 @@
|
||||||
using Npgsql;
|
using Npgsql;
|
||||||
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace BirthdayBot.Data
|
namespace BirthdayBot.Data
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Some database abstractions.
|
/// Database access and some abstractions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
class Database
|
internal static class Database
|
||||||
{
|
{
|
||||||
/*
|
public static string DBConnectionString { get; set; }
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private string DBConnectionString { get; }
|
public static async Task<NpgsqlConnection> OpenConnectionAsync()
|
||||||
|
|
||||||
public Database(string connString)
|
|
||||||
{
|
|
||||||
DBConnectionString = connString;
|
|
||||||
|
|
||||||
// Database initialization happens here as well.
|
|
||||||
SetupTables();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<NpgsqlConnection> OpenConnectionAsync()
|
|
||||||
{
|
{
|
||||||
|
if (DBConnectionString == null) throw new Exception("Database connection string not set");
|
||||||
var db = new NpgsqlConnection(DBConnectionString);
|
var db = new NpgsqlConnection(DBConnectionString);
|
||||||
await db.OpenAsync();
|
await db.OpenAsync();
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetupTables()
|
public static async Task DoInitialDatabaseSetupAsync()
|
||||||
{
|
{
|
||||||
using var db = OpenConnectionAsync().GetAwaiter().GetResult();
|
using var db = await OpenConnectionAsync();
|
||||||
GuildStateInformation.SetUpDatabaseTable(db); // Note: Call this first. (Foreign reference constraints.)
|
|
||||||
GuildUserSettings.SetUpDatabaseTable(db);
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue