IronQuery
A SQL-first query builder for C++
Loading...
Searching...
No Matches
join.hpp
1#pragma once
2
3#include <initializer_list>
4#include <iron_query/condition.hpp>
5#include <iron_query/virtual_table.hpp>
6#include <string>
7#include <string_view>
8
9namespace iron_query {
10
13struct [[nodiscard]] JoinKind {
15 virtual std::string_view ToString() const = 0;
16
19 virtual bool IsNatural() const { return false; }
20};
21
23struct [[nodiscard]] Inner final : JoinKind {
24 std::string_view ToString() const override;
25};
27struct [[nodiscard]] Cross final : JoinKind {
28 std::string_view ToString() const override;
29};
31struct [[nodiscard]] LeftOuter final : JoinKind {
32 std::string_view ToString() const override;
33};
35struct [[nodiscard]] RightOuter final : JoinKind {
36 std::string_view ToString() const override;
37};
39struct [[nodiscard]] FullOuter final : JoinKind {
40 std::string_view ToString() const override;
41};
43struct [[nodiscard]] NaturalInner final : JoinKind {
44 std::string_view ToString() const override;
45 bool IsNatural() const override { return true; }
46};
48struct [[nodiscard]] NaturalLeftOuter final : JoinKind {
49 std::string_view ToString() const override;
50 bool IsNatural() const override { return true; }
51};
53struct [[nodiscard]] NaturalRightOuter final : JoinKind {
54 std::string_view ToString() const override;
55 bool IsNatural() const override { return true; }
56};
58struct [[nodiscard]] NaturalFullOuter final : JoinKind {
59 std::string_view ToString() const override;
60 bool IsNatural() const override { return true; }
61};
62
66class [[nodiscard]] Join final : public VirtualTable {
67public:
69 Join(const VirtualTable &a, const VirtualTable &b, const JoinKind &kind);
70
76 Join On(Condition exp) &&;
77
83 Join Using(std::initializer_list<Expr> columns) &&;
84
85 std::string ToString() const override;
86
89 std::string ToStringAsFromItem() const override;
90
91private:
92 std::string a_, b_;
93 std::string kind_;
94 bool natural_;
95 std::string on_;
96 std::string using_;
97};
98
99} // namespace iron_query
A boolean-valued SQL predicate, e.g. the result of a comparison, LIKE/IN/BETWEEN/IS [NOT] NULL,...
Definition condition.hpp:25
Transitional representation for JOIN query.
Definition join.hpp:66
std::string ToStringAsFromItem() const override
Returns the join unbracketed: PostgreSQL only allows parentheses around a FROM-item join when an alia...
Join(const VirtualTable &a, const VirtualTable &b, const JoinKind &kind)
Builds a kind JOIN b.
Join On(Condition exp) &&
Sets the ON clause.
std::string ToString() const override
Renders this table value as single-line SQL text. See also ToStringFormatted for a multi-line,...
Join Using(std::initializer_list< Expr > columns) &&
Sets the USING (cols) clause: joins by equating same-named columns from each side.
Any table value, including physical table, table result, materialized view, etc.
Definition virtual_table.hpp:14
CROSS JOIN.
Definition join.hpp:27
std::string_view ToString() const override
The SQL keyword(s) for this join kind, e.g. "INNER".
FULL OUTER JOIN.
Definition join.hpp:39
std::string_view ToString() const override
The SQL keyword(s) for this join kind, e.g. "INNER".
INNER JOIN.
Definition join.hpp:23
std::string_view ToString() const override
The SQL keyword(s) for this join kind, e.g. "INNER".
Kind of SQL JOIN, e.g. Inner or LeftOuter.
Definition join.hpp:13
virtual std::string_view ToString() const =0
The SQL keyword(s) for this join kind, e.g. "INNER".
virtual bool IsNatural() const
Whether this join kind is a NATURAL join, which forbids an explicit ON/USING qualification.
Definition join.hpp:19
LEFT OUTER JOIN.
Definition join.hpp:31
std::string_view ToString() const override
The SQL keyword(s) for this join kind, e.g. "INNER".
NATURAL FULL OUTER JOIN.
Definition join.hpp:58
bool IsNatural() const override
Whether this join kind is a NATURAL join, which forbids an explicit ON/USING qualification.
Definition join.hpp:60
std::string_view ToString() const override
The SQL keyword(s) for this join kind, e.g. "INNER".
NATURAL INNER JOIN: joins on all identically named columns.
Definition join.hpp:43
bool IsNatural() const override
Whether this join kind is a NATURAL join, which forbids an explicit ON/USING qualification.
Definition join.hpp:45
std::string_view ToString() const override
The SQL keyword(s) for this join kind, e.g. "INNER".
NATURAL LEFT OUTER JOIN.
Definition join.hpp:48
bool IsNatural() const override
Whether this join kind is a NATURAL join, which forbids an explicit ON/USING qualification.
Definition join.hpp:50
std::string_view ToString() const override
The SQL keyword(s) for this join kind, e.g. "INNER".
NATURAL RIGHT OUTER JOIN.
Definition join.hpp:53
std::string_view ToString() const override
The SQL keyword(s) for this join kind, e.g. "INNER".
bool IsNatural() const override
Whether this join kind is a NATURAL join, which forbids an explicit ON/USING qualification.
Definition join.hpp:55
RIGHT OUTER JOIN.
Definition join.hpp:35
std::string_view ToString() const override
The SQL keyword(s) for this join kind, e.g. "INNER".