axumstart_db_macros 0.1.2

Proc macro powering axumstart_db's #[repository], SqlxInsert, and SqlxUpdate
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Fields};

use crate::dialect;

pub fn expand(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    match expand_inner(&input) {
        Ok(tokens) => TokenStream::from(tokens),
        Err(e) => TokenStream::from(e.to_compile_error()),
    }
}

/// Struct-level `#[key(col)]` — names the column `__sqlx_insert_into`/`__sqlx_upsert_into`/
/// `__sqlx_insert_all_into` select back on after a write, on backends with no RETURNING
/// (MySQL). Defaults to `id` if omitted. Has no effect on Postgres/SQLite, which use
/// `RETURNING *` directly and never need this.
fn extract_key_col(input: &DeriveInput) -> syn::Result<String> {
    for attr in &input.attrs {
        if attr.path().is_ident("key") {
            let ident: syn::Ident = attr.parse_args()?;
            return Ok(ident.to_string());
        }
    }
    Ok("id".to_string())
}

fn expand_inner(input: &DeriveInput) -> syn::Result<TokenStream2> {
    let name = &input.ident;

    let fields = match &input.data {
        Data::Struct(s) => match &s.fields {
            Fields::Named(f) => &f.named,
            _ => {
                return Err(syn::Error::new_spanned(
                    &input.ident,
                    "SqlxInsert requires named struct fields",
                ))
            }
        },
        _ => {
            return Err(syn::Error::new_spanned(
                &input.ident,
                "SqlxInsert can only be derived for structs",
            ))
        }
    };

    let field_names: Vec<_> = fields
        .iter()
        .map(|f| f.ident.as_ref().unwrap())
        .collect();

    if field_names.is_empty() {
        return Err(syn::Error::new_spanned(
            &input.ident,
            "SqlxInsert requires at least one field",
        ));
    }

    if dialect::is_mysql() {
        mysql_impl(name, &field_names, &extract_key_col(input)?)
    } else {
        returning_impl(name, &field_names)
    }
}

