using System;
using TelegramBotBase.Builder;
using TelegramBotBase.Builder.Interfaces;
namespace TelegramBotBase.Extensions.Serializer.Database.PostgreSql
{
///
/// Provides extension methods for configuring the use of PostgreSQL Server Database for session serialization.
///
public static class BotBaseBuilderExtensions
{
///
/// Uses an PostgreSQL Server Database to save and restore sessions.
///
/// The session serialization stage builder.
/// The connection string to the PostgreSQL database.
/// The fallback form type.
/// The prefix for database table names (default is "tgb_").
/// The language selection stage builder.
public static ILanguageSelectionStage UsePostgreSqlDatabase(
this ISessionSerializationStage builder,
string connectionString, Type fallbackForm = null,
string tablePrefix = "tgb_")
{
var serializer = new PostgreSqlSerializer(connectionString, tablePrefix, fallbackForm);
builder.UseSerialization(serializer);
return builder as BotBaseBuilder;
}
///
/// Uses an PostgreSQL Server Database to save and restore sessions.
///
/// The session serialization stage builder.
/// The host or IP address of the PostgreSQL server.
/// The port number for the PostgreSQL server.
/// The name of the PostgreSQL database.
/// The user ID for connecting to the PostgreSQL server.
/// The password for connecting to the PostgreSQL server.
/// The fallback form type.
/// The prefix for database table names (default is "tgb_").
/// The language selection stage builder.
public static ILanguageSelectionStage UsePostgreSqlDatabase(
this ISessionSerializationStage builder,
string hostOrIp, int port,
string databaseName, string userId,
string password, Type fallbackForm = null,
string tablePrefix = "tgb_")
{
var connectionString = $"Host={hostOrIp};Port={port};Database={databaseName};Username={userId};Password={password}";
var serializer = new PostgreSqlSerializer(connectionString, tablePrefix, fallbackForm);
builder.UseSerialization(serializer);
return builder as BotBaseBuilder;
}
///
/// Uses an PostgreSQL Server Database with Windows Authentication to save and restore sessions.
///
/// The session serialization stage builder.
/// The host or IP address of the PostgreSQL server.
/// The port number for the PostgreSQL server.
/// The name of the PostgreSQL database.
/// A flag indicating whether to use Windows Authentication (true) or not (false).
/// The fallback form type.
/// The prefix for database table names (default is "tgb_").
/// The language selection stage builder.
public static ILanguageSelectionStage UsePostgreSqlDatabase(
this ISessionSerializationStage builder,
string hostOrIp, int port,
string databaseName, bool integratedSecurity = true,
Type fallbackForm = null, string tablePrefix = "tgb_")
{
if (!integratedSecurity)
{
throw new ArgumentOutOfRangeException();
}
var connectionString = $"Host={hostOrIp};Port={port};Database={databaseName};Integrated Security=true;";
var serializer = new PostgreSqlSerializer(connectionString, tablePrefix, fallbackForm);
builder.UseSerialization(serializer);
return builder as BotBaseBuilder;
}
}
}