124 lines
3.9 KiB
C#
124 lines
3.9 KiB
C#
using Npgsql;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Pf2eDataScrapper
|
|
{
|
|
public partial class Pf2eDatabase
|
|
{
|
|
public NpgsqlConnection Connection;
|
|
|
|
public readonly string Username;
|
|
public readonly string DatabaseName;
|
|
|
|
|
|
private Pf2eDatabase()
|
|
{
|
|
Username = UsernamePattern().Match(EnvSettings.DatabaseConnection).Groups[1].Value;
|
|
DatabaseName = DatabaseNamePattern().Match(EnvSettings.DatabaseConnection).Groups[1].Value;
|
|
|
|
Connection = new NpgsqlConnection(EnvSettings.DatabaseConnection);
|
|
Connection.Open();
|
|
}
|
|
|
|
~Pf2eDatabase()
|
|
{
|
|
Connection.Close();
|
|
}
|
|
|
|
public static Pf2eDatabase Instance { get; } = new();
|
|
|
|
public NpgsqlTransaction? Transaction { get; private set; }
|
|
|
|
public void StartTransaction()
|
|
{
|
|
if (Transaction != null)
|
|
{
|
|
throw new InvalidOperationException("Transaction already started.");
|
|
}
|
|
|
|
Transaction = Connection.BeginTransaction();
|
|
}
|
|
|
|
public void Commit()
|
|
{
|
|
if (Transaction == null)
|
|
{
|
|
throw new InvalidOperationException("No transaction started.");
|
|
}
|
|
|
|
Transaction.Commit();
|
|
}
|
|
|
|
public void ExecuteCommand(string sql)
|
|
{
|
|
var command = GetCommand(sql);
|
|
|
|
command.ExecuteNonQuery();
|
|
}
|
|
|
|
public NpgsqlCommand GetCommand(string sql)
|
|
{
|
|
return new NpgsqlCommand(sql, Connection);
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
public void RebuildDatabase()
|
|
{
|
|
// Закрываем текущее соединение
|
|
Connection.Close();
|
|
|
|
// Получение имён
|
|
var databaseNamePattern = DatabaseNamePattern();
|
|
var userConnection = databaseNamePattern.Replace(EnvSettings.DatabaseConnection, ";");
|
|
var databaseName = databaseNamePattern.Match(EnvSettings.DatabaseConnection).Groups[1].Value;
|
|
var username = UserNamePattern().Match(EnvSettings.DatabaseConnection).Groups[1].Value;
|
|
|
|
// Временное подключение к базе данных
|
|
NpgsqlConnection tmpConnection = new(userConnection);
|
|
tmpConnection.Open();
|
|
|
|
// Удаляем базу данных
|
|
NpgsqlCommand dropCommand = new($"DROP DATABASE \"{databaseName}\";", tmpConnection);
|
|
dropCommand.ExecuteNonQuery();
|
|
|
|
// Создаём новую базу данных
|
|
NpgsqlCommand createCommand = new($@"
|
|
CREATE DATABASE {databaseName}
|
|
WITH OWNER = {username}
|
|
ENCODING = 'UTF8'
|
|
CONNECTION LIMIT = -1;
|
|
", tmpConnection);
|
|
createCommand.ExecuteNonQuery();
|
|
|
|
string[] permissionCommands = [
|
|
$"GRANT TEMPORARY, CONNECT ON DATABASE {databaseName} TO PUBLIC;",
|
|
$"GRANT ALL ON DATABASE {databaseName} TO {username} WITH GRANT OPTION;",
|
|
$"GRANT ALL ON DATABASE {databaseName} TO postgres;"
|
|
];
|
|
|
|
foreach (var command in permissionCommands)
|
|
{
|
|
NpgsqlCommand permissionCommand = new(command, tmpConnection);
|
|
permissionCommand.ExecuteNonQuery();
|
|
}
|
|
|
|
// Закрываем временное соединение
|
|
tmpConnection.Close();
|
|
|
|
// Заново создаём соединение
|
|
Connection = new NpgsqlConnection(EnvSettings.DatabaseConnection);
|
|
}
|
|
|
|
*/
|
|
|
|
[GeneratedRegex(@"(?:^|;)Database=(.*?)(?:$|;)")]
|
|
private static partial Regex DatabaseNamePattern();
|
|
|
|
[GeneratedRegex(@"(?:^|;)User=(.*?)(?:$|;)")]
|
|
private static partial Regex UsernamePattern();
|
|
}
|
|
}
|