es-entity-macros 0.12.7

Proc macros for es-entity
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Shared pieces of the combined index+events write statements.
//!
//! Every write path (`create`, `create_all`, `update`, `update_all`, `delete`,
//! `forget`) emits the same tail: an `INSERT INTO {events_table} … SELECT …
//! FROM UNNEST(…) RETURNING recorded_at` fed by arrays of serialized events.
//! Only the *head* — the data-modifying CTE that writes the index row — is
//! genuinely per-path. This module owns the tail, the preamble that gathers
//! the arrays, and the follow-up forgettable payload insert, so the
//! `event_context` branching and the `$n` placeholder arithmetic each exist
//! once instead of six times.

use proc_macro2::TokenStream;
use quote::quote;

/// Where the event rows' ids and sequences come from.
///
/// Two orthogonal axes: whether one entity's events or many are being written
/// (which decides what the arrays carry), and whether the id comes from a
/// data-modifying CTE in the same statement or is supplied directly (the
/// standalone `persist_events` helpers, used where there is no index write to
/// fold into).
pub enum EventSource<'a> {
    /// One entity, id bound directly — the standalone `persist_events`.
    PerEntityStandalone {
        id_param: usize,
        offset_param: usize,
    },
    /// One entity, id taken from a CTE the same statement writes. The event
    /// arrays are crossed with the single CTE row and the sequence comes from
    /// `ROW_NUMBER()`. `offset_param` is `None` where the entity is brand new
    /// and sequences therefore start at 1 (`create`).
    PerEntityCte {
        cte: &'a str,
        offset_param: Option<usize>,
    },
    /// Many entities, ids and sequences in the arrays — the standalone
    /// `persist_events_batch`.
    BatchStandalone,
    /// Many entities, joined back to a CTE the same statement writes, which
    /// both orders the writes and reveals index rows that vanished (a short
    /// `RETURNING` count).
    BatchCte { cte: &'a str },
}

impl EventSource<'_> {
    fn is_batch(&self) -> bool {
        matches!(self, Self::BatchStandalone | Self::BatchCte { .. })
    }
}

/// Emitter for the events-table half of a combined write statement.
pub struct EventsInsert<'a> {
    pub events_table: &'a str,
    pub event_ctx: bool,
}

impl<'a> EventsInsert<'a> {
    pub fn new(events_table: &'a str, event_ctx: bool) -> Self {
        Self {
            events_table,
            event_ctx,
        }
    }

    /// The `INSERT INTO {events} … RETURNING recorded_at` tail.
    ///
    /// `now_param` is the placeholder holding `op.maybe_now()` (shared with
    /// the head, which uses it for `created_at`); `first_array_param` is the
    /// first placeholder this tail owns.
    pub fn sql(
        &self,
        source: &EventSource<'_>,
        now_param: usize,
        first_array_param: usize,
    ) -> String {
        let ctx_col = if self.event_ctx { ", context" } else { "" };
        let ctx_sel = if self.event_ctx {
            ", unnested.context"
        } else {
            ""
        };
        let p = first_array_param;

        let (id_expr, sequence_expr, from_clause) = match source {
            EventSource::PerEntityStandalone {
                id_param,
                offset_param,
            } => (
                format!("${id_param}"),
                format!("ROW_NUMBER() OVER () + ${offset_param}"),
                self.per_entity_unnest(p, None),
            ),
            EventSource::PerEntityCte { cte, offset_param } => {
                let sequence_expr = match offset_param {
                    Some(offset) => format!("ROW_NUMBER() OVER () + ${offset}"),
                    None => "ROW_NUMBER() OVER ()".to_string(),
                };
                (
                    format!("{cte}.id"),
                    sequence_expr,
                    self.per_entity_unnest(p, Some(cte)),
                )
            }
            EventSource::BatchStandalone => (
                "unnested.id".to_string(),
                "unnested.sequence".to_string(),
                self.batch_unnest(p, None),
            ),
            EventSource::BatchCte { cte } => (
                "unnested.id".to_string(),
                "unnested.sequence".to_string(),
                self.batch_unnest(p, Some(cte)),
            ),
        };

        format!(
            "INSERT INTO {} (id, recorded_at, sequence, event_type, event{ctx_col}) \
             SELECT {id_expr}, COALESCE(${now_param}, NOW()), {sequence_expr}, unnested.event_type, unnested.event{ctx_sel} \
             {from_clause} \
             RETURNING recorded_at",
            self.events_table,
        )
    }

