Lightweight 0.20251202.0
Loading...
Searching...
No Matches
SqlQueryFormatter.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "Api.hpp"
6#include "SqlConnection.hpp"
7#include "SqlQuery/MigrationPlan.hpp"
8#include "SqlServerType.hpp"
9
10#include <string>
11#include <string_view>
12
13namespace Lightweight
14{
15
16struct SqlQualifiedTableColumnName;
17
18/// API to format SQL queries for different SQL dialects.
19class [[nodiscard]] LIGHTWEIGHT_API SqlQueryFormatter
20{
21 public:
22 SqlQueryFormatter() = default;
24 SqlQueryFormatter(SqlQueryFormatter const&) = default;
25 SqlQueryFormatter& operator=(SqlQueryFormatter&&) = default;
26 SqlQueryFormatter& operator=(SqlQueryFormatter const&) = default;
27 virtual ~SqlQueryFormatter() = default;
28
29 /// Converts a boolean value to a string literal.
30 [[nodiscard]] virtual std::string_view BooleanLiteral(bool value) const noexcept = 0;
31
32 // Returns a function to get a date from the database
33 [[nodiscard]] virtual std::string_view DateFunction() const noexcept = 0;
34
35 /// Converts a string value to a string literal.
36 [[nodiscard]] virtual std::string StringLiteral(std::string_view value) const noexcept = 0;
37
38 /// Converts a character value to a string literal.
39 [[nodiscard]] virtual std::string StringLiteral(char value) const noexcept = 0;
40
41 /// Converts a binary value to a hex-encoded string literal.
42 [[nodiscard]] virtual std::string BinaryLiteral(std::span<uint8_t const> data) const = 0;
43
44 /// Constructs an SQL INSERT query.
45 ///
46 /// @param intoTable The table to insert into.
47 /// @param fields The fields to insert into.
48 /// @param values The values to insert.
49 ///
50 /// The fields and values must be in the same order.
51 [[nodiscard]] virtual std::string Insert(std::string_view intoTable,
52 std::string_view fields,
53 std::string_view values) const = 0;
54
55 [[nodiscard]] virtual std::string Insert(std::string_view schema,
56 std::string_view intoTable,
57 std::string_view fields,
58 std::string_view values) const = 0;
59
60 /// Retrieves the last insert ID of the given table.
61 [[nodiscard]] virtual std::string QueryLastInsertId(std::string_view tableName) const = 0;
62
63 /// Constructs an SQL SELECT query for all rows.
64 [[nodiscard]] virtual std::string SelectAll(bool distinct,
65 std::string_view fields,
66 std::string_view fromTable,
67 std::string_view fromTableAlias,
68 std::string_view tableJoins,
69 std::string_view whereCondition,
70 std::string_view orderBy,
71 std::string_view groupBy) const = 0;
72
73 /// Constructs an SQL SELECT query for the first row.
74 [[nodiscard]] virtual std::string SelectFirst(bool distinct,
75 std::string_view fields,
76 std::string_view fromTable,
77 std::string_view fromTableAlias,
78 std::string_view tableJoins,
79 std::string_view whereCondition,
80 std::string_view orderBy,
81 size_t count) const = 0;
82
83 /// Constructs an SQL SELECT query for a range of rows.
84 [[nodiscard]] virtual std::string SelectRange(bool distinct,
85 std::string_view fields,
86 std::string_view fromTable,
87 std::string_view fromTableAlias,
88 std::string_view tableJoins,
89 std::string_view whereCondition,
90 std::string_view orderBy,
91 std::string_view groupBy,
92 std::size_t offset,
93 std::size_t limit) const = 0;
94
95 /// Constructs an SQL SELECT query retrieve the count of rows matching the given condition.
96 [[nodiscard]] virtual std::string SelectCount(bool distinct,
97 std::string_view fromTable,
98 std::string_view fromTableAlias,
99 std::string_view tableJoins,
100 std::string_view whereCondition) const = 0;
101
102 /// Constructs an SQL UPDATE query.
103 [[nodiscard]] virtual std::string Update(std::string_view table,
104 std::string_view tableAlias,
105 std::string_view setFields,
106 std::string_view whereCondition) const = 0;
107
108 /// Constructs an SQL DELETE query.
109 [[nodiscard]] virtual std::string Delete(std::string_view fromTable,
110 std::string_view fromTableAlias,
111 std::string_view tableJoins,
112 std::string_view whereCondition) const = 0;
113
114 using StringList = std::vector<std::string>;
115
116 /// Convert the given column type definition to the SQL type.
117 [[nodiscard]] virtual std::string ColumnType(SqlColumnTypeDefinition const& type) const = 0;
118
119 /// Constructs an SQL CREATE TABLE query.
120 [[nodiscard]] virtual StringList CreateTable(std::string_view schema,
121 std::string_view tableName,
122 std::vector<SqlColumnDeclaration> const& columns,
123 std::vector<SqlCompositeForeignKeyConstraint> const& foreignKeys) const = 0;
124
125 /// Constructs an SQL ALTER TABLE query.
126 [[nodiscard]] virtual StringList AlterTable(std::string_view schema,
127 std::string_view tableName,
128 std::vector<SqlAlterTableCommand> const& commands) const = 0;
129
130 /// Constructs an SQL DROP TABLE query.
131 ///
132 /// @param schema The schema name of the table to drop.
133 /// @param tableName The name of the table to drop.
134 /// @param ifExists If true, generates DROP TABLE IF EXISTS instead of DROP TABLE.
135 /// @param cascade If true, drops all foreign key constraints referencing this table first.
136 [[nodiscard]] virtual StringList DropTable(std::string_view schema,
137 std::string_view const& tableName,
138 bool ifExists = false,
139 bool cascade = false) const = 0;
140
141 /// Returns the SQL query to retrieve the full server version string.
142 ///
143 /// This query returns detailed version information specific to each database:
144 /// - SQL Server: Returns result of SELECT @@VERSION (includes build, edition, OS info)
145 /// - PostgreSQL: Returns result of SELECT version() (includes build info)
146 /// - SQLite: Returns result of SELECT sqlite_version()
147 [[nodiscard]] virtual std::string QueryServerVersion() const = 0;
148
149 /// Retrieves the SQL query formatter for SQLite.
150 static SqlQueryFormatter const& Sqlite();
151
152 /// Retrieves the SQL query formatter for Microsoft SQL server.
153 static SqlQueryFormatter const& SqlServer();
154
155 /// Retrieves the SQL query formatter for PostgreSQL.
156 static SqlQueryFormatter const& PostgrSQL();
157
158 /// Retrieves the SQL query formatter for the given SqlServerType.
159 static SqlQueryFormatter const* Get(SqlServerType serverType) noexcept;
160
161 protected:
162 static std::string FormatTableName(std::string_view schema, std::string_view table);
163};
164
165} // namespace Lightweight
API to format SQL queries for different SQL dialects.
virtual std::string_view BooleanLiteral(bool value) const noexcept=0
Converts a boolean value to a string literal.
Represents a SQL column declaration.
Represents a composite foreign key constraint.