Lightweight 0.20250627.0
Loading...
Searching...
No Matches
OracleFormatter.hpp
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3
4#include "../SqlQueryFormatter.hpp"
5#include "SQLiteFormatter.hpp"
6
7#include <reflection-cpp/reflection.hpp>
8
9#include <cassert>
10#include <format>
11
12class OracleSqlQueryFormatter final: public SQLiteQueryFormatter
13{
14 public:
15 [[nodiscard]] std::string QueryLastInsertId(std::string_view tableName) const override
16 {
17 return std::format("SELECT \"{}_SEQ\".CURRVAL FROM DUAL;", tableName);
18 }
19
20 [[nodiscard]] std::string_view BooleanLiteral(bool literalValue) const noexcept override
21 {
22 return literalValue ? "1" : "0";
23 }
24
25 [[nodiscard]] std::string SelectFirst(bool distinct,
26 // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
27 std::string_view fields,
28 std::string_view fromTable,
29 std::string_view fromTableAlias,
30 std::string_view tableJoins,
31 std::string_view whereCondition,
32 std::string_view orderBy,
33 size_t count) const override
34 {
35 std::stringstream sqlQueryString;
36 sqlQueryString << "SELECT";
37 if (distinct)
38 sqlQueryString << " DISTINCT";
39 sqlQueryString << " TOP " << count;
40 sqlQueryString << ' ' << fields;
41 sqlQueryString << " FROM \"" << fromTable << '"';
42 if (!fromTableAlias.empty())
43 sqlQueryString << " AS \"" << fromTableAlias << '"';
44 sqlQueryString << tableJoins;
45 sqlQueryString << whereCondition;
46 sqlQueryString << orderBy;
47 ;
48 return sqlQueryString.str();
49 }
50
51 [[nodiscard]] std::string SelectRange(bool distinct,
52 // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
53 std::string_view fields,
54 std::string_view fromTable,
55 std::string_view fromTableAlias,
56 std::string_view tableJoins,
57 std::string_view whereCondition,
58 std::string_view orderBy,
59 std::string_view groupBy,
60 std::size_t offset,
61 std::size_t limit) const override
62 {
63 assert(!orderBy.empty());
64 std::stringstream sqlQueryString;
65 sqlQueryString << "SELECT " << fields;
66 if (distinct)
67 sqlQueryString << " DISTINCT";
68 sqlQueryString << " FROM \"" << fromTable << "\"";
69 if (!fromTableAlias.empty())
70 sqlQueryString << " AS \"" << fromTableAlias << "\"";
71 sqlQueryString << tableJoins;
72 sqlQueryString << whereCondition;
73 sqlQueryString << groupBy;
74 sqlQueryString << orderBy;
75 sqlQueryString << " OFFSET " << offset << " ROWS FETCH NEXT " << limit << " ROWS ONLY";
76 return sqlQueryString.str();
77 }
78
79 [[nodiscard]] std::string ColumnType(SqlColumnTypeDefinition const& type) const override
80 {
81 using namespace SqlColumnTypeDefinitions;
82 return std::visit(detail::overloaded {
83 [](Bigint const&) -> std::string { return "NUMBER(21, 0)"; },
84 [](Binary const&) -> std::string { return "BLOB"; },
85 [](Bool const&) -> std::string { return "BIT"; },
86 [](Char const& type) -> std::string { return std::format("CHAR({})", type.size); },
87 [](Date const&) -> std::string { return "DATE"; },
88 [](DateTime const&) -> std::string { return "TIMESTAMP"; },
89 [](Decimal const& type) -> std::string {
90 return std::format("DECIMAL({}, {})", type.precision, type.scale);
91 },
92 [](Guid const&) -> std::string { return "RAW(16)"; },
93 [](Integer const&) -> std::string { return "INTEGER"; },
94 [](NChar const& type) -> std::string { return std::format("NCHAR({})", type.size); },
95 [](NVarchar const& type) -> std::string { return std::format("NVARCHAR2({})", type.size); },
96 [](Real const&) -> std::string { return "REAL"; },
97 [](Smallint const&) -> std::string { return "SMALLINT"; },
98 [](Text const& type) -> std::string {
99 if (type.size <= SqlOptimalMaxColumnSize)
100 return std::format("VARCHAR2({})", type.size);
101 else
102 return "CLOB";
103 },
104 [](Time const&) -> std::string { return "TIMESTAMP"; },
105 [](Timestamp const&) -> std::string { return "TIMESTAMP"; },
106 [](Tinyint const&) -> std::string { return "TINYINT"; },
107 [](VarBinary const& type) -> std::string { return std::format("VARBINARY({})", type.size); },
108 [](Varchar const& type) -> std::string { return std::format("VARCHAR({})", type.size); },
109 },
110 type);
111 }
112
113 [[nodiscard]] std::string BuildColumnDefinition(SqlColumnDeclaration const& column) const override
114 {
115 std::stringstream sqlQueryString;
116 sqlQueryString << '"' << column.name << "\" " << ColumnType(column.type);
117
118 if (column.required && column.primaryKey != SqlPrimaryKeyType::AUTO_INCREMENT)
119 sqlQueryString << " NOT NULL";
120
121 if (column.primaryKey == SqlPrimaryKeyType::AUTO_INCREMENT)
122 sqlQueryString << " GENERATED ALWAYS AS IDENTITY";
123 else if (column.primaryKey == SqlPrimaryKeyType::NONE && !column.index && column.unique)
124 sqlQueryString << " UNIQUE";
125
126 if (column.primaryKey != SqlPrimaryKeyType::NONE)
127 sqlQueryString << ",\n PRIMARY KEY (\"" << column.name << "\")";
128
129 return sqlQueryString.str();
130 }
131};
Represents a SQL column declaration.
bool index
Indicates if the column is indexed.
SqlColumnTypeDefinition type
The type of the column.
bool required
Indicates if the column is required (non-nullable).
SqlPrimaryKeyType primaryKey
The primary key type of the column.
bool unique
Indicates if the column is unique.
std::string name
The name of the column.