Изменение метода отчистки таблицы.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Pf2eDataScrapper.Importers.Implementations.Aonprd.Compendium;
|
||||
using Pf2eDataScrapper.Importers.Implementations.Aonprd.DataBuilder.Processors;
|
||||
using Pf2eModel;
|
||||
|
||||
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.DataBuilder;
|
||||
|
||||
@@ -11,11 +12,6 @@ namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.DataBuilder;
|
||||
/// <param name="loader"></param>
|
||||
internal class AonprdDataBuilder(AonprdLoader loader)
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс работы с базой данных.
|
||||
/// </summary>
|
||||
public readonly Pf2eDatabase Database = Pf2eDatabase.Instance;
|
||||
|
||||
/// <summary>
|
||||
/// Основной класс для работы с данными Архива Нефиса.
|
||||
/// </summary>
|
||||
@@ -38,7 +34,10 @@ internal class AonprdDataBuilder(AonprdLoader loader)
|
||||
{
|
||||
Console.WriteLine("Truncating all old data...");
|
||||
|
||||
Database.ExecuteCommand("SELECT f_truncate_tables();");
|
||||
using Pf2eDbContext context = new();
|
||||
string cmd = context.Entities.Truncate() + "\n" + context.TruncateAll();
|
||||
|
||||
Console.WriteLine(cmd);
|
||||
|
||||
Console.WriteLine("Truncation completed!");
|
||||
}
|
||||
|
||||
+5
-3
@@ -2,13 +2,12 @@
|
||||
using Konsole;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium;
|
||||
using Pf2eModel;
|
||||
|
||||
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder;
|
||||
|
||||
internal class PF2eFoundryDataBuilder
|
||||
{
|
||||
public Pf2eDatabase Database = Pf2eDatabase.Instance;
|
||||
|
||||
/// <summary>
|
||||
/// Загрузчик данных.
|
||||
/// </summary>
|
||||
@@ -51,7 +50,10 @@ internal class PF2eFoundryDataBuilder
|
||||
{
|
||||
Console.WriteLine("Truncating all old data...");
|
||||
|
||||
Database.ExecuteCommand("SELECT f_truncate_tables();");
|
||||
using Pf2eDbContext context = new();
|
||||
string cmd = context.Entities.Truncate() + "\n" + context.TruncateAll();
|
||||
|
||||
Console.WriteLine(cmd);
|
||||
|
||||
Console.WriteLine("Truncation completed!");
|
||||
}
|
||||
|
||||
@@ -1,152 +0,0 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Npgsql;
|
||||
|
||||
namespace Pf2eDataScrapper;
|
||||
|
||||
/// <summary>
|
||||
/// Main class for database connection and operations.
|
||||
/// </summary>
|
||||
public partial class Pf2eDatabase
|
||||
{
|
||||
/// <summary>
|
||||
/// Подключение к базе данных.
|
||||
/// </summary>
|
||||
public readonly NpgsqlConnection Connection;
|
||||
|
||||
/// <summary>
|
||||
/// Имя пользователя для подключения к базе данных.
|
||||
/// </summary>
|
||||
public readonly string Username;
|
||||
|
||||
/// <summary>
|
||||
/// Название базы данных.
|
||||
/// </summary>
|
||||
public readonly string DatabaseName;
|
||||
|
||||
private Pf2eDatabase()
|
||||
{
|
||||
Username = UsernamePattern().Match(EnvSettings.DatabaseConnection).Groups[1].Value;
|
||||
DatabaseName = DatabaseNamePattern().Match(EnvSettings.DatabaseConnection).Groups[1].Value;
|
||||
|
||||
Connection = new(EnvSettings.DatabaseConnection);
|
||||
Connection.Open();
|
||||
}
|
||||
|
||||
~Pf2eDatabase()
|
||||
{
|
||||
Connection.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Singleton instance of the database connection manager.
|
||||
/// </summary>
|
||||
public static Pf2eDatabase Instance { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Current transaction associated with this database connection.
|
||||
/// </summary>
|
||||
public NpgsqlTransaction? Transaction { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Begin a new transaction on the database connection.
|
||||
/// </summary>
|
||||
public void StartTransaction()
|
||||
{
|
||||
if (Transaction != null)
|
||||
{
|
||||
throw new InvalidOperationException("Transaction already started.");
|
||||
}
|
||||
|
||||
Transaction = Connection.BeginTransaction();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Commit the current transaction to the database.
|
||||
/// </summary>
|
||||
public void Commit()
|
||||
{
|
||||
if (Transaction == null)
|
||||
{
|
||||
throw new InvalidOperationException("No transaction started.");
|
||||
}
|
||||
|
||||
Transaction.Commit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute a SQL command against the database.
|
||||
/// </summary>
|
||||
/// <param name="sql">The SQL command to execute</param>
|
||||
public void ExecuteCommand(string sql)
|
||||
{
|
||||
NpgsqlCommand command = GetCommand(sql);
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new SQL command for the database connection.
|
||||
/// </summary>
|
||||
/// <param name="sql">The SQL command string</param>
|
||||
/// <returns>A new NpgsqlCommand object</returns>
|
||||
public NpgsqlCommand GetCommand(string sql)
|
||||
{
|
||||
return new(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();
|
||||
}
|
||||
Reference in New Issue
Block a user