relx 0.1.0
A Modern C++23 Type-Safe SQL Query Builder
Loading...
Searching...
No Matches
value.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "core.hpp"
4
5#include <optional>
6#include <sstream>
7#include <string>
8#include <vector>
9
10namespace relx::query {
11
14template <typename T>
15class Value : public SqlExpression {
16public:
17 using value_type = T;
18
19 explicit Value(T value) : value_(std::move(value)) {}
20
21 std::string to_sql() const override { return "?"; }
22
23 std::vector<std::string> bind_params() const override {
25 }
26
27 const T& value() const { return value_; }
28
29private:
30 T value_;
31};
32
34template <typename T>
35class Value<std::optional<T>> : public SqlExpression {
36public:
37 using value_type = std::optional<T>;
38
39 explicit Value(std::optional<T> value) : value_(std::move(value)) {}
40
41 std::string to_sql() const override {
42 if (value_.has_value()) {
43 return "?";
44 }
45 return "NULL";
46 }
47
48 std::vector<std::string> bind_params() const override {
49 if (value_.has_value()) {
51 }
52 return {};
53 }
54
55 const std::optional<T>& value() const { return value_; }
56
57private:
58 std::optional<T> value_;
59};
60
62template <>
63class Value<std::string> : public SqlExpression {
64public:
65 using value_type = std::string;
66
67 explicit Value(std::string value) : value_(std::move(value)) {}
68
69 std::string to_sql() const override { return "?"; }
70
71 std::vector<std::string> bind_params() const override { return {value_}; }
72
73 const std::string& value() const { return value_; }
74
75private:
76 std::string value_;
77};
78
80template <>
81class Value<std::string_view> : public SqlExpression {
82public:
83 using value_type = std::string_view;
84
85 explicit Value(std::string_view value) : value_(value) {}
86
87 std::string to_sql() const override { return "?"; }
88
89 std::vector<std::string> bind_params() const override { return {std::string(value_)}; }
90
91 std::string_view value() const { return value_; }
92
93private:
94 std::string_view value_;
95};
96
98template <>
99class Value<const char*> : public SqlExpression {
100public:
101 using value_type = const char*;
102
103 explicit Value(const char* value) : value_(value) {}
104
105 std::string to_sql() const override { return "?"; }
106
107 std::vector<std::string> bind_params() const override { return {std::string(value_)}; }
108
109 const char* value() const { return value_; }
110
111private:
112 const char* value_;
113};
114
119template <typename T>
120auto value(T val) {
121 return Value<T>(std::move(val));
122}
123
127inline auto val(const char* str) {
128 return Value<std::string_view>(str);
129}
130
134inline auto val(std::string str) {
135 return Value<std::string>(std::move(str));
136}
137
143inline auto val(std::string_view sv) {
144 return Value<std::string_view>(sv);
145}
146
150inline auto val(int i) {
151 return Value<int>(i);
152}
153
157inline auto val(long l) {
158 return Value<long>(l);
159}
160
164inline auto val(long long ll) {
165 return Value<long long>(ll);
166}
167
171inline auto val(double d) {
172 return Value<double>(d);
173}
174
178inline auto val(float f) {
179 return Value<float>(f);
180}
181
185inline auto val(bool b) {
186 return Value<bool>(b);
187}
188
193template <typename T>
194auto val(std::optional<T> opt) {
195 return Value<std::optional<T>>(std::move(opt));
196}
197
200inline auto val(std::nullopt_t) {
201 return Value<std::nullopt_t>(std::nullopt);
202}
203
204} // namespace relx::query
std::vector< std::string > bind_params() const override
Definition value.hpp:107
std::string to_sql() const override
Definition value.hpp:105
const char * value() const
Definition value.hpp:109
Value(std::optional< T > value)
Definition value.hpp:39
std::vector< std::string > bind_params() const override
Definition value.hpp:48
std::string to_sql() const override
Definition value.hpp:41
const std::optional< T > & value() const
Definition value.hpp:55
const std::string & value() const
Definition value.hpp:73
Value(std::string value)
Definition value.hpp:67
std::vector< std::string > bind_params() const override
Definition value.hpp:71
std::string to_sql() const override
Definition value.hpp:69
std::vector< std::string > bind_params() const override
Definition value.hpp:89
std::string_view value() const
Definition value.hpp:91
std::string to_sql() const override
Definition value.hpp:87
Value(std::string_view value)
Definition value.hpp:85
Represents a literal value in a SQL query.
Definition value.hpp:15
std::vector< std::string > bind_params() const override
Definition value.hpp:23
Value(T value)
Definition value.hpp:19
const T & value() const
Definition value.hpp:27
std::string to_sql() const override
Definition value.hpp:21
auto value(T val)
Create a value expression.
Definition value.hpp:120
auto val(const char *str)
Helper to create a value expression from a string literal.
Definition value.hpp:127
STL namespace.
Base class for SQL expressions.
Definition core.hpp:61
static std::string to_sql_string(const T &value)
Convert a C++ value to a SQL string representation.