Lightweight 0.20261105.1
Loading...
Searching...
No Matches
Connection.hpp File Reference
#include "../../Api.hpp"
#include "../../SqlBindingTypes.hpp"
#include "../../SqlConnectInfo.hpp"
#include "../../SqlError.hpp"
#include "../../SqlServerType.hpp"
#include "Catalog.hpp"
#include "Handles.hpp"
#include "SqlOdbcPrelude.hpp"
#include "Statement.hpp"
#include <expected>
#include <source_location>
#include <string>
#include <utility>
#include <sql.h>
#include <sqlext.h>

Go to the source code of this file.

Functions

LIGHTWEIGHT_API void Lightweight::Driver::Odbc::Allocate (Connection &connection) noexcept
 Allocates an environment handle set to ODBC 3 and a connection handle on it, unless connection already holds an environment handle.
 
LIGHTWEIGHT_API std::expected< void, SqlErrorInfo > Lightweight::Driver::Odbc::Close (Connection &connection, CloseDiagnostic diagnostic) noexcept
 Closes the connection: disconnects it, frees its connection and environment handles and leaves connection empty.
 
LIGHTWEIGHT_API std::string Lightweight::Driver::Odbc::DriverNameOf (Connection const &connection, SqlConnectionString const &connectionString)
 Names the driver connectionString reaches without connecting: its DRIVER, else the driver the driver manager lists for its DSN (SQLDataSources), else nothing.
 
LIGHTWEIGHT_API SqlResult Lightweight::Driver::Odbc::Disconnect (Connection &connection) noexcept
 Ends the session of the connection (SQLDisconnect), keeping its handles for the next connect.
 
LIGHTWEIGHT_FORCE_INLINE bool Lightweight::Driver::Odbc::IsAllocated (Connection const &connection) noexcept
 Tells whether connection holds a connection handle (connected or not).
 
LIGHTWEIGHT_API bool Lightweight::Driver::Odbc::IsAlive (Connection const &connection) noexcept
 Tells whether the driver still considers the connection alive (SQL_ATTR_CONNECTION_DEAD).
 
LIGHTWEIGHT_API SqlErrorInfo Lightweight::Driver::Odbc::LastError (Connection const &connection)
 Reads the first diagnostic record of the connection handle.
 
LIGHTWEIGHT_API std::string Lightweight::Driver::Odbc::ServerName (Connection const &connection)
 The name of the DBMS the connection is connected to (SQL_DBMS_NAME).
 
LIGHTWEIGHT_API std::string Lightweight::Driver::Odbc::ServerVersion (Connection const &connection)
 The version of the DBMS the connection is connected to (SQL_DBMS_VER).
 
LIGHTWEIGHT_API std::string Lightweight::Driver::Odbc::DriverName (Connection const &connection)
 The file name of the ODBC driver the connection runs on (SQL_DRIVER_NAME).
 
LIGHTWEIGHT_API std::string Lightweight::Driver::Odbc::DatabaseName (Connection const &connection)
 The name of the database in use (SQL_DATABASE_NAME).
 
LIGHTWEIGHT_API std::string Lightweight::Driver::Odbc::UserName (Connection const &connection)
 The name of the user the connection is logged in as (SQL_USER_NAME).
 
LIGHTWEIGHT_API bool Lightweight::Driver::Odbc::TransactionActive (Connection const &connection) noexcept
 Tells whether a transaction is open, i.e. autocommit is off.
 
LIGHTWEIGHT_API bool Lightweight::Driver::Odbc::TransactionsAllowed (Connection const &connection) noexcept
 Tells whether the data source supports transactions (SQL_TXN_CAPABLE).
 
LIGHTWEIGHT_API SqlResult Lightweight::Driver::Odbc::SetAutoCommit (Connection &connection, bool enabled) noexcept
 Switches autocommit mode on or off.
 
LIGHTWEIGHT_API SqlResult Lightweight::Driver::Odbc::Commit (Connection &connection) noexcept
 Commits the open transaction.
 
