pgorm 0.3.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
//! Example demonstrating PgClient - the unified client with monitoring and SQL checking
//!
//! Run with: cargo run --example pg_client -p pgorm
//!
//! Set DATABASE_URL in .env file or environment variable:
//! DATABASE_URL=postgres://postgres:postgres@localhost/pgorm_example

mod common;

use comfy_table::{Attribute, Cell, Color, ContentArrangement, Table, presets::UTF8_FULL};
use common::{
    print_banner, print_done, print_header, print_info, print_success, print_warning,
    setup_products_categories_schema,
};
use pgorm::{CheckMode, FromRow, Model, OrmError, PgClient, PgClientConfig, create_pool, query};
use std::env;
use std::time::Duration;

#[derive(Debug, FromRow, Model)]
#[orm(table = "products")]
#[allow(dead_code)]
struct Product {
    #[orm(id)]
    id: i64,
    name: String,
    price_cents: i64,
    in_stock: bool,
}

#[derive(Debug, FromRow, Model)]
#[orm(table = "categories")]
#[allow(dead_code)]
struct Category {
    #[orm(id)]
    id: i64,
    name: String,
}

/// Create a styled table for schema registry
fn create_registry_table(pg: &PgClient<impl pgorm::GenericClient>) -> Table {
    let mut table = Table::new();
    table
        .load_preset(UTF8_FULL)
        .set_content_arrangement(ContentArrangement::Dynamic)
        .set_header(vec![
            Cell::new("Table")
                .add_attribute(Attribute::Bold)
                .fg(Color::Cyan),
            Cell::new("Schema")
                .add_attribute(Attribute::Bold)
                .fg(Color::Cyan),
            Cell::new("Columns")
                .add_attribute(Attribute::Bold)
                .fg(Color::Cyan),
        ]);

    for t in pg.registry().tables() {
        let columns: Vec<String> = t
            .columns
            .iter()
            .map(|c| {
                if c.is_primary_key {
                    format!("{}*", c.name)
                } else {
                    c.name.clone()
                }
            })
            .collect();

        table.add_row(vec![
            Cell::new(&t.name).fg(Color::Green),
            Cell::new(&t.schema).fg(Color::DarkGrey),
            Cell::new(columns.join(", ")),
        ]);
    }

    table
}

/// Create a styled table for SQL check results
fn create_check_results_table(
    results: &[(String, String, Vec<pgorm::check::SchemaIssue>)],
) -> Table {
    let mut table = Table::new();
    table
        .load_preset(UTF8_FULL)
        .set_content_arrangement(ContentArrangement::Dynamic)
        .set_header(vec![
            Cell::new("Status")
                .add_attribute(Attribute::Bold)
                .fg(Color::Cyan),
            Cell::new("Test")
                .add_attribute(Attribute::Bold)
                .fg(Color::Cyan),
            Cell::new("SQL")
                .add_attribute(Attribute::Bold)
                .fg(Color::Cyan),
            Cell::new("Issues")
                .add_attribute(Attribute::Bold)
                .fg(Color::Cyan),
        ]);

    for (desc, sql, issues) in results {
        let (status, status_color) = if issues.is_empty() {
            ("✓ PASS", Color::Green)
        } else {
            ("✗ FAIL", Color::Red)
        };

        let issue_text = if issues.is_empty() {
            "None".to_string()
        } else {
            issues
                .iter()
                .map(|i| format!("{:?}: {}", i.kind, i.message))
                .collect::<Vec<_>>()
                .join("\n")
        };

        table.add_row(vec![
            Cell::new(status)
                .fg(status_color)
                .add_attribute(Attribute::Bold),
            Cell::new(desc),
            Cell::new(sql).fg(Color::DarkGrey),
            Cell::new(&issue_text),
        ]);
    }

    table
}

