Lightweight 0.20260213.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 /// Default constructor.
23 SqlQueryFormatter() = default;
24 /// Default move constructor.
26 /// Default copy constructor.
28 /// Default move assignment operator.
30 /// Default copy assignment operator.
32 virtual ~SqlQueryFormatter() = default;
33
34 /// Converts a boolean value to a string literal.
35 [[nodiscard]] virtual std::string_view BooleanLiteral(bool value) const noexcept = 0;
36
37 /// Returns the SQL function name used to retrieve the current date.
38 [[nodiscard]] virtual std::string_view DateFunction() const noexcept = 0;
39
40 /// Converts a string value to a string literal.
41 [[nodiscard]] virtual std::string StringLiteral(std::string_view value) const noexcept = 0;
42
43 /// Converts a character value to a string literal.
44 [[nodiscard]] virtual std::string StringLiteral(char value) const noexcept = 0;
45
46 /// Converts a binary value to a hex-encoded string literal.
47 [[nodiscard]] virtual std::string BinaryLiteral(std::span<uint8_t const> data) const = 0;
48
49 /// Formats a qualified table name with proper quoting for this database.
50 /// @param schema The schema name (can be empty for default schema)
51 /// @param table The table name
52 /// @return The properly quoted qualified table name (e.g., "schema"."table" or [schema].[table])
53 [[nodiscard]] virtual std::string QualifiedTableName(std::string_view schema, std::string_view table) const = 0;
54
55 /// Constructs an SQL INSERT query.
56 ///
57 /// @param intoTable The table to insert into.
58 /// @param fields The fields to insert into.
59 /// @param values The values to insert.
60 ///
61 /// The fields and values must be in the same order.
62 [[nodiscard]] virtual std::string Insert(std::string_view intoTable,
63 std::string_view fields,
64 std::string_view values) const = 0;
65
66 /// Constructs an SQL INSERT query with a schema prefix.
67 [[nodiscard]] virtual std::string Insert(std::string_view schema,
68 std::string_view intoTable,
69 std::string_view fields,
70 std::string_view values) const = 0;
71
72 /// Retrieves the last insert ID of the given table.
73 [[nodiscard]] virtual std::string QueryLastInsertId(std::string_view tableName) const = 0;
74
75 /// Constructs an SQL SELECT query for all rows.
76 [[nodiscard]] virtual std::string SelectAll(bool distinct,
77 std::string_view fields,
78 std::string_view fromTable,
79 std::string_view fromTableAlias,
80 std::string_view tableJoins,
81 std::string_view whereCondition,
82 std::string_view orderBy,
83 std::string_view groupBy) const = 0;
84
85 /// Constructs an SQL SELECT query for the first row.
86 [[nodiscard]] virtual std::string SelectFirst(bool distinct,
87 std::string_view fields,
88 std::string_view fromTable,
89 std::string_view fromTableAlias,
90 std::string_view tableJoins,
91 std::string_view whereCondition,
92 std::string_view orderBy,
93 size_t count) const = 0;
94
95 /// Constructs an SQL SELECT query for a range of rows.
96 [[nodiscard]] virtual std::string SelectRange(bool distinct,
97 std::string_view fields,
98 std::string_view fromTable,
99 std::string_view fromTableAlias,
100 std::string_view tableJoins,
101 std::string_view whereCondition,
102 std::string_view orderBy,
103 std::string_view groupBy,
104 std::size_t offset,
105 std::size_t limit) const = 0;
106
107 /// Constructs an SQL SELECT query retrieve the count of rows matching the given condition.
108 [[nodiscard]] virtual std::string SelectCount(bool distinct,
109 std::string_view fromTable,
110 std::string_view fromTableAlias,
111 std::string_view tableJoins,
112 std::string_view whereCondition) const = 0;
113
114 /// Constructs an SQL UPDATE query.
115 [[nodiscard]] virtual std::string Update(std::string_view table,
116 std::string_view tableAlias,
117 std::string_view setFields,
118 std::string_view whereCondition) const = 0;
119
120 /// Constructs an SQL DELETE query.
121 [[nodiscard]] virtual std::string Delete(std::string_view fromTable,
122 std::string_view fromTableAlias,
123 std::string_view tableJoins,
124 std::string_view whereCondition) const = 0;
125
126 /// Alias for a list of SQL statement strings.
127 using StringList = std::vector<std::string>;
128
129 /// Convert the given column type definition to the SQL type.
130 [[nodiscard]] virtual std::string ColumnType(SqlColumnTypeDefinition const& type) const = 0;
131
132 /// Constructs an SQL CREATE TABLE query.
133 ///
134 /// @param schema The schema name of the table to create.
135 /// @param tableName The name of the table to create.
136 /// @param columns The columns of the table.
137 /// @param foreignKeys The foreign key constraints of the table.
138 /// @param ifNotExists If true, generates CREATE TABLE IF NOT EXISTS instead of CREATE TABLE.
139 [[nodiscard]] virtual StringList CreateTable(std::string_view schema,
140 std::string_view tableName,
141 std::vector<SqlColumnDeclaration> const& columns,
142 std::vector<SqlCompositeForeignKeyConstraint> const& foreignKeys,
143 bool ifNotExists = false) const = 0;
144
145 /// Constructs an SQL ALTER TABLE query.
146 [[nodiscard]] virtual StringList AlterTable(std::string_view schema,
147 std::string_view tableName,
148 std::vector<SqlAlterTableCommand> const& commands) const = 0;
149
150 /// Constructs an SQL DROP TABLE query.
151 ///
152 /// @param schema The schema name of the table to drop.
153 /// @param tableName The name of the table to drop.
154 /// @param ifExists If true, generates DROP TABLE IF EXISTS instead of DROP TABLE.
155 /// @param cascade If true, drops all foreign key constraints referencing this table first.
156 [[nodiscard]] virtual StringList DropTable(std::string_view schema,
157 std::string_view const& tableName,
158 bool ifExists = false,
159 bool cascade = false) const = 0;
160
161 /// Returns the SQL query to retrieve the full server version string.
162 ///
163 /// This query returns detailed version information specific to each database:
164 /// - SQL Server: Returns result of SELECT @@VERSION (includes build, edition, OS info)
165 /// - PostgreSQL: Returns result of SELECT version() (includes build info)
166 /// - SQLite: Returns result of SELECT sqlite_version()
167 [[nodiscard]] virtual std::string QueryServerVersion() const = 0;
168
169 /// Retrieves the SQL query formatter for SQLite.
170 static SqlQueryFormatter const& Sqlite();
171
172 /// Retrieves the SQL query formatter for Microsoft SQL server.
173 static SqlQueryFormatter const& SqlServer();
174
175 /// Retrieves the SQL query formatter for PostgreSQL.
176 static SqlQueryFormatter const& PostgrSQL();
177
178 /// Retrieves the SQL query formatter for the given SqlServerType.
179 static SqlQueryFormatter const* Get(SqlServerType serverType) noexcept;
180
181 protected:
182 /// Formats a table name with optional schema prefix.
183 static std::string FormatTableName(std::string_view schema, std::string_view table);
184};
185
186} // namespace Lightweight
API to format SQL queries for different SQL dialects.
SqlQueryFormatter(SqlQueryFormatter const &)=default
Default copy constructor.
SqlQueryFormatter()=default
Default constructor.
SqlQueryFormatter & operator=(SqlQueryFormatter &&)=default
Default move assignment operator.
virtual std::string_view DateFunction() const noexcept=0
Returns the SQL function name used to retrieve the current date.
std::vector< std::string > StringList
Alias for a list of SQL statement strings.
SqlQueryFormatter(SqlQueryFormatter &&)=default
Default move constructor.
SqlQueryFormatter & operator=(SqlQueryFormatter const &)=default
Default copy assignment operator.
virtual std::string_view BooleanLiteral(bool value) const noexcept=0
Converts a boolean value to a string literal.
std::variant< SqlAlterTableCommands::RenameTable, SqlAlterTableCommands::AddColumn, SqlAlterTableCommands::AddColumnIfNotExists, SqlAlterTableCommands::AlterColumn, SqlAlterTableCommands::AddIndex, SqlAlterTableCommands::RenameColumn, SqlAlterTableCommands::DropColumn, SqlAlterTableCommands::DropColumnIfExists, SqlAlterTableCommands::DropIndex, SqlAlterTableCommands::DropIndexIfExists, SqlAlterTableCommands::AddForeignKey, SqlAlterTableCommands::AddCompositeForeignKey, SqlAlterTableCommands::DropForeignKey > SqlAlterTableCommand
Represents a single SQL ALTER TABLE command.
Represents a SQL column declaration.
Represents a composite foreign key constraint.