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 "../Concept.hpp"
#include "Handles.hpp"
#include <expected>
#include <source_location>
#include <string>

Go to the source code of this file.

Functions

LIGHTWEIGHT_API void Lightweight::Driver::Sqlite3::Allocate (Connection &connection) noexcept
 Creates the connection's state, holding no database yet, unless connection already has one.
 
LIGHTWEIGHT_API std::expected< void, SqlErrorInfo > Lightweight::Driver::Sqlite3::Close (Connection &connection, CloseDiagnostic diagnostic) noexcept
 Closes the connection and leaves connection empty; safe on an empty one.
 
LIGHTWEIGHT_API SqlResult Lightweight::Driver::Sqlite3::Disconnect (Connection &connection) noexcept
 Ends the session of the connection, keeping its state for the next connect: finalizes the native statement of every statement still allocated on it, orphaning their states (they then fail with 08003), and closes the database.
 
bool Lightweight::Driver::Sqlite3::IsAllocated (Connection const &connection) noexcept
 Tells whether connection has its state (connected or not).
 
LIGHTWEIGHT_API bool Lightweight::Driver::Sqlite3::IsAlive (Connection const &connection) noexcept
 Tells whether a database is open; a native connection never dies on its own.
 
LIGHTWEIGHT_API SqlErrorInfo Lightweight::Driver::Sqlite3::LastError (Connection const &connection)
 The last failure of a connection-level call.
 
LIGHTWEIGHT_API std::string Lightweight::Driver::Sqlite3::ServerName (Connection const &connection)
 The name of the DBMS.
 
LIGHTWEIGHT_API std::string Lightweight::Driver::Sqlite3::ServerVersion (Connection const &connection)
 The version of the DBMS: the SQLite library the driver is linked against.
 
LIGHTWEIGHT_API std::string Lightweight::Driver::Sqlite3::DriverName (Connection const &connection)
 The name of the driver the connection runs on, as SqlConnection::DriverName() reports it (for ODBC, the driver library the driver manager loaded). Not Driver::Name ("SQLite3"), the kind of driver that error messages name.
 
LIGHTWEIGHT_API std::string Lightweight::Driver::Sqlite3::DatabaseName (Connection const &connection)
 The database the connection was opened on.
 
LIGHTWEIGHT_API std::string Lightweight::Driver::Sqlite3::UserName (Connection const &connection)
 The user the connection is logged in as; SQLite has no users.
 
LIGHTWEIGHT_API bool Lightweight::Driver::Sqlite3::TransactionActive (Connection const &connection) noexcept
 Tells whether a transaction is open (sqlite3_get_autocommit is 0).
 
LIGHTWEIGHT_API bool Lightweight::Driver::Sqlite3::TransactionsAllowed (Connection const &connection) noexcept
 Tells whether the data source supports transactions; SQLite always does.
 
LIGHTWEIGHT_API SqlResult Lightweight::Driver::Sqlite3::SetAutoCommit (Connection &connection, bool enabled) noexcept
 Switches autocommit mode on or off: off begins a transaction (BEGIN, or adopts one that is open already), on commits it as Commit does.
 
LIGHTWEIGHT_API SqlResult Lightweight::Driver::Sqlite3::Commit (Connection &connection) noexcept
 Commits the transaction SetAutoCommit began (COMMIT); nothing to do when none was begun.
 
LIGHTWEIGHT_API SqlResult Lightweight::Driver::Sqlite3::Rollback (Connection &connection) noexcept
 Rolls back the open transaction (ROLLBACK); nothing to do when none is open, also when SQLite already rolled back the transaction SetAutoCommit began (see Commit). Ends the begun transaction either way.
 
LIGHTWEIGHT_API SqlResult Lightweight::Driver::Sqlite3::SetIsolation (Connection &connection, SqlIsolationMode mode) noexcept
 Sets the transaction isolation level: SQLite transactions are serializable, which satisfies every standard level, so a valid one is accepted and nothing is issued.
 
LIGHTWEIGHT_API bool Lightweight::Driver::Sqlite3::RollbackOpenTransaction (Connection &connection) noexcept
 Rolls back the open transaction (ROLLBACK), if one is open.
 
::Lightweight::Driver::CallOutcome Lightweight::Driver::Sqlite3::CheckResult (Connection const &connection, SqlResult result, std::source_location location) noexcept
 The non-throwing verdict on result of a call on connection; this driver has no fault seam, so nothing is ever injected.
 
