Lightweight 0.20251201.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 /// Constructs an SQL INSERT query.
42 ///
43 /// @param intoTable The table to insert into.
44 /// @param fields The fields to insert into.
45 /// @param values The values to insert.
46 ///
47 /// The fields and values must be in the same order.
48 [[nodiscard]] virtual std::string Insert(std::string_view intoTable,
49 std::string_view fields,
50 std::string_view values) const = 0;
51
52 /// Retrieves the last insert ID of the given table.
53 [[nodiscard]] virtual std::string QueryLastInsertId(std::string_view tableName) const = 0;
54
55 /// Constructs an SQL SELECT query for all rows.
56 [[nodiscard]] virtual std::string SelectAll(bool distinct,
57 std::string_view fields,
58 std::string_view fromTable,
59 std::string_view fromTableAlias,
60 std::string_view tableJoins,
61 std::string_view whereCondition,
62 std::string_view orderBy,
63 std::string_view groupBy) const = 0;
64
65 /// Constructs an SQL SELECT query for the first row.
66 [[nodiscard]] virtual std::string SelectFirst(bool distinct,
67 std::string_view fields,
68 std::string_view fromTable,
69 std::string_view fromTableAlias,
70 std::string_view tableJoins,
71 std::string_view whereCondition,
72 std::string_view orderBy,
73 size_t count) const = 0;
74
75 /// Constructs an SQL SELECT query for a range of rows.
76 [[nodiscard]] virtual std::string SelectRange(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,
84 std::size_t offset,
85 std::size_t limit) const = 0;
86
87 /// Constructs an SQL SELECT query retrieve the count of rows matching the given condition.
88 [[nodiscard]] virtual std::string SelectCount(bool distinct,
89 std::string_view fromTable,
90 std::string_view fromTableAlias,
91 std::string_view tableJoins,
92 std::string_view whereCondition) const = 0;
93
94 /// Constructs an SQL UPDATE query.
95 [[nodiscard]] virtual std::string Update(std::string_view table,
96 std::string_view tableAlias,
97 std::string_view setFields,
98 std::string_view whereCondition) const = 0;
99
100 /// Constructs an SQL DELETE query.
101 [[nodiscard]] virtual std::string Delete(std::string_view fromTable,
102 std::string_view fromTableAlias,
103 std::string_view tableJoins,
104 std::string_view whereCondition) const = 0;
105
106 using StringList = std::vector<std::string>;
107
108 /// Convert the given column type definition to the SQL type.
109 [[nodiscard]] virtual std::string ColumnType(SqlColumnTypeDefinition const& type) const = 0;
110
111 /// Constructs an SQL CREATE TABLE query.
112 [[nodiscard]] virtual StringList CreateTable(std::string_view tableName,
113 std::vector<SqlColumnDeclaration> const& columns) const = 0;
114
115 /// Constructs an SQL ALTER TABLE query.
116 [[nodiscard]] virtual StringList AlterTable(std::string_view tableName,
117 std::vector<SqlAlterTableCommand> const& commands) const = 0;
118
119 /// Constructs an SQL DROP TABLE query.
120 [[nodiscard]] virtual StringList DropTable(std::string_view const& tableName) const = 0;
121
122 /// Retrieves the SQL query formatter for SQLite.
123 static SqlQueryFormatter const& Sqlite();
124
125 /// Retrieves the SQL query formatter for Microsoft SQL server.
126 static SqlQueryFormatter const& SqlServer();
127
128 /// Retrieves the SQL query formatter for PostgreSQL.
129 static SqlQueryFormatter const& PostgrSQL();
130
131 /// Retrieves the SQL query formatter for Oracle database.
132 static SqlQueryFormatter const& OracleSQL();
133
134 /// Retrieves the SQL query formatter for the given SqlServerType.
135 static SqlQueryFormatter const* Get(SqlServerType serverType) noexcept;
136};
137
138} // 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.