19namespace orm::db::detail
21[[nodiscard]]
constexpr auto isPortableBindName(std::string_view name)
noexcept ->
bool
28 for (
const auto character : name)
30 const auto asciiLetter = (character >=
'a' and character <=
'z') or (character >=
'A' and character <=
'Z');
31 const auto asciiDigit = character >=
'0' and character <=
'9';
33 if (not asciiLetter and not asciiDigit and character !=
'_')
46inline constexpr auto noSqlNode = std::numeric_limits<std::size_t>::max();
47enum class SqlOperation
69 std::array<std::string_view, 2> pathParts{};
70 std::size_t pathSize{};
72 query::detail::AggregateFunction function{};
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{};
91 query::detail::OrderDirection direction{};
93 std::string_view rawSql{};
98 std::string_view alias{};
103 std::size_t parameter{};
105struct SqlBindingDescriptor
109 std::string_view rawName{};
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{};
125 bool shouldJoin =
true;
128 bool boundPagination{};
129 std::size_t limitParameter{};
130 std::size_t offsetParameter{};
131 std::size_t literalLimit{};
132 std::size_t literalOffset{};
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;
150 bool shouldJoin =
true;
153 bool boundPagination{};
154 std::size_t limitParameter{};
155 std::size_t offsetParameter{};
156 std::size_t literalLimit{};
157 std::size_t literalOffset{};
159 [[nodiscard]]
constexpr auto view() const -> SqlQueryView
161 return {operation, nodes, predicate, having, orders, groups, projections,
162 assignments, includes, bindings, isDistinct, shouldJoin, hasLimit, hasOffset,
163 boundPagination, limitParameter, offsetParameter, literalLimit, literalOffset};
167[[nodiscard]]
constexpr auto decimal(std::size_t value) -> std::string
172 result.push_back(
static_cast<char>(
'0' + value % 10));
174 }
while (value != 0);
175 std::ranges::reverse(result);
178template <
typename... Parts>
179[[nodiscard]]
constexpr auto sqlConcat(
const Parts&... parts) -> std::string
182 (result.append(parts), ...);
185[[nodiscard]]
constexpr auto parameterName(
const SqlBindingDescriptor& descriptor) -> std::string
187 return descriptor.rawName.empty() ? sqlConcat(
"orm_p", decimal(descriptor.index)) : std::string{descriptor.rawName};
189[[nodiscard]]
constexpr auto quoteStandardIdentifier(std::string_view identifier,
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"};
197 throw std::invalid_argument{
"PostgreSQL identifiers must not exceed 63 bytes"};
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"};
202 std::string result(1, quote);
203 for (
const auto character : identifier)
205 if (character == quote)