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
// SPDX-FileCopyrightText: 2025-2026 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT
//! Database row struct generation.
//!
//! Generates a `{Name}Row` struct that maps directly to database query results.
//! This struct implements `sqlx::FromRow` for automatic deserialization.
//!
//! # Generated Struct
//!
//! For an entity `User`, generates:
//!
//! ```rust,ignore
//! #[derive(Debug, Clone)]
//! #[cfg_attr(feature = "postgres", derive(sqlx::FromRow))]
//! pub struct UserRow {
//! pub id: Uuid,
//! pub name: String,
//! pub email: String,
//! pub created_at: DateTime<Utc>,
//! }
//! ```
//!
//! # Purpose
//!
//! The Row struct serves as an intermediate type between raw database results
//! and the domain entity. This separation allows:
//!
//! - **Type safety**: Database columns map to explicit Rust types
//! - **Decoupling**: Entity can evolve independently of DB schema
//! - **Validation**: Conversion from Row to Entity can include validation
//!
//! # Field Inclusion
//!
//! Unlike DTOs, the Row struct includes ALL fields from the entity:
//!
//! | Field Type | In Row | Reason |
//! |------------|--------|--------|
//! | `#[id]` | Yes | Primary key from DB |
//! | `#[auto]` | Yes | Timestamps from DB |
//! | `#[field(skip)]` | Yes | Still stored in DB |
//! | Regular fields | Yes | All data columns |
//!
//! # Conditional Compilation
//!
//! The `sqlx::FromRow` derive is gated behind `#[cfg(feature = "postgres")]`.
//! This allows using the crate without sqlx for DTO-only scenarios.
use TokenStream;
use quote;
use ;
use cratemarker;
/// Generates the `{Name}Row` struct for database query results.
///
/// Returns an empty `TokenStream` if `sql = "none"` is specified,
/// as Row structs are only needed for database operations.