crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
crawlservpp::Wrapper::PreparedSqlStatement Class Reference

RAII wrapper for prepared MySQL statements. More...

#include <PreparedSqlStatement.hpp>

Construction and Destruction

 PreparedSqlStatement ()=default
 Default constructor creating an empty statement. More...
 
 PreparedSqlStatement (sql::Connection *setConnection, std::string_view sqlQuery)
 Constructor preparing a MySQL statement for the given connection and SQL query. More...
 
virtual ~PreparedSqlStatement ()
 Destructor clearing the prepared MySQL statement if necessary. More...
 

Getters

sql::PreparedStatement & get ()
 Gets a reference to the prepared MySQL statement. More...
 
bool valid () const noexcept
 Checks whether the prepared MySQL statement is valid. More...
 

Lifetime

void prepare ()
 Prepares the MySQL statement. More...
 
void clear ()
 Clears the prepared MySQL statement. More...
 
void refresh (sql::Connection *newConnection)
 Refreshes the prepared MySQL statement using the new connection given. More...
 

Copy and Move

The class is not copyable, only moveable.

 PreparedSqlStatement (PreparedSqlStatement &)=delete
 Deleted copy constructor. More...
 
PreparedSqlStatementoperator= (PreparedSqlStatement &)=delete
 Deleted copy assignment operator. More...
 
 PreparedSqlStatement (PreparedSqlStatement &&other) noexcept
 Move constructor. More...
 
PreparedSqlStatementoperator= (PreparedSqlStatement &&other) noexcept
 Move assignment operator. More...
 

Detailed Description

RAII wrapper for prepared MySQL statements.

Prepares the statement on creation and clears it on destruction using a smart pointer, avoiding memory leaks.

Additionally stores the underlying SQL statement as string, to be able to re-create it on connection loss.

At the moment, this class is used exclusively by the Main::Database class.

For more information about the MySQL Connector/C++ used, visit its website.

Note
The class owns the pointer to the prepared MySQL statement.

Constructor & Destructor Documentation

◆ PreparedSqlStatement() [1/4]

crawlservpp::Wrapper::PreparedSqlStatement::PreparedSqlStatement ( )
default

Default constructor creating an empty statement.

Warning
This constructor should only be used for reserving memory, e.g. by using std::vector<PreparedSqlStatement>::resize().

◆ PreparedSqlStatement() [2/4]

crawlservpp::Wrapper::PreparedSqlStatement::PreparedSqlStatement ( sql::Connection *  setConnection,
std::string_view  sqlQuery 
)
inline

Constructor preparing a MySQL statement for the given connection and SQL query.

Parameters
setConnectionA pointer to the MySQL connection to use.
sqlQueryA string view containing the SQL query to prepare.

References prepare().

◆ ~PreparedSqlStatement()

crawlservpp::Wrapper::PreparedSqlStatement::~PreparedSqlStatement ( )
inlinevirtual

Destructor clearing the prepared MySQL statement if necessary.

References clear().

◆ PreparedSqlStatement() [3/4]

crawlservpp::Wrapper::PreparedSqlStatement::PreparedSqlStatement ( PreparedSqlStatement )
delete

Deleted copy constructor.

◆ PreparedSqlStatement() [4/4]

crawlservpp::Wrapper::PreparedSqlStatement::PreparedSqlStatement ( PreparedSqlStatement &&  other)
inlinenoexcept

Move constructor.

Moves the prepared MySQL statement from the specified location into this instance of the class.

Note
The other statement will be invalidated by this move.
Parameters
otherThe prepared MySQL statement to move from.
See also
valid

Member Function Documentation

◆ clear()

void crawlservpp::Wrapper::PreparedSqlStatement::clear ( )
inline

Clears the prepared MySQL statement.

The prepared MySQL statement will be invalid and valid() will return false afterwards.

Note
Does nothing if the underlying MySQL statement is not valid.

Referenced by operator=(), prepare(), refresh(), and ~PreparedSqlStatement().

◆ get()

sql::PreparedStatement & crawlservpp::Wrapper::PreparedSqlStatement::get ( )
inline

Gets a reference to the prepared MySQL statement.

Returns
A reference to the prepared MySQL statement.
Exceptions
std::runtime_errorif the MySQL statement is invalid.

◆ operator=() [1/2]

PreparedSqlStatement& crawlservpp::Wrapper::PreparedSqlStatement::operator= ( PreparedSqlStatement )
delete

Deleted copy assignment operator.

◆ operator=() [2/2]

PreparedSqlStatement & crawlservpp::Wrapper::PreparedSqlStatement::operator= ( PreparedSqlStatement &&  other)
inlinenoexcept

Move assignment operator.

Moves the prepared MySQL statement from the specified location into this instance of the class.

If there already is a prepared MySQL statement in this instance of the class, it will be cleared.

Note
The other statement will be invalidated by this move.
Nothing will be done if used on itself.
Parameters
otherThe prepared MySQL statement to move from.
Returns
A reference to the instance containing the prepared MySQL statement after moving (i.e. *this).
See also
valid

References clear().

◆ prepare()

void crawlservpp::Wrapper::PreparedSqlStatement::prepare ( )
inline

Prepares the MySQL statement.

If a statement has been prepared before, it will be cleared if necessary.

Note
If no SQL query is stored inside this instance of the class, no new MySQL statement will be prepared.

References clear().

Referenced by PreparedSqlStatement(), and refresh().

◆ refresh()

void crawlservpp::Wrapper::PreparedSqlStatement::refresh ( sql::Connection *  newConnection)
inline

Refreshes the prepared MySQL statement using the new connection given.

The old prepared MySQL statement will be cleared before setting the new connection and preparing the same SQL query using the new connection.

This function is used to handle connection losses, which will invalidate the previous connection pointer.

Parameters
newConnectionA pointer to the new connection, which will be used from now on.

References clear(), and prepare().

◆ valid()

bool crawlservpp::Wrapper::PreparedSqlStatement::valid ( ) const
inlinenoexcept

Checks whether the prepared MySQL statement is valid.

Returns
True, if the prepared MySQL statement is valid. False otherwise.

The documentation for this class was generated from the following file: