|
1 | | -use spacetimedb::{reducer, table, view, AnonymousViewContext, Identity, ReducerContext, ViewContext}; |
| 1 | +use spacetimedb::{reducer, table, view, AnonymousViewContext, Identity, ReducerContext, ViewContext, Query}; |
2 | 2 |
|
3 | 3 | #[table(name = test)] |
4 | 4 | struct Test { |
@@ -155,4 +155,71 @@ fn scheduled_table_view(_: &ViewContext, _args: ScheduledTable) -> Vec<Player> { |
155 | 155 | vec![] |
156 | 156 | } |
157 | 157 |
|
| 158 | +#[table(name = player_info)] |
| 159 | +struct PlayerInfo { |
| 160 | + #[unique] |
| 161 | + identity: Identity, |
| 162 | + #[index(btree)] |
| 163 | + weight: u32, |
| 164 | + age: u8, |
| 165 | +} |
| 166 | + |
| 167 | + |
| 168 | +/// Where with mismatched types |
| 169 | +#[view(name = view_bad_where, public)] |
| 170 | +fn view_bad_where(ctx: &ViewContext) -> Query<Player> { |
| 171 | + ctx.from |
| 172 | + .player() |
| 173 | + .r#where(|a| a.identity.eq(42)) |
| 174 | + .build() |
| 175 | +} |
| 176 | + |
| 177 | +// Where with mismatched int types |
| 178 | +// u8 with 4200u32 |
| 179 | +#[view(name = view_bad_where_int_types, public)] |
| 180 | +fn view_bad_where_int_types(ctx: &ViewContext) -> Query<PlayerInfo> { |
| 181 | + ctx.from |
| 182 | + .player_info() |
| 183 | + .r#where(|a| a.age.eq(4200u32)) |
| 184 | + .build() |
| 185 | +} |
| 186 | + |
| 187 | + |
| 188 | +/// Joining incompatible types |
| 189 | +/// -- weight is u32, identity is Identity |
| 190 | +#[view(name = view_bad_join, public)] |
| 191 | +fn view_bad_join(ctx: &ViewContext) -> Query<PlayerInfo> { |
| 192 | + ctx.from |
| 193 | + .player_info() |
| 194 | + .left_semijoin(ctx.from.player(), |a, b| a.weight.eq(b.identity)) |
| 195 | + .build() |
| 196 | +} |
| 197 | + |
| 198 | +// Joining non-idx columna |
| 199 | +// -- age is not indexed |
| 200 | +#[view(name = view_join_non_indexed_column, public)] |
| 201 | +fn view_join_non_indexed_column(ctx: &ViewContext) -> Query<PlayerInfo> { |
| 202 | + ctx.from |
| 203 | + .player() |
| 204 | + .right_semijoin(ctx.from.player_info(), |a, b| a.identity.eq(b.age)) |
| 205 | + .build() |
| 206 | +} |
| 207 | + |
| 208 | +// Right join returns right table's type |
| 209 | +// -- should be PlayerInfo, not Player |
| 210 | +#[view(name = view_right_join_wrong_return_type, public)] |
| 211 | +fn view_right_join_wrong_return_type(ctx: &ViewContext) -> Query<Player> { |
| 212 | + ctx.from |
| 213 | + .player() |
| 214 | + .right_semijoin(ctx.from.player_info(), |a, b| a.identity.eq(b.identity)) |
| 215 | + .build() |
| 216 | +} |
| 217 | + |
| 218 | +/// Using non-existent table |
| 219 | +/// -- xyz table does not exist |
| 220 | +#[view(name = view_nonexistent_table, public)] |
| 221 | +fn view_nonexistent_table(ctx: &ViewContext) -> Query<T> { |
| 222 | + ctx.from.xyz().build() |
| 223 | +} |
| 224 | + |
158 | 225 | fn main() {} |
0 commit comments