mirror of
https://github.com/clockworklabs/SpacetimeDB.git
synced 2026-06-28 08:49:38 -04:00
346e2b2514
# 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>
103 lines
3.0 KiB
C++
103 lines
3.0 KiB
C++
#include <spacetimedb.h>
|
|
|
|
using namespace SpacetimeDB;
|
|
|
|
template<typename TRow>
|
|
auto TableFor(const char* table_name) {
|
|
return QueryBuilder{}.table<TRow>(
|
|
table_name,
|
|
query_builder::HasCols<TRow>::get(table_name),
|
|
query_builder::HasIxCols<TRow>::get(table_name));
|
|
}
|
|
|
|
struct User {
|
|
Identity identity;
|
|
bool online;
|
|
};
|
|
SPACETIMEDB_STRUCT(User, identity, online)
|
|
SPACETIMEDB_TABLE(User, user, Public)
|
|
FIELD_PrimaryKey(user, identity)
|
|
|
|
struct UserMembership {
|
|
uint64_t id;
|
|
Identity user_identity;
|
|
};
|
|
SPACETIMEDB_STRUCT(UserMembership, id, user_identity)
|
|
SPACETIMEDB_TABLE(UserMembership, user_membership, Public)
|
|
FIELD_PrimaryKey(user_membership, id)
|
|
FIELD_Index(user_membership, user_identity)
|
|
|
|
struct AutoIncUser {
|
|
uint64_t id;
|
|
bool online;
|
|
};
|
|
SPACETIMEDB_STRUCT(AutoIncUser, id, online)
|
|
SPACETIMEDB_TABLE(AutoIncUser, auto_inc_user, Public)
|
|
FIELD_PrimaryKeyAutoInc(auto_inc_user, id)
|
|
|
|
struct AutoIncMembership {
|
|
uint64_t id;
|
|
uint64_t auto_inc_user_id;
|
|
};
|
|
SPACETIMEDB_STRUCT(AutoIncMembership, id, auto_inc_user_id)
|
|
SPACETIMEDB_TABLE(AutoIncMembership, auto_inc_membership, Public)
|
|
FIELD_PrimaryKey(auto_inc_membership, id)
|
|
FIELD_Index(auto_inc_membership, auto_inc_user_id)
|
|
|
|
SPACETIMEDB_CLIENT_VISIBILITY_FILTER(
|
|
online_users_filter,
|
|
TableFor<User>("user").where([](const auto& users) {
|
|
return users.online;
|
|
}))
|
|
|
|
SPACETIMEDB_VIEW(Query<User>, online_users, Public, AnonymousViewContext ctx) {
|
|
return ctx.from[user].where([](const auto& users) {
|
|
return users.online;
|
|
});
|
|
}
|
|
|
|
SPACETIMEDB_VIEW(Query<User>, online_users_filter_alias, Public, AnonymousViewContext ctx) {
|
|
return ctx.from[user].filter([](const auto& users) {
|
|
return users.online;
|
|
});
|
|
}
|
|
|
|
SPACETIMEDB_VIEW(std::optional<User>, first_online_user, Public, AnonymousViewContext ctx) {
|
|
(void)ctx;
|
|
return std::optional<User>(User{Identity{}, true});
|
|
}
|
|
|
|
SPACETIMEDB_VIEW(Query<User>, online_users_copy, Public, AnonymousViewContext ctx) {
|
|
return ctx.from[online_users_view].where([](const auto& users) {
|
|
return users.online;
|
|
});
|
|
}
|
|
|
|
SPACETIMEDB_VIEW(Query<User>, first_online_user_copy, Public, AnonymousViewContext ctx) {
|
|
return ctx.from[first_online_user_view].where([](const auto& users) {
|
|
return users.online;
|
|
});
|
|
}
|
|
|
|
SPACETIMEDB_VIEW(Query<User>, online_member_users, Public, AnonymousViewContext ctx) {
|
|
return ctx.from[user_membership].right_semijoin(
|
|
ctx.from[user],
|
|
[](const auto& memberships, const auto& users) {
|
|
return memberships.user_identity.eq(users.identity);
|
|
})
|
|
.where([](const auto& users) {
|
|
return users.online;
|
|
});
|
|
}
|
|
|
|
SPACETIMEDB_VIEW(Query<AutoIncUser>, online_auto_inc_users, Public, AnonymousViewContext ctx) {
|
|
return ctx.from[auto_inc_membership].right_semijoin(
|
|
ctx.from[auto_inc_user],
|
|
[](const auto& memberships, const auto& users) {
|
|
return memberships.auto_inc_user_id.eq(users.id);
|
|
})
|
|
.where([](const auto& users) {
|
|
return users.online;
|
|
});
|
|
}
|