crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
PreparedSqlStatement.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2020 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * PreparedSqlStatement.hpp
24  *
25  * RAII wrapper for prepared SQL statement pointer.
26  *
27  * Created on: Feb 7, 2019
28  * Author: ans
29  */
30 
31 #ifndef WRAPPER_PREPAREDSQLSTATEMENT_HPP_
32 #define WRAPPER_PREPAREDSQLSTATEMENT_HPP_
33 
34 #include "../Helper/Portability/mysqlcppconn.h"
35 
36 #include <cppconn/prepared_statement.h>
37 #include <mysql_connection.h>
38 
39 #include <memory> // std::unique_ptr
40 #include <stdexcept> // std::runtime_error
41 #include <string> // std::string
42 #include <string_view> // std::string_view
43 #include <utility> // std::move
44 
45 namespace crawlservpp::Wrapper {
46 
47  /*
48  * DECLARATION
49  */
50 
52 
69  public:
72 
74 
78  PreparedSqlStatement() = default;
79 
80  PreparedSqlStatement(sql::Connection * setConnection, std::string_view sqlQuery);
81  virtual ~PreparedSqlStatement();
82 
86 
87  [[nodiscard]] sql::PreparedStatement& get();
88  [[nodiscard]] bool valid() const noexcept;
89 
93 
94  void prepare();
95  void clear();
96  void refresh(sql::Connection * newConnection);
97 
99 
102 
106 
109 
110  PreparedSqlStatement(PreparedSqlStatement&& other) noexcept;
112 
114 
115  private:
116  // pointer to the connection (not owned)
117  sql::Connection * connection{nullptr};
118 
119  // internal storage of the SQL query (for recovery on connection loss)
120  std::string query;
121 
122  // unique pointer to the prepared MySQL statement
123  std::unique_ptr<sql::PreparedStatement> ptr;
124  };
125 
126  /*
127  * IMPLEMENTATION
128  */
129 
130  /*
131  * CONSTRUCTION AND DESTRUCTION
132  */
133 
135 
141  sql::Connection * setConnection,
142  std::string_view sqlQuery
143  ) : connection(setConnection), query(sqlQuery) {
144  this->prepare();
145  }
146 
149  this->clear();
150  }
151 
152  /*
153  * GETTERS
154  */
155 
157 
162  inline sql::PreparedStatement& PreparedSqlStatement::get() {
163  if(this->ptr == nullptr) {
164  throw std::runtime_error("get(): No SQL statement prepared");
165  }
166 
167  return *(this->ptr);
168  }
169 
171 
175  inline bool PreparedSqlStatement::valid() const noexcept {
176  return this->ptr.operator bool();
177  }
178 
179  /*
180  * LIFETIME
181  */
182 
184 
192  this->clear();
193 
194  if(!(this->query.empty())) {
195  this->ptr.reset(this->connection->prepareStatement(this->query));
196  }
197  }
198 
200 
208  if(this->ptr != nullptr) {
209  this->ptr->close();
210 
211  this->ptr.reset();
212  }
213  }
214 
216 
227  inline void PreparedSqlStatement::refresh(sql::Connection * newConnection) {
228  this->clear();
229 
230  this->connection = newConnection;
231 
232  this->prepare();
233  }
234 
235  /*
236  * COPY AND MOVE
237  */
238 
240 
251  : connection(other.connection), query(std::move(other.query)), ptr(std::move(other.ptr)) {}
252 
254 
273  if(&other != this) {
274  this->clear();
275 
276  this->connection = other.connection;
277 
278  this->query = std::move(other.query);
279 
280  this->ptr = std::move(other.ptr);
281 
282  other.connection = nullptr;
283  }
284 
285  return *this;
286  }
287 
288 } /* namespace crawlservpp::Wrapper */
289 
290 #endif /* WRAPPER_PREPAREDSQLSTATEMENT_HPP_ */
void refresh(sql::Connection *newConnection)
Refreshes the prepared MySQL statement using the new connection given.
Definition: PreparedSqlStatement.hpp:227
bool valid() const noexcept
Checks whether the prepared MySQL statement is valid.
Definition: PreparedSqlStatement.hpp:175
void prepare()
Prepares the MySQL statement.
Definition: PreparedSqlStatement.hpp:191
RAII wrapper for prepared MySQL statements.
Definition: PreparedSqlStatement.hpp:68
sql::PreparedStatement & get()
Gets a reference to the prepared MySQL statement.
Definition: PreparedSqlStatement.hpp:162
void clear()
Clears the prepared MySQL statement.
Definition: PreparedSqlStatement.hpp:207
virtual ~PreparedSqlStatement()
Destructor clearing the prepared MySQL statement if necessary.
Definition: PreparedSqlStatement.hpp:148
Namespace for RAII wrappers and Wrapper::Database.
Definition: Database.hpp:109
PreparedSqlStatement()=default
Default constructor creating an empty statement.
PreparedSqlStatement & operator=(PreparedSqlStatement &)=delete
Deleted copy assignment operator.