Lightweight 0.20250627.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
31class SqlQueryBuilder;
34
35/// @brief Represents a connection to a SQL database.
36class SqlConnection final
37{
38 public:
39 /// @brief Constructs a new SQL connection to the default connection.
40 ///
41 /// The default connection is set via SetDefaultConnectInfo.
42 /// In case the default connection is not set, the connection will fail.
43 /// And in case the connection fails, the last error will be set.
44 LIGHTWEIGHT_API SqlConnection();
45
46 /// @brief Constructs a new SQL connection to the given connect informaton.
47 ///
48 /// @param connectInfo The connection information to use. If not provided,
49 /// no connection will be established.
50 LIGHTWEIGHT_API explicit SqlConnection(std::optional<SqlConnectionString> connectInfo);
51
52 LIGHTWEIGHT_API SqlConnection(SqlConnection&& /*other*/) noexcept;
53 LIGHTWEIGHT_API SqlConnection& operator=(SqlConnection&& /*other*/) noexcept;
54 SqlConnection(SqlConnection const& /*other*/) = delete;
55 SqlConnection& operator=(SqlConnection const& /*other*/) = delete;
56
57 /// Destructs this SQL connection object,
58 LIGHTWEIGHT_API ~SqlConnection() noexcept;
59
60 /// Retrieves the default connection information.
61 LIGHTWEIGHT_API static SqlConnectionString const& DefaultConnectionString() noexcept;
62
63 /// Sets the default connection information.
64 ///
65 /// @param connectionString The connection information to use.
66 LIGHTWEIGHT_API static void SetDefaultConnectionString(SqlConnectionString const& connectionString) noexcept;
67
68 /// Sets the default connection information as SqlConnectionDataSource.
69 LIGHTWEIGHT_API static void SetDefaultDataSource(SqlConnectionDataSource const& dataSource) noexcept;
70
71 /// Sets a callback to be called after each connection being established.
72 LIGHTWEIGHT_API static void SetPostConnectedHook(std::function<void(SqlConnection&)> hook);
73
74 /// Resets the post connected hook.
75 LIGHTWEIGHT_API static void ResetPostConnectedHook();
76
77 /// @brief Retrieves the connection ID.
78 ///
79 /// This is a unique identifier for the connection, which is useful for debugging purposes.
80 /// Note, this ID will not change if the connection is moved nor when it is reused via the connection pool.
81 [[nodiscard]] uint64_t ConnectionId() const noexcept
82 {
83 return m_connectionId;
84 }
85
86 /// Closes the connection (attempting to put it back into the connect[[ion pool).
87 LIGHTWEIGHT_API void Close() noexcept;
88
89 /// Connects to the given database with the given username and password.
90 ///
91 /// @retval true if the connection was successful.
92 /// @retval false if the connection failed. Use LastError() to retrieve the error information.
93 LIGHTWEIGHT_API bool Connect(SqlConnectionDataSource const& info) noexcept;
94
95 /// Connects to the given database with the given username and password.
96 ///
97 /// @retval true if the connection was successful.
98 /// @retval false if the connection failed. Use LastError() to retrieve the error information.
99 LIGHTWEIGHT_API bool Connect(SqlConnectionString sqlConnectionString) noexcept;
100
101 /// Retrieves the last error information with respect to this SQL connection handle.
102 [[nodiscard]] LIGHTWEIGHT_API SqlErrorInfo LastError() const;
103
104 /// Retrieves the name of the database in use.
105 [[nodiscard]] LIGHTWEIGHT_API std::string DatabaseName() const;
106
107 /// Retrieves the name of the user.
108 [[nodiscard]] LIGHTWEIGHT_API std::string UserName() const;
109
110 /// Retrieves the name of the server.
111 [[nodiscard]] LIGHTWEIGHT_API std::string ServerName() const;
112
113 /// Retrieves the reported server version.
114 [[nodiscard]] LIGHTWEIGHT_API std::string ServerVersion() const;
115
116 /// Retrieves the type of the server.
117 [[nodiscard]] SqlServerType ServerType() const noexcept;
118
119 /// Retrieves the name of the driver used for this connection.
120 [[nodiscard]] std::string const& DriverName() const noexcept;
121
122 /// Retrieves a query formatter suitable for the SQL server being connected.
123 [[nodiscard]] SqlQueryFormatter const& QueryFormatter() const noexcept;
124
125 /// Creates a new query builder for the given table, compatible with the current connection.
126 ///
127 /// @param table The table to query.
128 /// If not provided, the query will be a generic query builder.
129 [[nodiscard]] LIGHTWEIGHT_API SqlQueryBuilder Query(std::string_view const& table = {}) const;
130
131 /// Creates a new query builder for the given table with an alias, compatible with the current connection.
132 ///
133 /// @param table The table to query.
134 /// @param tableAlias The alias to use for the table.
135 [[nodiscard]] LIGHTWEIGHT_API SqlQueryBuilder QueryAs(std::string_view const& table,
136 std::string_view const& tableAlias) const;
137
138 /// Creates a new migration query builder, compatible the current connection.
139 [[nodiscard]] LIGHTWEIGHT_API SqlMigrationQueryBuilder Migration() const;
140
141 /// Tests if a transaction is active.
142 [[nodiscard]] LIGHTWEIGHT_API bool TransactionActive() const noexcept;
143
144 /// Tests if transactions are allowed.
145 [[nodiscard]] LIGHTWEIGHT_API bool TransactionsAllowed() const noexcept;
146
147 /// Tests if the connection is still active.
148 [[nodiscard]] LIGHTWEIGHT_API bool IsAlive() const noexcept;
149
150 /// Retrieves the connection information.
151 [[nodiscard]] LIGHTWEIGHT_API SqlConnectionString const& ConnectionString() const noexcept;
152
153 /// Retrieves the native handle.
154 [[nodiscard]] SQLHDBC NativeHandle() const noexcept
155 {
156 return m_hDbc;
157 }
158
159 /// Retrieves the last time the connection was used.
160 [[nodiscard]] LIGHTWEIGHT_API std::chrono::steady_clock::time_point LastUsed() const noexcept;
161
162 /// Sets the last time the connection was used.
163 LIGHTWEIGHT_API void SetLastUsed(std::chrono::steady_clock::time_point lastUsed) noexcept;
164
165 /// Checks the result of an SQL operation, and throws an exception if it is not successful.
166 LIGHTWEIGHT_API void RequireSuccess(SQLRETURN sqlResult,
167 std::source_location sourceLocation = std::source_location::current()) const;
168
169 private:
170 void PostConnect();
171
172 // Private data members
173 SQLHENV m_hEnv {};
174 SQLHDBC m_hDbc {};
175 uint64_t m_connectionId;
176 SqlServerType m_serverType = SqlServerType::UNKNOWN;
177 SqlQueryFormatter const* m_queryFormatter {};
178 std::string m_driverName;
179
180 struct Data;
181 Data* m_data {};
182};
183
184inline SqlServerType SqlConnection::ServerType() const noexcept
185{
186 return m_serverType;
187}
188
189inline std::string const& SqlConnection::DriverName() const noexcept
190{
191 return m_driverName;
192}
193
195{
196 return *m_queryFormatter;
197}
Represents a connection to a SQL database.
static LIGHTWEIGHT_API void SetDefaultDataSource(SqlConnectionDataSource const &dataSource) noexcept
Sets the default connection information as SqlConnectionDataSource.
LIGHTWEIGHT_API SqlQueryBuilder Query(std::string_view const &table={}) const
SqlServerType ServerType() const noexcept
Retrieves the type of the server.
LIGHTWEIGHT_API SqlConnection()
Constructs a new SQL connection to the default connection.
LIGHTWEIGHT_API void SetLastUsed(std::chrono::steady_clock::time_point lastUsed) noexcept
Sets the last time the connection was used.
uint64_t ConnectionId() const noexcept
Retrieves the connection ID.
LIGHTWEIGHT_API SqlQueryBuilder QueryAs(std::string_view const &table, std::string_view const &tableAlias) const
static LIGHTWEIGHT_API void SetDefaultConnectionString(SqlConnectionString const &connectionString) noexcept
LIGHTWEIGHT_API bool TransactionActive() const noexcept
Tests if a transaction is active.
SQLHDBC NativeHandle() const noexcept
Retrieves the native handle.
static LIGHTWEIGHT_API void ResetPostConnectedHook()
Resets the post connected hook.
LIGHTWEIGHT_API bool TransactionsAllowed() const noexcept
Tests if transactions are allowed.
LIGHTWEIGHT_API std::string ServerName() const
Retrieves the name of the server.
LIGHTWEIGHT_API std::string UserName() const
Retrieves the name of the user.
LIGHTWEIGHT_API bool Connect(SqlConnectionDataSource const &info) noexcept
static LIGHTWEIGHT_API void SetPostConnectedHook(std::function< void(SqlConnection &)> hook)
Sets a callback to be called after each connection being established.
LIGHTWEIGHT_API bool IsAlive() const noexcept
Tests if the connection is still active.
static LIGHTWEIGHT_API SqlConnectionString const & DefaultConnectionString() noexcept
Retrieves the default connection information.
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 DatabaseName() const
Retrieves the name of the database in use.
LIGHTWEIGHT_API void Close() noexcept
Closes the connection (attempting to put it back into the connect[[ion pool).
LIGHTWEIGHT_API SqlMigrationQueryBuilder Migration() const
Creates a new migration query builder, compatible the current connection.
SqlQueryFormatter const & QueryFormatter() const noexcept
Retrieves a query formatter suitable for the SQL server being connected.
LIGHTWEIGHT_API SqlConnection(std::optional< SqlConnectionString > connectInfo)
Constructs a new SQL connection to the given connect informaton.
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.
LIGHTWEIGHT_API SqlErrorInfo LastError() const
Retrieves the last error information with respect to this SQL connection handle.
LIGHTWEIGHT_API std::string ServerVersion() const
Retrieves the reported server version.
LIGHTWEIGHT_API std::chrono::steady_clock::time_point LastUsed() const noexcept
Retrieves the last time the connection was used.
Query builder for building SQL migration queries.
Definition Migrate.hpp:182
API Entry point for building SQL queries.
Definition SqlQuery.hpp:26
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:29