LIGHTWEIGHT_API SqlResult Lightweight::Driver::Odbc::Rollback (Connection &connection) noexcept
 Rolls back the open transaction.
 
LIGHTWEIGHT_API SqlResult Lightweight::Driver::Odbc::SetIsolation (Connection &connection, SqlIsolationMode mode) noexcept
 Sets the transaction isolation level of the connection.
 
LIGHTWEIGHT_API bool Lightweight::Driver::Odbc::RollbackOpenTransaction (Connection &connection) noexcept
 Rolls back the open transaction and switches autocommit back on (RestoreAutoCommitAfterRollback), stopping at the first step that fails.
 
detail::OdbcCallOutcome Lightweight::Driver::Odbc::CheckResult (Connection const &connection, SqlResult result, std::source_location sourceLocation)
 The non-throwing, fault-seam-aware verdict on result of a call on connection (see detail::CheckOdbcConnectionCall()).
 
LIGHTWEIGHT_API bool Lightweight::Driver::Odbc::ReadsBoundColumnsAgain (Connection const &connection) noexcept
 Tells whether the driver can read a column bound with SQLBindCol again with SQLGetData (SQL_GD_BOUND in SQL_GETDATA_EXTENSIONS).
 
LIGHTWEIGHT_API::Lightweight::detail::NarrowTextBinding Lightweight::Driver::Odbc::NarrowTextBinding (Connection const &connection, SqlServerType serverType, SqlConnectInfo const &connectInfo)
 Tells how narrow strings are bound on a connection to serverType.
 
LIGHTWEIGHT_API std::expected< void, SqlErrorInfo > Lightweight::Driver::Odbc::Connect (Connection &connection, SqlConnectionString const &connectionString, std::source_location location)
 Connects through a connection string (SQLDriverConnect) and switches autocommit on.
 
LIGHTWEIGHT_API std::expected< void, SqlErrorInfo > Lightweight::Driver::Odbc::Connect (Connection &connection, SqlConnectionDataSource const &dataSource, std::source_location location)
 Connects to an ODBC data source (SQLConnect) and switches autocommit on.
 
LIGHTWEIGHT_API bool Lightweight::Driver::Odbc::AcceptsWideQueryText (Connection const &connection) noexcept
 Tells whether the driver behind connection receives wide text unchanged.
 
LIGHTWEIGHT_API SqlResult Lightweight::Driver::Odbc::RestoreAutoCommitAfterRollback (Connection &connection) noexcept
 Switches autocommit back on after SqlConnection::RollbackOpenTransaction() rolled back.
 
LIGHTWEIGHT_API void Lightweight::Driver::Odbc::RegisterTextEntry (Connection const &connection, bool acceptsWideQueryText) noexcept
 Records whether the connection's driver takes text through the W entry points, so that the diagnostics of the connection, and of every statement allocated on it later, are read accordingly.
 
LIGHTWEIGHT_API void Lightweight::Driver::Odbc::RememberTextEntry (SqlConnectionString const &connectionString, bool acceptsWideQueryText) noexcept
 Records, process-wide, whether the driver a connection string names accepts wide text, so a later Connect() through it chooses its entry point before connecting. Best effort.
 
LIGHTWEIGHT_API void Lightweight::Driver::Odbc::RememberTextEntry (SqlConnectionDataSource const &dataSource, bool acceptsWideQueryText) noexcept
 Records, process-wide, whether the driver behind a data source accepts wide text, so a later Connect() to it chooses its entry point before connecting. Best effort.
 

Detailed Description

The connection and transaction operations of the ODBC driver: everything SqlConnection and SqlTransaction ask an ODBC connection handle to do. The first group is the connection half of the Lightweight::Driver::SqlDriver concept; the second group is ODBC-only (connecting through a connection string or a data source, and the per-driver choice between the W and the ANSI entry points).

