Lightweight 0.20251202.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 /// Constructs a new query builder for the given schema and table.
53 /// @param schema The schema name (e.g., "public", "dbo")
54 /// @param table The table name
55 LIGHTWEIGHT_API SqlQueryBuilder& FromSchemaTable(std::string_view schema, std::string_view table);
56
57 /// Initiates INSERT query building
58 ///
59 /// @param boundInputs Optional vector to store bound inputs.
60 /// If provided, the inputs will be appended to this vector and can be used
61 /// to bind the values to the query via SqlStatement::ExecuteWithVariants(...)
62 LIGHTWEIGHT_API SqlInsertQueryBuilder Insert(std::vector<SqlVariant>* boundInputs = nullptr) noexcept;
63
64 /// Constructs a query to retrieve the last insert ID for the given table.
65 LIGHTWEIGHT_API SqlLastInsertIdQuery LastInsertId();
66
67 /// Initiates SELECT query building.
68 LIGHTWEIGHT_API SqlSelectQueryBuilder Select() noexcept;
69
70 /// Initiates UPDATE query building.
71 ///
72 /// @param boundInputs Optional vector to store bound inputs.
73 /// If provided, the inputs will be appended to this vector and can be used
74 /// to bind the values to the query via SqlStatement::ExecuteWithVariants(...)
75 LIGHTWEIGHT_API SqlUpdateQueryBuilder Update(std::vector<SqlVariant>* boundInputs = nullptr) noexcept;
76
77 /// Initiates DELETE query building.
78 LIGHTWEIGHT_API SqlDeleteQueryBuilder Delete() noexcept;
79
80 /// Initiates query for building database migrations.
81 LIGHTWEIGHT_API SqlMigrationQueryBuilder Migration();
82
83 private:
84 SqlQueryFormatter const& m_formatter;
85 std::string m_schema;
86 std::string m_table;
87 std::string m_tableAlias;
88};
89
90inline LIGHTWEIGHT_FORCE_INLINE SqlQueryBuilder::SqlQueryBuilder(SqlQueryFormatter const& formatter,
91 std::string&& table,
92 std::string&& alias) noexcept:
93 m_formatter { formatter },
94 m_table { std::move(table) },
95 m_tableAlias { std::move(alias) }
96{
97}
98
99} // 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:430
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 & 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: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.