pgorm 0.2.0

A model-definition-first, AI-friendly PostgreSQL ORM for Rust
Documentation
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//! Bulk update and delete operations.
//!
//! This module provides [`SetExpr`], [`UpdateManyBuilder`], and [`DeleteManyBuilder`]
//! for performing batch UPDATE and DELETE operations with type-safe conditions.
//!
//! # Example
//! ```ignore
//! use pgorm::prelude::*;
//! use pgorm::SetExpr;
//!
//! // Bulk update
//! let affected = pgorm::sql("users")
//!     .update_many([
//!         SetExpr::set("status", "inactive")?,
//!     ])
//!     .filter(Condition::lt("last_login", one_year_ago)?)
//!     .execute(&client)
//!     .await?;
//!
//! // Bulk delete
//! let deleted = pgorm::sql("sessions")
//!     .delete_many()
//!     .filter(Condition::lt("expires_at", now)?)
//!     .execute(&client)
//!     .await?;
//! ```

use crate::builder::WhereExpr;
use crate::client::{GenericClient, StreamingClient};
use crate::error::{OrmError, OrmResult};
use crate::ident::{Ident, IntoIdent};
use crate::row::FromRow;
use crate::sql::{FromRowStream, Sql};
use std::sync::Arc;
use tokio_postgres::types::ToSql;

// ==================== SetExpr ====================

/// A SET clause expression for bulk updates.
///
/// # Example
/// ```ignore
/// use pgorm::SetExpr;
///
/// // Simple value assignment: SET status = $1
/// SetExpr::set("status", "inactive")?;
///
/// // Increment: SET view_count = view_count + 1
/// SetExpr::increment("view_count", 1)?;
///
/// // Raw SQL expression: SET updated_at = NOW()
/// SetExpr::raw("updated_at = NOW()");
/// ```
pub enum SetExpr {
    /// `column = $n` (parameterized value)
    Value {
        column: Ident,
        value: Arc<dyn ToSql + Send + Sync>,
    },
    /// `column = column + amount` (increment/decrement)
    Increment { column: Ident, amount: i64 },
    /// Raw SQL expression (escape hatch), e.g. `"updated_at = NOW()"`
    Raw(String),
}

impl SetExpr {
    /// Create a SET clause that assigns a parameterized value: `col = $n`
    pub fn set<T: ToSql + Send + Sync + 'static>(
        column: impl IntoIdent,
        value: T,
    ) -> OrmResult<Self> {
        Ok(SetExpr::Value {
            column: column.into_ident()?,
            value: Arc::new(value),
        })
    }

    /// Create a SET clause that increments a column: `col = col + amount`
    ///
    /// Supports negative values for decrement.
    pub fn increment(column: impl IntoIdent, amount: i64) -> OrmResult<Self> {
        Ok(SetExpr::Increment {
            column: column.into_ident()?,
            amount,
        })
    }

    /// Create a SET clause with a raw SQL expression.
    ///
    /// The string should be a complete assignment expression, e.g. `"updated_at = NOW()"`.
    ///
    /// **Warning**: This bypasses SQL injection protection. Only use with trusted SQL.
    pub fn raw(expr: impl Into<String>) -> Self {
        SetExpr::Raw(expr.into())
    }

    fn append_to_sql(&self, sql: &mut Sql) {
        match self {
            SetExpr::Value { column, value } => {
                sql.push_ident_ref(column);
                sql.push(" = ");
                sql.push_bind_value(value.clone());
            }
            SetExpr::Increment { column, amount } => {
                sql.push_ident_ref(column);
                sql.push(" = ");
                sql.push_ident_ref(column);
                if *amount >= 0 {
                    let s = format!(" + {amount}");
                    sql.push(&s);
                } else {
                    let s = format!(" - {}", amount.abs());
                    sql.push(&s);
                }
            }
            SetExpr::Raw(expr) => {
                sql.push(expr);
            }
        }
    }
}

// ==================== UpdateManyBuilder ====================

