Files
Jason Larabie 346e2b2514 Add C++ query builder (#4664)
# Description of Changes
- Added a query builder for C++ module bindings
  - Added query-builder table/filter/join types
- Added semijoin support with compile-time checks for lookup-table and
indexed-field usage
  - Added support for returning query-builder queries from C++ views
- Hooked query-builder metadata into the C++ table/view macros and V10
module-def path
- Added test coverage for the new C++ query-builder behavior
  - Compile tests for pass/fail cases
  - SQL tests for generated query output
  - Added a C++ test module for view primary key coverage
- **Update:** Switched the core to pass the columns and index-columns
metadata with the table source for better client-side codegen to have
some shared code between server + client.
# API and ABI breaking changes
- No intended API or ABI breaking changes
- Adds a new public query-builder API to the C++ bindings
- C++ views can now return query-builder query types in addition to
materialized row results

# Expected complexity level and risk

3 - Mostly contained to C++ bindings, but it touches macros, view
registration/serialization, and module-def generation, so there are a
few places where the pieces need to stay in sync.

# Testing

I've done end to end testing of I think every type as well as built some
tests to confirm the SQL output.

- [x] Run the C++ query-builder SQL tests
[crates/bindings-cpp/tests/query-builder-compile/run_query_builder_compile_tests.sh]
- [x] Smoke test a generated C++ module using query-builder views

---------

Signed-off-by: Jason Larabie <jason@clockworklabs.io>
Co-authored-by: Ryan <r.ekhoff@clockworklabs.io>
Co-authored-by: Zeke Foppa <196249+bfops@users.noreply.github.com>
2026-06-12 13:02:36 +00:00

99 lines
2.8 KiB
C++

#pragma once
#include "spacetimedb/query_builder/expr.h"
#include "spacetimedb/query_builder/join.h"
#include "spacetimedb/query_builder/table.h"
#include <optional>
#include <type_traits>
#include <utility>
#include <vector>
namespace SpacetimeDB {
template<typename TRow>
using Query = query_builder::RawQuery<TRow>;
namespace detail {
template<typename TRow>
struct NamedQuerySourceTag {
using type = TRow;
const char* __table_name_internal;
};
struct NotAQuerySourceTag {};
template<typename T>
struct query_source_row_type {};
template<typename T>
concept HasQuerySourceRowType = requires {
typename query_source_row_type<std::remove_cvref_t<T>>::type;
};
template<typename T>
using query_source_row_type_t = typename query_source_row_type<std::remove_cvref_t<T>>::type;
template<typename T>
requires query_builder::QueryBuilderReturn<T>
struct query_source_row_type<T> {
using type = query_builder::query_row_type_t<T>;
};
template<typename TRow>
struct query_source_row_type<std::vector<TRow>> {
using type = TRow;
};
template<typename TRow>
struct query_source_row_type<std::optional<TRow>> {
using type = TRow;
};
template<typename TReturn>
constexpr auto MakeQuerySourceTag(const char* source_name) {
if constexpr (HasQuerySourceRowType<TReturn>) {
return NamedQuerySourceTag<query_source_row_type_t<TReturn>>{source_name};
} else {
return NotAQuerySourceTag{};
}
}
template<typename TSourceTag>
constexpr const char* GetQuerySourceName(const TSourceTag& tag) {
return tag.__table_name_internal;
}
} // namespace detail
class QueryBuilder {
public:
template<typename TRow, typename TCols, typename TIxCols>
[[nodiscard]] constexpr query_builder::Table<TRow, TCols, TIxCols> table(const char* table_name, TCols cols, TIxCols ix_cols) const {
return query_builder::Table<TRow, TCols, TIxCols>(table_name, std::move(cols), std::move(ix_cols));
}
template<typename TTableTag>
[[nodiscard]] constexpr auto table(TTableTag tag) const
-> query_builder::Table<
typename std::remove_cvref_t<TTableTag>::type,
decltype(query_builder::HasCols<typename std::remove_cvref_t<TTableTag>::type>::get(std::declval<const char*>())),
decltype(query_builder::HasIxCols<typename std::remove_cvref_t<TTableTag>::type>::get(std::declval<const char*>()))> {
using TRow = typename std::remove_cvref_t<TTableTag>::type;
const char* table_name = detail::GetQuerySourceName(tag);
return table<TRow>(
table_name,
query_builder::HasCols<TRow>::get(table_name),
query_builder::HasIxCols<TRow>::get(table_name));
}
template<typename TTableTag>
[[nodiscard]] constexpr auto operator[](TTableTag tag) const
-> decltype(table(tag)) {
return table(tag);
}
};
} // namespace SpacetimeDB