using System.Data.Common;
using Khaemhat.Data.Schema;
namespace Khaemhat.Data;
///
/// A database engine (SPEC §5.1): connections, DDL, creation and removal of databases, server information. Implemented
/// by SqliteProvider and PostgresProvider.
///
public interface IDbProvider
{
/// The engine.
DbEngine Engine { get; }
/// The SQL dialect.
ISqlDialect Dialect { get; }
/// The DDL generator used by migrations.
DdlGenerator Ddl { get; }
/// The driver connection string for a database URL.
/// The database URL (its engine must be ).
/// The connection string. It contains credentials: never log it.
string BuildConnectionString(DatabaseUrl url);
/// Creates a closed connection. Session settings (pragmas, timeouts) are applied when it opens.
/// The connection string.
/// The connection.
DbConnection CreateConnection(string connectionString);
/// Creates the database if it does not exist (CREATE DATABASE, or the SQLite file).
/// The connection string of the database.
/// Cancellation.
/// A task.
Task EnsureDatabaseExistsAsync(string connectionString, CancellationToken cancellationToken = default);
/// Drops the database. Only the company registry and the restore service call it.
/// The connection string of the database.
/// Cancellation.
/// A task.
Task DropDatabaseAsync(string connectionString, CancellationToken cancellationToken = default);
/// Whether the database exists.
/// The connection string of the database.
/// Cancellation.
/// if it exists.
Task DatabaseExistsAsync(string connectionString, CancellationToken cancellationToken = default);
/// Version, size, connections and warnings of the server and database (SPEC §12.1).
/// The connection string of the database.
/// Cancellation.
/// The information.
Task GetServerInfoAsync(string connectionString, CancellationToken cancellationToken = default);
/// Whether an exception is transient (busy, connection lost) and the statement may be retried outside a transaction.
/// The exception.
/// if transient.
bool IsTransient(Exception exception);
///
/// Enters the write scope of a database: on SQLite it serializes writers of the process (SqliteWriteLock);
/// on PostgreSQL it does nothing. Dispose the result to leave.
///
/// The connection string of the database.
/// Cancellation.
/// The scope.
ValueTask EnterWriteScopeAsync(string connectionString, CancellationToken cancellationToken = default);
}