Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
database::query::join_builder Class Reference

Builds JOIN clauses for SQL queries. More...

#include <join_builder.h>

Collaboration diagram for database::query::join_builder:
Collaboration graph

Public Member Functions

 join_builder ()
 
join_builderjoin (const std::string &table, const std::string &condition, const std::string &alias="")
 Add an INNER JOIN.
 
join_builderleft_join (const std::string &table, const std::string &condition, const std::string &alias="")
 Add a LEFT JOIN.
 
join_builderright_join (const std::string &table, const std::string &condition, const std::string &alias="")
 Add a RIGHT JOIN.
 
join_builderfull_outer_join (const std::string &table, const std::string &condition, const std::string &alias="")
 Add a FULL OUTER JOIN.
 
join_buildercross_join (const std::string &table, const std::string &alias="")
 Add a CROSS JOIN.
 
join_builderadd (const join_spec &spec)
 Add a join with explicit type.
 
std::string build (const value_formatter &formatter) const
 Build the JOIN clauses.
 
bool empty () const
 Check if builder is empty.
 
void clear ()
 Clear all joins.
 
size_t size () const
 Get number of joins.
 

Private Member Functions

std::string join_type_to_string (join_type type) const
 

Private Attributes

std::vector< join_specjoins_
 

Detailed Description

Builds JOIN clauses for SQL queries.

Features:

  • Multiple JOIN types (INNER, LEFT, RIGHT, FULL OUTER, CROSS)
  • Table aliasing
  • Multiple joins chaining

Example:

join_builder builder;
// Simple join
builder.join("orders", "users.id = orders.user_id");
// Left join with alias
builder.left_join("products", "orders.product_id = products.id", "p");
// Build the JOIN clauses
std::string joins = builder.build(fmt);
// Result: "INNER JOIN orders ON users.id = orders.user_id
// LEFT JOIN products AS p ON orders.product_id = products.id"
Builds JOIN clauses for SQL queries.
join_builder & join(const std::string &table, const std::string &condition, const std::string &alias="")
Add an INNER JOIN.
std::string build(const value_formatter &formatter) const
Build the JOIN clauses.
join_builder & left_join(const std::string &table, const std::string &condition, const std::string &alias="")
Add a LEFT JOIN.
Formats database values for different backends.
@ postgres
Indicates a PostgreSQL database.

Thread Safety:

  • NOT thread-safe (stateful builder pattern)
  • Each thread should use separate instance

Definition at line 65 of file join_builder.h.

Constructor & Destructor Documentation

◆ join_builder()

database::query::join_builder::join_builder ( )
default

References add(), build(), clear(), cross_join(), empty(), full_outer_join(), join(), join_builder(), left_join(), right_join(), and size().

Referenced by join_builder().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

◆ add()

join_builder & database::query::join_builder::add ( const join_spec & spec)

Add a join with explicit type.

Parameters
specJoin specification
Returns
Reference to this builder

Definition at line 41 of file join_builder.cpp.

41 {
42 joins_.push_back(spec);
43 return *this;
44}
std::vector< join_spec > joins_

References joins_.

Referenced by join_builder().

Here is the caller graph for this function:

◆ build()

std::string database::query::join_builder::build ( const value_formatter & formatter) const

Build the JOIN clauses.

Parameters
formatterValue formatter for escaping identifiers
Returns
SQL JOIN clauses

Definition at line 46 of file join_builder.cpp.

46 {
47 if (joins_.empty()) {
48 return "";
49 }
50
51 std::ostringstream oss;
52 for (size_t i = 0; i < joins_.size(); ++i) {
53 const auto& join = joins_[i];
54
55 if (i > 0) {
56 oss << " ";
57 }
58
59 oss << join_type_to_string(join.type) << " JOIN ";
60 oss << formatter.escape_identifier(join.table);
61
62 if (!join.alias.empty()) {
63 oss << " AS " << formatter.escape_identifier(join.alias);
64 }
65
66 if (join.type != join_type::cross && !join.condition.empty()) {
67 oss << " ON " << join.condition;
68 }
69 }
70
71 return oss.str();
72}
bool empty() const
Check if builder is empty.
std::string join_type_to_string(join_type type) const

References database::query::cross, empty(), database::query::value_formatter::escape_identifier(), join(), join_type_to_string(), and joins_.

