Lightweight 0.20260921.0
Loading...
Searching...
No Matches
Delete.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "Core.hpp"
6
7#include <string>
8#include <vector>
9
10namespace Lightweight
11{
12
13/// @brief Query builder for building DELETE FROM ... queries.
14///
15/// @ingroup QueryBuilder
16class LIGHTWEIGHT_API SqlDeleteQueryBuilder final: public SqlWhereClauseBuilder<SqlDeleteQueryBuilder>
17{
18 public:
19 /// Constructs a DELETE query builder.
20 ///
21 /// @param formatter Dialect the query is written in.
22 /// @param table Table to delete from.
23 /// @param tableAlias Alias for that table, empty for none.
24 /// @param inputBindings Receives the WHERE values as bound parameters instead of having them
25 /// written into the query text; null keeps them inline. Values are
26 /// appended, never cleared. Execute the result with
27 /// SqlStatement::ExecuteWithVariants(), not ExecuteDirect(), which would
28 /// leave the markers unbound. Both the vector and anything a stored
29 /// std::string_view / std::u16string_view points at must outlive the
30 /// execution, because the binder hands the driver that pointer directly.
31 /// @note An explicit SqlWildcard emits its marker without recording a value here, and a
32 /// WhereIn() set larger than SqlMaxBoundSetSize falls back to inline literals; either
33 /// makes the marker count differ from the vector size.
34 explicit SqlDeleteQueryBuilder(SqlQueryFormatter const& formatter,
35 std::string table,
36 std::string tableAlias,
37 std::vector<SqlVariant>* inputBindings = nullptr) noexcept:
39 m_formatter { formatter }
40 {
41 m_searchCondition.tableName = std::move(table);
42 m_searchCondition.tableAlias = std::move(tableAlias);
43 m_searchCondition.inputBindings = inputBindings;
44 }
45
46 /// Returns the search condition for the query.
47 SqlSearchCondition& SearchCondition() noexcept // NOLINT(bugprone-derived-method-shadowing-base-method)
48 {
49 return m_searchCondition;
50 }
51
52 // clang-format off
53 /// Returns the SQL query formatter.
54 [[nodiscard]] SqlQueryFormatter const& Formatter() const noexcept // NOLINT(bugprone-derived-method-shadowing-base-method)
55 {
56 // clang-format on
57 return m_formatter;
58 }
59
60 /// Finalizes building the query as DELETE FROM ... query.
61 [[nodiscard]] std::string ToSql() const;
62
63 private:
64 SqlQueryFormatter const& m_formatter;
65 SqlSearchCondition m_searchCondition;
66};
67
68inline LIGHTWEIGHT_FORCE_INLINE std::string SqlDeleteQueryBuilder::ToSql() const
69{
70 return m_formatter.Delete(m_searchCondition.tableName,
71 m_searchCondition.tableAlias,
72 m_searchCondition.tableJoins,
73 m_searchCondition.condition);
74}
75
76} // namespace Lightweight
Query builder for building DELETE FROM ... queries.
Definition Delete.hpp:17
SqlSearchCondition & SearchCondition() noexcept
Returns the search condition for the query.
Definition Delete.hpp:47
SqlQueryFormatter const & Formatter() const noexcept
Returns the SQL query formatter.
Definition Delete.hpp:54
SqlDeleteQueryBuilder(SqlQueryFormatter const &formatter, std::string table, std::string tableAlias, std::vector< SqlVariant > *inputBindings=nullptr) noexcept
Definition Delete.hpp:34
std::string ToSql() const
Finalizes building the query as DELETE FROM ... query.
Definition Delete.hpp:68
API to format SQL queries for different SQL dialects.
virtual std::string Delete(std::string_view fromTable, std::string_view fromTableAlias, std::string_view tableJoins, std::string_view whereCondition) const =0
Constructs an SQL DELETE query.
LIGHTWEIGHT_API std::vector< std::string > ToSql(SqlQueryFormatter const &formatter, SqlMigrationPlanElement const &element)