6#include "SqlDataBinder.hpp"
10#include <source_location>
40 LIGHTWEIGHT_API
virtual ~SqlLogger() =
default;
57 virtual void OnWarning(std::string_view
const& message) = 0;
60 virtual void OnError(SqlError errorCode, std::source_location sourceLocation = std::source_location::current()) = 0;
64 std::source_location sourceLocation = std::source_location::current()) = 0;
88 virtual void OnPrepare(std::string_view
const& query) = 0;
94 if (_supportsBindLogging)
96 using value_type = std::remove_cvref_t<T>;
97 if constexpr (SqlDataBinderSupportsInspect<value_type>)
99 OnBind(name, std::string(SqlDataBinder<value_type>::Inspect(std::forward<T>(value))));
105 virtual void OnBind(std::string_view
const& name, std::string value) = 0;
108 virtual void OnExecute(std::string_view
const& query) = 0;
143 bool _supportsBindLogging = false;
149 void OnWarning(std::string_view
const& )
override {}
150 void OnError(SqlError , std::source_location )
override {}
159 void OnPrepare(std::string_view
const& )
override {}
160 void OnBind(std::string_view
const& , std::string )
override {}
161 void OnExecute(std::string_view
const& )
override {}
184 _tag { std::move(tag) }
186 SqlLogger::GetLogger().OnScopedTimerStart(_tag);
191 SqlLogger::GetLogger().OnScopedTimerStop(_tag);
Represents a connection to a SQL database.
Represents a logger for SQL operations.
MessageWriter _messageWriter
The function used to write log messages.
virtual void OnExecuteDirect(std::string_view const &query)=0
Invoked when a direct query is executed.
virtual void OnExecute(std::string_view const &query)=0
Invoked when a prepared query is executed.
virtual void OnError(SqlError errorCode, std::source_location sourceLocation=std::source_location::current())=0
Invoked on ODBC SQL error occurred.
static LIGHTWEIGHT_API Null & NullLogger() noexcept
Retrieves a null logger that does nothing.
static LIGHTWEIGHT_API SqlLogger & GetLogger()
Retrieves the currently configured logger.
LIGHTWEIGHT_API SqlLogger(SqlLogger const &)=default
Default copy constructor.
virtual void OnFetchEnd()=0
Invoked when fetching is done.
LIGHTWEIGHT_API SqlLogger & operator=(SqlLogger &&)=default
Default move assignment operator.
static LIGHTWEIGHT_API SqlLogger & TraceLogger()
Retrieves a logger that logs to the trace logger.
static LIGHTWEIGHT_API void SetLogger(SqlLogger &logger)
virtual void OnError(SqlErrorInfo const &errorInfo, std::source_location sourceLocation=std::source_location::current())=0
Invoked an ODBC SQL error occurred, with extended error information.
LIGHTWEIGHT_API void SetLoggingSink(MessageWriter writer={})
virtual void OnBind(std::string_view const &name, std::string value)=0
Invoked when an input parameter is bound, by name.
virtual void OnConnectionReuse(SqlConnection const &connection)=0
Invoked when a connection is reused.
static LIGHTWEIGHT_API SqlLogger & StandardLogger()
Retrieves a logger that logs to standard output.
virtual void OnPrepare(std::string_view const &query)=0
Invoked when a query is prepared.
virtual void OnExecuteBatch()=0
Invoked when a batch of queries is executed.
void OnBindInputParameter(std::string_view const &name, T &&value)
Invoked when an input parameter is bound.
LIGHTWEIGHT_API SqlLogger(SqlLogger &&)=default
Default move constructor.
std::function< void(std::string)> MessageWriter
Type definition for a function that writes messages.
virtual void OnConnectionClosed(SqlConnection const &connection)=0
Invoked when a connection is closed.
SupportBindLogging
Mandates the support for logging bind operations.
virtual void OnScopedTimerStart(std::string const &tag)=0
Invoked when a scoped code region needs to be timed and logged. The region starts with this call.
virtual void OnConnectionIdle(SqlConnection const &connection)=0
Invoked when a connection is idle.
virtual void OnScopedTimerStop(std::string const &tag)=0
Invoked when a scoped code region needs to be timed and logged. The region ends with this call.
virtual void OnWarning(std::string_view const &message)=0
Invoked on a warning.
LIGHTWEIGHT_API SqlLogger & operator=(SqlLogger const &)=default
Default copy assignment operator.
virtual void OnFetchRow()=0
Invoked when a row is fetched.
virtual void OnConnectionOpened(SqlConnection const &connection)=0
Invoked when a connection is opened.
LIGHTWEIGHT_API SqlLogger(SupportBindLogging supportBindLogging, MessageWriter writer={})
SqlScopedTimeLogger & operator=(SqlScopedTimeLogger const &)=default
Default copy assignment operator.
SqlScopedTimeLogger(std::string tag)
Constructs a scoped time logger with the given tag.
SqlScopedTimeLogger(SqlScopedTimeLogger const &)=default
Default copy constructor.
Represents an ODBC SQL error.