6#include "../SqlBindingTypes.hpp"
7#include "../SqlError.hpp"
15#include <source_location>
29enum class SqlDriverId : std::uint8_t
42inline constexpr std::string_view SqlStateNotSupportedByDriver =
"IM001";
49inline constexpr std::int16_t SqlUnknownParameterType = 0;
53namespace Lightweight::Driver
57enum class CloseDiagnostic : std::uint8_t
86 [[nodiscard]]
explicit constexpr operator bool() const noexcept
99 template <
typename ReadFromHandle>
100 requires std::is_invocable_r_v<SqlErrorInfo, ReadFromHandle const&>
112[[nodiscard]] LIGHTWEIGHT_API
SqlErrorInfo NotSupportedByDriver(SqlDriverId driver, std::string_view operation);
128[[nodiscard]] LIGHTWEIGHT_API SqlFailureAction ClassifyResult(SqlResult result,
SqlErrorInfo const& errorInfo)
noexcept;
174 SqlResultColumnType
type = SqlResultColumnType::Unknown;
193 std::default_initializable<H> && std::is_trivially_copyable_v<H> && std::equality_comparable<H>;
211 D::Statement
const& constStmt,
212 D::Connection
const& connection,
213 std::string_view query,
215 SqlColumnIndex column,
217 std::ptrdiff_t& rowCount,
218 std::chrono::seconds timeout,
219 std::chrono::seconds& timeoutOut,
221 std::source_location location,
222 SqlCatalogRequest
const& catalogRequest,
224 SqlColumnDescription& description,
225 SqlOutputColumnLength length,
226 std::uint16_t parameter,
228 std::size_t columnSize,
229 SqlIndicator& lengthIndicator) {
230 {
Allocate(connection, stmt) } -> std::same_as<SqlResult>;
231 {
Free(stmt) } -> std::same_as<void>;
232 {
Abandon(stmt) } -> std::same_as<void>;
233 {
Prepare(stmt, query, wideQueryText) } -> std::same_as<SqlResult>;
234 {
Execute(stmt) } -> std::same_as<SqlResult>;
235 {
ExecuteDirect(stmt, query, wideQueryText) } -> std::same_as<SqlResult>;
236 {
Fetch(stmt) } -> std::same_as<SqlResult>;
244 {
NumParameters(constStmt, count) } -> std::same_as<SqlResult>;
246 {
ColumnType(constStmt, column) } -> std::same_as<std::expected<SqlResultColumnType, SqlErrorInfo>>;
248 {
QueryTimeout(constStmt, timeoutOut) } -> std::same_as<SqlResult>;
249 {
Diagnose(constStmt) } -> std::same_as<SqlErrorInfo>;
250 {
RequireSuccess(constStmt, result, location) } -> std::same_as<void>;
251 {
CheckResult(constStmt, result, location) } -> std::same_as<CallOutcome>;
252 {
OpenCatalog(constStmt, catalogRequest) } -> std::same_as<SqlResult>;
253 {
CatalogColumnType(constStmt, catalogDataType) } -> std::same_as<SqlResultColumnType>;
254 {
DescribeColumn(constStmt, column, description) } -> std::same_as<SqlResult>;
258 { BindUtf16TextParameter(stmt, parameter, utf16Text, columnSize, lengthIndicator) } -> std::same_as<SqlResult>;
269 D::Connection
const& constConnection,
271 SqlIsolationMode isolationMode,
272 CloseDiagnostic closeDiagnostic,
274 std::source_location location) {
275 {
Allocate(connection) } -> std::same_as<void>;
276 {
Close(connection, closeDiagnostic) } -> std::same_as<std::expected<void, SqlErrorInfo>>;
277 {
Disconnect(connection) } -> std::same_as<SqlResult>;
278 {
IsAllocated(constConnection) } -> std::same_as<bool>;
279 {
IsAlive(constConnection) } -> std::same_as<bool>;
280 {
LastError(constConnection) } -> std::same_as<SqlErrorInfo>;
281 {
ServerName(constConnection) } -> std::same_as<std::string>;
282 {
ServerVersion(constConnection) } -> std::same_as<std::string>;
283 {
DriverName(constConnection) } -> std::same_as<std::string>;
284 {
DatabaseName(constConnection) } -> std::same_as<std::string>;
285 {
UserName(constConnection) } -> std::same_as<std::string>;
288 {
SetAutoCommit(connection, enabled) } -> std::same_as<SqlResult>;
289 {
Commit(connection) } -> std::same_as<SqlResult>;
290 {
Rollback(connection) } -> std::same_as<SqlResult>;
291 {
SetIsolation(connection, isolationMode) } -> std::same_as<SqlResult>;
293 {
CheckResult(constConnection, result, location) } -> std::same_as<CallOutcome>;
294 { SearchPatternEscape(constConnection) } -> std::same_as<std::expected<std::string, SqlErrorInfo>>;
318 typename D::Statement;
319 typename D::Connection;
320 { D::Id } -> std::convertible_to<SqlDriverId>;
321 { D::Name } -> std::convertible_to<std::string_view>;
322 { D::SupportsParameterArrays } -> std::convertible_to<bool>;
323 { D::SupportsRowArrayFetch } -> std::convertible_to<bool>;
324 { D::StatementsDieWithConnection } -> std::convertible_to<bool>;
325 { D::TranscodesNarrowText } -> std::convertible_to<bool>;
326 { D::HasCatalogFunctions } -> std::convertible_to<bool>;
bool IsAllocated(ConnectionHandle const &connection) noexcept
Tells whether connection holds a driver connection (connected or not).
std::string ServerVersion(ConnectionHandle const &connection)
The version of the DBMS the connection is connected to.
void ResetParameterArrays(StatementHandle &statement) noexcept
Restores single-row parameter binding after a parameter-array execution. Not reported.
void CloseCursor(StatementHandle &statement) noexcept
Closes the cursor of a result set that was read to its end. Pending result sets are left alone.
SqlResult UnbindColumns(StatementHandle &statement) noexcept
Unbinds every bound result column.
bool TransactionActive(ConnectionHandle const &connection) noexcept
Tells whether a transaction is open, i.e. autocommit is off.
std::optional< std::int16_t > DescribeResultColumnSqlType(StatementHandle const &statement, SqlColumnIndex column) noexcept
The driver's own SQL type code of a result column, as the driver's leaf binders read it (see SqlDataB...
void Allocate(ConnectionHandle &connection) noexcept
Allocates the driver's connection resources, unless the connection already holds them.
void RequireSuccess(StatementHandle const &statement, SqlResult result, std::source_location location=std::source_location::current())
Throws unless result indicates success: std::invalid_argument for a soft failure the caller is expect...
SqlResult SetAutoCommit(ConnectionHandle &connection, bool enabled) noexcept
Switches autocommit mode on or off.
SqlResult NumResultColumns(StatementHandle const &statement, std::int16_t &count) noexcept
Reports the number of columns of the current result set.
std::string ServerName(ConnectionHandle const &connection)
The name of the DBMS the connection is connected to.
SqlResult SetQueryTimeout(StatementHandle &statement, std::chrono::seconds timeout) noexcept
Sets the query timeout of the statement.
void DiscardResults(StatementHandle &statement) noexcept
Drains every pending result set and closes the cursor, so the handle can run another query....
void Free(StatementHandle &statement) noexcept
Frees the statement's driver handle and leaves statement empty, bound to the same driver.
SqlResult OpenCatalog(StatementHandle const &statement, SqlCatalogRequest const &request)
Opens the catalog result set request names on statement, in the layout the ODBC specification gives t...
SqlResult NumParameters(StatementHandle const &statement, std::int16_t &count) noexcept
Reports the number of parameters of the prepared statement.
SqlResultColumnType CatalogColumnType(StatementHandle const &statement, int dataType) noexcept
The type a DATA_TYPE code of the driver's catalog result sets (see OpenCatalog) stands for.
SqlResult SetIsolation(ConnectionHandle &connection, SqlIsolationMode mode) noexcept
Sets the transaction isolation level of the connection.
SqlResult QueryTimeout(StatementHandle const &statement, std::chrono::seconds &timeout) noexcept
Reports the query timeout of the statement.
void ResetRowArrayAttributes(StatementHandle &statement) noexcept
Restores single-row fetching after a row-array fetch. Not reported; nothing for a driver without row ...
SqlResult NumRowsAffected(StatementHandle const &statement, std::ptrdiff_t &count) noexcept
Reports the number of rows the last executed statement affected.
void Abandon(StatementHandle &statement) noexcept
Gives up the statement's driver handle after its connection session ended, and leaves statement empty...
bool RollbackOpenTransaction(ConnectionHandle &connection) noexcept
Rolls back the open transaction and switches autocommit back on.
SqlErrorInfo LastError(ConnectionHandle const &connection)
Reads the connection's diagnostics: the first record its last failed call left.
std::size_t DeclaredBoundColumnSize(StatementHandle const &statement, SqlColumnIndex column, SqlOutputColumnLength length) noexcept
The declared length of a result column as a bound buffer size (see SqlDataBinderCallback::MinimumOutp...
SqlResult Disconnect(ConnectionHandle &connection) noexcept
Ends the connection's session, keeping its driver resources for the next connect.
SqlResult Commit(ConnectionHandle &connection) noexcept
Commits the open transaction.
SqlErrorInfo Diagnose(StatementHandle const &statement)
Reads the statement's diagnostics: the first record its last failed call left.
std::string DriverName(ConnectionHandle const &connection)
The name of the driver (library) the connection runs on.
SqlResult DescribeColumn(StatementHandle const &statement, SqlColumnIndex column, SqlColumnDescription &description)
Describes a result column: its type and its size.
SqlResult ResetParameters(StatementHandle &statement) noexcept
Drops every parameter binding.
std::expected< SqlResultColumnType, SqlErrorInfo > ColumnType(StatementHandle const &statement, SqlColumnIndex column)
The SQL data type of a result column, as the driver describes it.
std::string UserName(ConnectionHandle const &connection)
The name of the user the connection is logged in as.
std::expected< void, SqlErrorInfo > Close(ConnectionHandle &connection, CloseDiagnostic diagnostic) noexcept
Closes the connection: disconnects it, frees its driver resources and leaves connection empty,...
SqlResult Rollback(ConnectionHandle &connection) noexcept
Rolls back the open transaction.
SqlResult MoreResults(StatementHandle &statement) noexcept
Advances to the next pending result set.
bool TransactionsAllowed(ConnectionHandle const &connection) noexcept
Tells whether the data source supports transactions.
std::string DatabaseName(ConnectionHandle const &connection)
The name of the database in use.
CallOutcome CheckResult(StatementHandle const &statement, SqlResult result, std::source_location location)
The non-throwing, fault-seam-aware verdict on result.
bool IsAlive(ConnectionHandle const &connection) noexcept
Tells whether the driver still considers the connection alive.
The connection and transaction operations a driver provides, as free functions found by argument-depe...
A per-driver handle struct: plain data the tagged handles store verbatim.
The statement operations a driver provides, as free functions found by argument-dependent lookup on i...
A database driver the library can be built with.
@ Execute
The execution of a prepared statement (SqlStatement::Execute).
@ ExecuteDirect
The execution of a one-shot statement (SqlStatement::ExecuteDirect).
@ Prepare
The preparation of a statement (SqlStatement::Prepare).
Verdict of a non-throwing checked driver call, carrying the diagnostic that justifies a failure verdi...
SqlErrorInfo EffectiveError(ReadFromHandle const &readFromHandle) const
The diagnostic describing this failure, from whichever source actually has one.
std::optional< SqlErrorInfo > injectedError
bool succeeded
Whether the checked call should be treated as having succeeded.
What a catalog call asks for: the ODBC catalog function and its arguments, in driver-neutral terms.
Kind
The catalog function.
@ Columns
every column of a table
@ Tables
the tables of a catalog and schema, narrowed to a table name search pattern
@ PrimaryKeys
the primary key of a table
@ Statistics
the indexes of a table
@ ForeignKeys
the foreign keys between a primary-key table and a foreign-key table
std::string_view foreignTable
ForeignKeys only: the name of the foreign-key table; empty for any.
Kind kind
The catalog function to open.
std::string_view catalog
The catalog (database) name; empty for none. For ForeignKeys: of the primary-key table.
std::string_view foreignCatalog
ForeignKeys only: the catalog name of the foreign-key table; empty for none.
bool uniqueOnly
Statistics only: whether to report the unique indexes only.
std::string_view foreignSchema
ForeignKeys only: the schema name of the foreign-key table; empty for none.
What a driver reports about a result column (see DescribeColumn).
std::size_t columnSize
The column size (maximum character count for character columns); 0 when the driver cannot bound it.
SqlResultColumnType type
The column's type, SqlResultColumnType::Unknown for one without an enumerator.
Represents a SQL error as reported by the database driver.