LIGHTWEIGHT_API std::expected< std::string, SqlErrorInfo > Lightweight::Driver::Sqlite3::SearchPatternEscape (Connection const &connection)
 Fails: this driver has no catalog functions, so no search pattern of theirs to escape (the dialect's SqlCatalogQueries::searchPatternEscape states the escape of the SQL catalog, see SqlQueryFormatter::CatalogQueries()).
 
LIGHTWEIGHT_API std::expected< void, SqlErrorInfo > Lightweight::Driver::Sqlite3::Connect (Connection &connection, SqliteConnectInfo const &info, std::source_location location)
 Opens the database info names (sqlite3_open_v2), with extended result codes on.
 

Detailed Description

The connection and transaction operations of the SQLite3 driver: the connection half of the Lightweight::Driver::SqlDriver concept, plus connecting from a Lightweight::SqliteConnectInfo.

A Lightweight::Driver::Sqlite3::Connection is one pointer to a driver-owned ConnectionState (private State.hpp). Every failure of a connection-level call is kept in that state, as the diagnostics Lightweight::Driver::Sqlite3::LastError reports.

Definition in file Connection.hpp.

Function Documentation

◆ Allocate()

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

Creates the connection's state, holding no database yet, unless connection already has one.

Running out of memory is not reported: the state stays null, and the connect that follows reports it.

Parameters
connectionThe connection to allocate the state of.

References Lightweight::Driver::Allocate().

◆ Close()

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

Closes the connection and leaves connection empty; safe on an empty one.

Finalizes the native statement of every statement still allocated on the connection and orphans their states (they stay valid until their statement frees them), closes the database and destroys the state.

Parameters
connectionThe connection.
diagnosticUnused: there is no refusal to read a diagnostic of.
Returns
Nothing: closing a SQLite database is never refused (sqlite3_close_v2 defers what it cannot do yet).

References Lightweight::Driver::Close().

◆ Disconnect()

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

Ends the session of the connection, keeping its state for the next connect: finalizes the native statement of every statement still allocated on it, orphaning their states (they then fail with 08003), and closes the database.

Parameters
connectionThe connection; one that is not open has nothing to end.
Returns
SqlResult::Success.

References Lightweight::Driver::Disconnect().

◆ IsAllocated()

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

Tells whether connection has its state (connected or not).

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

Definition at line 57 of file Connection.hpp.

References Lightweight::Driver::IsAllocated().

◆ IsAlive()

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

Tells whether a database is open; a native connection never dies on its own.

Parameters
connectionThe connection.
Returns
true between a successful Connect and Close.

References Lightweight::Driver::IsAlive().

◆ LastError()

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

The last failure of a connection-level call.

Kept in the connection's state, so a failed open reports its error after the native connection it came from is closed.

Parameters
connectionThe connection.
Returns
The diagnostics; value-initialized fields when nothing failed since the last successful connect.

References Lightweight::Driver::LastError().

◆ ServerName()

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

The name of the DBMS.

Parameters
connectionThe connection.
Returns
"SQLite", the name ServerTypeFromServerName recognizes as SQLite.

References Lightweight::Driver::ServerName().

◆ ServerVersion()

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

The version of the DBMS: the SQLite library the driver is linked against.

Parameters
connectionThe connection.
Returns
LibraryVersion.

References Lightweight::Driver::ServerVersion().

◆ DriverName()

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

The name of the driver the connection runs on, as SqlConnection::DriverName() reports it (for ODBC, the driver library the driver manager loaded). Not Driver::Name ("SQLite3"), the kind of driver that error messages name.

Parameters
connectionThe connection.
Returns
"sqlite3-native".

References Lightweight::Driver::DriverName().

◆ DatabaseName()

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

The database the connection was opened on.

Parameters
connectionThe connection.
Returns
SqliteConnectInfo::database of the last successful Connect; empty before one.

References Lightweight::Driver::DatabaseName().

◆ UserName()

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

The user the connection is logged in as; SQLite has no users.

Parameters
connectionThe connection.
Returns
An empty string.

References Lightweight::Driver::UserName().

◆ TransactionActive()

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

Tells whether a transaction is open (sqlite3_get_autocommit is 0).

On ODBC the same question reads "is autocommit off"; SqlTransaction hides the difference.

Parameters
connectionThe connection.
Returns
true when a database is open and a transaction is open on it.

References Lightweight::Driver::TransactionActive().

◆ TransactionsAllowed()

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

Tells whether the data source supports transactions; SQLite always does.

Parameters
connectionThe connection.
Returns
true.

References Lightweight::Driver::TransactionsAllowed().

◆ SetAutoCommit()

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

Switches autocommit mode on or off: off begins a transaction (BEGIN, or adopts one that is open already), on commits it as Commit does.

Parameters
connectionThe connected connection.
enabledWhether every statement commits on its own.
Returns
Success, or Error with the failure kept for LastError (25000 as for Commit).

References Lightweight::Driver::SetAutoCommit().

◆ Commit()

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

Commits the transaction SetAutoCommit began (COMMIT); nothing to do when none was begun.

SQLite rolls a transaction back by itself when a statement in it fails with a conflict resolution of ROLLBACK (ON CONFLICT ROLLBACK, RAISE(ROLLBACK, ...) in a trigger) or with a full disk or an I/O error; the statements run after that run in autocommit mode, each committed on its own. A transaction that was begun but is not open any more is therefore not committed silently: Error with SQLSTATE 25000 ("invalid transaction state", which the retry classifiers take as permanent, as they take the SQLite ODBC driver's failure of that commit). Only a successful COMMIT ends the begun transaction: after any failed commit (this one, a busy database, a deferred foreign key) it stays begun, so a retried commit fails again instead of reporting success; Rollback ends it.

Parameters
connectionThe connection.
Returns
Success, or Error with the failure kept for LastError; 08003 when the connection is not open.

References Lightweight::Driver::Commit().

◆ Rollback()

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

Rolls back the open transaction (ROLLBACK); nothing to do when none is open, also when SQLite already rolled back the transaction SetAutoCommit began (see Commit). Ends the begun transaction either way.

Parameters
connectionThe connection.
Returns
Success, or Error with the failure kept for LastError; 08003 when the connection is not open.

References Lightweight::Driver::Rollback().

◆ SetIsolation()

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

Sets the transaction isolation level: SQLite transactions are serializable, which satisfies every standard level, so a valid one is accepted and nothing is issued.

Parameters
connectionThe connection.
modeThe isolation level.
Returns
Success for a SqlIsolationMode enumerator; Error with SQLSTATE HY024 (kept for LastError) for any other value.

References Lightweight::Driver::SetIsolation().

◆ RollbackOpenTransaction()

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

Rolls back the open transaction (ROLLBACK), if one is open.

Parameters
connectionThe connection.
Returns
Whether the connection is in autocommit mode afterwards; false when no database is open.

References Lightweight::Driver::RollbackOpenTransaction().

◆ CheckResult()

::Lightweight::Driver::CallOutcome Lightweight::Driver::Sqlite3::CheckResult ( Connection const &  connection,
SqlResult  result,
std::source_location  location 
)
inlinenoexcept

The non-throwing verdict on result of a call on connection; this driver has no fault seam, so nothing is ever injected.

Parameters
connectionThe connection result came from (unused).
resultThe result to check.
locationWhere the check originated (unused).
Returns
{ Succeeded(result), std::nullopt }.

Definition at line 165 of file Connection.hpp.

References Lightweight::Driver::CheckResult(), and Lightweight::Driver::CallOutcome::succeeded.

◆ SearchPatternEscape()

LIGHTWEIGHT_API std::expected< std::string, SqlErrorInfo > Lightweight::Driver::Sqlite3::SearchPatternEscape ( Connection const &  connection)

Fails: this driver has no catalog functions, so no search pattern of theirs to escape (the dialect's SqlCatalogQueries::searchPatternEscape states the escape of the SQL catalog, see SqlQueryFormatter::CatalogQueries()).

Parameters
connectionThe connection (unused).
Returns
NotSupportedByDriver(SqlDriverId::Sqlite3, "catalog access") (SQLSTATE IM001).

◆ Connect()

LIGHTWEIGHT_API std::expected< void, SqlErrorInfo > Lightweight::Driver::Sqlite3::Connect ( Connection &  connection,
SqliteConnectInfo const &  info,
std::source_location  location 
)

Opens the database info names (sqlite3_open_v2), with extended result codes on.

Closes a database the connection still has open first (as Close does, keeping the state: its statements are orphaned), and allocates the state if connection has none. Statements allocated while the connection was not open stay registered and become usable. The open flags map one to one onto SQLite's; a combination SQLite does not define (no access flag, both ReadOnly and ReadWrite, Create with ReadOnly, an unknown flag) is rejected with SQLSTATE HY024 before anything is opened, and so is any connect with HY000 when the SQLite library is built single-threaded (SQLITE_THREADSAFE=0). The SQLite settings of info (busy timeout, journal mode) are not applied here — SqlConnection applies them through the query formatter once connected, as it does on the ODBC path.

Parameters
connectionThe connection.
infoThe database and how to open it.
locationWhere the connect originated (unused by this driver; the ODBC driver's fault seam needs it).
Returns
Nothing on success; else the diagnostics of the failed open, which are also kept in the state so LastError reports them after the failed native connection is closed.

References Lightweight::Driver::Connect().