using System.Data.Common;
using Khaemhat.Data.Schema;
namespace Khaemhat.Data;
///
/// What differs between SQL engines (SPEC ยง5.1): quoting, parameters, paging, insert/upsert forms, case-insensitive
/// search, advisory locks, value conversion and physical types. Everything above the providers uses only this
/// interface, so no code outside Khaemhat.Data.Sqlite and Khaemhat.Data.PostgreSql knows the engine.
///
public interface ISqlDialect
{
/// The engine.
DbEngine Engine { get; }
/// The current UTC timestamp, in the same form the dialect stores .
string CurrentTimestampUtc { get; }
/// Quotes a validated identifier.
/// The identifier (snake_case).
/// The quoted identifier.
string Quote(string identifier);
/// The placeholder of a named parameter, for example @p0.
/// The parameter name without prefix.
/// The placeholder.
string Parameter(string name);
/// The paging clause, appended after ORDER BY.
/// Rows to skip.
/// Rows to return.
/// The clause, starting with a space.
string Paging(int offset, int limit);
/// An insert that returns the generated key.
/// The table.
/// The inserted columns.
/// The parameter placeholders, one per column.
/// The generated key column.
/// The statement.
string InsertReturningId(string table, IReadOnlyList columns, IReadOnlyList parameters, string keyColumn);
/// An insert that updates the existing row on a conflict of .
/// The table.
/// The inserted columns.
/// The parameter placeholders, one per column.
/// The columns of the unique key.
/// The columns to update on conflict.
/// The statement.
string Upsert(
string table, IReadOnlyList columns, IReadOnlyList parameters,
IReadOnlyList conflictColumns, IReadOnlyList updateColumns);
/// A case-insensitive LIKE with \ as escape character.
/// The column (or expression).
/// The pattern, normally a parameter.
/// The condition.
SqlFragment CaseInsensitiveLike(SqlFragment column, SqlFragment pattern);
///
/// A statement that takes a lock held until the end of the current transaction (numerators, closings, scheduler);
/// empty where writes are already serialized.
///
/// The lock key, for example numerator:FV:2026.
/// The statement, possibly empty.
SqlFragment AdvisoryLock(string key);
/// Converts a CLR value to what the driver stores for ; null becomes .
/// The value.
/// The logical type.
/// The value for the driver.
object ToDbValue(object? value, ColumnType type);
/// Converts a value read from the driver to the canonical CLR type of (see ).
/// The value read.
/// The logical type.
/// The CLR value, or .
object? FromDbValue(object? value, ColumnType type);
/// The physical type of a column.
/// The logical type.
/// The length for .
/// The scale for .
/// The SQL type.
string SqlType(ColumnType type, int? length, int? scale);
/// A constant as SQL literal, for DDL defaults only (values in statements are always parameters).
/// The constant.
/// The logical type.
/// The literal.
string Literal(object? value, ColumnType type);
/// Sets the value (converted with ) and the driver type of a parameter.
/// The parameter.
/// The CLR value.
/// The logical type, or to infer it from the value.
void ConfigureParameter(DbParameter parameter, object? value, ColumnType? type);
}