4#include "../SqlQueryFormatter.hpp"
5#include "SQLiteFormatter.hpp"
7#include <reflection-cpp/reflection.hpp>
12class OracleSqlQueryFormatter final:
public SQLiteQueryFormatter
15 [[nodiscard]] std::string QueryLastInsertId(std::string_view tableName)
const override
17 return std::format(
"SELECT \"{}_SEQ\".CURRVAL FROM DUAL;", tableName);
20 [[nodiscard]] std::string_view BooleanLiteral(
bool literalValue)
const noexcept override
22 return literalValue ?
"1" :
"0";
25 [[nodiscard]] std::string SelectFirst(
bool distinct,
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
35 std::stringstream sqlQueryString;
36 sqlQueryString <<
"SELECT";
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;
48 return sqlQueryString.str();
51 [[nodiscard]] std::string SelectRange(
bool distinct,
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,
61 std::size_t limit)
const override
63 assert(!orderBy.empty());
64 std::stringstream sqlQueryString;
65 sqlQueryString <<
"SELECT " << fields;
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();
79 [[nodiscard]] std::string ColumnType(SqlColumnTypeDefinition
const& type)
const override
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);
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);
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); },
113 [[nodiscard]] std::string BuildColumnDefinition(
SqlColumnDeclaration const& column)
const override
115 std::stringstream sqlQueryString;
116 sqlQueryString <<
'"' << column.
name <<
"\" " << ColumnType(column.
type);
119 sqlQueryString <<
" NOT NULL";
121 if (column.
primaryKey == SqlPrimaryKeyType::AUTO_INCREMENT)
122 sqlQueryString <<
" GENERATED ALWAYS AS IDENTITY";
124 sqlQueryString <<
" UNIQUE";
126 if (column.
primaryKey != SqlPrimaryKeyType::NONE)
127 sqlQueryString <<
",\n PRIMARY KEY (\"" << column.
name <<
"\")";
129 return sqlQueryString.str();
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.