AnCH Framework 0.1
Another C++ Hack Framework
 
Loading...
Searching...
No Matches
connection.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3 Copyright (C) 2012 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser 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.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19*/
20#pragma once
21#include <functional>
22
23#include "sql/sqlException.hpp"
24#include "sql/resultSet.hpp"
25#include "sql/preparedStatement.hpp"
26
27
28namespace anch::sql {
29
41 std::string driver = "";
42
44 std::string database;
45
47 std::string hostname;
48
50 int port;
51
53 std::string user;
54
56 std::string password;
57
59 std::string application = "AnCH";
60 };
61
71 class Connection {
72
73 // Attributes +
74 protected:
76 bool _valid;
77
80
82 std::map<std::string, PreparedStatement*> _stmts;
83
85 bool _errors;
86 // Attributes -
87
88 // Constructors +
89 public:
96
100 Connection(const Connection&) = delete;
101 // Constructors -
102
103 // Destructor +
104 public:
108 virtual ~Connection() noexcept;
109 // Destructor -
110
111 // Methods +
112 public:
120
126 void begin();
127
134 void commit();
135
142 void rollback();
143
147 void release() noexcept;
148
159
169 ResultSet* query(const std::string& query);
170
179 void queryMapRow(const std::string& sqlQuery, std::function<void(ResultSet&)> rowMapper);
180
189 void queryExtract(const std::string& sqlQuery, std::function<void(ResultSet&)> resExtractor);
190
202 template<typename T, typename... Q>
203 ResultSet* query(const std::string& query, const T& value, const Q&... values);
204
215 template<typename T, typename... Q>
216 void queryMapRow(const std::string& query, std::function<void(ResultSet&)> rowMapper, const T& value, const Q&... values);
217
228 template<typename T, typename... Q>
229 void queryExtract(const std::string& query, std::function<void(ResultSet&)> resExtractor, const T& value, const Q&... values);
230
240 uint64_t update(const std::string& query);
241
253 template<typename T, typename... Q>
254 uint64_t update(const std::string& query, const T& value, const Q&... values);
255
267 template<typename T, typename Iterable>
268 uint64_t batchUpdate(const std::string& query, std::function<void(PreparedStatement&, const T&)> mapper, const Iterable& values);
269
270 protected:
280 virtual ResultSet* executeQuery(const std::string& query) = 0;
281
291 virtual uint64_t executeUpdate(const std::string& query) = 0;
292
298 virtual void sendStartTransaction() = 0;
299
305 virtual void sendCommit() = 0;
306
312 virtual void sendRollback() = 0;
313
323 virtual PreparedStatement* makePrepared(const std::string& query) = 0;
324
325 private:
334 static void extract(ResultSet* res, std::function<void(ResultSet&)> resExtractor);
335
344 static void mapRow(ResultSet* res, std::function<void(ResultSet&)> rowMapper);
345
356 template<typename T, typename... Q>
357 static void bindParameters(PreparedStatement& stmt, std::size_t& idx, const T& value, const Q&... values);
358
368 template<typename T>
369 static void bindParameters(PreparedStatement& stmt, std::size_t& idx, const T& value);
370 // Methods -
371
372 // Accessors +
373 public:
379 bool isValid() const noexcept;
380
381 protected:
387 void setValid(bool valid) noexcept;
388 // Accessors -
389
390 };
391
392}
393
394#include "sql/impl/connection.hpp"
virtual void sendCommit()=0
uint64_t update(const std::string &query)
uint64_t batchUpdate(const std::string &query, std::function< void(PreparedStatement &, const T &)> mapper, const Iterable &values)
void queryMapRow(const std::string &sqlQuery, std::function< void(ResultSet &)> rowMapper)
virtual uint64_t executeUpdate(const std::string &query)=0
virtual void sendStartTransaction()=0
virtual ResultSet * executeQuery(const std::string &query)=0
virtual void sendRollback()=0
Connection(const Connection &)=delete
void queryExtract(const std::string &sqlQuery, std::function< void(ResultSet &)> resExtractor)
ResultSet * query(const std::string &query)
bool _errors
Definition connection.hpp:85
PreparedStatement & prepareStatement(const std::string &query)
virtual PreparedStatement * makePrepared(const std::string &query)=0
void setValid(bool valid) noexcept
bool _transaction
Definition connection.hpp:79
std::map< std::string, PreparedStatement * > _stmts
Definition connection.hpp:82
virtual ~Connection() noexcept
void release() noexcept
bool _valid
Definition connection.hpp:76
bool isValid() const noexcept
SQL prepared statement.
Definition preparedStatement.hpp:43
SQL result representation.
Definition resultSet.hpp:50
SQL namespace.
Definition clauses.hpp:26
SQL database connection configuration.
Definition connection.hpp:39
std::string driver
Definition connection.hpp:41
std::string password
Definition connection.hpp:56
std::string application
Definition connection.hpp:59
int port
Definition connection.hpp:50
std::string hostname
Definition connection.hpp:47
std::string user
Definition connection.hpp:53
std::string database
Definition connection.hpp:44