/// Builder for bulk UPDATE operations.
///
/// Created via [`Sql::update_many`].
///
/// # Example
/// ```ignore
/// pgorm::sql("users")
///     .update_many([
///         SetExpr::set("status", "inactive")?,
///     ])
///     .filter(Condition::lt("last_login", one_year_ago)?)
///     .execute(&client)
///     .await?;
/// ```
#[must_use]
pub struct UpdateManyBuilder {
    pub(crate) table: Ident,
    pub(crate) sets: Vec<SetExpr>,
    pub(crate) where_clause: Option<WhereExpr>,
    pub(crate) all_rows: bool,
}

impl UpdateManyBuilder {
    /// Add a WHERE condition.
    pub fn filter(mut self, condition: impl Into<WhereExpr>) -> Self {
        let new_where = condition.into();
        self.where_clause = Some(match self.where_clause.take() {
            Some(existing) => existing.and_with(new_where),
            None => new_where,
        });
        self
    }

    /// Explicitly allow updating all rows without a WHERE clause.
    ///
    /// Without this, executing without a `.filter()` returns an error.
    pub fn all_rows(mut self) -> Self {
        self.all_rows = true;
        self
    }

    /// Build the SQL statement without executing it.
    ///
    /// Useful for inspecting the generated SQL.
    pub fn build_sql(&self) -> OrmResult<Sql> {
        if self.where_clause.is_none() && !self.all_rows {
            return Err(OrmError::Validation(
                "update_many requires a .filter() condition or .all_rows() to proceed. \
                 This prevents accidental full-table updates."
                    .to_string(),
            ));
        }

        let mut sql = Sql::new("UPDATE ");
        sql.push_ident_ref(&self.table);
        sql.push(" SET ");

        for (i, set) in self.sets.iter().enumerate() {
            if i > 0 {
                sql.push(", ");
            }
            set.append_to_sql(&mut sql);
        }

        if let Some(ref where_clause) = self.where_clause {
            sql.push(" WHERE ");
            where_clause.append_to_sql(&mut sql);
        }

        Ok(sql)
    }

    /// Execute the update, returning the number of affected rows.
    pub async fn execute(self, conn: &impl GenericClient) -> OrmResult<u64> {
        let sql = self.build_sql()?;
        sql.execute(conn).await
    }

    /// Execute the update and return the affected rows.
    ///
    /// Appends `RETURNING *` to the query.
    pub async fn returning<T: FromRow>(self, conn: &impl GenericClient) -> OrmResult<Vec<T>> {
        let mut sql = self.build_sql()?;
        sql.push(" RETURNING *");
        sql.fetch_all_as(conn).await
    }

    /// Execute the update and return a stream of affected rows.
    ///
    /// Appends `RETURNING *` to the query.
    pub async fn returning_stream<T: FromRow>(
        self,
        conn: &impl StreamingClient,
    ) -> OrmResult<FromRowStream<T>> {
        let mut sql = self.build_sql()?;
        sql.push(" RETURNING *");
        sql.stream_as(conn).await
    }
}

// ==================== DeleteManyBuilder ====================

/// Builder for bulk DELETE operations.
///
/// Created via [`Sql::delete_many`].
///
/// # Example
/// ```ignore
/// pgorm::sql("sessions")
///     .delete_many()
///     .filter(Condition::lt("expires_at", now)?)
///     .execute(&client)
///     .await?;
/// ```
#[must_use]
pub struct DeleteManyBuilder {
    pub(crate) table: Ident,
    pub(crate) where_clause: Option<WhereExpr>,
    pub(crate) all_rows: bool,
}

impl DeleteManyBuilder {
    /// Add a WHERE condition.
    pub fn filter(mut self, condition: impl Into<WhereExpr>) -> Self {
        let new_where = condition.into();
        self.where_clause = Some(match self.where_clause.take() {
            Some(existing) => existing.and_with(new_where),
            None => new_where,
        });
        self
    }

    /// Explicitly allow deleting all rows without a WHERE clause.
    ///
    /// Without this, executing without a `.filter()` returns an error.
    pub fn all_rows(mut self) -> Self {
        self.all_rows = true;
        self
    }

