Lightweight 0.20261105.1
Loading...
Searching...
No Matches
Handles.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "../../Api.hpp"
6#include "../Concept.hpp"
7
8#include <string_view>
9
10// SQLite's two handle types, declared here once so that no public header needs sqlite3.h: it is included only
11// by the driver's .cpp files and its private State.hpp.
12struct sqlite3;
13struct sqlite3_stmt;
14
15namespace Lightweight
16{
17class SqlConnection;
18class SqlStatement;
19} // namespace Lightweight
20
21namespace Lightweight::Driver::Sqlite3
22{
23
24/// @brief The driver-owned state of one SQLite connection (defined in the driver's private State.hpp).
25struct ConnectionState;
26
27/// @brief The driver-owned state of one SQLite statement (defined in the driver's private State.hpp).
28struct StatementState;
29
30/// @brief The SQLite3 driver's connection handle struct: one pointer to the driver-owned connection state.
31///
32/// Threading: a connection and its statements are used by one thread at a time, their destruction included (a
33/// statement must not be freed on one thread while another thread uses its connection). The driver keeps its
34/// bookkeeping of a connection's statements without a lock. Different connections may be used on different threads
35/// at the same time; the driver refuses to connect with a SQLite library built single-threaded
36/// (@c SQLITE_THREADSAFE=0).
38{
39 /// The connection's state; null for a connection that holds nothing.
41
42 /// Compares the state pointers.
43 bool operator==(Connection const&) const = default;
44};
45
46/// @brief The SQLite3 driver's statement handle struct: one pointer to the driver-owned statement state.
48{
49 /// The statement's state; null for a statement that holds nothing.
51
52 /// Compares the state pointers.
53 bool operator==(Statement const&) const = default;
54};
55
56/// @brief The native SQLite3 driver: names its handle structs, its driver id and its capabilities.
57struct Driver
58{
59 /// The statement handle struct.
61
62 /// The connection handle struct.
64
65 /// The id the tagged handles carry for this driver.
66 static constexpr SqlDriverId Id = SqlDriverId::Sqlite3;
67
68 /// The driver's name, as error messages show it (@c IM001 "... is not supported by the SQLite3 driver") and
69 /// @c Driver::NameOf reports it: the kind of driver, beside @c "ODBC". Not what @c SqlConnection::DriverName()
70 /// reports: that names the driver a connection runs on — for ODBC the driver library the driver manager
71 /// loaded, for this driver the fixed @c "sqlite3-native" (see @c Driver::Sqlite3::DriverName).
72 static constexpr std::string_view Name = "SQLite3";
73
74 /// SQLite executes one parameter set per step; a batch is a loop over rows, not one native call.
75 static constexpr bool SupportsParameterArrays = false;
76
77 /// SQLite steps one row at a time; there is no block fetch into array-bound columns.
78 static constexpr bool SupportsRowArrayFetch = false;
79
80 /// Whether closing a connection destroys its statement handles, so that they must not be freed afterwards.
81 /// False: a statement's state outlives its connection, orphaned, until the statement is freed.
82 static constexpr bool StatementsDieWithConnection = false;
83
84 /// Narrow text is passed to and from SQLite as UTF-8, byte-exact; there is no client codepage.
85 static constexpr bool TranscodesNarrowText = false;
86
87 /// SQLite has no catalog functions: SqlSchema reads the catalog with the SQL of the SQLite formatter.
88 static constexpr bool HasCatalogFunctions = false;
89};
90
91/// @brief The native SQLite connection of @p connection.
92///
93/// For calls the driver does not offer (a user-defined function, a backup, a pragma read through the C API). The
94/// handle stays the driver's:
95/// - Statements prepared on it are the caller's to finalize, before the connection is closed or connected again:
96/// closing finalizes every statement SQLite still lists on the database, these included, so a later
97/// @c sqlite3_finalize of one of them is a double free.
98/// - It belongs to the SQLite library Lightweight is linked against. When that is a private copy (the bundled
99/// fallback build, linked with hidden visibility, or a static vcpkg triplet), a @c sqlite3_* function of another
100/// copy in the same process must never be called on it: only one built into the same binary as Lightweight's.
101/// An application using the installed package reads which case it is in from the CMake variable
102/// @c Lightweight_SQLITE3_SHARED: @c TRUE when that SQLite is a shared library the application can link and call
103/// on this handle, @c FALSE when it is a private copy or not known to be shared (then treat it as private).
104/// - The connection's thread rule applies: one thread at a time, as for the connection itself.
105///
106/// @param connection Any connection handle struct.
107/// @return The native connection; null until connected.
108[[nodiscard]] LIGHTWEIGHT_API ::sqlite3* NativeHandle(Connection const& connection) noexcept;
109
110/// @brief The native SQLite prepared statement of @p stmt.
111/// @param stmt Any statement handle struct.
112/// @return The native statement; null when nothing is prepared.
113[[nodiscard]] LIGHTWEIGHT_API ::sqlite3_stmt* NativeHandle(Statement const& stmt) noexcept;
114
115/// @brief The native SQLite connection of @p connection; the rules of the handle-struct overload apply (statements
116/// prepared on it are finalized when the connection closes, and only the SQLite library Lightweight links may be
117/// called on it).
118/// @param connection Any connection, including a closed or moved-from one.
119/// @return The native connection; null when the connection is closed or belongs to another driver.
120[[nodiscard]] LIGHTWEIGHT_API ::sqlite3* NativeHandle(SqlConnection const& connection) noexcept;
121
122/// @brief The native SQLite prepared statement of @p stmt.
123/// @param stmt Any statement, including a moved-from one.
124/// @return The native statement; null when nothing is prepared or the statement belongs to another driver.
125[[nodiscard]] LIGHTWEIGHT_API ::sqlite3_stmt* NativeHandle(SqlStatement const& stmt) noexcept;
126
127/// @brief The version number of the SQLite library the driver is linked against.
128/// @return @c sqlite3_libversion_number(), e.g. 3050004 for SQLite 3.50.4.
129[[nodiscard]] LIGHTWEIGHT_API int LibraryVersionNumber() noexcept;
130
131/// @brief The version of the SQLite library the driver is linked against.
132/// @return @c sqlite3_libversion(), e.g. "3.50.4".
133[[nodiscard]] LIGHTWEIGHT_API std::string_view LibraryVersion() noexcept;
134
135/// @brief The oldest SQLite the driver supports (3.37.0: STRICT tables), as a @ref LibraryVersionNumber.
136inline constexpr int MinimumLibraryVersionNumber = 3'037'000;
137
138} // namespace Lightweight::Driver::Sqlite3
Represents a connection to a SQL database.
High level API for (prepared) raw SQL statements.
The driver-owned state of one SQLite connection: everything mutable behind a Connection.
Definition State.hpp:147
The SQLite3 driver's connection handle struct: one pointer to the driver-owned connection state.
Definition Handles.hpp:38
ConnectionState * state
The connection's state; null for a connection that holds nothing.
Definition Handles.hpp:40
bool operator==(Connection const &) const =default
Compares the state pointers.
The native SQLite3 driver: names its handle structs, its driver id and its capabilities.
Definition Handles.hpp:58
static constexpr bool HasCatalogFunctions
SQLite has no catalog functions: SqlSchema reads the catalog with the SQL of the SQLite formatter.
Definition Handles.hpp:88
static constexpr bool SupportsRowArrayFetch
SQLite steps one row at a time; there is no block fetch into array-bound columns.
Definition Handles.hpp:78
static constexpr bool StatementsDieWithConnection
Definition Handles.hpp:82
static constexpr bool SupportsParameterArrays
SQLite executes one parameter set per step; a batch is a loop over rows, not one native call.
Definition Handles.hpp:75
static constexpr std::string_view Name
Definition Handles.hpp:72
static constexpr bool TranscodesNarrowText
Narrow text is passed to and from SQLite as UTF-8, byte-exact; there is no client codepage.
Definition Handles.hpp:85
static constexpr SqlDriverId Id
The id the tagged handles carry for this driver.
Definition Handles.hpp:66
The driver-owned state of one SQLite statement: everything mutable behind a Statement.
Definition State.hpp:83
The SQLite3 driver's statement handle struct: one pointer to the driver-owned statement state.
Definition Handles.hpp:48
bool operator==(Statement const &) const =default
Compares the state pointers.
StatementState * state
The statement's state; null for a statement that holds nothing.
Definition Handles.hpp:50