Lightweight 0.20260213.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
14/// @defgroup QueryBuilder Query Builder
15/// @brief Classes and functions for building SQL queries.
16
17struct [[nodiscard]] SqlLastInsertIdQuery
18{
19 std::string tableName;
20 SqlQueryFormatter const* formatter;
21
22 [[nodiscard]] std::string ToSql() const
23 {
24 return formatter->QueryLastInsertId(tableName);
25 }
26};
27
28/// @brief API Entry point for building SQL queries.
29///
30/// @ingroup QueryBuilder
31class [[nodiscard]] SqlQueryBuilder final
32{
33 public:
34 /// Constructs a new query builder for the given table.
35 explicit SqlQueryBuilder(SqlQueryFormatter const& formatter,
36 std::string&& table = {},
37 std::string&& alias = {}) noexcept;
38
39 /// Constructs a new query builder for the given table.
40 LIGHTWEIGHT_API SqlQueryBuilder& FromTable(std::string table);
41
42 /// Constructs a new query builder for the given table.
43 LIGHTWEIGHT_API SqlQueryBuilder& FromTable(std::string_view table);
44
45 /// Constructs a new query builder for the given table.
46 template <size_t N>
47 SqlQueryBuilder& FromTable(char const (&table)[N])
48 {
49 return FromTable(std::string_view { table, N - 1 });
50 }
51
52 /// Constructs a new query builder for the given table with an alias.
53 LIGHTWEIGHT_API SqlQueryBuilder& FromTableAs(std::string table, std::string alias);
54
55 /// Constructs a new query builder for the given schema and table.
56 /// @param schema The schema name (e.g., "public", "dbo")
57 /// @param table The table name
58 LIGHTWEIGHT_API SqlQueryBuilder& FromSchemaTable(std::string_view schema, std::string_view table);
59
60 /// Initiates INSERT query building
61 ///
62 /// @param boundInputs Optional vector to store bound inputs.
63 /// If provided, the inputs will be appended to this vector and can be used
64 /// to bind the values to the query via SqlStatement::ExecuteWithVariants(...)
65 LIGHTWEIGHT_API SqlInsertQueryBuilder Insert(std::vector<SqlVariant>* boundInputs = nullptr) noexcept;
66
67 /// Constructs a query to retrieve the last insert ID for the given table.
68 LIGHTWEIGHT_API SqlLastInsertIdQuery LastInsertId();
69
70 /// Initiates SELECT query building.
71 LIGHTWEIGHT_API SqlSelectQueryBuilder Select() noexcept;
72
73 /// Initiates UPDATE query building.
74 ///
75 /// @param boundInputs Optional vector to store bound inputs.
76 /// If provided, the inputs will be appended to this vector and can be used
77 /// to bind the values to the query via SqlStatement::ExecuteWithVariants(...)
78 LIGHTWEIGHT_API SqlUpdateQueryBuilder Update(std::vector<SqlVariant>* boundInputs = nullptr) noexcept;
79
80 /// Initiates DELETE query building.
81 LIGHTWEIGHT_API SqlDeleteQueryBuilder Delete() noexcept;
82
83 /// Initiates query for building database migrations.
84 LIGHTWEIGHT_API SqlMigrationQueryBuilder Migration();
85
86 private:
87 SqlQueryFormatter const& m_formatter;
88 std::string m_schema;
89 std::string m_table;
90 std::string m_tableAlias;
91};
92
93inline LIGHTWEIGHT_FORCE_INLINE SqlQueryBuilder::SqlQueryBuilder(SqlQueryFormatter const& formatter,
94 std::string&& table,
95 std::string&& alias) noexcept:
96 m_formatter { formatter },
97 m_table { std::move(table) },
98 m_tableAlias { std::move(alias) }
99{
100}
101
102} // 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:436
API Entry point for building SQL queries.
Definition SqlQuery.hpp:32
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 & FromSchemaTable(std::string_view schema, std::string_view table)
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:47
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
LIGHTWEIGHT_API std::vector< std::string > ToSql(SqlQueryFormatter const &formatter, SqlMigrationPlanElement const &element)
Represents a value that can be any of the supported SQL data types.