Lightweight 0.20260303.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 /// Constructs a scoped trace logger for the given SQL connection, logging to standard output.
25 explicit SqlScopedTraceLogger(SqlConnection& connection):
26 SqlScopedTraceLogger(connection.NativeHandle(),
27#if defined(_WIN32) || defined(_WIN64)
28 "CONOUT$"
29#else
30 "/dev/stdout"
31#endif
32 )
33 {
34 }
35
36 /// Constructs a scoped trace logger for the given SQL statement, logging to standard output.
38 SqlScopedTraceLogger(stmt.Connection().NativeHandle(),
39#if defined(_WIN32) || defined(_WIN64)
40 "CONOUT$"
41#else
42 "/dev/stdout"
43#endif
44 )
45 {
46 }
47
48 /// Constructs a scoped trace logger for the given native ODBC handle, logging to the specified file.
49 explicit SqlScopedTraceLogger(SQLHDBC hDbc, std::filesystem::path const& logFile):
50 m_nativeConnection { hDbc }
51 {
52 SQLSetConnectAttrA(m_nativeConnection, SQL_ATTR_TRACEFILE, (SQLPOINTER) logFile.string().c_str(), SQL_NTS);
53 SQLSetConnectAttrA(m_nativeConnection, SQL_ATTR_TRACE, (SQLPOINTER) SQL_OPT_TRACE_ON, SQL_IS_UINTEGER);
54 }
55
57 SqlScopedTraceLogger& operator=(SqlScopedTraceLogger&&) = delete;
59 SqlScopedTraceLogger& operator=(SqlScopedTraceLogger const&) = delete;
60
62 {
63 SQLSetConnectAttrA(m_nativeConnection, SQL_ATTR_TRACE, (SQLPOINTER) SQL_OPT_TRACE_OFF, SQL_IS_UINTEGER);
64 }
65};
66
67} // namespace Lightweight
Represents a connection to a SQL database.
Enables protocol-level ODBC trace logging for the given connection.
SqlScopedTraceLogger(SQLHDBC hDbc, std::filesystem::path const &logFile)
Constructs a scoped trace logger for the given native ODBC handle, logging to the specified file.
SqlScopedTraceLogger(SqlStatement &stmt)
Constructs a scoped trace logger for the given SQL statement, logging to standard output.
SqlScopedTraceLogger(SqlConnection &connection)
Constructs a scoped trace logger for the given SQL connection, logging to standard output.
High level API for (prepared) raw SQL statements.