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 /// Formats a qualified table name with proper quoting for this database.
45 /// @param schema The schema name (can be empty for default schema)
46 /// @param table The table name
47 /// @return The properly quoted qualified table name (e.g., "schema"."table" or [schema].[table])
48 [[nodiscard]] virtual std::string QualifiedTableName(std::string_view schema, std::string_view table) const = 0;
49
50 /// Constructs an SQL INSERT query.
51 ///
52 /// @param intoTable The table to insert into.
53 /// @param fields The fields to insert into.
54 /// @param values The values to insert.
55 ///
56 /// The fields and values must be in the same order.
57 [[nodiscard]] virtual std::string Insert(std::string_view intoTable,
58 std::string_view fields,
59 std::string_view values) const = 0;
60
61 [[nodiscard]] virtual std::string Insert(std::string_view schema,
62 std::string_view intoTable,
63 std::string_view fields,
64 std::string_view values) const = 0;
65
66 /// Retrieves the last insert ID of the given table.
67 [[nodiscard]] virtual std::string QueryLastInsertId(std::string_view tableName) const = 0;
68
69 /// Constructs an SQL SELECT query for all rows.
70 [[nodiscard]] virtual std::string SelectAll(bool distinct,
71 std::string_view fields,
72 std::string_view fromTable,
73 std::string_view fromTableAlias,
74 std::string_view tableJoins,
75 std::string_view whereCondition,
76 std::string_view orderBy,
77 std::string_view groupBy) const = 0;
78
79 /// Constructs an SQL SELECT query for the first row.
80 [[nodiscard]] virtual std::string SelectFirst(bool distinct,
81 std::string_view fields,
82 std::string_view fromTable,
83 std::string_view fromTableAlias,
84 std::string_view tableJoins,
85 std::string_view whereCondition,
86 std::string_view orderBy,
87 size_t count) const = 0;
88
89 /// Constructs an SQL SELECT query for a range of rows.
90 [[nodiscard]] virtual std::string SelectRange(bool distinct,
91 std::string_view fields,
92 std::string_view fromTable,
93 std::string_view fromTableAlias,
94 std::string_view tableJoins,
95 std::string_view whereCondition,
96 std::string_view orderBy,
97 std::string_view groupBy,
98 std::size_t offset,
99 std::size_t limit) const = 0;
100
101 /// Constructs an SQL SELECT query retrieve the count of rows matching the given condition.
102 [[nodiscard]] virtual std::string SelectCount(bool distinct,
103 std::string_view fromTable,
104 std::string_view fromTableAlias,
105 std::string_view tableJoins,
106 std::string_view whereCondition) const = 0;
107
108 /// Constructs an SQL UPDATE query.
109 [[nodiscard]] virtual std::string Update(std::string_view table,
110 std::string_view tableAlias,
111 std::string_view setFields,
112 std::string_view whereCondition) const = 0;
113
114 /// Constructs an SQL DELETE query.
115 [[nodiscard]] virtual std::string Delete(std::string_view fromTable,
116 std::string_view fromTableAlias,
117 std::string_view tableJoins,
118 std::string_view whereCondition) const = 0;
119
120 using StringList = std::vector<std::string>;
121
122 /// Convert the given column type definition to the SQL type.
123 [[nodiscard]] virtual std::string ColumnType(SqlColumnTypeDefinition const& type) const = 0;
124
125 /// Constructs an SQL CREATE TABLE query.
126 ///
127 /// @param schema The schema name of the table to create.
128 /// @param tableName The name of the table to create.
129 /// @param columns The columns of the table.
130 /// @param foreignKeys The foreign key constraints of the table.
131 /// @param ifNotExists If true, generates CREATE TABLE IF NOT EXISTS instead of CREATE TABLE.
132 [[nodiscard]] virtual StringList CreateTable(std::string_view schema,
133 std::string_view tableName,
134 std::vector<SqlColumnDeclaration> const& columns,
135 std::vector<SqlCompositeForeignKeyConstraint> const& foreignKeys,
136 bool ifNotExists = false) const = 0;
137
138 /// Constructs an SQL ALTER TABLE query.
139 [[nodiscard]] virtual StringList AlterTable(std::string_view schema,
140 std::string_view tableName,
141 std::vector<SqlAlterTableCommand> const& commands) const = 0;
142
143 /// Constructs an SQL DROP TABLE query.
144 ///
145 /// @param schema The schema name of the table to drop.
146 /// @param tableName The name of the table to drop.
147 /// @param ifExists If true, generates DROP TABLE IF EXISTS instead of DROP TABLE.
148 /// @param cascade If true, drops all foreign key constraints referencing this table first.
149 [[nodiscard]] virtual StringList DropTable(std::string_view schema,
150 std::string_view const& tableName,
151 bool ifExists = false,
152 bool cascade = false) const = 0;
153
154 /// Returns the SQL query to retrieve the full server version string.
155 ///
156 /// This query returns detailed version information specific to each database:
157 /// - SQL Server: Returns result of SELECT @@VERSION (includes build, edition, OS info)
158 /// - PostgreSQL: Returns result of SELECT version() (includes build info)
159 /// - SQLite: Returns result of SELECT sqlite_version()
160 [[nodiscard]] virtual std::string QueryServerVersion() const = 0;
161
162 /// Retrieves the SQL query formatter for SQLite.
163 static SqlQueryFormatter const& Sqlite();
164
165 /// Retrieves the SQL query formatter for Microsoft SQL server.
166 static SqlQueryFormatter const& SqlServer();
167
168 /// Retrieves the SQL query formatter for PostgreSQL.
169 static SqlQueryFormatter const& PostgrSQL();
170
171 /// Retrieves the SQL query formatter for the given SqlServerType.
172 static SqlQueryFormatter const* Get(SqlServerType serverType) noexcept;
173
174 protected:
175 static std::string FormatTableName(std::string_view schema, std::string_view table);
176};
177
178} // 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.