relx 0.1.0
A Modern C++23 Type-Safe SQL Query Builder
Loading...
Searching...
No Matches
delete.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "condition.hpp"
5#include "core.hpp"
6#include "operators.hpp"
7#include "value.hpp"
8
9#include <memory>
10#include <optional>
11#include <sstream>
12#include <string>
13#include <tuple>
14#include <utility>
15#include <vector>
16
17namespace relx::query {
18
22template <TableType Table, typename Where = std::nullopt_t>
24public:
25 using table_type = Table;
26 using where_type = Where;
27
31 explicit DeleteQuery(Table table, Where where = std::nullopt)
32 : table_(std::move(table)), where_(std::move(where)) {}
33
36 std::string to_sql() const {
37 std::stringstream ss;
38 ss << "DELETE FROM " << table_.table_name;
39
40 // Add WHERE clause
41 if constexpr (!std::is_same_v<Where, std::nullopt_t>) {
42 if (where_.has_value()) {
43 ss << " WHERE " << where_.value().to_sql();
44 }
45 }
46
47 return ss.str();
48 }
49
52 std::vector<std::string> bind_params() const {
53 std::vector<std::string> params;
54
55 // Collect parameters from WHERE clause
56 if constexpr (!std::is_same_v<Where, std::nullopt_t>) {
57 if (where_.has_value()) {
58 auto where_params = where_.value().bind_params();
59 params.insert(params.end(), where_params.begin(), where_params.end());
60 }
61 }
62
63 return params;
64 }
65
70 template <ConditionExpr Condition>
71 auto where(const Condition& cond) const {
72 return DeleteQuery<Table, std::optional<Condition>>(table_, std::optional<Condition>(cond));
73 }
74
81 template <ColumnType Col, std::ranges::range Range>
82 requires std::convertible_to<std::ranges::range_value_t<Range>, std::string>
83 auto where_in(const Col& column, const Range& values) const {
84 auto col_expr = column_ref(column);
85 auto in_condition = in(col_expr, values);
86 return where(in_condition);
87 }
88
89private:
90 Table table_;
91 Where where_;
92};
93
98template <TableType Table>
99auto delete_from(const Table& table) {
100 return DeleteQuery<Table>(table);
101}
102
103} // namespace relx::query
Base DELETE query builder.
Definition delete.hpp:23
DeleteQuery(Table table, Where where=std::nullopt)
Constructor for the DELETE query builder.
Definition delete.hpp:31
auto where(const Condition &cond) const
Add a WHERE clause to the query.
Definition delete.hpp:71
std::string to_sql() const
Generate the SQL for this DELETE query.
Definition delete.hpp:36
auto where_in(const Col &column, const Range &values) const
Set a condition for filtering the rows to delete using IN with values.
Definition delete.hpp:83
std::vector< std::string > bind_params() const
Get the bind parameters for this DELETE query.
Definition delete.hpp:52
Represents a column in a database table.
Definition column.hpp:217
auto in(const schema::column< TableT, Name, T, Modifiers... > &col, Range values)
Create an IN condition with type checking for columns.
auto column_ref(const Column &col)
Create a column reference expression.
auto delete_from(const Table &table)
Create a DELETE query for the specified table.
Definition delete.hpp:99
STL namespace.