relx 0.1.0
A Modern C++23 Type-Safe SQL Query Builder
Loading...
Searching...
No Matches
core.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "../schema/column.hpp"
4#include "../schema/core.hpp"
5#include "../schema/table.hpp"
6
7#include <concepts>
8#include <expected>
9#include <ranges>
10#include <string>
11#include <string_view>
12#include <type_traits>
13#include <utility>
14#include <vector>
15
16namespace relx::query {
17
19struct QueryError {
20 std::string message;
21};
22
24template <typename T>
25using QueryResult = std::expected<T, QueryError>;
26
28template <typename T>
29concept SqlExpr = requires(T t) {
30 { t.to_sql() } -> std::convertible_to<std::string>;
31 { t.bind_params() } -> std::ranges::range;
32};
33
35template <typename T>
37
39template <typename T>
40concept ColumnType = requires(T t) {
41 { T::name } -> std::convertible_to<std::string_view>;
42 typename T::value_type;
43};
44
46template <typename T>
47concept ColumnList = std::ranges::range<T> && requires {
48 typename std::ranges::range_value_t<T>;
50
52template <typename T>
53concept TableList = std::ranges::range<T> && requires { typename std::ranges::range_value_t<T>; } &&
55
57template <typename T>
59
62 virtual ~SqlExpression() = default;
63 virtual std::string to_sql() const = 0;
64 virtual std::vector<std::string> bind_params() const = 0;
65};
66
68enum class JoinType { Inner, Left, Right, Full, Cross };
69
71inline std::string join_type_to_string(JoinType type) {
72 switch (type) {
73 case JoinType::Inner:
74 return "INNER JOIN";
75 case JoinType::Left:
76 return "LEFT JOIN";
77 case JoinType::Right:
78 return "RIGHT JOIN";
79 case JoinType::Full:
80 return "FULL JOIN";
81 case JoinType::Cross:
82 return "CROSS JOIN";
83 default:
84 return "JOIN";
85 }
86}
87
88} // namespace relx::query
Concept for a sequence of column references.
Definition core.hpp:47
Concept for column types.
Definition core.hpp:40
Concept for a condition expression.
Definition core.hpp:58
Concept for SQL expression components.
Definition core.hpp:29
Concept for a sequence of table references.
Definition core.hpp:53
Concept for database table types.
Definition core.hpp:36
Concept for a database table type.
Definition table.hpp:31
std::string join_type_to_string(JoinType type)
Convert a JoinType to its SQL string representation.
Definition core.hpp:71
std::expected< T, QueryError > QueryResult
Type alias for result of query operations.
Definition core.hpp:25
JoinType
Types of JOIN operations.
Definition core.hpp:68
Error type for query operations.
Definition core.hpp:19
std::string message
Definition core.hpp:20
Base class for SQL expressions.
Definition core.hpp:61
virtual std::vector< std::string > bind_params() const =0
virtual std::string to_sql() const =0
virtual ~SqlExpression()=default