/// Create a styled table for query statistics
fn create_stats_table(stats: &pgorm::monitor::QueryStats) -> Table {
    let mut table = Table::new();
    table
        .load_preset(UTF8_FULL)
        .set_content_arrangement(ContentArrangement::Dynamic)
        .set_header(vec![
            Cell::new("Metric")
                .add_attribute(Attribute::Bold)
                .fg(Color::Cyan),
            Cell::new("Value")
                .add_attribute(Attribute::Bold)
                .fg(Color::Cyan),
        ]);

    table.add_row(vec![
        Cell::new("Total Queries"),
        Cell::new(stats.total_queries.to_string()).fg(Color::Yellow),
    ]);
    table.add_row(vec![
        Cell::new("Total Duration"),
        Cell::new(format!("{:?}", stats.total_duration)).fg(Color::Yellow),
    ]);
    table.add_row(vec![
        Cell::new("SELECT Count"),
        Cell::new(stats.select_count.to_string()).fg(Color::Green),
    ]);
    table.add_row(vec![
        Cell::new("INSERT Count"),
        Cell::new(stats.insert_count.to_string()).fg(Color::Blue),
    ]);
    table.add_row(vec![
        Cell::new("UPDATE Count"),
        Cell::new(stats.update_count.to_string()).fg(Color::Magenta),
    ]);
    table.add_row(vec![
        Cell::new("DELETE Count"),
        Cell::new(stats.delete_count.to_string()).fg(Color::Red),
    ]);
    table.add_row(vec![
        Cell::new("Max Duration"),
        Cell::new(format!("{:?}", stats.max_duration)).fg(Color::Yellow),
    ]);
    if stats.total_queries > 0 {
        let avg = stats.total_duration / stats.total_queries as u32;
        table.add_row(vec![
            Cell::new("Avg Duration"),
            Cell::new(format!("{avg:?}")).fg(Color::Yellow),
        ]);
    }

    table
}

/// Create a styled table for validation results
fn create_validation_table(
    results: &[(&str, Result<Vec<tokio_postgres::Row>, OrmError>)],
) -> Table {
    let mut table = Table::new();
    table
        .load_preset(UTF8_FULL)
        .set_content_arrangement(ContentArrangement::Dynamic)
        .set_header(vec![
            Cell::new("Status")
                .add_attribute(Attribute::Bold)
                .fg(Color::Cyan),
            Cell::new("Description")
                .add_attribute(Attribute::Bold)
                .fg(Color::Cyan),
            Cell::new("Result")
                .add_attribute(Attribute::Bold)
                .fg(Color::Cyan),
        ]);

    for (desc, result) in results {
        let (status, status_color, result_text) = match result {
            Ok(rows) => ("✓", Color::Green, format!("Success: {} rows", rows.len())),
            Err(OrmError::Validation(msg)) => ("✗", Color::Red, format!("Validation: {msg}")),
            Err(e) => ("âš ", Color::Yellow, format!("DB Error: {e}")),
        };

        table.add_row(vec![
            Cell::new(status)
                .fg(status_color)
                .add_attribute(Attribute::Bold),
            Cell::new(*desc),
            Cell::new(&result_text),
        ]);
    }

    table
}

