Lightweight 0.20260303.0
Loading...
Searching...
No Matches
SqlConnection.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#if defined(_WIN32) || defined(_WIN64)
6 #include <Windows.h>
7#endif
8
9#include "Api.hpp"
10#include "SqlConnectInfo.hpp"
11#include "SqlError.hpp"
12#include "SqlLogger.hpp"
13#include "SqlServerType.hpp"
14
15#include <atomic>
16#include <chrono>
17#include <expected>
18#include <format>
19#include <functional>
20#include <memory>
21#include <optional>
22#include <string>
23#include <string_view>
24#include <system_error>
25
26#include <sql.h>
27#include <sqlext.h>
28#include <sqlspi.h>
29#include <sqltypes.h>
30
31namespace Lightweight
32{
33
34class SqlQueryBuilder;
35class SqlMigrationQueryBuilder;
36class SqlQueryFormatter;
37
38/// @brief Represents a connection to a SQL database.
39class SqlConnection final
40{
41 public:
42 /// @brief Constructs a new SQL connection to the default connection.
43 ///
44 /// The default connection is set via SetDefaultConnectInfo.
45 /// In case the default connection is not set, the connection will fail.
46 /// And in case the connection fails, the last error will be set.
47 LIGHTWEIGHT_API SqlConnection();
48
49 /// @brief Constructs a new SQL connection to the given connect informaton.
50 ///
51 /// @param connectInfo The connection information to use. If not provided,
52 /// no connection will be established.
53 LIGHTWEIGHT_API explicit SqlConnection(std::optional<SqlConnectionString> connectInfo);
54
55 /// Move constructor.
56 LIGHTWEIGHT_API SqlConnection(SqlConnection&& /*other*/) noexcept;
57 /// Move assignment operator.
58 LIGHTWEIGHT_API SqlConnection& operator=(SqlConnection&& /*other*/) noexcept;
59 SqlConnection(SqlConnection const& /*other*/) = delete;
60 SqlConnection& operator=(SqlConnection const& /*other*/) = delete;
61
62 /// Destructs this SQL connection object,
63 LIGHTWEIGHT_API ~SqlConnection() noexcept;
64
65 /// Retrieves the default connection information.
66 LIGHTWEIGHT_API static SqlConnectionString const& DefaultConnectionString() noexcept;
67
68 /// Sets the default connection information.
69 ///
70 /// @param connectionString The connection information to use.
71 LIGHTWEIGHT_API static void SetDefaultConnectionString(SqlConnectionString const& connectionString) noexcept;
72
73 /// Sets the default connection information as SqlConnectionDataSource.
74 LIGHTWEIGHT_API static void SetDefaultDataSource(SqlConnectionDataSource const& dataSource) noexcept;
75
76 /// Sets a callback to be called after each connection being established.
77 LIGHTWEIGHT_API static void SetPostConnectedHook(std::function<void(SqlConnection&)> hook);
78
79 /// Resets the post connected hook.
80 LIGHTWEIGHT_API static void ResetPostConnectedHook();
81
82 /// @brief Retrieves the connection ID.
83 ///
84 /// This is a unique identifier for the connection, which is useful for debugging purposes.
85 /// Note, this ID will not change if the connection is moved nor when it is reused via the connection pool.
86 [[nodiscard]] uint64_t ConnectionId() const noexcept
87 {
88 return m_connectionId;
89 }
90
91 /// Closes the connection (attempting to put it back into the connect[[ion pool).
92 LIGHTWEIGHT_API void Close() noexcept;
93
94 /// Connects to the given database with the given username and password.
95 ///
96 /// This method can be called on a connection that has been closed via Close().
97 /// If the ODBC handles have been freed, they will be automatically reallocated.
98 ///
99 /// @retval true if the connection was successful.
100 /// @retval false if the connection failed. Use LastError() to retrieve the error information.
101 LIGHTWEIGHT_API bool Connect(SqlConnectionDataSource const& info) noexcept;
102
103 /// Connects to the given database with the given connection string.
104 ///
105 /// This method can be called on a connection that has been closed via Close().
106 /// If the ODBC handles have been freed, they will be automatically reallocated.
107 ///
108 /// @retval true if the connection was successful.
109 /// @retval false if the connection failed. Use LastError() to retrieve the error information.
110 LIGHTWEIGHT_API bool Connect(SqlConnectionString sqlConnectionString) noexcept;
111
112 /// Retrieves the last error information with respect to this SQL connection handle.
113 [[nodiscard]] LIGHTWEIGHT_API SqlErrorInfo LastError() const;
114
115 /// Retrieves the name of the database in use.
116 [[nodiscard]] LIGHTWEIGHT_API std::string DatabaseName() const;
117
118 /// Retrieves the name of the user.
119 [[nodiscard]] LIGHTWEIGHT_API std::string UserName() const;
120
121 /// Retrieves the name of the server.
122 [[nodiscard]] LIGHTWEIGHT_API std::string ServerName() const;
123
124 /// Retrieves the reported server version.
125 [[nodiscard]] LIGHTWEIGHT_API std::string ServerVersion() const;
126
127 /// Retrieves the type of the server.
128 [[nodiscard]] SqlServerType ServerType() const noexcept;
129
130 /// Retrieves the name of the driver used for this connection.
131 [[nodiscard]] std::string const& DriverName() const noexcept;
132
133 /// Retrieves a query formatter suitable for the SQL server being connected.
134 [[nodiscard]] SqlQueryFormatter const& QueryFormatter() const noexcept;
135
136 /// Creates a new query builder for the given table, compatible with the current connection.
137 ///
138 /// @param table The table to query.
139 /// If not provided, the query will be a generic query builder.
140 [[nodiscard]] LIGHTWEIGHT_API SqlQueryBuilder Query(std::string_view const& table = {}) const;
141
142 /// Creates a new query builder for the given table with an alias, compatible with the current connection.
143 ///
144 /// @param table The table to query.
145 /// @param tableAlias The alias to use for the table.
146 [[nodiscard]] LIGHTWEIGHT_API SqlQueryBuilder QueryAs(std::string_view const& table,
147 std::string_view const& tableAlias) const;
148
149 /// Creates a new migration query builder, compatible the current connection.
150 [[nodiscard]] LIGHTWEIGHT_API SqlMigrationQueryBuilder Migration() const;
151
152 /// Tests if a transaction is active.
153 [[nodiscard]] LIGHTWEIGHT_API bool TransactionActive() const noexcept;
154
155 /// Tests if transactions are allowed.
156 [[nodiscard]] LIGHTWEIGHT_API bool TransactionsAllowed() const noexcept;
157
158 /// Tests if the connection is still active.
159 [[nodiscard]] LIGHTWEIGHT_API bool IsAlive() const noexcept;
160
161 /// Retrieves the connection information.
162 [[nodiscard]] LIGHTWEIGHT_API SqlConnectionString const& ConnectionString() const noexcept;
163
164 /// Retrieves the native handle.
165 [[nodiscard]] SQLHDBC NativeHandle() const noexcept
166 {
167 return m_hDbc;
168 }
169
170 /// Retrieves the last time the connection was used.
171 [[nodiscard]] LIGHTWEIGHT_API std::chrono::steady_clock::time_point LastUsed() const noexcept;
172
173 /// Sets the last time the connection was used.
174 LIGHTWEIGHT_API void SetLastUsed(std::chrono::steady_clock::time_point lastUsed) noexcept;
175
176 /// Checks the result of an SQL operation, and throws an exception if it is not successful.
177 LIGHTWEIGHT_API void RequireSuccess(SQLRETURN sqlResult,
178 std::source_location sourceLocation = std::source_location::current()) const;
179
180 private:
181 /// Ensures ODBC handles are allocated. Called by Connect() methods.
182 /// If handles were freed by Close(), this method reallocates them.
183 void EnsureHandlesAllocated();
184
185 void PostConnect();
186
187 // Private data members
188 // Note: move/move assignment operators implemented manually
189 // if adding new data members, make sure to update them accordingly.
190 SQLHENV m_hEnv {};
191 SQLHDBC m_hDbc {};
192 uint64_t m_connectionId;
193 SqlServerType m_serverType = SqlServerType::UNKNOWN;
194 SqlQueryFormatter const* m_queryFormatter {};
195 std::string m_driverName;
196
197 struct Data;
198 Data* m_data {};
199};
200
201inline SqlServerType SqlConnection::ServerType() const noexcept
202{
203 return m_serverType;
204}
205
206inline std::string const& SqlConnection::DriverName() const noexcept
207{
208 return m_driverName;
209}
210
212{
213 return *m_queryFormatter;
214}
215
216} // namespace Lightweight
Represents a connection to a SQL database.
LIGHTWEIGHT_API void RequireSuccess(SQLRETURN sqlResult, std::source_location sourceLocation=std::source_location::current()) const
Checks the result of an SQL operation, and throws an exception if it is not successful.
static LIGHTWEIGHT_API void ResetPostConnectedHook()
Resets the post connected hook.
LIGHTWEIGHT_API std::string ServerName() const
Retrieves the name of the server.
SqlServerType ServerType() const noexcept
Retrieves the type of the server.
LIGHTWEIGHT_API SqlMigrationQueryBuilder Migration() const
Creates a new migration query builder, compatible the current connection.
LIGHTWEIGHT_API bool TransactionActive() const noexcept
Tests if a transaction is active.
LIGHTWEIGHT_API bool IsAlive() const noexcept
Tests if the connection is still active.
LIGHTWEIGHT_API SqlQueryBuilder Query(std::string_view const &table={}) const
LIGHTWEIGHT_API SqlQueryBuilder QueryAs(std::string_view const &table, std::string_view const &tableAlias) const
LIGHTWEIGHT_API SqlConnection(std::optional< SqlConnectionString > connectInfo)
Constructs a new SQL connection to the given connect informaton.
LIGHTWEIGHT_API bool TransactionsAllowed() const noexcept
Tests if transactions are allowed.
uint64_t ConnectionId() const noexcept
Retrieves the connection ID.
SqlQueryFormatter const & QueryFormatter() const noexcept
Retrieves a query formatter suitable for the SQL server being connected.
LIGHTWEIGHT_API void SetLastUsed(std::chrono::steady_clock::time_point lastUsed) noexcept
Sets the last time the connection was used.
LIGHTWEIGHT_API void Close() noexcept
Closes the connection (attempting to put it back into the connect[[ion pool).
static LIGHTWEIGHT_API void SetDefaultDataSource(SqlConnectionDataSource const &dataSource) noexcept
Sets the default connection information as SqlConnectionDataSource.
static LIGHTWEIGHT_API SqlConnectionString const & DefaultConnectionString() noexcept
Retrieves the default connection information.
LIGHTWEIGHT_API SqlErrorInfo LastError() const
Retrieves the last error information with respect to this SQL connection handle.
LIGHTWEIGHT_API SqlConnection()
Constructs a new SQL connection to the default connection.
LIGHTWEIGHT_API SqlConnection(SqlConnection &&) noexcept
Move constructor.
static LIGHTWEIGHT_API void SetDefaultConnectionString(SqlConnectionString const &connectionString) noexcept
LIGHTWEIGHT_API std::string DatabaseName() const
Retrieves the name of the database in use.
LIGHTWEIGHT_API std::chrono::steady_clock::time_point LastUsed() const noexcept
Retrieves the last time the connection was used.
SQLHDBC NativeHandle() const noexcept
Retrieves the native handle.
LIGHTWEIGHT_API std::string ServerVersion() const
Retrieves the reported server version.
LIGHTWEIGHT_API SqlConnectionString const & ConnectionString() const noexcept
Retrieves the connection information.
std::string const & DriverName() const noexcept
Retrieves the name of the driver used for this connection.
LIGHTWEIGHT_API std::string UserName() const
Retrieves the name of the user.
static LIGHTWEIGHT_API void SetPostConnectedHook(std::function< void(SqlConnection &)> hook)
Sets a callback to be called after each connection being established.
LIGHTWEIGHT_API bool Connect(SqlConnectionDataSource const &info) noexcept
Query builder for building SQL migration queries.
Definition Migrate.hpp:436
API Entry point for building SQL queries.
Definition SqlQuery.hpp:32
API to format SQL queries for different SQL dialects.
Represents a connection data source as a DSN, username, password, and timeout.
Represents an ODBC connection string.
Represents an ODBC SQL error.
Definition SqlError.hpp:33