Lightweight 0.20250904.0
Loading...
Searching...
No Matches
SqlScopedTraceLogger.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "Api.hpp"
6#include "SqlConnection.hpp"
7#include "SqlStatement.hpp"
8
9#include <filesystem>
10
11namespace Lightweight
12{
13
14/// @brief Enables protocol-level ODBC trace logging for the given connection.
15///
16/// The trace logging is active for the lifetime of this object.
17///
18/// The logging output is sent to the standard output stream.
19class LIGHTWEIGHT_API SqlScopedTraceLogger
20{
21 SQLHDBC m_nativeConnection;
22
23 public:
24 explicit SqlScopedTraceLogger(SqlConnection& connection):
26#if defined(_WIN32) || defined(_WIN64)
27 "CONOUT$"
28#else
29 "/dev/stdout"
30#endif
31 )
32 {
33 }
34
35 explicit SqlScopedTraceLogger(SqlStatement& stmt):
37#if defined(_WIN32) || defined(_WIN64)
38 "CONOUT$"
39#else
40 "/dev/stdout"
41#endif
42 )
43 {
44 }
45
46 explicit SqlScopedTraceLogger(SQLHDBC hDbc, std::filesystem::path const& logFile):
47 m_nativeConnection { hDbc }
48 {
49 SQLSetConnectAttrA(m_nativeConnection, SQL_ATTR_TRACEFILE, (SQLPOINTER) logFile.string().c_str(), SQL_NTS);
50 SQLSetConnectAttrA(m_nativeConnection, SQL_ATTR_TRACE, (SQLPOINTER) SQL_OPT_TRACE_ON, SQL_IS_UINTEGER);
51 }
52
54 SqlScopedTraceLogger& operator=(SqlScopedTraceLogger&&) = delete;
56 SqlScopedTraceLogger& operator=(SqlScopedTraceLogger const&) = delete;
57
59 {
60 SQLSetConnectAttrA(m_nativeConnection, SQL_ATTR_TRACE, (SQLPOINTER) SQL_OPT_TRACE_OFF, SQL_IS_UINTEGER);
61 }
62};
63
64} // namespace Lightweight
Represents a connection to a SQL database.
SQLHDBC NativeHandle() const noexcept
Retrieves the native handle.
Enables protocol-level ODBC trace logging for the given connection.
High level API for (prepared) raw SQL statements.
LIGHTWEIGHT_API SqlConnection & Connection() noexcept
Retrieves the connection associated with this statement.