AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
parameters.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 <chrono>
25#include <concepts>
26#include <cstdint>
27#include <initializer_list>
28#include <optional>
29#include <ranges>
30#include <string>
31#include <string_view>
32#include <utility>
33#include <vector>
34
35#include "date/date.hpp"
36#include "date/dateTime.hpp"
37#include "date/time.hpp"
38
39#ifdef ANCH_UUID
40#include "uuid.hpp"
41#endif
42
43namespace anch::sql {
44
50 using ParameterValue = std::optional<std::string>;
51
52 // Values conversion +
62 ANCH_SQL_EXPORT anch::sql::ParameterValue toParameter(int16_t value);
63
73 ANCH_SQL_EXPORT anch::sql::ParameterValue toParameter(uint16_t value);
74
84 ANCH_SQL_EXPORT anch::sql::ParameterValue toParameter(int32_t value);
85
95 ANCH_SQL_EXPORT anch::sql::ParameterValue toParameter(uint32_t value);
96
106 ANCH_SQL_EXPORT anch::sql::ParameterValue toParameter(int64_t value);
107
117 ANCH_SQL_EXPORT anch::sql::ParameterValue toParameter(uint64_t value);
118
131 template<std::same_as<bool> B>
133
144 ANCH_SQL_EXPORT anch::sql::ParameterValue toParameter(double value);
145
155 ANCH_SQL_EXPORT anch::sql::ParameterValue toParameter(const std::string& value);
156
166 ANCH_SQL_EXPORT anch::sql::ParameterValue toParameter(const char* value);
167
178
189
200
201#ifdef ANCH_UUID
212 ANCH_SQL_EXPORT anch::sql::ParameterValue toParameter(const anch::UUID& value);
213#endif
214
229 template<typename Duration>
230 anch::sql::ParameterValue toParameter(const std::chrono::sys_time<Duration>& value);
231
239 inline anch::sql::ParameterValue toParameter(std::nullopt_t) {
240 return std::nullopt;
241 }
242
254 template<typename T>
255 anch::sql::ParameterValue toParameter(const std::optional<T>& value);
256 // Values conversion -
257
267 template<typename T>
268 concept BindableScalar = requires(const T& value) {
269 { anch::sql::toParameter(value) } -> std::same_as<anch::sql::ParameterValue>;
270 };
271
281 template<typename T>
282 concept BindableList = std::ranges::input_range<const T> && !std::convertible_to<const T&, std::string_view>
284
292 template<typename T>
294
300 using ParameterList = std::vector<anch::sql::ParameterValue>;
301
311 ANCH_SQL_EXPORT std::string arrayLiteral(const anch::sql::ParameterList& elements);
312
318 struct ANCH_SQL_EXPORT BoundValue {
321
323 std::optional<anch::sql::ParameterList> list;
324 };
325
337 template<anch::sql::Bindable T>
338 anch::sql::BoundValue toBound(const T& value);
339
345 struct ANCH_SQL_EXPORT Parameter {
347 std::string name;
348
351
353 std::optional<anch::sql::ParameterList> list;
354
363 template<anch::sql::Bindable T>
364 Parameter(std::string parameterName, const T& parameterValue);
365 };
366
377 class ANCH_SQL_EXPORT Parameters {
378
379 // Attributes +
380 private:
382 std::vector<anch::sql::Parameter> _parameters;
383 // Attributes -
384
385 // Constructors +
386 public:
392 Parameters(std::initializer_list<anch::sql::Parameter> parameters = {});
393 // Constructors -
394
395 // Methods +
396 public:
407 template<anch::sql::Bindable T>
408 Parameters& add(std::string_view name, const T& value);
409
417 Parameters& addNull(std::string_view name);
418
427
433 const std::vector<anch::sql::Parameter>& values() const noexcept;
434
435 private:
442 void put(std::string_view name, anch::sql::BoundValue value);
443 // Methods -
444
445 };
446
447}
448
449namespace anch::sql {
450
451 template<std::same_as<bool> B>
453 toParameter(B value) {
454 return std::string(value ? "1" : "0");
455 }
456
457 template<typename Duration>
459 toParameter(const std::chrono::sys_time<Duration>& value) {
460 return anch::sql::toParameter(anch::date::DateTime(std::chrono::time_point_cast<std::chrono::system_clock::duration>(value)));
461 }
462
463 template<typename T>
465 toParameter(const std::optional<T>& value) {
466 if(value.has_value()) {
467 return anch::sql::toParameter(*value);
468 }
469 return std::nullopt;
470 }
471
472 template<anch::sql::Bindable T>
474 toBound(const T& value) {
475 if constexpr (anch::sql::BindableScalar<T>) {
476 return anch::sql::BoundValue{anch::sql::toParameter(value), std::nullopt};
477 } else {
479 for(const auto& element : value) {
480 elements.push_back(anch::sql::toParameter(element));
481 }
482 std::string literal = anch::sql::arrayLiteral(elements);
483 return anch::sql::BoundValue{std::move(literal), std::move(elements)};
484 }
485 }
486
487 template<anch::sql::Bindable T>
488 Parameter::Parameter(std::string parameterName, const T& parameterValue): name(std::move(parameterName)), value(), list() {
489 anch::sql::BoundValue bound = anch::sql::toBound(parameterValue);
490 value = std::move(bound.value);
491 list = std::move(bound.list);
492 }
493
494 template<anch::sql::Bindable T>
496 Parameters::add(std::string_view name, const T& value) {
497 put(name, anch::sql::toBound(value));
498 return *this;
499 }
500
501}
UUID generator and parser.
Definition uuid.hpp:51
Calendar day (RFC 3339 full-date , JSON schema and OpenAPI date format, XSD xs:date ).
Definition date.hpp:63
Point in time.
Definition dateTime.hpp:89
Time of day (RFC 3339 partial-time , SQL TIME , XSD xs:time without time zone).
Definition time.hpp:48
Named parameters of a query ( :name ), bound by Connection.
Definition parameters.hpp:377
const std::vector< anch::sql::Parameter > & values() const noexcept
Parameters & addNull(std::string_view name)
Parameters & add(std::string_view name, const T &value)
Definition parameters.hpp:496
Parameters & add(anch::sql::Parameter parameter)
Parameters(std::initializer_list< anch::sql::Parameter > parameters={})
Definition parameters.hpp:293
Definition parameters.hpp:282
Definition parameters.hpp:268
SQL namespace.
Definition clauses.hpp:28
std::optional< std::string > ParameterValue
Definition parameters.hpp:50
std::vector< anch::sql::ParameterValue > ParameterList
Definition parameters.hpp:300
anch::sql::ParameterValue toParameter(int16_t value)
anch::sql::BoundValue toBound(const T &value)
Definition parameters.hpp:474
std::string arrayLiteral(const anch::sql::ParameterList &elements)
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
Named parameter value ( Parameters item).
Definition parameters.hpp:345
std::optional< anch::sql::ParameterList > list
Definition parameters.hpp:353
Parameter(std::string parameterName, const T &parameterValue)
Definition parameters.hpp:488
anch::sql::ParameterValue value
Definition parameters.hpp:350
std::string name
Definition parameters.hpp:347