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