relx 0.1.0
A Modern C++23 Type-Safe SQL Query Builder
Loading...
Searching...
No Matches
schema_adapter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "../schema/column.hpp"
4#include "../schema/table.hpp"
6#include "core.hpp"
7#include "meta.hpp"
8
9#include <string>
10#include <string_view>
11#include <type_traits>
12#include <vector>
13
14namespace relx::query {
15
18template <ColumnType C>
20public:
21 using value_type = typename C::value_type;
22
23 // TODO delete table_name
24 explicit SchemaColumnAdapter(const C& col, std::string_view table_name = "")
25 : col_(col), table_name_(table_name.empty() ? get_parent_table_name() : table_name) {}
26
27 std::string to_sql() const override { return qualified_name(); }
28
29 std::vector<std::string> bind_params() const override { return {}; }
30
31 std::string column_name() const override { return std::string(C::name); }
32
33 std::string table_name() const override { return std::string(table_name_); }
34
35 const C& column() const { return col_; }
36
37private:
38 const C& col_;
39 std::string_view table_name_;
40
41 std::string qualified_name() const override {
42 if (table_name_.empty()) {
43 return std::string(C::name);
44 }
45 return std::string(table_name_) + "." + std::string(C::name);
46 }
47
48 std::string_view get_parent_table_name() const {
49 // Get table name from parent table if available
50 if constexpr (requires { typename C::table_type; }) {
51 using parent_table = typename C::table_type;
52 return parent_table::table_name;
53 } else {
54 return "";
55 }
56 }
57};
58
61template <TableType T>
63public:
64 static constexpr auto table_name = T::table_name;
65
66 explicit SchemaTableAdapter(const T& table) : table_(table) {}
67
69 template <ColumnType C>
70 auto get_column(const C& col) const {
72 }
73
74 const T& schema_table() const { return table_; }
75
76private:
77 const T& table_;
78};
79
84template <ColumnType C>
85auto to_expr(const C& col, std::string_view table_name = "") {
86 return SchemaColumnAdapter<C>(col, table_name);
87}
88
93template <TableType T>
94auto to_table(const T& table) {
95 return SchemaTableAdapter<T>(table);
96}
97
98} // namespace relx::query
Base class for column expressions.
Adapter to convert schema::column to a ColumnRef This allows direct use of schema columns in query ex...
std::string table_name() const override
typename C::value_type value_type
std::vector< std::string > bind_params() const override
std::string column_name() const override
std::string to_sql() const override
SchemaColumnAdapter(const C &col, std::string_view table_name="")
Adapter to convert schema::table to work with query builder This maintains table name and enables col...
auto get_column(const C &col) const
Get a column from this table as a SQL expression.
static constexpr auto table_name
auto to_expr(const C &col, std::string_view table_name="")
Helper to wrap a schema column in a SQL expression.
auto to_table(const T &table)
Helper to wrap a schema table in a table adapter.