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
//! The SQL family's rows: SQLite, Postgres and MySQL.
//!
//! One CONCRETE type per declared database — `VideoTrackSqlite`, `VideoTrackPostgres` — rather than
//! a generic `Row<DB>`. A generic row was probed and works; the ruling is concrete, for three
//! reasons that all point the same way: a stored type may differ per backend, so the parameter
//! would have to carry the columns as well as the dialect; the derives differ per family; and a
//! compiler error that names `VideoTrackPostgres` says what went wrong, where one naming a
//! projection of a type parameter makes the reader do the substitution.
//!
//! What is family-specific lives here: the derive list, how a column's external name reaches the
//! field that holds it, and which [`DatabaseKind`]s the file answers for. The mechanical assembly —
//! the fields, the accessors, the conversions — is [`super::DatabaseBackend::row`]'s, and mongo's
//! file passes it different answers to the same questions.
//!
//! ## The `FromRow` impl is WRITTEN here, not derived
//!
//! A declaration that stores SQL rows needs `sqlx` nowhere in its own manifest: every sqlx name the
//! emission writes goes through the framework's `__private` shelf, exactly as mongo's serde derives
//! do. What it asks of a consumer instead is `ingraph` carrying the shelf item — feature `sqlx`, or
//! any SQL backend tier that implies it.
//!
//! Reaching it by shelving the DERIVE was tried and cannot work, and that is the derive vendor's
//! doing rather than a matter of effort. `sqlx_macros_core` 0.9's `derives::row` writes `::sqlx::`
//! ABSOLUTELY — nine distinct items over 38 occurrences: `Row`, `Result`, `FromRow`, `ColumnIndex`,
//! `decode::Decode`, `types::Type`, `types::Json`, `Error::ColumnDecode`, `Error::ColumnNotFound` —
//! and neither `SqlxContainerAttributes` (`transparent`, `type_name`, `rename_all`, `repr`,
//! `no_pg_array`, `default`) nor `SqlxChildAttributes` (`rename`, `default`, `flatten`, `try_from`,
//! `skip`, `json`) carries a crate-path key to redirect them. A leading `::` resolves against the
//! EXTERN PRELUDE, so no `use` and no shelf path can reach one: `#[derive(#krate::__private::sqlx::
//! FromRow)]` would have moved one resolution and left the other nine — and where the declarer's
//! sqlx did not unify with the framework's it would have been actively worse, one expansion naming
//! two different sqlx crates at once. serde's derive takes `#[serde(crate = "…")]` and binds its
//! whole expansion to it, which is why [`super::mongo`] can go on deriving.
//!
//! Writing the impl is what works, because the emitter already owns every column name and the
//! emission used exactly ONE sqlx attribute — `rename` — and none of
//! `flatten`/`json`/`try_from`/`default`/`skip`. That is what bounds the job: only SIX of the
//! derive's nine items are reachable without those attributes (`Json` needs `json`, and both `Error`
//! variants need `try_from` or `default`), and the decode reaches THREE of the six — `FromRow`,
//! `Row` and `Result`. `Decode`, `Type` and `ColumnIndex` moved behind the framework's own read
//! seam; see below.
//!
//! ## What it mirrors, and the three places it departs
//!
//! [`from_row`] keeps `sqlx_macros_core`'s named-struct SHAPE rather than inventing one: generic
//! over the ROW with its own `'a`, one read per field in column order, and the reads bound to a
//! `where` clause the caller discharges at a concrete dialect. Staying generic over the row is a
//! decision and not an inheritance: binding each row to its dialect's concrete `Row` would arguably
//! be more type-safe, and it would change what a consumer can write, so it is a separate question
//! from this one.
//!
//! The parameter is not SPELLED `R`, which is the one place the shape departs on purpose. A bare
//! `R` shadows a declaration's own type of that name, so a column declared `R` would be bounded and
//! decoded as the parameter — see [`super::parameter`], which every emitted parameter goes through.
//!
//! Error behaviour is INHERITED rather than reimplemented. `Row::try_get` is what produces
//! `ColumnNotFound` (from the `&str` index) and `ColumnDecode` (from an incompatible type or a
//! failed decode); the read seam forwards both unchanged, so nothing here constructs an error and a
//! caller sees the same variants the derive would have handed them.
//!
//! Three departures:
//!
//! - **A read is one per FIELD and not one per column**, and it goes through `ingraph`'s `Assemble`
//! rather than through `try_get` directly. The derive's model is one column per field, which
//! cannot read a field whose TYPE spans several: `tb: Timebase` occupies `tb_num` and `tb_den`,
//! and neither name exists anywhere — both are joined while a statement is written, from the
//! nested list `SqlQuery::COLUMNS` holds. So the read descends the same list, and the type that
//! answers its own width is the type that reads its own columns back. The emission judges nothing
//! about width here for the same reason it judges nothing in `COLUMNS`: `tb: Timebase` and
//! `title: String` are the same tokens. One spelling per field, and the compiler selects.
//!
//! The `Decode`/`Type` pair per column goes with it. What a field costs the `where` clause is one
//! predicate — `Assemble<'a, Row>` — and what that predicate needs of the dialect is the seam's to
//! state, which is what keeps a composite's own requirements out of an emission that cannot see
//! them. `ColumnIndex` goes the same way: nothing in the body indexes a row any more.
//!
//! - An EDGE-STORED COLLECTION is not read at all. A collection kept in a second table has no
//! column of this row to name, so the decode binds the EMPTY collection and the materialization
//! pass fills it. That is the collapse the engine states — a parent with no elements and a parent
//! absent from the gather are one answer — held here by what the decode writes rather than by a
//! case anything tests for. Its predicate goes with it: a column that is never read needs none.
//!
//! - `from_row` carries no `#[inline]`, where every other emitted method carries one. That rule's
//! stated reason is "a one-line cross-crate wrapper", which a decode of N columns is not — and
//! sqlx does not inline its own either.
//!
//! ## What the change costs the row's fields
//!
//! `#[sqlx(rename = "…")]` is GONE from a SQL row, and not as a tidy-up: a helper attribute is only
//! legal where a derive on the same item registers it, so a row keeping the attribute without the
//! derive would not compile at all. The column's external name moves to the read that fetches it —
//! the same string in the same emission, so a reader of the expansion still sees each column's real
//! name, at the read rather than at the field.
//!
//! A `flatten` column is the one field with no name to write, which is the same fact its comparison
//! states by pushing no segment and its `COLUMNS` entry states by lending none: its type's segments
//! ARE the holder's columns. It reads through the seam's other entry point, and the two spellings
//! are one word apart exactly as the column list's are.
use quote;
use ;
use crate;
/// The SQL family's answers to the questions a row's emission asks — among them what the derives
/// need told at the container, which for a plain `Debug`/`Clone` pair is nothing.
pub
/// The `FromRow` impl `#[derive(sqlx::FromRow)]` would have written, emitted so that every sqlx name
/// in it can go through the shelf — see the module doc for why the derive could not, and for what
/// the read seam changed about its shape.
///
/// Every read is under the column's EXTERNAL name, which is what makes the decode name-addressed
/// rather than positional: a `SELECT` may hand the columns back in any order, and two columns of one
/// type are a transposition waiting to happen if position decides. A composite's segments are named
/// UNDER that one, by the walk rather than by anything written here.