relx 0.1.0
A Modern C++23 Type-Safe SQL Query Builder
Loading...
Searching...
No Matches
unique_constraint.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "core.hpp"
4#include "meta.hpp"
5
6#include <string_view>
7#include <type_traits>
8
9namespace relx::schema {
10
13template <auto ColumnPtr>
15public:
18 std::string sql_definition() const {
19 // Extract the column name from the pointer
20 using column_type = typename member_pointer_type<decltype(ColumnPtr)>::type;
21
22 return "UNIQUE (" + std::string(column_type::name) + ")";
23 }
24};
25
28template <auto... ColumnPtrs>
30public:
33
36 std::string sql_definition() const {
37 std::string result = "UNIQUE (";
38 result += get_column_names();
39 result += ")";
40 return result;
41 }
42
43private:
44 // Helper to get comma-separated column names
45 std::string get_column_names() const {
46 // Using fold expression to concatenate column names
47 std::string names;
48 (append_column_name<ColumnPtrs>(names, names.empty() ? "" : ", "), ...);
49 return names;
50 }
51
52 // Helper to append a single column name
53 template <auto ColumnPtr>
54 void append_column_name(std::string& names, const std::string& separator) const {
55 using column_type = typename member_pointer_type<decltype(ColumnPtr)>::type;
56 names += separator + std::string(column_type::name);
57 }
58};
59
60} // namespace relx::schema
Represents a composite UNIQUE constraint on multiple columns.
composite_unique_constraint()=default
Default constructor.
std::string sql_definition() const
Get SQL definition for the composite UNIQUE constraint.
Represents a UNIQUE constraint on a table.
std::string sql_definition() const
Get SQL definition for the UNIQUE constraint.
Helper to extract the member type from a member pointer.
Definition meta.hpp:28