#[tokio::main]
async fn main() -> Result<(), OrmError> {
    dotenvy::dotenv().ok();

    print_banner("PgClient Demo - Schema Checking");

    let database_url =
        env::var("DATABASE_URL").expect("DATABASE_URL must be set in .env or environment");

    let pool = create_pool(&database_url)?;
    let client = pool.get().await?;

    // Setup: Create tables
    print_header("Setup: Creating Tables");
    setup_products_categories_schema(&client).await?;
    print_success("Tables created: products, categories");

    // ============================================
    // Example 1: Basic usage with defaults
    // ============================================
    print_header("1. Schema Registry (Auto-Registered Models)");

    let pg = PgClient::new(&client);

    print_info(&format!(
        "Registered {} tables automatically via #[derive(Model)]",
        pg.registry().len()
    ));

    println!();
    println!("{}", create_registry_table(&pg));

    // Insert some data
    query("INSERT INTO products (name, price_cents, in_stock) VALUES ($1, $2, $3)")
        .bind("Laptop")
        .bind(99999_i64)
        .bind(true)
        .execute(&pg)
        .await?;

    query("INSERT INTO products (name, price_cents, in_stock) VALUES ($1, $2, $3)")
        .bind("Mouse")
        .bind(2999_i64)
        .bind(true)
        .execute(&pg)
        .await?;

    let products = Product::select_all(&pg).await?;
    print_success(&format!("Inserted and queried {} products", products.len()));

    // ============================================
    // Example 2: Direct SQL checking
    // ============================================
    print_header("2. SQL Schema Validation");

    let test_queries = vec![
        (
            "Valid: existing columns".to_string(),
            "SELECT id, name FROM products".to_string(),
        ),
        (
            "Invalid: missing column 'email'".to_string(),
            "SELECT id, email FROM products".to_string(),
        ),
        (
            "Valid: JOIN query".to_string(),
            "SELECT * FROM products JOIN categories ON true".to_string(),
        ),
        (
            "Valid: multi-table".to_string(),
            "SELECT products.id, categories.id FROM products, categories".to_string(),
        ),
        (
            "Invalid: table 'orders'".to_string(),
            "SELECT id FROM orders".to_string(),
        ),
        (
            "Invalid: qualified column".to_string(),
            "SELECT products.nonexistent FROM products".to_string(),
        ),
    ];

    let results: Vec<_> = test_queries
        .iter()
        .map(|(desc, sql)| {
            let issues = pg.registry().check_sql(sql);
            (desc.clone(), sql.clone(), issues)
        })
        .collect();

    println!();
    println!("{}", create_check_results_table(&results));

    // ============================================
    // Example 3: Strict mode validation
    // ============================================
    print_header("3. Strict Mode (Blocks Invalid Queries)");

    let pg_strict = PgClient::with_config(&client, PgClientConfig::new().strict());

    let validation_tests: Vec<(&str, Result<Vec<tokio_postgres::Row>, OrmError>)> = vec![
        (
            "Valid query: SELECT id, name FROM products",
            query("SELECT id, name FROM products")
                .fetch_all(&pg_strict)
                .await,
        ),
        (
            "Invalid: non-existent table 'orders'",
            query("SELECT id FROM orders").fetch_all(&pg_strict).await,
        ),
        (
            "Invalid: non-existent column 'description'",
            query("SELECT id, description FROM products")
                .fetch_all(&pg_strict)
                .await,
        ),
        (
            "Invalid: qualified non-existent column",
            query("SELECT products.nonexistent FROM products")
                .fetch_all(&pg_strict)
                .await,
        ),
    ];

    println!();
    println!("{}", create_validation_table(&validation_tests));

    // ============================================
    // Example 4: WarnOnly mode
    // ============================================
    print_header("4. WarnOnly Mode (Logs But Doesn't Block)");

    let pg_warn = PgClient::with_config(&client, PgClientConfig::new());

    print_info("Running query with invalid column (will warn but attempt execution)...");
    println!();

    let result = query("SELECT id, nonexistent FROM products")
        .fetch_all(&pg_warn)
        .await;

    match result {
        Ok(_) => print_success("Query attempted (passed validation phase)"),
        Err(e) => print_warning(&format!("DB error (expected): {e}")),
    }

    // ============================================
    // Example 5: Query Statistics
    // ============================================
    print_header("5. Query Statistics");

    let pg_full = PgClient::with_config(
        &client,
        PgClientConfig::new()
            .check_mode(CheckMode::WarnOnly)
            .timeout(Duration::from_secs(30))
            .slow_threshold(Duration::from_millis(100))
            .with_stats()
            .log_slow_queries(Duration::from_millis(50)),
    );

    // Run several queries
    for _ in 0..5 {
        let _ = Product::select_all(&pg_full).await?;
    }

    // Insert
    query("INSERT INTO categories (name) VALUES ($1)")
        .bind("Electronics")
        .execute(&pg_full)
        .await?;

    // Update
    query("UPDATE products SET in_stock = $1 WHERE id = $2")
        .bind(false)
        .bind(1_i64)
        .execute(&pg_full)
        .await?;

    println!();
    println!("{}", create_stats_table(&pg_full.stats()));

    // ============================================
    // Summary
    // ============================================
    print_header("Summary");

    let mut summary_table = Table::new();
    summary_table
        .load_preset(UTF8_FULL)
        .set_content_arrangement(ContentArrangement::Dynamic)
        .set_header(vec![
            Cell::new("Feature")
                .add_attribute(Attribute::Bold)
                .fg(Color::Cyan),
            Cell::new("Description")
                .add_attribute(Attribute::Bold)
                .fg(Color::Cyan),
        ]);

    summary_table.add_row(vec![
        Cell::new("Auto-Registration").fg(Color::Green),
        Cell::new("Models with #[derive(Model)] are automatically registered"),
    ]);
    summary_table.add_row(vec![
        Cell::new("Schema Checking").fg(Color::Green),
        Cell::new("Validates SQL against registered table schemas"),
    ]);
    summary_table.add_row(vec![
        Cell::new("Check Modes").fg(Color::Green),
        Cell::new("Disabled, WarnOnly, or Strict"),
    ]);
    summary_table.add_row(vec![
        Cell::new("Query Statistics").fg(Color::Green),
        Cell::new("Tracks query counts, durations, and types"),
    ]);
    summary_table.add_row(vec![
        Cell::new("Slow Query Logging").fg(Color::Green),
        Cell::new("Configurable threshold for alerting"),
    ]);

    println!();
    println!("{summary_table}");

    print_done();

    Ok(())
}