AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
preparedStatement.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 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 <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-sql-export.hpp"
23
24#include <cstdint>
25#include <optional>
26#include <string>
27#include <string_view>
28#include <vector>
29
30#include "sql/sqlException.hpp"
31#include "sql/resultSet.hpp"
32#include "sql/parameters.hpp"
33#include "sql/fields.hpp"
34#include "sql/placeholders.hpp"
35
36
37namespace anch::sql {
38
39 class Connection;
40
54 class ANCH_SQL_EXPORT PreparedStatement {
55
56 friend class Connection;
57 friend class ResultSet;
58
59 // Attributes +
60 protected:
62 std::string _query;
63
65 std::vector<std::string> _names;
66
68 std::vector<anch::sql::ParameterValue> _values;
69
71 std::vector<std::optional<anch::sql::ParameterList>> _lists;
72
74 std::vector<bool> _bound;
75
77 std::vector<anch::sql::NamedQuery::Binding> _bindings;
78
80 std::optional<anch::sql::NamedQuery> _named;
81
84
86 std::string _prepared;
87
89 std::vector<std::size_t> _shape;
90
91 private:
93 anch::sql::Connection* _connection;
94
96 std::size_t _openResults;
97
99 bool _invalid;
100 // Attributes -
101
102 // Constructors +
103 public:
108
113 // Constructors -
114
115 // Destructor +
116 public:
120 virtual ~PreparedStatement() noexcept;
121 // Destructor -
122
123 // Methods +
124 public:
132 ResultSet* executeQuery();
133
141 uint64_t executeUpdate();
142
158 template<anch::sql::Bindable T>
159 void set(std::string_view name, const T& value);
160
170 void setNull(std::string_view name);
171
181 void set(const anch::sql::Parameters& parameters);
182
193 template<anch::Described T>
194 void set(const anch::sql::FieldsOf<T>& fields);
195
201 void clearParameters() noexcept;
202
210 std::size_t getParameterCount() const noexcept;
211
219 const std::vector<std::string>& getParameterNames() const noexcept;
220
228 const std::string& getQuery() const noexcept;
229
230 protected:
242 std::string parse(const std::string& query, const anch::sql::SqlSyntax& syntax, anch::sql::ParameterStyle style);
243
253 virtual void doPrepare(const std::string& query) = 0;
254
255 public:
261 std::size_t getBindingCount() const noexcept;
262
271 const anch::sql::ParameterValue& getBindingValue(std::size_t idx) const;
272
273 protected:
281 virtual ResultSet* doExecuteQuery() = 0;
282
290 virtual uint64_t doExecuteUpdate() = 0;
291
299 virtual void deallocate() noexcept;
300
308 void invalidate() noexcept;
309
310 private:
319 void bind(std::string_view name, anch::sql::BoundValue value);
320
326 void reshape();
327
333 void checkBindings() const;
334 // Methods -
335
336 };
337
338 template<anch::sql::Bindable T>
339 void
340 PreparedStatement::set(std::string_view name, const T& value) {
341 bind(name, anch::sql::toBound(value));
342 }
343
344 template<anch::Described T>
345 void
349 for(std::size_t slot = 0 ; slot < _names.size() ; ++slot) {
350 const typename anch::sql::FieldsMapper<T>::Field* field = mapper.find(_names[slot]);
351 if(field != nullptr) {
352 anch::sql::BoundValue bound = field->read(fields.object());
353 _values[slot] = std::move(bound.value);
354 _lists[slot] = std::move(bound.list);
355 _bound[slot] = true;
356 }
357 }
358 }
359
360}
SQL connection virtual class.
Definition connection.hpp:109
Parameters of a described type: the common description of objects ( anch::Mapping<T> ,...
Definition fields.hpp:51
static const FieldsMapper< T > & instance()
Definition fields.hpp:229
const Field * find(std::string_view name) const noexcept
Definition fields.hpp:236
Fields of an object, bound to the parameters of a query with the same names ( fieldsOf ).
Definition fields.hpp:170
const T & object() const noexcept
Definition fields.hpp:294
Named parameters of a query ( :name ), bound by Connection.
Definition parameters.hpp:377
std::vector< anch::sql::ParameterValue > _values
Definition preparedStatement.hpp:68
virtual ResultSet * doExecuteQuery()=0
std::vector< std::size_t > _shape
Definition preparedStatement.hpp:89
const std::vector< std::string > & getParameterNames() const noexcept
virtual uint64_t doExecuteUpdate()=0
const anch::sql::ParameterValue & getBindingValue(std::size_t idx) const
std::size_t getParameterCount() const noexcept
std::string _prepared
Definition preparedStatement.hpp:86
std::vector< std::optional< anch::sql::ParameterList > > _lists
Definition preparedStatement.hpp:71
void set(std::string_view name, const T &value)
Definition preparedStatement.hpp:340
std::vector< std::string > _names
Definition preparedStatement.hpp:65
std::string _query
Definition preparedStatement.hpp:62
virtual void deallocate() noexcept
std::vector< anch::sql::NamedQuery::Binding > _bindings
Definition preparedStatement.hpp:77
void setNull(std::string_view name)
std::string parse(const std::string &query, const anch::sql::SqlSyntax &syntax, anch::sql::ParameterStyle style)
virtual void doPrepare(const std::string &query)=0
anch::sql::ParameterStyle _style
Definition preparedStatement.hpp:83
std::size_t getBindingCount() const noexcept
const std::string & getQuery() const noexcept
void clearParameters() noexcept
std::vector< bool > _bound
Definition preparedStatement.hpp:74
std::optional< anch::sql::NamedQuery > _named
Definition preparedStatement.hpp:80
Type described by an anch::Mapping specialization.
Definition mapping.hpp:66
Definition parameters.hpp:293
SQL namespace.
Definition clauses.hpp:28
std::optional< std::string > ParameterValue
Definition parameters.hpp:50
anch::sql::BoundValue toBound(const T &value)
Definition parameters.hpp:474
ParameterStyle
Parameters style of a database driver ( NamedQuery::render ).
Definition placeholders.hpp:102
AnCH framework base namespace.
Definition app.hpp:30
Value bound to a parameter: value, or list of values.
Definition parameters.hpp:318
std::optional< anch::sql::ParameterList > list
Definition parameters.hpp:323
anch::sql::ParameterValue value
Definition parameters.hpp:320
Field which has a value.
Definition fields.hpp:57
std::function< anch::sql::BoundValue(const T &)> read
Definition fields.hpp:62
Lexical rules of an SQL dialect.
Definition placeholders.hpp:42