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
//! The statements of a transactional outbox table: the take of a record by its id, the mark of a
//! processed one, and the recovery of the unprocessed records of a name.
use crateDialect;
use crateTableSpec;
use crate;
/// The statements a dialect builds for an outbox table, which records what a service publishes
/// until a consumer has processed it.
///
/// An outbox table is described by a [`TableSpec`] whose id identifies a record, whose
/// [`group`](TableSpec::group) column holds the name the record was published under, and whose
/// optional [`processed_at`](TableSpec::processed_at) column marks a processed record. A table
/// without it deletes a processed record instead. The table's [`Form`](crate::Form) does not
/// apply: a record is taken by its id, never claimed. `#[derive(Outbox)]` in `ruststream-sqlx`
/// builds these statements at compile time with each built-in dialect, and on the database's own
/// clock ([`TableSpec::database_clock`]). [`Postgres`](crate::Postgres), [`MySql`](crate::MySql)
/// and [`Sqlite`](crate::Sqlite) implement it.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "postgres")]
/// # fn main() -> Result<(), ruststream_sqlx_dialect::StatementError> {
/// use ruststream_sqlx_dialect::{Column, Form, OutboxDialect, Param, Postgres, TableSpec};
///
/// // outbox: id BIGSERIAL PRIMARY KEY, name TEXT, payload BYTEA, processed_at TIMESTAMPTZ
/// const OUTBOX: TableSpec<'static> = TableSpec::new("outbox", Column::new("id"), Form::RowLock)
/// .group(Column::new("name"))
/// .processed_at(Column::new("processed_at"))
/// .payload(Column::new("payload"))
/// .database_clock();
///
/// // What a consumer of a tracked message runs: the take of its record, then the mark once the
/// // handler acknowledged it.
/// let fetch = Postgres.outbox_fetch(&OUTBOX)?;
/// assert_eq!(
/// fetch.sql(),
/// r#"SELECT "id", "name", "processed_at", "payload" FROM "outbox" WHERE "id" = $1 AND "processed_at" IS NULL"#
/// );
/// let mark = Postgres.outbox_mark(&OUTBOX)?;
/// assert_eq!(
/// mark.sql(),
/// r#"UPDATE "outbox" SET "processed_at" = statement_timestamp() WHERE "id" = $1"#
/// );
///
/// // What the republish at startup runs for each registered name.
/// let recover = Postgres.outbox_recover(&OUTBOX)?;
/// assert_eq!(recover.params(), [Param::Group]);
/// # Ok(())
/// # }
/// # #[cfg(not(feature = "postgres"))]
/// # fn main() {}
/// ```