Referenced by join_builder().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ clear()

void database::query::join_builder::clear ( )

Clear all joins.

Definition at line 78 of file join_builder.cpp.

78 {
79 joins_.clear();
80}

References joins_.

Referenced by join_builder().

Here is the caller graph for this function:

◆ cross_join()

join_builder & database::query::join_builder::cross_join ( const std::string & table,
const std::string & alias = "" )

Add a CROSS JOIN.

Parameters
tableTable to join
aliasOptional table alias
Returns
Reference to this builder

Definition at line 36 of file join_builder.cpp.

36 {
37 joins_.push_back({table, "", join_type::cross, alias});
38 return *this;
39}

References database::query::cross, and joins_.

Referenced by join_builder().

Here is the caller graph for this function:

◆ empty()

bool database::query::join_builder::empty ( ) const

Check if builder is empty.

Returns
true if no joins added

Definition at line 74 of file join_builder.cpp.

74 {
75 return joins_.empty();
76}

References joins_.

Referenced by build(), and join_builder().

Here is the caller graph for this function:

◆ full_outer_join()

join_builder & database::query::join_builder::full_outer_join ( const std::string & table,
const std::string & condition,
const std::string & alias = "" )

Add a FULL OUTER JOIN.

Parameters
tableTable to join
conditionON condition
aliasOptional table alias
Returns
Reference to this builder

Definition at line 30 of file join_builder.cpp.

31 {
32 joins_.push_back({table, condition, join_type::full_outer, alias});
33 return *this;
34}
@ full_outer
FULL OUTER JOIN.

References database::query::full_outer, and joins_.

Referenced by join_builder().

Here is the caller graph for this function:

◆ join()

join_builder & database::query::join_builder::join ( const std::string & table,
const std::string & condition,
const std::string & alias = "" )

Add an INNER JOIN.

Parameters
tableTable to join
conditionON condition
aliasOptional table alias
Returns
Reference to this builder

Definition at line 12 of file join_builder.cpp.

13 {
14 joins_.push_back({table, condition, join_type::inner, alias});
15 return *this;
16}

References database::query::inner, and joins_.

Referenced by build(), and join_builder().

Here is the caller graph for this function:

◆ join_type_to_string()

std::string database::query::join_builder::join_type_to_string ( join_type type) const
private

Definition at line 86 of file join_builder.cpp.

86 {
87 switch (type) {
89 return "INNER";
90 case join_type::left:
91 return "LEFT";
93 return "RIGHT";
95 return "FULL OUTER";
97 return "CROSS";
98 default:
99 return "INNER";
100 }
101}
@ right
RIGHT OUTER JOIN.
@ left
LEFT OUTER JOIN.

References database::query::cross, database::query::full_outer, database::query::inner, database::query::left, and database::query::right.

Referenced by build().

Here is the caller graph for this function:

◆ left_join()

join_builder & database::query::join_builder::left_join ( const std::string & table,
const std::string & condition,
const std::string & alias = "" )

Add a LEFT JOIN.

Parameters
tableTable to join
conditionON condition
aliasOptional table alias
Returns
Reference to this builder

Definition at line 18 of file join_builder.cpp.

19 {
20 joins_.push_back({table, condition, join_type::left, alias});
21 return *this;
22}

References joins_, and database::query::left.

Referenced by join_builder().

Here is the caller graph for this function:

◆ right_join()

join_builder & database::query::join_builder::right_join ( const std::string & table,
const std::string & condition,
const std::string & alias = "" )

Add a RIGHT JOIN.

Parameters
tableTable to join
conditionON condition
aliasOptional table alias
Returns
Reference to this builder

Definition at line 24 of file join_builder.cpp.

25 {
26 joins_.push_back({table, condition, join_type::right, alias});
27 return *this;
28}

References joins_, and database::query::right.

Referenced by join_builder().

Here is the caller graph for this function:

◆ size()

size_t database::query::join_builder::size ( ) const

Get number of joins.

Returns
Number of joins added

Definition at line 82 of file join_builder.cpp.

82 {
83 return joins_.size();
84}

References joins_.

Referenced by join_builder().

Here is the caller graph for this function:

Member Data Documentation

◆ joins_

std::vector<join_spec> database::query::join_builder::joins_
private

The documentation for this class was generated from the following files: