relx 0.1.0
A Modern C++23 Type-Safe SQL Query Builder
Loading...
Searching...
No Matches
check_constraint.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "core.hpp"
4#include "fixed_string.hpp"
5#include "meta.hpp"
6
7#include <string>
8#include <string_view>
9
10namespace relx::schema {
11
16template <fixed_string Condition, fixed_string Name = "">
18public:
21 constexpr std::string sql_definition() const {
22 std::string result;
23
24 // Add constraint name if provided
25 if constexpr (!Name.empty()) {
26 result = "CONSTRAINT " + std::string(std::string_view(Name)) + " ";
27 }
28
29 // Use the condition as-is
30 result += "CHECK (" + std::string(std::string_view(Condition)) + ")";
31 return result;
32 }
33
36 constexpr std::string_view condition() const { return std::string_view(Condition); }
37
40 constexpr std::string_view name() const { return std::string_view(Name); }
41};
42
48template <auto ColumnPtr, fixed_string Condition, fixed_string Name = "">
50public:
53 constexpr std::string sql_definition() const {
54 std::string result;
55
56 // Add constraint name if provided
57 if constexpr (!Name.empty()) {
58 result = "CONSTRAINT " + std::string(std::string_view(Name)) + " ";
59 }
60
61 // Use the column name and condition
62 using column_type = typename member_pointer_type<decltype(ColumnPtr)>::type;
63
64 // Append the condition, replacing "column" or "COLUMN" with the actual column name if needed
65 std::string condition_str = std::string(std::string_view(Condition));
66 result += "CHECK (" + condition_str + ")";
67
68 return result;
69 }
70
73 constexpr std::string_view condition() const { return std::string_view(Condition); }
74
77 constexpr std::string_view name() const { return std::string_view(Name); }
78
81 std::string_view column_name() const {
82 using column_type = typename member_pointer_type<decltype(ColumnPtr)>::type;
83 return column_type::name;
84 }
85};
86
91template <fixed_string Condition, fixed_string Name>
92constexpr auto named_check() {
94}
95
99template <fixed_string Condition>
100constexpr auto table_check() {
101 return table_check_constraint<Condition, "">();
102}
103
108template <auto ColumnPtr, fixed_string Condition>
109constexpr auto column_check() {
110 return column_check_constraint<ColumnPtr, Condition, "">();
111}
112
118template <auto ColumnPtr, fixed_string Condition, fixed_string Name>
122
123} // namespace relx::schema
Check constraint explicitly associated with a column.
constexpr std::string_view name() const
Get the name as a string_view.
constexpr std::string sql_definition() const
Get SQL definition for the column-specific CHECK constraint.
std::string_view column_name() const
Get the associated column name.
constexpr std::string_view condition() const
Get the condition as a string_view.
Check constraint that accepts a condition string at compile time.
constexpr std::string sql_definition() const
Get SQL definition for the CHECK constraint.
constexpr std::string_view name() const
Get the name as a string_view.
constexpr std::string_view condition() const
Get the condition as a string_view.
constexpr auto table_check()
Helper function to create an unnamed check constraint at compile time.
constexpr auto named_check()
Helper function to create a named check constraint at compile time.
constexpr auto column_check()
Helper function to create a column-bound check constraint at compile time.
constexpr auto named_column_check()
Helper function to create a named column-bound check constraint at compile time.
Compile-time string type that can be used as template non-type parameter in C++20.
Helper to extract the member type from a member pointer.
Definition meta.hpp:28