    /// `FROM` clause for the one-entity shapes: the arrays carry only the
    /// event type and body, optionally crossed with a CTE row supplying the id.
    fn per_entity_unnest(&self, p: usize, cte: Option<&str>) -> String {
        let ctx_col = if self.event_ctx { ", context" } else { "" };
        let ctx_unnest = if self.event_ctx {
            format!(", ${}::JSONB[]", p + 2)
        } else {
            String::new()
        };
        let cross_join = match cte {
            Some(cte) => format!("{cte} CROSS JOIN "),
            None => String::new(),
        };
        format!(
            "FROM {cross_join}UNNEST(${}::TEXT[], ${}::JSONB[]{ctx_unnest}) AS unnested(event_type, event{ctx_col})",
            p,
            p + 1,
        )
    }

    /// `FROM` clause for the many-entity shapes: the arrays also carry ids and
    /// sequences, optionally joined back to a CTE.
    fn batch_unnest(&self, p: usize, cte: Option<&str>) -> String {
        let ctx_col = if self.event_ctx { ", context" } else { "" };
        let ctx_unnest = if self.event_ctx {
            format!(", ${}::JSONB[]", p + 4)
        } else {
            String::new()
        };
        let join = match cte {
            Some(cte) => format!(" JOIN {cte} ON {cte}.id = unnested.id"),
            None => String::new(),
        };
        format!(
            "FROM UNNEST(${}, ${}::INT[], ${}::TEXT[], ${}::JSONB[]{ctx_unnest}) AS unnested(id, sequence, event_type, event{ctx_col}){join}",
            p,
            p + 1,
            p + 2,
            p + 3,
        )
    }