/// Postgres/SQLite: `RETURNING *` gets the written row back in one round-trip.
fn returning_impl(name: &syn::Ident, field_names: &[&syn::Ident]) -> syn::Result<TokenStream2> {
    let db = crate::krate::db_path();
    let cols = field_names.iter().map(|f| f.to_string()).collect::<Vec<_>>().join(", ");
    let placeholders =
        (1..=field_names.len()).map(dialect::placeholder).collect::<Vec<_>>().join(", ");

    let sql_template = format!("INSERT INTO \"{{}}\" ({cols}) VALUES ({placeholders}) RETURNING *");
    let void_sql_template = format!("INSERT INTO \"{{}}\" ({cols}) VALUES ({placeholders})");
    let ignore_sql_template =
        format!("INSERT INTO \"{{}}\" ({cols}) VALUES ({placeholders}) ON CONFLICT DO NOTHING");
    let insert_all_prefix_template = format!("INSERT INTO \"{{}}\" ({cols}) ");

    let bind_calls: Vec<TokenStream2> = field_names.iter().map(|f| quote!(.bind(self.#f))).collect();
    let push_bind_calls: Vec<TokenStream2> =
        field_names.iter().map(|f| quote!(__b.push_bind(__row.#f);)).collect();
    let col_strs: Vec<String> = field_names.iter().map(|f| f.to_string()).collect();
    let upsert_placeholders = placeholders;

    Ok(quote! {
        impl #name {
            // Accepts any sqlx Executor (DbPool, &mut DbConnection, etc.)
            pub async fn __sqlx_insert_into<'e, E, Row>(
                self,
                table: &'static str,
                executor: E,
            ) -> ::sqlx::Result<Row>
            where
                E: ::sqlx::Executor<'e, Database = #db::Db>,
                Row: for<'r> ::sqlx::FromRow<'r, <#db::Db as ::sqlx::Database>::Row> + Send + Unpin,
            {
                static __SQL: ::std::sync::OnceLock<::std::boxed::Box<str>> =
                    ::std::sync::OnceLock::new();
                let sql: &'static str = __SQL
                    .get_or_init(|| ::std::format!(#sql_template, table).into_boxed_str())
                    .as_ref();
                ::sqlx::query_as::<_, Row>(sql)
                    #(#bind_calls)*
                    .fetch_one(executor)
                    .await
            }

            // Same INSERT, no RETURNING — for callers that don't want the row back.
            pub async fn __sqlx_insert_into_void<'e, E>(
                self,
                table: &'static str,
                executor: E,
            ) -> ::sqlx::Result<()>
            where
                E: ::sqlx::Executor<'e, Database = #db::Db>,
            {
                static __SQL_VOID: ::std::sync::OnceLock<::std::boxed::Box<str>> =
                    ::std::sync::OnceLock::new();
                let sql: &'static str = __SQL_VOID
                    .get_or_init(|| ::std::format!(#void_sql_template, table).into_boxed_str())
                    .as_ref();
                ::sqlx::query(sql)
                    #(#bind_calls)*
                    .execute(executor)
                    .await
                    .map(|_| ())
            }

            // INSERT ... ON CONFLICT DO NOTHING — returns () on success
            pub async fn __sqlx_insert_ignore_into<'e, E>(
                self,
                table: &'static str,
                executor: E,
            ) -> ::sqlx::Result<()>
            where
                E: ::sqlx::Executor<'e, Database = #db::Db>,
            {
                static __SQL_IGNORE: ::std::sync::OnceLock<::std::boxed::Box<str>> =
                    ::std::sync::OnceLock::new();
                let sql: &'static str = __SQL_IGNORE
                    .get_or_init(|| ::std::format!(#ignore_sql_template, table).into_boxed_str())
                    .as_ref();
                ::sqlx::query(sql)
                    #(#bind_calls)*
                    .execute(executor)
                    .await
                    .map(|_| ())
            }

            // Single multi-row INSERT ... RETURNING *. One round-trip for the whole batch.
            pub async fn __sqlx_insert_all_into<'e, E, Row>(
                rows: ::std::vec::Vec<Self>,
                table: &'static str,
                executor: E,
            ) -> ::sqlx::Result<::std::vec::Vec<Row>>
            where
                E: ::sqlx::Executor<'e, Database = #db::Db>,
                Row: for<'r> ::sqlx::FromRow<'r, <#db::Db as ::sqlx::Database>::Row> + Send + Unpin,
            {
                if rows.is_empty() {
                    return ::std::result::Result::Ok(::std::vec::Vec::new());
                }
                let mut __qb = ::sqlx::QueryBuilder::<#db::Db>::new(
                    ::std::format!(#insert_all_prefix_template, table),
                );
                __qb.push_values(rows, |mut __b, __row| {
                    #(#push_bind_calls)*
                });
                __qb.push(" RETURNING *");
                __qb.build_query_as::<Row>().fetch_all(executor).await
            }

            // Same multi-row INSERT, no RETURNING — for callers that don't want the rows back.
            pub async fn __sqlx_insert_all_into_void<'e, E>(
                rows: ::std::vec::Vec<Self>,
                table: &'static str,
                executor: E,
            ) -> ::sqlx::Result<()>
            where
                E: ::sqlx::Executor<'e, Database = #db::Db>,
            {
                if rows.is_empty() {
                    return ::std::result::Result::Ok(());
                }
                let mut __qb = ::sqlx::QueryBuilder::<#db::Db>::new(
                    ::std::format!(#insert_all_prefix_template, table),
                );
                __qb.push_values(rows, |mut __b, __row| {
                    #(#push_bind_calls)*
                });
                __qb.build().execute(executor).await.map(|_| ())
            }

            // INSERT ... ON CONFLICT (conflict_col) DO UPDATE SET all_other_cols = EXCLUDED.col RETURNING *
            pub async fn __sqlx_upsert_into<'e, E, Row>(
                self,
                table: &'static str,
                conflict_col: &'static str,
                executor: E,
            ) -> ::sqlx::Result<Row>
            where
                E: ::sqlx::Executor<'e, Database = #db::Db>,
                Row: for<'r> ::sqlx::FromRow<'r, <#db::Db as ::sqlx::Database>::Row> + Send + Unpin,
            {
                static __SQL_UPSERT: ::std::sync::OnceLock<::std::boxed::Box<str>> =
                    ::std::sync::OnceLock::new();
                let sql: &'static str = __SQL_UPSERT
                    .get_or_init(|| {
                        static __COLS: &[&str] = &[#(#col_strs),*];
                        let col_list = __COLS.join(", ");
                        let set_clause = __COLS
                            .iter()
                            .filter(|&&c| c != conflict_col)
                            .map(|c| ::std::format!("{c} = EXCLUDED.{c}"))
                            .collect::<::std::vec::Vec<_>>()
                            .join(", ");
                        ::std::format!(
                            "INSERT INTO \"{table}\" ({col_list}) VALUES ({}) ON CONFLICT ({conflict_col}) DO UPDATE SET {set_clause} RETURNING *",
                            #upsert_placeholders,
                        )
                        .into_boxed_str()
                    })
                    .as_ref();
                ::sqlx::query_as::<_, Row>(sql)
                    #(#bind_calls)*
                    .fetch_one(executor)
                    .await
            }
        }
    })
}

/// MySQL: no RETURNING. Every write is followed by a `SELECT ... WHERE {key} = ?` on the
/// same executor to hand back the row, so `E` must be `Copy` (references are — this rules
/// out combining these methods with `#[transactional]`'s `&mut DbConnection`, which isn't
/// `Copy`; only pool-based calls work for insert/insert_all/upsert on MySQL).
fn mysql_impl(name: &syn::Ident, field_names: &[&syn::Ident], key_col: &str) -> syn::Result<TokenStream2> {
    let db = crate::krate::db_path();
    let cols = field_names.iter().map(|f| f.to_string()).collect::<Vec<_>>().join(", ");
    let placeholders = (1..=field_names.len()).map(dialect::placeholder).collect::<Vec<_>>().join(", ");

    let insert_sql_template = format!("INSERT INTO `{{}}` ({cols}) VALUES ({placeholders})");
    let ignore_sql_template =
        format!("INSERT IGNORE INTO `{{}}` ({cols}) VALUES ({placeholders})");
    let insert_all_prefix_template = format!("INSERT INTO `{{}}` ({cols}) ");
    let select_by_key_template = format!("SELECT * FROM `{{}}` WHERE {key_col} = ?");
    let select_range_template =
        format!("SELECT * FROM `{{}}` WHERE {key_col} >= ? AND {key_col} < ? ORDER BY {key_col}");

    let bind_calls: Vec<TokenStream2> = field_names.iter().map(|f| quote!(.bind(self.#f))).collect();
    let push_bind_calls: Vec<TokenStream2> =
        field_names.iter().map(|f| quote!(__b.push_bind(__row.#f);)).collect();
    let col_strs: Vec<String> = field_names.iter().map(|f| f.to_string()).collect();
    // The INSERT statement's bind_calls move every field out of `self`, so the upsert's
    // follow-up SELECT (which binds whichever field matches the runtime `conflict_col`)
    // needs its own pre-`clone()`d copy of each field to bind from instead.
    let clone_idents: Vec<syn::Ident> = (0..field_names.len())
        .map(|i| quote::format_ident!("__clone_{i}"))
        .collect();
    let clone_stmts: Vec<TokenStream2> = field_names
        .iter()
        .zip(&clone_idents)
        .map(|(f, c)| quote!(let #c = self.#f.clone();))
        .collect();

    Ok(quote! {
        impl #name {
            pub async fn __sqlx_insert_into<'e, E, Row>(
                self,
                table: &'static str,
                executor: E,
            ) -> ::sqlx::Result<Row>
            where
                E: ::sqlx::Executor<'e, Database = #db::Db> + ::std::marker::Copy,
                Row: for<'r> ::sqlx::FromRow<'r, <#db::Db as ::sqlx::Database>::Row> + Send + Unpin,
            {
                static __INSERT_SQL: ::std::sync::OnceLock<::std::boxed::Box<str>> =
                    ::std::sync::OnceLock::new();
                let insert_sql: &'static str = __INSERT_SQL
                    .get_or_init(|| ::std::format!(#insert_sql_template, table).into_boxed_str())
                    .as_ref();
                static __SELECT_SQL: ::std::sync::OnceLock<::std::boxed::Box<str>> =
                    ::std::sync::OnceLock::new();
                let select_sql: &'static str = __SELECT_SQL
                    .get_or_init(|| ::std::format!(#select_by_key_template, table).into_boxed_str())
                    .as_ref();

                let __id = ::sqlx::query(insert_sql)
                    #(#bind_calls)*
                    .execute(executor)
                    .await?
                    .last_insert_id();
                ::sqlx::query_as::<_, Row>(select_sql)
                    .bind(__id)
                    .fetch_one(executor)
                    .await
            }

            // Same INSERT, no follow-up SELECT — for callers that don't want the row back.
            // Unlike the Row-returning version above, this doesn't need `E: Copy` (only one
            // statement), so it also works with `#[transactional]`'s `&mut DbConnection`.
            pub async fn __sqlx_insert_into_void<'e, E>(
                self,
                table: &'static str,
                executor: E,
            ) -> ::sqlx::Result<()>
            where
                E: ::sqlx::Executor<'e, Database = #db::Db>,
            {
                static __INSERT_SQL_VOID: ::std::sync::OnceLock<::std::boxed::Box<str>> =
                    ::std::sync::OnceLock::new();
                let insert_sql: &'static str = __INSERT_SQL_VOID
                    .get_or_init(|| ::std::format!(#insert_sql_template, table).into_boxed_str())
                    .as_ref();
                ::sqlx::query(insert_sql)
                    #(#bind_calls)*
                    .execute(executor)
                    .await
                    .map(|_| ())
            }

            // INSERT IGNORE — single statement, no follow-up SELECT needed.
            pub async fn __sqlx_insert_ignore_into<'e, E>(
                self,
                table: &'static str,
                executor: E,
            ) -> ::sqlx::Result<()>
            where
                E: ::sqlx::Executor<'e, Database = #db::Db>,
            {
                static __SQL_IGNORE: ::std::sync::OnceLock<::std::boxed::Box<str>> =
                    ::std::sync::OnceLock::new();
                let sql: &'static str = __SQL_IGNORE
                    .get_or_init(|| ::std::format!(#ignore_sql_template, table).into_boxed_str())
                    .as_ref();
                ::sqlx::query(sql)
                    #(#bind_calls)*
                    .execute(executor)
                    .await
                    .map(|_| ())
            }

            // Multi-row INSERT, then re-SELECT the batch by primary key range. Relies on
            // MySQL's contiguous auto-increment allocation for a single multi-row insert
            // statement (true under the default innodb_autoinc_lock_mode) — if the table
            // doesn't use an auto-increment `{key_col}`, this will not return the right rows.
            pub async fn __sqlx_insert_all_into<'e, E, Row>(
                rows: ::std::vec::Vec<Self>,
                table: &'static str,
                executor: E,
            ) -> ::sqlx::Result<::std::vec::Vec<Row>>
            where
                E: ::sqlx::Executor<'e, Database = #db::Db> + ::std::marker::Copy,
                Row: for<'r> ::sqlx::FromRow<'r, <#db::Db as ::sqlx::Database>::Row> + Send + Unpin,
            {
                if rows.is_empty() {
                    return ::std::result::Result::Ok(::std::vec::Vec::new());
                }
                let __count = rows.len() as u64;
                let mut __qb = ::sqlx::QueryBuilder::<#db::Db>::new(
                    ::std::format!(#insert_all_prefix_template, table),
                );
                __qb.push_values(rows, |mut __b, __row| {
                    #(#push_bind_calls)*
                });
                let __first_id = __qb.build().execute(executor).await?.last_insert_id();

                static __SELECT_RANGE_SQL: ::std::sync::OnceLock<::std::boxed::Box<str>> =
                    ::std::sync::OnceLock::new();
                let select_sql: &'static str = __SELECT_RANGE_SQL
                    .get_or_init(|| ::std::format!(#select_range_template, table).into_boxed_str())
                    .as_ref();
                ::sqlx::query_as::<_, Row>(select_sql)
                    .bind(__first_id)
                    .bind(__first_id + __count)
                    .fetch_all(executor)
                    .await
            }

            // Same multi-row INSERT, no follow-up SELECT — for callers that don't want the
            // rows back. Unlike the Row-returning version above, this doesn't need `E: Copy`
            // (only one statement), so it also works with `#[transactional]`.
            pub async fn __sqlx_insert_all_into_void<'e, E>(
                rows: ::std::vec::Vec<Self>,
                table: &'static str,
                executor: E,
            ) -> ::sqlx::Result<()>
            where
                E: ::sqlx::Executor<'e, Database = #db::Db>,
            {
                if rows.is_empty() {
                    return ::std::result::Result::Ok(());
                }
                let mut __qb = ::sqlx::QueryBuilder::<#db::Db>::new(
                    ::std::format!(#insert_all_prefix_template, table),
                );
                __qb.push_values(rows, |mut __b, __row| {
                    #(#push_bind_calls)*
                });
                __qb.build().execute(executor).await.map(|_| ())
            }

            // INSERT ... ON DUPLICATE KEY UPDATE col = VALUES(col), ... then SELECT-back by
            // the conflict column (MySQL has no RETURNING and infers the conflicting unique
            // key automatically — it isn't named the way Postgres/SQLite's ON CONFLICT is).
            pub async fn __sqlx_upsert_into<'e, E, Row>(
                self,
                table: &'static str,
                conflict_col: &'static str,
                executor: E,
            ) -> ::sqlx::Result<Row>
            where
                E: ::sqlx::Executor<'e, Database = #db::Db> + ::std::marker::Copy,
                Row: for<'r> ::sqlx::FromRow<'r, <#db::Db as ::sqlx::Database>::Row> + Send + Unpin,
            {
                static __SQL_UPSERT: ::std::sync::OnceLock<::std::boxed::Box<str>> =
                    ::std::sync::OnceLock::new();
                let sql: &'static str = __SQL_UPSERT
                    .get_or_init(|| {
                        static __COLS: &[&str] = &[#(#col_strs),*];
                        let col_list = __COLS.join(", ");
                        let placeholders = (1..=__COLS.len())
                            .map(|_| "?")
                            .collect::<::std::vec::Vec<_>>()
                            .join(", ");
                        let set_clause = __COLS
                            .iter()
                            .map(|c| ::std::format!("{c} = VALUES({c})"))
                            .collect::<::std::vec::Vec<_>>()
                            .join(", ");
                        ::std::format!(
                            "INSERT INTO `{table}` ({col_list}) VALUES ({placeholders}) ON DUPLICATE KEY UPDATE {set_clause}"
                        )
                        .into_boxed_str()
                    })
                    .as_ref();
                static __SELECT_SQL: ::std::sync::OnceLock<::std::boxed::Box<str>> =
                    ::std::sync::OnceLock::new();
                let select_sql: &'static str = __SELECT_SQL
                    .get_or_init(|| ::std::format!("SELECT * FROM `{}` WHERE {} = ?", table, conflict_col).into_boxed_str())
                    .as_ref();

                #(#clone_stmts)*
                ::sqlx::query(sql)
                    #(#bind_calls)*
                    .execute(executor)
                    .await?;

                match conflict_col {
                    #( #col_strs => return ::sqlx::query_as::<_, Row>(select_sql).bind(#clone_idents).fetch_one(executor).await, )*
                    other => ::std::panic!("__sqlx_upsert_into: `{}` is not a field of this values struct", other),
                }
            }
        }
    })
}