Lightweight 0.20250904.0
Loading...
Searching...
No Matches
SqlQuery.hpp
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3
4#include "Api.hpp"
5#include "SqlQuery/Delete.hpp"
6#include "SqlQuery/Insert.hpp"
7#include "SqlQuery/Migrate.hpp"
8#include "SqlQuery/Select.hpp"
9#include "SqlQuery/Update.hpp"
10
11namespace Lightweight
12{
13
14struct [[nodiscard]] SqlLastInsertIdQuery
15{
16 std::string tableName;
17 SqlQueryFormatter const* formatter;
18
19 [[nodiscard]] std::string ToSql() const
20 {
21 return formatter->QueryLastInsertId(tableName);
22 }
23};
24
25/// @brief API Entry point for building SQL queries.
26///
27/// @ingroup QueryBuilder
28class [[nodiscard]] SqlQueryBuilder final
29{
30 public:
31 /// Constructs a new query builder for the given table.
32 explicit SqlQueryBuilder(SqlQueryFormatter const& formatter,
33 std::string&& table = {},
34 std::string&& alias = {}) noexcept;
35
36 /// Constructs a new query builder for the given table.
37 LIGHTWEIGHT_API SqlQueryBuilder& FromTable(std::string table);
38
39 /// Constructs a new query builder for the given table.
40 LIGHTWEIGHT_API SqlQueryBuilder& FromTable(std::string_view table);
41
42 /// Constructs a new query builder for the given table.
43 template <size_t N>
44 SqlQueryBuilder& FromTable(char const (&table)[N])
45 {
46 return FromTable(std::string_view { table, N - 1 });
47 }
48
49 /// Constructs a new query builder for the given table with an alias.
50 LIGHTWEIGHT_API SqlQueryBuilder& FromTableAs(std::string table, std::string alias);
51
52 /// Initiates INSERT query building
53 ///
54 /// @param boundInputs Optional vector to store bound inputs.
55 /// If provided, the inputs will be appended to this vector and can be used
56 /// to bind the values to the query via SqlStatement::ExecuteWithVariants(...)
57 LIGHTWEIGHT_API SqlInsertQueryBuilder Insert(std::vector<SqlVariant>* boundInputs = nullptr) noexcept;
58
59 /// Constructs a query to retrieve the last insert ID for the given table.
60 LIGHTWEIGHT_API SqlLastInsertIdQuery LastInsertId();
61
62 /// Initiates SELECT query building.
63 LIGHTWEIGHT_API SqlSelectQueryBuilder Select() noexcept;
64
65 /// Initiates UPDATE query building.
66 ///
67 /// @param boundInputs Optional vector to store bound inputs.
68 /// If provided, the inputs will be appended to this vector and can be used
69 /// to bind the values to the query via SqlStatement::ExecuteWithVariants(...)
70 LIGHTWEIGHT_API SqlUpdateQueryBuilder Update(std::vector<SqlVariant>* boundInputs = nullptr) noexcept;
71
72 /// Initiates DELETE query building.
73 LIGHTWEIGHT_API SqlDeleteQueryBuilder Delete() noexcept;
74
75 /// Initiates query for building database migrations.
76 LIGHTWEIGHT_API SqlMigrationQueryBuilder Migration();
77
78 private:
79 SqlQueryFormatter const& m_formatter;
80 std::string m_table;
81 std::string m_tableAlias;
82};
83
84inline LIGHTWEIGHT_FORCE_INLINE SqlQueryBuilder::SqlQueryBuilder(SqlQueryFormatter const& formatter,
85 std::string&& table,
86 std::string&& alias) noexcept:
87 m_formatter { formatter },
88 m_table { std::move(table) },
89 m_tableAlias { std::move(alias) }
90{
91}
92
93} // namespace Lightweight
Query builder for building DELETE FROM ... queries.
Definition Delete.hpp:16
Query builder for building INSERT INTO ... queries.
Definition Insert.hpp:20
Query builder for building SQL migration queries.
Definition Migrate.hpp:185
API Entry point for building SQL queries.
Definition SqlQuery.hpp:29
LIGHTWEIGHT_API SqlQueryBuilder & FromTableAs(std::string table, std::string alias)
Constructs a new query builder for the given table with an alias.
LIGHTWEIGHT_API SqlQueryBuilder & FromTable(std::string table)
Constructs a new query builder for the given table.
LIGHTWEIGHT_API SqlInsertQueryBuilder Insert(std::vector< SqlVariant > *boundInputs=nullptr) noexcept
SqlQueryBuilder & FromTable(char const (&table)[N])
Constructs a new query builder for the given table.
Definition SqlQuery.hpp:44
LIGHTWEIGHT_API SqlQueryBuilder & FromTable(std::string_view table)
Constructs a new query builder for the given table.
API to format SQL queries for different SQL dialects.
Query builder for building SELECT ... queries.
Definition Select.hpp:84
Represents a value that can be any of the supported SQL data types.