    /// Statements that gather the serialized events for one entity into the
    /// locals the tail's arguments name. `events` is an expression evaluating
    /// to `&EntityEvents<_>`.
    pub fn gather_per_entity(&self, events: TokenStream) -> TokenStream {
        let ctx_var = if self.event_ctx {
            quote! { let contexts = #events.serialize_new_event_contexts(); }
        } else {
            quote! {}
        };
        quote! {
            let offset = #events.len_persisted();
            let events_types = #events.new_event_types();
            let serialized_events = #events.serialize_new_events();
            #ctx_var
        }
    }

    /// Declarations for the batch gather accumulators.
    pub fn batch_declarations(&self, id_type: &syn::Ident) -> TokenStream {
        let ctx_var = if self.event_ctx {
            quote! { let mut all_contexts: Vec<es_entity::ContextData> = Vec::new(); }
        } else {
            quote! {}
        };
        quote! {
            let mut all_ids: Vec<&#id_type> = Vec::new();
            let mut all_sequences: Vec<i32> = Vec::new();
            let mut all_types = Vec::new();
            let mut all_serialized = Vec::new();
            #ctx_var
        }
    }

    /// Per-entity body of the batch gather loop. `events` evaluates to
    /// `&EntityEvents<_>` and `id` to `&{IdType}`; both are re-read rather
    /// than threaded so callers keep their own borrow shapes.
    pub fn gather_batch(&self, events: TokenStream, id: TokenStream) -> TokenStream {
        let ctx_extend = if self.event_ctx {
            quote! {
                if let Some(contexts) = #events.serialize_new_event_contexts() {
                    all_contexts.extend(contexts);
                }
            }
        } else {
            quote! {}
        };
        quote! {
            let offset = #events.len_persisted() + 1;
            let types = #events.new_event_types();
            let serialized = #events.serialize_new_events();
            #ctx_extend

            let n_new = serialized.len();
            all_types.extend(types);
            all_serialized.extend(serialized);
            all_ids.extend(std::iter::repeat(#id).take(n_new));
            all_sequences.extend((offset..).take(n_new).map(|i| i as i32));
        }
    }

    /// The argument expressions the tail binds, in placeholder order, starting
    /// with `op.maybe_now()`. Callers wrap them for their own call style —
    /// positional `query!` arguments, `PgArguments::add`, or `.bind`.
    pub fn arg_exprs(&self, source: &EventSource<'_>) -> Vec<TokenStream> {
        let mut args = vec![quote! { op.maybe_now() }];
        if source.is_batch() {
            args.push(quote! { &all_ids });
            args.push(quote! { &all_sequences });
            args.push(quote! { &all_types });
            args.push(quote! { &all_serialized });
            if self.event_ctx {
                args.push(quote! {
                    &if all_contexts.is_empty() {
                        None
                    } else {
                        Some(all_contexts)
                    }
                });
            }
        } else {
            let has_offset = !matches!(
                source,
                EventSource::PerEntityCte {
                    offset_param: None,
                    ..
                }
            );
            if has_offset {
                args.push(quote! { offset as i32 });
            }
            args.push(quote! { &events_types });
            args.push(quote! { &serialized_events });
            if self.event_ctx {
                args.push(quote! { contexts.as_deref() as Option<&[es_entity::ContextData]> });
            }
        }
        args
    }
}

/// Emitter for the follow-up forgettable payload insert.
///
/// Payloads live in a third table, so they cannot join the combined statement:
/// sub-statements of a data-modifying CTE do not see each other's writes, and
/// `forget`'s payload delete has to be able to see them.
pub struct ForgettablePayloads<'a> {
    pub table: &'a str,
    pub id_type: &'a syn::Ident,
    pub event_type: &'a syn::Ident,
}

impl ForgettablePayloads<'_> {
    /// Collects and inserts the payloads of one entity's new events.
    /// Requires `offset` (the pre-persist `len_persisted`) and `id` in scope.
    pub fn insert_per_entity(&self, events: TokenStream, error: &syn::Ident) -> TokenStream {
        let Self {
            table,
            id_type,
            event_type,
        } = self;
        let query = format!(
            "INSERT INTO {table} (entity_id, sequence, payload) SELECT $1, unnested.sequence, unnested.payload FROM UNNEST($2::INT[], $3::JSONB[]) AS unnested(sequence, payload)"
        );
        quote! {
            let mut payload_sequences: Vec<i32> = Vec::new();
            let mut payload_values: Vec<es_entity::prelude::serde_json::Value> = Vec::new();
            for (idx, event_with_ctx) in #events.iter_new_events().enumerate() {
                if let Some(payload) = #event_type::extract_forgettable_payloads(&event_with_ctx.event) {
                    payload_sequences.push((offset + 1 + idx) as i32);
                    payload_values.push(payload);
                }
            }
            if !payload_sequences.is_empty() {
                Self::extract_concurrent_modification(
                    sqlx::query!(
                        #query,
                        id as &#id_type,
                        &payload_sequences,
                        &payload_values,
                    )
                    .execute(op.as_executor())
                    .await,
                    #error::ConcurrentModification,
                )?;
            }
        }
    }

    /// Declarations for the batch payload accumulators.
    pub fn batch_declarations(&self) -> TokenStream {
        let id_type = self.id_type;
        quote! {
            let mut payload_ids: Vec<&#id_type> = Vec::new();
            let mut payload_sequences: Vec<i32> = Vec::new();
            let mut payload_values: Vec<es_entity::prelude::serde_json::Value> = Vec::new();
        }
    }

    /// Per-entity body of the batch payload gather. Requires `offset` (the
    /// 1-based first sequence for this entity) in scope.
    pub fn gather_batch(&self, events: TokenStream, id: TokenStream) -> TokenStream {
        let event_type = self.event_type;
        quote! {
            for (idx, event_with_ctx) in #events.iter_new_events().enumerate() {
                if let Some(payload) = #event_type::extract_forgettable_payloads(&event_with_ctx.event) {
                    payload_ids.push(#id);
                    payload_sequences.push((offset + idx) as i32);
                    payload_values.push(payload);
                }
            }
        }
    }

    /// The batch payload insert.
    pub fn insert_batch(&self, error: &syn::Ident) -> TokenStream {
        let query = format!(
            "INSERT INTO {} (entity_id, sequence, payload) SELECT unnested.entity_id, unnested.sequence, unnested.payload FROM UNNEST($1, $2::INT[], $3::JSONB[]) AS unnested(entity_id, sequence, payload)",
            self.table
        );
        quote! {
            if !payload_sequences.is_empty() {
                Self::extract_concurrent_modification(
                    sqlx::query(#query)
                        .bind(&payload_ids)
                        .bind(&payload_sequences)
                        .bind(&payload_values)
                        .execute(op.as_executor())
                        .await,
                    #error::ConcurrentModification,
                )?;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn per_entity_sql_without_offset_matches_create() {
        let insert = EventsInsert::new("entity_events", false);
        let source = EventSource::PerEntityCte {
            cte: "new_row",
            offset_param: None,
        };
        assert_eq!(
            insert.sql(&source, 2, 3),
            "INSERT INTO entity_events (id, recorded_at, sequence, event_type, event) \
             SELECT new_row.id, COALESCE($2, NOW()), ROW_NUMBER() OVER (), unnested.event_type, unnested.event \
             FROM new_row CROSS JOIN UNNEST($3::TEXT[], $4::JSONB[]) AS unnested(event_type, event) \
             RETURNING recorded_at"
        );
    }

    #[test]
    fn per_entity_sql_with_offset_and_context() {
        let insert = EventsInsert::new("entity_events", true);
        let source = EventSource::PerEntityCte {
            cte: "updated",
            offset_param: Some(4),
        };
        assert_eq!(
            insert.sql(&source, 3, 5),
            "INSERT INTO entity_events (id, recorded_at, sequence, event_type, event, context) \
             SELECT updated.id, COALESCE($3, NOW()), ROW_NUMBER() OVER () + $4, unnested.event_type, unnested.event, unnested.context \
             FROM updated CROSS JOIN UNNEST($5::TEXT[], $6::JSONB[], $7::JSONB[]) AS unnested(event_type, event, context) \
             RETURNING recorded_at"
        );
    }

    #[test]
    fn batch_sql_joins_the_cte() {
        let insert = EventsInsert::new("entity_events", false);
        let source = EventSource::BatchCte { cte: "new_rows" };
        assert_eq!(
            insert.sql(&source, 1, 4),
            "INSERT INTO entity_events (id, recorded_at, sequence, event_type, event) \
             SELECT unnested.id, COALESCE($1, NOW()), unnested.sequence, unnested.event_type, unnested.event \
             FROM UNNEST($4, $5::INT[], $6::TEXT[], $7::JSONB[]) AS unnested(id, sequence, event_type, event) \
             JOIN new_rows ON new_rows.id = unnested.id \
             RETURNING recorded_at"
        );
    }

    #[test]
    fn arg_exprs_follow_placeholder_order() {
        let insert = EventsInsert::new("entity_events", false);
        let per_entity = insert.arg_exprs(&EventSource::PerEntityCte {
            cte: "updated",
            offset_param: Some(3),
        });
        let rendered: Vec<_> = per_entity.iter().map(|t| t.to_string()).collect();
        assert_eq!(
            rendered,
            vec![
                "op . maybe_now ()",
                "offset as i32",
                "& events_types",
                "& serialized_events"
            ]
        );
    }
}