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
// SPDX-FileCopyrightText: 2025-2026 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT
//! Insertable struct generation for INSERT operations.
//!
//! Generates an `Insertable{Name}` struct optimized for database INSERT
//! queries. This struct owns all values needed for a single INSERT operation.
//!
//! # Generated Struct
//!
//! For an entity `User`, generates:
//!
//! ```rust,ignore
//! #[derive(Debug, Clone)]
//! pub struct InsertableUser {
//! pub id: Uuid,
//! pub name: String,
//! pub email: String,
//! pub created_at: DateTime<Utc>,
//! }
//! ```
//!
//! # Purpose
//!
//! The Insertable struct provides a clean interface for INSERT operations:
//!
//! - **Value ownership**: All fields are owned, ready for binding to SQL
//! - **Complete record**: Contains all columns needed for INSERT
//! - **Type conversion**: Generated `From<Entity>` and `From<&Entity>` impls
//!
//! # Usage in Repository
//!
//! ```rust,ignore
//! async fn create(&self, dto: CreateUserRequest) -> Result<User, Error> {
//! let entity = User::from(dto); // DTO → Entity (generates ID)
//! let insertable = InsertableUser::from(&entity); // Entity → Insertable
//!
//! sqlx::query("INSERT INTO users (...) VALUES ($1, $2, ...)")
//! .bind(insertable.id)
//! .bind(insertable.name)
//! // ...
//! .execute(self).await?;
//!
//! Ok(entity)
//! }
//! ```
//!
//! # Field Inclusion
//!
//! Like Row, the Insertable struct includes ALL fields:
//!
//! | Field Type | Included | Value Source |
//! |------------|----------|--------------|
//! | `#[id]` | Yes | Auto-generated UUID |
//! | `#[auto]` | Yes | `Default::default()` |
//! | `#[field(create)]` | Yes | From CreateRequest DTO |
//! | `#[field(skip)]` | Yes | `Default::default()` |
use TokenStream;
use quote;
use ;
use cratemarker;
/// Generates the `Insertable{Name}` struct for INSERT operations.
///
/// Returns an empty `TokenStream` if `sql = "none"` is specified,
/// as Insertable structs are only needed for database operations.