    /// Build the SQL statement without executing it.
    ///
    /// Useful for inspecting the generated SQL.
    pub fn build_sql(&self) -> OrmResult<Sql> {
        if self.where_clause.is_none() && !self.all_rows {
            return Err(OrmError::Validation(
                "delete_many requires a .filter() condition or .all_rows() to proceed. \
                 This prevents accidental full-table deletes."
                    .to_string(),
            ));
        }

        let mut sql = Sql::new("DELETE FROM ");
        sql.push_ident_ref(&self.table);

        if let Some(ref where_clause) = self.where_clause {
            sql.push(" WHERE ");
            where_clause.append_to_sql(&mut sql);
        }

        Ok(sql)
    }

    /// Execute the delete, returning the number of affected rows.
    pub async fn execute(self, conn: &impl GenericClient) -> OrmResult<u64> {
        let sql = self.build_sql()?;
        sql.execute(conn).await
    }

    /// Execute the delete and return the deleted rows.
    ///
    /// Appends `RETURNING *` to the query.
    pub async fn returning<T: FromRow>(self, conn: &impl GenericClient) -> OrmResult<Vec<T>> {
        let mut sql = self.build_sql()?;
        sql.push(" RETURNING *");
        sql.fetch_all_as(conn).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::condition::Condition;

    #[test]
    fn update_many_basic_sql() {
        let builder = UpdateManyBuilder {
            table: Ident::parse("users").unwrap(),
            sets: vec![SetExpr::set("status", "inactive").unwrap()],
            where_clause: Some(WhereExpr::Atom(Condition::eq("active", true).unwrap())),
            all_rows: false,
        };
        let sql = builder.build_sql().unwrap();
        assert_eq!(
            sql.to_sql(),
            "UPDATE users SET status = $1 WHERE active = $2"
        );
        assert_eq!(sql.params_ref().len(), 2);
    }

    #[test]
    fn update_many_multiple_sets() {
        let builder = UpdateManyBuilder {
            table: Ident::parse("orders").unwrap(),
            sets: vec![
                SetExpr::set("status", "shipped").unwrap(),
                SetExpr::raw("shipped_at = NOW()"),
            ],
            where_clause: Some(WhereExpr::Atom(Condition::eq("id", 1_i64).unwrap())),
            all_rows: false,
        };
        let sql = builder.build_sql().unwrap();
        assert_eq!(
            sql.to_sql(),
            "UPDATE orders SET status = $1, shipped_at = NOW() WHERE id = $2"
        );
        assert_eq!(sql.params_ref().len(), 2);
    }

    #[test]
    fn update_many_increment() {
        let builder = UpdateManyBuilder {
            table: Ident::parse("products").unwrap(),
            sets: vec![SetExpr::increment("view_count", 1).unwrap()],
            where_clause: Some(WhereExpr::Atom(Condition::eq("id", 42_i64).unwrap())),
            all_rows: false,
        };
        let sql = builder.build_sql().unwrap();
        assert_eq!(
            sql.to_sql(),
            "UPDATE products SET view_count = view_count + 1 WHERE id = $1"
        );
        assert_eq!(sql.params_ref().len(), 1);
    }

    #[test]
    fn update_many_decrement() {
        let builder = UpdateManyBuilder {
            table: Ident::parse("products").unwrap(),
            sets: vec![SetExpr::increment("stock", -5).unwrap()],
            where_clause: Some(WhereExpr::Atom(Condition::eq("id", 1_i64).unwrap())),
            all_rows: false,
        };
        let sql = builder.build_sql().unwrap();
        assert_eq!(
            sql.to_sql(),
            "UPDATE products SET stock = stock - 5 WHERE id = $1"
        );
    }

    #[test]
    fn update_many_all_rows() {
        let builder = UpdateManyBuilder {
            table: Ident::parse("temp_data").unwrap(),
            sets: vec![SetExpr::set("status", "archived").unwrap()],
            where_clause: None,
            all_rows: true,
        };
        let sql = builder.build_sql().unwrap();
        assert_eq!(sql.to_sql(), "UPDATE temp_data SET status = $1");
    }

    #[test]
    fn update_many_rejects_no_where() {
        let builder = UpdateManyBuilder {
            table: Ident::parse("users").unwrap(),
            sets: vec![SetExpr::set("status", "x").unwrap()],
            where_clause: None,
            all_rows: false,
        };
        assert!(builder.build_sql().is_err());
    }

    #[test]
    fn delete_many_basic_sql() {
        let builder = DeleteManyBuilder {
            table: Ident::parse("sessions").unwrap(),
            where_clause: Some(WhereExpr::raw("expires_at < NOW()")),
            all_rows: false,
        };
        let sql = builder.build_sql().unwrap();
        assert_eq!(
            sql.to_sql(),
            "DELETE FROM sessions WHERE expires_at < NOW()"
        );
    }

    #[test]
    fn delete_many_with_condition() {
        let builder = DeleteManyBuilder {
            table: Ident::parse("audit_logs").unwrap(),
            where_clause: Some(WhereExpr::And(vec![
                WhereExpr::Atom(Condition::eq("level", "debug").unwrap()),
                WhereExpr::Atom(Condition::eq("archived", true).unwrap()),
            ])),
            all_rows: false,
        };
        let sql = builder.build_sql().unwrap();
        assert_eq!(
            sql.to_sql(),
            "DELETE FROM audit_logs WHERE (level = $1 AND archived = $2)"
        );
        assert_eq!(sql.params_ref().len(), 2);
    }

    #[test]
    fn delete_many_all_rows() {
        let builder = DeleteManyBuilder {
            table: Ident::parse("temp_data").unwrap(),
            where_clause: None,
            all_rows: true,
        };
        let sql = builder.build_sql().unwrap();
        assert_eq!(sql.to_sql(), "DELETE FROM temp_data");
    }

    #[test]
    fn delete_many_rejects_no_where() {
        let builder = DeleteManyBuilder {
            table: Ident::parse("users").unwrap(),
            where_clause: None,
            all_rows: false,
        };
        assert!(builder.build_sql().is_err());
    }

    #[test]
    fn update_many_via_sql_builder() {
        let builder = crate::sql("users")
            .update_many([SetExpr::set("status", "inactive").unwrap()])
            .unwrap()
            .filter(Condition::eq("active", true).unwrap());
        let sql = builder.build_sql().unwrap();
        assert_eq!(
            sql.to_sql(),
            "UPDATE users SET status = $1 WHERE active = $2"
        );
    }

    #[test]
    fn delete_many_via_sql_builder() {
        let builder = crate::sql("sessions")
            .delete_many()
            .unwrap()
            .filter(WhereExpr::raw("expires_at < NOW()"));
        let sql = builder.build_sql().unwrap();
        assert_eq!(
            sql.to_sql(),
            "DELETE FROM sessions WHERE expires_at < NOW()"
        );
    }

    #[test]
    fn update_many_filter_combines_with_and() {
        let builder = crate::sql("orders")
            .update_many([SetExpr::set("status", "archived").unwrap()])
            .unwrap()
            .filter(Condition::eq("status", "cancelled").unwrap())
            .filter(Condition::eq("archived", false).unwrap());
        let sql = builder.build_sql().unwrap();
        assert_eq!(
            sql.to_sql(),
            "UPDATE orders SET status = $1 WHERE (status = $2 AND archived = $3)"
        );
    }

    #[test]
    fn delete_many_filter_combines_with_and() {
        let builder = crate::sql("logs")
            .delete_many()
            .unwrap()
            .filter(Condition::eq("level", "debug").unwrap())
            .filter(Condition::eq("archived", true).unwrap());
        let sql = builder.build_sql().unwrap();
        assert_eq!(
            sql.to_sql(),
            "DELETE FROM logs WHERE (level = $1 AND archived = $2)"
        );
    }

    #[test]
    fn set_expr_validates_column_name() {
        assert!(SetExpr::set("valid_column", "value").is_ok());
        assert!(SetExpr::set("1invalid", "value").is_err());
        assert!(SetExpr::set("has space", "value").is_err());
        assert!(SetExpr::increment("valid_col", 1).is_ok());
        assert!(SetExpr::increment("bad;col", 1).is_err());
    }
}