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
//! Derive macros for pgorm
//!
//! Provides `#[derive(FromRow)]` and `#[derive(Model)]` macros.
use TokenStream;
use ;
/// Derive `FromRow` trait for a struct.
///
/// # Example
///
/// ```ignore
/// use pgorm::FromRow;
///
/// #[derive(FromRow)]
/// struct User {
/// id: i64,
/// username: String,
/// #[orm(column = "email_address")]
/// email: Option<String>,
/// }
/// ```
///
/// # Attributes
///
/// - `#[orm(column = "name")]` - Map field to a different column name
/// Derive `Model` metadata for a struct.
///
/// # Example
///
/// ```ignore
/// use pgorm::Model;
///
/// #[derive(Model)]
/// #[orm(table = "users")]
/// struct User {
/// #[orm(id)]
/// user_id: i64,
/// username: String,
/// email: Option<String>,
/// }
/// ```
///
/// # Generated
///
/// - `TABLE: &'static str` - Table name
/// - `COL_*: &'static str` - Column name constants
/// - `SELECT_LIST: &'static str` - Comma-separated column list
/// - `fn select_list_as(alias: &str) -> String` - Aliased column list for JOINs
///
/// # Attributes
///
/// Struct-level:
///
/// - `#[orm(table = "name")]` - Specify table name (required)
/// - `#[orm(join(table = "...", on = "...", type = "inner|left|right|full|cross"))]` - Add JOINs (optional, repeatable)
/// - `#[orm(has_many(ChildType, foreign_key = "...", as = "..."))]` - Generate select_has_many helpers (optional, repeatable)
/// - `#[orm(belongs_to(ParentType, foreign_key = "...", as = "..."))]` - Generate select_belongs_to helpers (optional, repeatable)
///
/// Field-level:
///
/// - `#[orm(id)]` - Mark field as primary key
/// - `#[orm(column = "name")]` - Map field to a different column name
/// - `#[orm(table = "name")]` - Mark field as coming from a joined table (for view/join models)
/// Derive `ViewModel` metadata for a struct.
///
/// This is an alias of `Model` intended to express that the type is a read/view model
/// (optionally including JOINs), while write models are derived separately.
/// Derive `InsertModel` helpers for inserting into a table.
///
/// # Attributes
///
/// Struct-level:
///
/// - `#[orm(table = "name")]` - Specify table name (required)
/// - `#[orm(returning = "TypePath")]` - Enable `insert_returning` helpers (optional)
/// - Conflict handling (Postgres `ON CONFLICT`):
/// - `#[orm(conflict_target = "col1,col2")]` - conflict target columns (optional)
/// - `#[orm(conflict_constraint = "constraint_name")]` - conflict constraint (optional)
/// - `#[orm(conflict_update = "col1,col2")]` - columns to update on conflict (optional)
/// - Multi-table write graphs (advanced): function-style attrs like `#[orm(has_many(...))]`,
/// `#[orm(belongs_to(...))]`, `#[orm(before_insert(...))]`. See `docs/design/multi-table-writes-final.md`.
///
/// Field-level:
///
/// - `#[orm(id)]` - Mark field as primary key (optional)
/// - `#[orm(skip_insert)]` - Never include this field in INSERT
/// - `#[orm(default)]` - Use SQL `DEFAULT` for this field
/// - `#[orm(auto_now_add)]` - Use `NOW()` for this field on insert
/// - `#[orm(column = "name")]` / `#[orm(table = "name")]` - Override column/table mapping (optional)
/// Derive `UpdateModel` helpers for updating a table (patch-style).
///
/// # Attributes
///
/// Struct-level:
///
/// - `#[orm(table = "name")]` - Specify table name (required)
/// - One of:
/// - `#[orm(id_column = "id")]` - Explicit primary key column
/// - `#[orm(model = "TypePath")]` - Derive primary key column from a `Model`
/// - `#[orm(returning = "TypePath")]` where `TypePath::ID` exists
/// - `#[orm(returning = "TypePath")]` - Enable `update_by_id_returning` helpers (optional)
/// - Multi-table write graphs (advanced): see `docs/design/multi-table-writes-final.md`.
///
/// Field-level:
///
/// - `#[orm(skip_update)]` - Never include this field in UPDATE
/// - `#[orm(default)]` - Use SQL `DEFAULT` for this field
/// - `#[orm(auto_now)]` - Use `NOW()` for this field on update
/// - `#[orm(column = "name")]` / `#[orm(table = "name")]` - Override column/table mapping (optional)
/// Derive `QueryParams` helpers for building dynamic queries from a params struct.
///
/// # Example
///
/// ```ignore
/// use pgorm::QueryParams;
///
/// #[derive(QueryParams)]
/// #[orm(model = "User")]
/// struct UserSearchParams<'a> {
/// #[orm(eq(UserQuery::COL_ID))]
/// id: Option<i64>,
/// #[orm(eq(UserQuery::COL_EMAIL))]
/// email: Option<&'a str>,
/// }
///
/// let q = UserSearchParams { id, email }.into_query()?;
/// ```
///
/// # Attributes
///
/// Struct-level:
/// - `#[orm(model = "TypePath")]` - The model type that provides `Model::query()`
///
/// Field-level:
/// - `#[orm(eq(COL))]` - Equality filter (auto uses `eq_opt_str` for `&str`/`String`)
/// - `#[orm(eq_str(COL))]` - Equality filter, forcing string conversion
/// - `#[orm(eq_map(COL, map_fn))]` - Equality filter after mapping (e.g. parse)
/// - `#[orm(map(map_fn))]` - Optional mapper (returns `Option<T>`; `None` means "skip filter")
/// - `#[orm(ne(COL))]` / `#[orm(gt(COL))]` / `#[orm(gte(COL))]` / `#[orm(lt(COL))]` / `#[orm(lte(COL))]`
/// - `#[orm(like(COL))]` / `#[orm(ilike(COL))]` / `#[orm(not_like(COL))]` / `#[orm(not_ilike(COL))]`
/// - `#[orm(in_list(COL))]` / `#[orm(not_in(COL))]`
/// - `#[orm(between(COL))]` / `#[orm(not_between(COL))]` (expects `(T, T)` or `Option<(T, T)>`)
/// - `#[orm(is_null(COL))]` / `#[orm(is_not_null(COL))]` (expects `bool` or `Option<bool>`)
/// - `#[orm(order_by)]` - Replace the `OrderBy` builder (expects `OrderBy` or `Option<OrderBy>`)
/// - `#[orm(order_by_asc)]` / `#[orm(order_by_desc)]` - Add an ORDER BY column (expects a column ident or `Option<...>`)
/// - `#[orm(order_by_raw)]` - Add a raw ORDER BY item (escape hatch)
/// - `#[orm(paginate)]` - Replace the `Pagination` builder (expects `Pagination` or `Option<Pagination>`)
/// - `#[orm(limit)]` / `#[orm(offset)]` - Set LIMIT/OFFSET (expects `i64` or `Option<i64>`)
/// - `#[orm(page)]` - Page-based pagination (expects `(page, per_page)` or `Option<(page, per_page)>`)
/// - `#[orm(page(per_page = EXPR))]` - Page-based pagination from a page number (expects `i64`/`Option<i64>`)
/// - `#[orm(raw)]` - Raw WHERE fragment (escape hatch)
/// - `#[orm(and)]` / `#[orm(or)]` - Combine a `WhereExpr` (escape hatch)
/// - `#[orm(skip)]` - Ignore this field
/// Derive `PgEnum` helpers to map a Rust enum to a PostgreSQL ENUM type.
///
/// # Example
///
/// ```ignore
/// use pgorm::PgEnum;
///
/// #[derive(PgEnum, Debug, Clone, PartialEq)]
/// #[orm(pg_type = "order_status")]
/// pub enum OrderStatus {
/// #[orm(rename = "pending")]
/// Pending,
/// Processing, // defaults to "processing" (snake_case)
/// Shipped,
/// Delivered,
/// Cancelled,
/// }
/// ```
///
/// # Generated
///
/// - `impl ToSql for OrderStatus`
/// - `impl<'a> FromSql<'a> for OrderStatus`
/// - `impl PgType for OrderStatus` (returns `"{pg_type}[]"`)
///
/// # Attributes
///
/// Enum-level:
/// - `#[orm(pg_type = "name")]` - PostgreSQL ENUM type name (required)
///
/// Variant-level:
/// - `#[orm(rename = "name")]` - Override the SQL string for this variant (optional, defaults to snake_case)
/// Derive `PgComposite` helpers to map a Rust struct to a PostgreSQL composite type.
///
/// # Example
///
/// ```ignore
/// use pgorm::PgComposite;
///
/// #[derive(PgComposite, Debug, Clone)]
/// #[orm(pg_type = "address")]
/// pub struct Address {
/// pub street: String,
/// pub city: String,
/// pub zip_code: String,
/// pub country: String,
/// }
/// ```
///
/// # Generated
///
/// - `impl ToSql for Address`
/// - `impl<'a> FromSql<'a> for Address`
/// - `impl PgType for Address` (returns `"{pg_type}[]"`)
///
/// # Attributes
///
/// Struct-level:
/// - `#[orm(pg_type = "name")]` - PostgreSQL composite type name (required)
///
/// # Limitations
///
/// - Only flat structs with named fields are supported.
/// - Nested composite types are not supported.
/// - All fields must implement `ToSql` and `FromSql`.