ORM C++
Loading...
Searching...
No Matches
program.cppm
Go to the documentation of this file.
1module;
2
3#include <algorithm>
4#include <array>
5#include <cstddef>
6#include <limits>
7#include <span>
8#include <stdexcept>
9#include <string>
10#include <string_view>
11#include <vector>
12
13export module orm:sql_program;
14
15import :foundation;
16import :model;
17import :expressions;
18
19namespace orm::db::detail
20{
21[[nodiscard]] constexpr auto isPortableBindName(std::string_view name) noexcept -> bool
22{
23 if (name.empty())
24 {
25 return false;
26 }
27
28 for (const auto character : name)
29 {
30 const auto asciiLetter = (character >= 'a' and character <= 'z') or (character >= 'A' and character <= 'Z');
31 const auto asciiDigit = character >= '0' and character <= '9';
32
33 if (not asciiLetter and not asciiDigit and character != '_')
34 {
35 return false;
36 }
37 }
38
39 return true;
40}
41} // namespace orm::db::detail
42namespace orm::db
43{
44namespace detail
45{
46inline constexpr auto noSqlNode = std::numeric_limits<std::size_t>::max();
47enum class SqlOperation
48{
49 Select,
50 Update,
51 Remove
52};
53enum class SqlNodeKind
54{
55 Comparison,
56 Null,
57 List,
58 Between,
59 And,
60 Or,
61 Not,
62 Collection,
63 Raw,
64 Invalid
65};
66
67struct SqlSource
68{
69 std::array<std::string_view, 2> pathParts{};
70 std::size_t pathSize{};
71 bool isAggregate{};
72 query::detail::AggregateFunction function{};
73};
74
75struct SqlNode
76{
77 SqlNodeKind kind{};
78 SqlSource source{};
79 unsigned operation{};
80 std::size_t left = noSqlNode;
81 std::size_t right = noSqlNode;
82 std::size_t firstParameter{};
83 std::size_t parameterCount{};
84 std::string_view relation{};
85 std::string_view rawSql{};
86};
87
88struct SqlOrder
89{
90 SqlSource source{};
91 query::detail::OrderDirection direction{};
92 bool isRaw{};
93 std::string_view rawSql{};
94};
95struct SqlProjection
96{
97 SqlSource source{};
98 std::string_view alias{};
99};
100struct SqlAssignment
101{
102 SqlSource source{};
103 std::size_t parameter{};
104};
105struct SqlBindingDescriptor
106{
107 model::ColumnType logicalType{};
108 std::size_t index{};
109 std::string_view rawName{};
110};
111
112struct SqlQueryView
113{
114 SqlOperation operation{};
115 std::span<const SqlNode> nodes{};
116 std::size_t predicate = noSqlNode;
117 std::size_t having = noSqlNode;
118 std::span<const SqlOrder> orders{};
119 std::span<const SqlSource> groups{};
120 std::span<const SqlProjection> projections{};
121 std::span<const SqlAssignment> assignments{};
122 std::span<const std::string_view> includes{};
123 std::span<const SqlBindingDescriptor> bindings{};
124 bool isDistinct{};
125 bool shouldJoin = true;
126 bool hasLimit{};
127 bool hasOffset{};
128 bool boundPagination{};
129 std::size_t limitParameter{};
130 std::size_t offsetParameter{};
131 std::size_t literalLimit{};
132 std::size_t literalOffset{};
133};
134
135// Temporary storage is also valid during C++20 constant evaluation. The
136// compiled statement copies it into exactly sized arrays before returning.
137struct SqlProgram
138{
139 SqlOperation operation{};
140 std::vector<SqlNode> nodes;
141 std::size_t predicate = noSqlNode;
142 std::size_t having = noSqlNode;
143 std::vector<SqlOrder> orders;
144 std::vector<SqlSource> groups;
145 std::vector<SqlProjection> projections;
146 std::vector<SqlAssignment> assignments;
147 std::vector<std::string_view> includes;
148 std::vector<SqlBindingDescriptor> bindings;
149 bool isDistinct{};
150 bool shouldJoin = true;
151 bool hasLimit{};
152 bool hasOffset{};
153 bool boundPagination{};
154 std::size_t limitParameter{};
155 std::size_t offsetParameter{};
156 std::size_t literalLimit{};
157 std::size_t literalOffset{};
158
159 [[nodiscard]] constexpr auto view() const -> SqlQueryView
160 {
161 return {operation, nodes, predicate, having, orders, groups, projections,
162 assignments, includes, bindings, isDistinct, shouldJoin, hasLimit, hasOffset,
163 boundPagination, limitParameter, offsetParameter, literalLimit, literalOffset};
164 }
165};
166
167[[nodiscard]] constexpr auto decimal(std::size_t value) -> std::string
168{
169 std::string result;
170 do
171 {
172 result.push_back(static_cast<char>('0' + value % 10));
173 value /= 10;
174 } while (value != 0);
175 std::ranges::reverse(result);
176 return result;
177}
178template <typename... Parts>
179[[nodiscard]] constexpr auto sqlConcat(const Parts&... parts) -> std::string
180{
181 std::string result;
182 (result.append(parts), ...);
183 return result;
184}
185[[nodiscard]] constexpr auto parameterName(const SqlBindingDescriptor& descriptor) -> std::string
186{
187 return descriptor.rawName.empty() ? sqlConcat("orm_p", decimal(descriptor.index)) : std::string{descriptor.rawName};
188}
189[[nodiscard]] constexpr auto quoteStandardIdentifier(std::string_view identifier,
190 CompiledSqlFlavor flavor) -> std::string
191{
192 if (identifier.empty())
193 throw std::invalid_argument{"SQL identifier cannot be empty"};
194 if (identifier.find('\0') != std::string_view::npos)
195 throw std::invalid_argument{"SQL identifiers must not contain an embedded NUL byte"};
196 if (flavor == CompiledSqlFlavor::PostgreSQL && identifier.size() > 63)
197 throw std::invalid_argument{"PostgreSQL identifiers must not exceed 63 bytes"};
198 if (flavor == CompiledSqlFlavor::MySQLFamily &&
199 std::ranges::count_if(identifier, [](unsigned char c) { return (c & 0xc0) != 0x80; }) > 64)
200 throw std::invalid_argument{"MySQL-family identifiers must not exceed 64 characters"};
201 const auto quote = flavor == CompiledSqlFlavor::MySQLFamily ? '`' : '"';
202 std::string result(1, quote);
203 for (const auto character : identifier)
204 {
205 if (character == quote)
206 result += quote;
207 result += character;
208 }
209 result += quote;
210 return result;
211}
212} // namespace detail
213} // namespace orm::db
CompiledSqlFlavor
Definition types.cppm:83