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
//! By-name subscriptions as a feature of the dialect: the headers and times their statements read
//! and bind, per database.
use BTreeMap;
use HeaderMap;
use Dialect;
use Arguments;
use BoxDynError;
use Json;
use ;
use ;
use unread;
use NamedTime;
use crateHeaderColumn;
use crateBuiltIn;
/// Subscriptions by name on the database `DB`, as a feature of a dialect: the headers and the
/// times their rows hold, which no struct of the service's own names.
///
/// A by-name subscription (`#[subscriber("emails")]`) reads the table the name's route leads to.
/// Where the route's row leaves every event to the crate, the subscription reads the role
/// columns into a row of the crate's own and binds its statements from there, with no box and no
/// dynamic call per message. The ids, payloads, keys and attempts it reads are integers, text
/// and bytes, which every sqlx driver decodes alike; the headers kept as JSON and the times are
/// the database's own types, and this trait reads and binds them. A dialect that implements it
/// for its database gets subscriptions by name; on a dialect that does not, a by-name mount does
/// not compile, and its tables take [`InboxQueue`](crate::InboxQueue) descriptors.
///
/// [`BuiltIn<DB>`](crate::BuiltIn) implements it for each built-in database. A dialect of the
/// service's own that wraps a built-in one delegates to it; one for a driver outside sqlx reads
/// and binds through that driver.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "postgres")]
/// # mod demo {
/// # use std::num::NonZeroUsize;
/// # use ruststream::OutgoingMessage;
/// # use ruststream_sqlx::dialect::{self, ClaimShape, Dialect, Opening, RowLock, Statement, StatementError, TableName, TableSpec};
/// # use sqlx::PgConnection;
/// use ruststream::HeaderMap;
/// use ruststream_sqlx::prelude::*;
/// use ruststream_sqlx::{BuiltIn, ByName, NamedTime};
/// use serde::Deserialize;
/// use sqlx::error::BoxDynError;
/// use sqlx::postgres::{PgArguments, PgValueRef};
/// use sqlx::{PgPool, Postgres};
///
/// /// A dialect of the service's own over Postgres.
/// #[derive(Debug)]
/// pub struct Audited;
/// # impl Dialect for Audited {
/// # fn name(&self) -> &'static str { "audited" }
/// # fn quote_into(&self, ident: &str, out: &mut String) { dialect::Postgres.quote_into(ident, out) }
/// # fn placeholder_into(&self, index: NonZeroUsize, out: &mut String) { dialect::Postgres.placeholder_into(index, out) }
/// # fn fetch(&self, spec: &TableSpec<'_>) -> Result<Statement, StatementError> { dialect::Postgres.fetch(spec) }
/// # fn ack(&self, spec: &TableSpec<'_>) -> Result<Statement, StatementError> { dialect::Postgres.ack(spec) }
/// # fn retry(&self, spec: &TableSpec<'_>) -> Result<Option<Statement>, StatementError> { dialect::Postgres.retry(spec) }
/// # fn retry_after(&self, spec: &TableSpec<'_>) -> Result<Statement, StatementError> { dialect::Postgres.retry_after(spec) }
/// # fn discard(&self, spec: &TableSpec<'_>) -> Result<Statement, StatementError> { dialect::Postgres.discard(spec) }
/// # fn dead_letter_group(&self, spec: &TableSpec<'_>) -> Result<Statement, StatementError> { dialect::Postgres.dead_letter_group(spec) }
/// # fn dead_letter_table(&self, spec: &TableSpec<'_>, target: TableName<'_>) -> Result<Vec<Statement>, StatementError> { dialect::Postgres.dead_letter_table(spec, target) }
/// # fn insert(&self, spec: &TableSpec<'_>) -> Result<Statement, StatementError> { dialect::Postgres.insert(spec) }
/// # fn begin(&self, opening: Opening) -> Result<Option<&'static str>, StatementError> { dialect::Postgres.begin(opening) }
/// # fn fifo_guard(&self, spec: &TableSpec<'_>) -> Result<Option<Statement>, StatementError> { dialect::Postgres.fifo_guard(spec) }
/// # }
/// # impl RowLock for Audited {
/// # fn lock_claim(&self, spec: &TableSpec<'_>, shape: ClaimShape) -> Result<Statement, StatementError> { dialect::Postgres.lock_claim(spec, shape) }
/// # }
///
/// // Its rows keep their headers and times as the built-in dialect reads them, so
/// // `#[subscriber("emails")]` mounts on it.
/// impl ByName<Postgres> for Audited {
/// fn headers(value: PgValueRef<'_>) -> Result<HeaderMap, BoxDynError> {
/// <BuiltIn<Postgres> as ByName<Postgres>>::headers(value)
/// }
///
/// fn bind_time(arguments: &mut PgArguments, time: NamedTime) -> Result<(), sqlx::Error> {
/// <BuiltIn<Postgres> as ByName<Postgres>>::bind_time(arguments, time)
/// }
/// }
///
/// #[derive(Inbox, sqlx::FromRow)]
/// #[inbox(table = "email_jobs")]
/// pub struct SendEmail {
/// #[field(id, generated)]
/// job_id: i64,
/// #[field(group)]
/// name: String,
/// #[field(payload)]
/// payload: Vec<u8>,
/// }
/// # impl Publish<Postgres> for SendEmail {
/// # async fn publish(_: &mut PgConnection, _: &OutgoingMessage<'_>) -> Result<(), sqlx::Error> { Ok(()) }
/// # }
///
/// #[derive(Deserialize)]
/// struct Email {
/// to: String,
/// }
///
/// #[subscriber("emails")]
/// async fn send(email: &Email) -> HandlerOutcome {
/// tracing::info!(to = %email.to, "sending");
/// HandlerOutcome::ack()
/// }
///
/// pub fn app(pool: PgPool) -> RustStream {
/// let broker = SqlxBroker::with_dialect(pool, Audited).route::<SendEmail>("emails");
/// RustStream::new(AppInfo::new("mailer", "0.1.0")).with_broker(broker, |b| {
/// b.include(send);
/// })
/// }
/// # }
/// # fn main() {}
/// ```
/// Implements [`ByName`] for the built-in dialect of a database whose driver decodes JSON and
/// binds the `chrono` and `time` types, with the features that bring them.
by_name!;
by_name!;
by_name!;
// Why errors: `sqlx::Any` decodes no JSON and binds no time, so no row on an `AnyPool` has a
// `headers` column of JSON or a time column, and a by-name subscription there reaches neither.