Every function is a thin wrapper: the same ODBC calls with the same arguments in the same order as the code it was moved from. Calls whose result the caller checks hand it back as Lightweight::SqlResult, so the caller keeps its check at the same point.

Definition in file Connection.hpp.

Function Documentation

◆ Allocate()

LIGHTWEIGHT_API void Lightweight::Driver::Odbc::Allocate ( Connection &  connection)
noexcept

Allocates an environment handle set to ODBC 3 and a connection handle on it, unless connection already holds an environment handle.

The results are not reported: a handle that failed to allocate stays null, and the connect that follows reports the failure. A connection whose environment handle exists but whose connection handle failed to allocate is left as it is.

Parameters
connectionThe connection to allocate the handles of.

References Lightweight::Driver::Allocate().

◆ Close()

LIGHTWEIGHT_API std::expected< void, SqlErrorInfo > Lightweight::Driver::Odbc::Close ( Connection &  connection,
CloseDiagnostic  diagnostic 
)
noexcept

Closes the connection: disconnects it, frees its connection and environment handles and leaves connection empty.

The handles are freed whatever the disconnect reports; a refused disconnect (e.g. the SQLite ODBC driver's 25000 "unfinished statements" while statements are allocated) is reported, with the diagnostic read before the connection handle is freed.

Parameters
connectionThe connection; it must own its handles (not be borrowed).
diagnosticWhether the diagnostic of a refused disconnect is read: a handle never connected refuses with 08003 ("connection not open"), which is not read (CloseDiagnostic::Skip).
Returns
Nothing, or the diagnostic of a disconnect the driver refused, when read.

References Lightweight::Driver::Close().

◆ DriverNameOf()

LIGHTWEIGHT_API std::string Lightweight::Driver::Odbc::DriverNameOf ( Connection const &  connection,
SqlConnectionString const &  connectionString 
)

Names the driver connectionString reaches without connecting: its DRIVER, else the driver the driver manager lists for its DSN (SQLDataSources), else nothing.

Parameters
connectionThe connection; its environment handle is asked for the data sources.
connectionStringThe connection string to connect from.
Returns
The driver's name or library path, or empty.

References Lightweight::Driver::DriverNameOf().

◆ Disconnect()

LIGHTWEIGHT_API SqlResult Lightweight::Driver::Odbc::Disconnect ( Connection &  connection)
noexcept

Ends the session of the connection (SQLDisconnect), keeping its handles for the next connect.

A successful disconnect also forgets that the connection handle takes text through the ANSI entry points (see RegisterTextEntry()). A refused one leaves the session, and that record, as they were.

Parameters
connectionThe connection; may hold no connection handle, which is nothing to end.
Returns
The result of SQLDisconnect; SqlResult::Success without a connection handle.

References Lightweight::Driver::Disconnect().

◆ IsAllocated()

LIGHTWEIGHT_FORCE_INLINE bool Lightweight::Driver::Odbc::IsAllocated ( Connection const &  connection)
inlinenoexcept

Tells whether connection holds a connection handle (connected or not).

Parameters
connectionThe connection.
Returns
true unless the connection handle is null.

Definition at line 83 of file Connection.hpp.

References Lightweight::Driver::IsAllocated().

◆ IsAlive()

LIGHTWEIGHT_API bool Lightweight::Driver::Odbc::IsAlive ( Connection const &  connection)
noexcept

Tells whether the driver still considers the connection alive (SQL_ATTR_CONNECTION_DEAD).

Parameters
connectionThe connection.
Returns
true when the attribute could be read and reports a live connection.

References Lightweight::Driver::IsAlive().

◆ LastError()

LIGHTWEIGHT_API SqlErrorInfo Lightweight::Driver::Odbc::LastError ( Connection const &  connection)

Reads the first diagnostic record of the connection handle.

Unlike Diagnose(Connection const&), never falls back to the environment handle.

Parameters
connectionThe connection.
Returns
The diagnostics; value-initialized fields when the driver has none to report.

References Lightweight::Driver::LastError().

◆ ServerName()

LIGHTWEIGHT_API std::string Lightweight::Driver::Odbc::ServerName ( Connection const &  connection)

The name of the DBMS the connection is connected to (SQL_DBMS_NAME).

Parameters
connectionThe connected connection.
Returns
The name in UTF-8, or an empty string if the driver cannot tell.

References Lightweight::Driver::ServerName().

◆ ServerVersion()

LIGHTWEIGHT_API std::string Lightweight::Driver::Odbc::ServerVersion ( Connection const &  connection)

The version of the DBMS the connection is connected to (SQL_DBMS_VER).

Parameters
connectionThe connected connection.
Returns
The version in UTF-8, or an empty string if the driver cannot tell.

References Lightweight::Driver::ServerVersion().

◆ DriverName()

LIGHTWEIGHT_API std::string Lightweight::Driver::Odbc::DriverName ( Connection const &  connection)

The file name of the ODBC driver the connection runs on (SQL_DRIVER_NAME).

Parameters
connectionThe connected connection.
Returns
The name in UTF-8, or an empty string if the driver cannot tell.

References Lightweight::Driver::DriverName().

◆ DatabaseName()

LIGHTWEIGHT_API std::string Lightweight::Driver::Odbc::DatabaseName ( Connection const &  connection)

The name of the database in use (SQL_DATABASE_NAME).

Parameters
connectionThe connected connection.
Returns
The name in UTF-8, or an empty string if the driver cannot tell.

References Lightweight::Driver::DatabaseName().

◆ UserName()

LIGHTWEIGHT_API std::string Lightweight::Driver::Odbc::UserName ( Connection const &  connection)

The name of the user the connection is logged in as (SQL_USER_NAME).

Parameters
connectionThe connected connection.
Returns
The name in UTF-8, or an empty string if the driver cannot tell.

References Lightweight::Driver::UserName().

◆ TransactionActive()

LIGHTWEIGHT_API bool Lightweight::Driver::Odbc::TransactionActive ( Connection const &  connection)
noexcept

Tells whether a transaction is open, i.e. autocommit is off.

Parameters
connectionThe connection.
Returns
true when the autocommit attribute was read without diagnostics and is off.

References Lightweight::Driver::TransactionActive().

◆ TransactionsAllowed()

LIGHTWEIGHT_API bool Lightweight::Driver::Odbc::TransactionsAllowed ( Connection const &  connection)
noexcept

Tells whether the data source supports transactions (SQL_TXN_CAPABLE).

Parameters
connectionThe connected connection.
Returns
true when the capability was read without diagnostics and is not SQL_TC_NONE.

References Lightweight::Driver::TransactionsAllowed().

◆ SetAutoCommit()

LIGHTWEIGHT_API SqlResult Lightweight::Driver::Odbc::SetAutoCommit ( Connection &  connection,
bool  enabled 
)
noexcept

Switches autocommit mode on or off.

Parameters
connectionThe connection.
enabledWhether every statement commits on its own.
Returns
The result of SQLSetConnectAttr.

References Lightweight::Driver::SetAutoCommit().

◆ Commit()

LIGHTWEIGHT_API SqlResult Lightweight::Driver::Odbc::Commit ( Connection &  connection)
noexcept

Commits the open transaction.

Parameters
connectionThe connection.
Returns
The result of SQLEndTran.

References Lightweight::Driver::Commit().

◆ Rollback()

LIGHTWEIGHT_API SqlResult Lightweight::Driver::Odbc::Rollback ( Connection &  connection)
noexcept

Rolls back the open transaction.

Parameters
connectionThe connection.
Returns
The result of SQLEndTran.

References Lightweight::Driver::Rollback().

◆ SetIsolation()

LIGHTWEIGHT_API SqlResult Lightweight::Driver::Odbc::SetIsolation ( Connection &  connection,
SqlIsolationMode  mode 
)
noexcept

Sets the transaction isolation level of the connection.

Parameters
connectionThe connection.
modeThe isolation level; not SqlIsolationMode::DriverDefault, which has no ODBC value.
Returns
The result of SQLSetConnectAttr.

References Lightweight::Driver::SetIsolation().

◆ RollbackOpenTransaction()

LIGHTWEIGHT_API bool Lightweight::Driver::Odbc::RollbackOpenTransaction ( Connection &  connection)
noexcept

Rolls back the open transaction and switches autocommit back on (RestoreAutoCommitAfterRollback), stopping at the first step that fails.

Parameters
connectionThe connection, with a transaction open.
Returns
Whether both steps succeeded.

References Lightweight::Driver::RollbackOpenTransaction().

◆ CheckResult()

detail::OdbcCallOutcome Lightweight::Driver::Odbc::CheckResult ( Connection const &  connection,
SqlResult  result,
std::source_location  sourceLocation 
)
inline

The non-throwing, fault-seam-aware verdict on result of a call on connection (see detail::CheckOdbcConnectionCall()).

Parameters
connectionThe connection result came from.
resultThe result to check.
sourceLocationWhere the check originated; forwarded to the fault source.
Returns
The verdict, plus the injected diagnostic when one was injected.

Definition at line 170 of file Connection.hpp.

References Lightweight::Driver::CheckResult(), and Lightweight::Driver::Odbc::Connection::dbc.

◆ ReadsBoundColumnsAgain()

LIGHTWEIGHT_API bool Lightweight::Driver::Odbc::ReadsBoundColumnsAgain ( Connection const &  connection)
noexcept

Tells whether the driver can read a column bound with SQLBindCol again with SQLGetData (SQL_GD_BOUND in SQL_GETDATA_EXTENSIONS).

Parameters
connectionThe connected connection.
Returns
true when the driver reports SQL_GD_BOUND; false when it does not, or cannot be asked.

References Lightweight::Driver::ReadsBoundColumnsAgain().

◆ NarrowTextBinding()

LIGHTWEIGHT_API::Lightweight::detail::NarrowTextBinding Lightweight::Driver::Odbc::NarrowTextBinding ( Connection const &  connection,
SqlServerType  serverType,
SqlConnectInfo const &  connectInfo 
)

Tells how narrow strings are bound on a connection to serverType.

The SQLite ODBC driver's OEMCP option (Windows only) makes it convert SQL_C_CHAR with the thread's ANSI code page. The driver takes the option from the connection string, then from the data source's profile; when neither sets it, it is on for a data-source connect (SQLConnect) and off for a connection string. Every other case is detail::NarrowTextBindingOf(serverType).

Parameters
connectionThe connected connection.
serverTypeThe connected backend.
connectInfoWhat the connection was connected from: a connection string, or a data source.
Returns
The binding the narrow string binders use on this connection.

◆ Connect() [1/2]

LIGHTWEIGHT_API std::expected< void, SqlErrorInfo > Lightweight::Driver::Odbc::Connect ( Connection &  connection,
SqlConnectionString const &  connectionString,
std::source_location  location 
)

Connects through a connection string (SQLDriverConnect) and switches autocommit on.

The session the connection handle had must have been ended first (Disconnect()). A connection string holding non-ASCII text goes to the W or the ANSI entry point as RememberTextEntry() recorded for its driver; failures are not reported to the logger.

Parameters
connectionThe connection; its handles must be allocated.
connectionStringThe connection string.
locationWhere the connect originated; the fault seam sees it as the location of the connection checks. Deliberately without a default: the fault-seam tests match these checks by the caller's function name.
Returns
Nothing on success, else the diagnostics of the failed step (an injected one through the fault seam where a test installed one).

References Lightweight::Driver::Connect().

◆ Connect() [2/2]

LIGHTWEIGHT_API std::expected< void, SqlErrorInfo > Lightweight::Driver::Odbc::Connect ( Connection &  connection,
SqlConnectionDataSource const &  dataSource,
std::source_location  location 
)

Connects to an ODBC data source (SQLConnect) and switches autocommit on.

The session the connection handle had must have been ended first (Disconnect()). Sets the login timeout and, on an explicit request, the SQL Server encryption attribute. Every failure but running out of memory is reported to the logger.

Parameters
connectionThe connection; its handles must be allocated.
dataSourceThe data source, credentials, timeout and encryption request.
locationWhere the connect originated; the fault seam sees it as the location of the connection checks. Deliberately without a default: the fault-seam tests match these checks by the caller's function name.
Returns
Nothing on success, else the diagnostics of the failed step (an injected one through the fault seam where a test installed one).

References Lightweight::Driver::Connect().

◆ AcceptsWideQueryText()

LIGHTWEIGHT_API bool Lightweight::Driver::Odbc::AcceptsWideQueryText ( Connection const &  connection)
noexcept

Tells whether the driver behind connection receives wide text unchanged.

Off Windows, the driver echoes, through SQLNativeSqlW and without a server round trip, a statement holding a CJK character: a driver manager narrowing wide text for an ANSI-only driver cannot carry it. The Windows driver manager converts wide text for such a driver to the ANSI code page it expects, so there the wide entry points are always the right ones.

Parameters
connectionThe connected connection.
Returns
true when the W entry points carry text intact, including when the driver cannot echo it.

References Lightweight::Driver::AcceptsWideQueryText().

◆ RestoreAutoCommitAfterRollback()

LIGHTWEIGHT_API SqlResult Lightweight::Driver::Odbc::RestoreAutoCommitAfterRollback ( Connection &  connection)
noexcept

Switches autocommit back on after SqlConnection::RollbackOpenTransaction() rolled back.

Deliberately keeps the wide entry point (SQLSetConnectAttrW) this call site has always used, whereas SetAutoCommit() keeps the ANSI one SqlTransaction has always used.

Parameters
connectionThe connection.
Returns
The result of SQLSetConnectAttrW.

References Lightweight::Driver::Odbc::RestoreAutoCommitAfterRollback().

Referenced by Lightweight::Driver::Odbc::RestoreAutoCommitAfterRollback().

◆ RegisterTextEntry()

LIGHTWEIGHT_API void Lightweight::Driver::Odbc::RegisterTextEntry ( Connection const &  connection,
bool  acceptsWideQueryText 
)
noexcept

Records whether the connection's driver takes text through the W entry points, so that the diagnostics of the connection, and of every statement allocated on it later, are read accordingly.

Parameters
connectionThe connected connection.
acceptsWideQueryTextWhat AcceptsWideQueryText() found.

References Lightweight::Driver::RegisterTextEntry().

◆ RememberTextEntry() [1/2]

LIGHTWEIGHT_API void Lightweight::Driver::Odbc::RememberTextEntry ( SqlConnectionString const &  connectionString,
bool  acceptsWideQueryText 
)
noexcept

Records, process-wide, whether the driver a connection string names accepts wide text, so a later Connect() through it chooses its entry point before connecting. Best effort.

Parameters
connectionStringThe connection string a connection was established with.
acceptsWideQueryTextWhat AcceptsWideQueryText() found for that connection.

References Lightweight::Driver::Odbc::RememberTextEntry().

Referenced by Lightweight::Driver::Odbc::RememberTextEntry(), and Lightweight::Driver::Odbc::RememberTextEntry().

◆ RememberTextEntry() [2/2]

LIGHTWEIGHT_API void Lightweight::Driver::Odbc::RememberTextEntry ( SqlConnectionDataSource const &  dataSource,
bool  acceptsWideQueryText 
)
noexcept

Records, process-wide, whether the driver behind a data source accepts wide text, so a later Connect() to it chooses its entry point before connecting. Best effort.

Parameters
dataSourceThe data source a connection was established to.
acceptsWideQueryTextWhat AcceptsWideQueryText() found for that connection.

References Lightweight::Driver::Odbc::RememberTextEntry().