oxide-sql-core 0.2.0

Type-safe SQL parser and builder with compile-time validation
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
//! Invoicing System - Type-Safe Schema Example
//!
//! This example demonstrates a complete invoicing system with:
//! - Multi-tenant architecture (multiple companies)
//! - Multi-currency support
//! - Client management
//! - Invoice lifecycle (draft -> sent -> paid/overdue)
//!
//! Run with: cargo run --example invoicing

use oxide_sql_core::builder::{Select, col};
use oxide_sql_derive::Table;

// =============================================================================
// SCHEMA DEFINITIONS
// =============================================================================

/// Company - the tenant in a multi-tenant invoicing system.
#[allow(dead_code)]
#[derive(Debug, Clone, Table)]
#[table(name = "companies")]
pub struct Company {
    #[column(primary_key)]
    pub id: i64,
    pub name: String,
    #[column(nullable)]
    pub tax_id: Option<String>,
    pub default_currency: String,
    #[column(nullable)]
    pub address: Option<String>,
    pub created_at: String,
}

/// Client - customers who receive invoices.
#[allow(dead_code)]
#[derive(Debug, Clone, Table)]
#[table(name = "clients")]
pub struct Client {
    #[column(primary_key)]
    pub id: i64,
    pub company_id: i64,
    pub name: String,
    #[column(nullable)]
    pub email: Option<String>,
    #[column(nullable)]
    pub tax_id: Option<String>,
    pub preferred_currency: String,
    #[column(nullable)]
    pub payment_terms_days: Option<i64>,
    pub created_at: String,
}

/// Invoice - the main billing document.
#[allow(dead_code)]
#[derive(Debug, Clone, Table)]
#[table(name = "invoices")]
pub struct Invoice {
    #[column(primary_key)]
    pub id: i64,
    pub company_id: i64,
    pub client_id: i64,
    pub invoice_number: String,
    pub status: String,
    pub currency: String,
    pub subtotal_cents: i64,
    pub tax_rate_percent: f64,
    pub tax_amount_cents: i64,
    pub total_cents: i64,
    pub issue_date: String,
    pub due_date: String,
    #[column(nullable)]
    pub paid_at: Option<String>,
    #[column(nullable)]
    pub notes: Option<String>,
    pub created_at: String,
}

/// InvoiceLine - individual items on an invoice.
#[allow(dead_code)]
#[derive(Debug, Clone, Table)]
#[table(name = "invoice_lines")]
pub struct InvoiceLine {
    #[column(primary_key)]
    pub id: i64,
    pub invoice_id: i64,
    pub description: String,
    pub quantity: f64,
    pub unit_price_cents: i64,
    pub total_cents: i64,
    pub sort_order: i64,
}

/// Payment - records of payments received.
#[allow(dead_code)]
#[derive(Debug, Clone, Table)]
#[table(name = "payments")]
pub struct Payment {
    #[column(primary_key)]
    pub id: i64,
    pub invoice_id: i64,
    pub amount_cents: i64,
    pub currency: String,
    pub payment_method: String,
    #[column(nullable)]
    pub reference: Option<String>,
    pub paid_at: String,
}

/// ExchangeRate - for multi-currency support.
#[allow(dead_code)]
#[derive(Debug, Clone, Table)]
#[table(name = "exchange_rates")]
pub struct ExchangeRate {
    #[column(primary_key)]
    pub id: i64,
    pub from_currency: String,
    pub to_currency: String,
    pub rate: f64,
    pub effective_date: String,
}

// =============================================================================
// HELPER TO PRINT SQL
// =============================================================================

fn print_sql(description: &str, sql: &str) {
    println!("-- {}", description);
    println!("{};", sql);
    println!();
}

// =============================================================================
// EXAMPLE QUERIES
// =============================================================================

fn main() {
    println!("-- =============================================================================");
    println!("-- INVOICING SYSTEM - SQL QUERIES");
    println!("-- =============================================================================");
    println!();

    let company_id = 1_i64;
    let today = "2024-01-15";

    // -------------------------------------------------------------------------
    // DASHBOARD QUERIES
    // -------------------------------------------------------------------------
    println!("-- Dashboard Queries");
    println!("-- -----------------");
    println!();

    // 1. Get all invoices for the current month
    let (sql, _) = Select::<InvoiceTable, _, _>::new()
        .select::<(
            InvoiceColumns::Id,
            InvoiceColumns::InvoiceNumber,
            InvoiceColumns::ClientId,
            InvoiceColumns::Status,
            InvoiceColumns::TotalCents,
            InvoiceColumns::Currency,
            InvoiceColumns::DueDate,
        )>()
        .from_table()
        .where_clause(
            col(Invoice::company_id())
                .eq(company_id)
                .and(col(Invoice::issue_date()).gt_eq("2024-01-01"))
                .and(col(Invoice::issue_date()).lt_eq("2024-01-31")),
        )
        .order_by(Invoice::issue_date(), false)
        .build();
    print_sql("Invoices for January 2024", &sql);

    // 2. Overdue invoices
    let (sql, _) = Select::<InvoiceTable, _, _>::new()
        .select_all()
        .from_table()
        .where_clause(
            col(Invoice::company_id())
                .eq(company_id)
                .and(col(Invoice::status()).eq("sent"))
                .and(col(Invoice::due_date()).lt(today)),
        )
        .order_by(Invoice::due_date(), true)
        .build();
    print_sql("Overdue invoices", &sql);

    // 3. Draft invoices pending review
    let (sql, _) = Select::<InvoiceTable, _, _>::new()
        .select::<(
            InvoiceColumns::Id,
            InvoiceColumns::InvoiceNumber,
            InvoiceColumns::ClientId,
            InvoiceColumns::TotalCents,
            InvoiceColumns::CreatedAt,
        )>()
        .from_table()
        .where_clause(
            col(Invoice::company_id())
                .eq(company_id)
                .and(col(Invoice::status()).eq("draft")),
        )
        .order_by(Invoice::created_at(), false)
        .build();
    print_sql("Draft invoices pending review", &sql);

    // -------------------------------------------------------------------------
    // CLIENT QUERIES
    // -------------------------------------------------------------------------
    println!("-- Client Queries");
    println!("-- --------------");
    println!();

    // 4. All clients for a company
    let (sql, _) = Select::<ClientTable, _, _>::new()
        .select::<(
            ClientColumns::Id,
            ClientColumns::Name,
            ClientColumns::Email,
            ClientColumns::PreferredCurrency,
        )>()
        .from_table()
        .where_clause(col(Client::company_id()).eq(company_id))
        .order_by(Client::name(), true)
        .build();
    print_sql("All clients for company", &sql);

    // 5. Clients preferring EUR
    let (sql, _) = Select::<ClientTable, _, _>::new()
        .select_all()
        .from_table()
        .where_clause(
            col(Client::company_id())
                .eq(company_id)
                .and(col(Client::preferred_currency()).eq("EUR")),
        )
        .order_by(Client::name(), true)
        .build();
    print_sql("EUR-preferring clients", &sql);

    // 6. Search clients by name
    let (sql, _) = Select::<ClientTable, _, _>::new()
        .select::<(ClientColumns::Id, ClientColumns::Name, ClientColumns::Email)>()
        .from_table()
        .where_clause(
            col(Client::company_id())
                .eq(company_id)
                .and(col(Client::name()).like("%acme%")),
        )
        .limit(10)
        .build();
    print_sql("Search clients by name", &sql);

    // -------------------------------------------------------------------------
    // INVOICE QUERIES
    // -------------------------------------------------------------------------
    println!("-- Invoice Queries");
    println!("-- ---------------");
    println!();

    // 7. All invoices for a specific client
    let client_id = 42_i64;
    let (sql, _) = Select::<InvoiceTable, _, _>::new()
        .select_all()
        .from_table()
        .where_clause(
            col(Invoice::company_id())
                .eq(company_id)
                .and(col(Invoice::client_id()).eq(client_id)),
        )
        .order_by(Invoice::issue_date(), false)
        .build();
    print_sql(&format!("All invoices for client {}", client_id), &sql);

    // 8. Unpaid invoices (sent or overdue)
    let (sql, _) = Select::<InvoiceTable, _, _>::new()
        .select::<(
            InvoiceColumns::Id,
            InvoiceColumns::InvoiceNumber,
            InvoiceColumns::Status,
            InvoiceColumns::TotalCents,
            InvoiceColumns::DueDate,
        )>()
        .from_table()
        .where_clause(
            col(Invoice::company_id())
                .eq(company_id)
                .and(col(Invoice::status()).in_list(vec!["sent", "overdue"])),
        )
        .order_by(Invoice::due_date(), true)
        .build();
    print_sql("Unpaid invoices (sent or overdue)", &sql);

    // 9. Large EUR invoices (> 10,000 EUR)
    let min_amount = 1000000_i64;
    let (sql, _) = Select::<InvoiceTable, _, _>::new()
        .select::<(
            InvoiceColumns::Id,
            InvoiceColumns::InvoiceNumber,
            InvoiceColumns::ClientId,
            InvoiceColumns::TotalCents,
        )>()
        .from_table()
        .where_clause(
            col(Invoice::company_id())
                .eq(company_id)
                .and(col(Invoice::total_cents()).gt(min_amount))
                .and(col(Invoice::currency()).eq("EUR")),
        )
        .order_by(Invoice::total_cents(), false)
        .build();
    print_sql("Large EUR invoices (> 10,000 EUR)", &sql);

    // 10. Recently paid invoices
    let (sql, _) = Select::<InvoiceTable, _, _>::new()
        .select::<(
            InvoiceColumns::Id,
            InvoiceColumns::InvoiceNumber,
            InvoiceColumns::TotalCents,
            InvoiceColumns::PaidAt,
        )>()
        .from_table()
        .where_clause(
            col(Invoice::company_id())
                .eq(company_id)
                .and(col(Invoice::status()).eq("paid"))
                .and(col(Invoice::paid_at()).gt_eq("2024-01-08")),
        )
        .order_by(Invoice::paid_at(), false)
        .build();
    print_sql("Recently paid invoices (last 7 days)", &sql);

    // 11. Foreign currency invoices
    let (sql, _) = Select::<InvoiceTable, _, _>::new()
        .select::<(
            InvoiceColumns::Id,
            InvoiceColumns::InvoiceNumber,
            InvoiceColumns::Currency,
            InvoiceColumns::TotalCents,
        )>()
        .from_table()
        .where_clause(
            col(Invoice::company_id())
                .eq(company_id)
                .and(col(Invoice::currency()).not_eq("EUR")),
        )
        .order_by(Invoice::currency(), true)
        .build();
    print_sql("Foreign currency invoices (not EUR)", &sql);

    // -------------------------------------------------------------------------
    // INVOICE LINE QUERIES
    // -------------------------------------------------------------------------
    println!("-- Invoice Line Queries");
    println!("-- --------------------");
    println!();

    // 12. Invoice lines for a specific invoice
    let invoice_id = 123_i64;
    let (sql, _) = Select::<InvoiceLineTable, _, _>::new()
        .select_all()
        .from_table()
        .where_clause(col(InvoiceLine::invoice_id()).eq(invoice_id))
        .order_by(InvoiceLine::sort_order(), true)
        .build();
    print_sql(&format!("Invoice lines for invoice {}", invoice_id), &sql);

    // 13. High-value line items
    let (sql, _) = Select::<InvoiceLineTable, _, _>::new()
        .select::<(
            InvoiceLineColumns::Id,
            InvoiceLineColumns::InvoiceId,
            InvoiceLineColumns::Description,
            InvoiceLineColumns::TotalCents,
        )>()
        .from_table()
        .where_clause(col(InvoiceLine::total_cents()).gt(100000_i64))
        .order_by(InvoiceLine::total_cents(), false)
        .limit(50)
        .build();
    print_sql("High-value line items (> 1,000)", &sql);

    // -------------------------------------------------------------------------
    // PAYMENT QUERIES
    // -------------------------------------------------------------------------
    println!("-- Payment Queries");
    println!("-- ---------------");
    println!();

    // 14. All payments for an invoice
    let (sql, _) = Select::<PaymentTable, _, _>::new()
        .select_all()
        .from_table()
        .where_clause(col(Payment::invoice_id()).eq(invoice_id))
        .order_by(Payment::paid_at(), false)
        .build();
    print_sql(&format!("Payments for invoice {}", invoice_id), &sql);

    // 15. Recent bank/card payments
    let (sql, _) = Select::<PaymentTable, _, _>::new()
        .select::<(
            PaymentColumns::Id,
            PaymentColumns::InvoiceId,
            PaymentColumns::AmountCents,
            PaymentColumns::PaymentMethod,
            PaymentColumns::PaidAt,
        )>()
        .from_table()
        .where_clause(
            col(Payment::payment_method())
                .in_list(vec!["bank_transfer", "credit_card"])
                .and(col(Payment::paid_at()).gt_eq("2024-01-08")),
        )
        .order_by(Payment::paid_at(), false)
        .limit(100)
        .build();
    print_sql("Recent bank/card payments", &sql);

    // -------------------------------------------------------------------------
    // EXCHANGE RATE QUERIES
    // -------------------------------------------------------------------------
    println!("-- Exchange Rate Queries");
    println!("-- ---------------------");
    println!();

    // 16. Latest USD -> EUR rate
    let (sql, _) = Select::<ExchangeRateTable, _, _>::new()
        .select::<(
            ExchangeRateColumns::Rate,
            ExchangeRateColumns::EffectiveDate,
        )>()
        .from_table()
        .where_clause(
            col(ExchangeRate::from_currency())
                .eq("USD")
                .and(col(ExchangeRate::to_currency()).eq("EUR")),
        )
        .order_by(ExchangeRate::effective_date(), false)
        .limit(1)
        .build();
    print_sql("Latest USD -> EUR exchange rate", &sql);

    // 17. Historical EUR -> GBP rates
    let (sql, _) = Select::<ExchangeRateTable, _, _>::new()
        .select_all()
        .from_table()
        .where_clause(
            col(ExchangeRate::from_currency())
                .eq("EUR")
                .and(col(ExchangeRate::to_currency()).eq("GBP"))
                .and(col(ExchangeRate::effective_date()).gt_eq("2024-01-01"))
                .and(col(ExchangeRate::effective_date()).lt_eq("2024-01-31")),
        )
        .order_by(ExchangeRate::effective_date(), true)
        .build();
    print_sql("EUR -> GBP rates for January 2024", &sql);

    // -------------------------------------------------------------------------
    // PAGINATION EXAMPLE
    // -------------------------------------------------------------------------
    println!("-- Pagination Example");
    println!("-- ------------------");
    println!();

    let page = 3_i64;
    let per_page = 25_i64;
    let offset = (page - 1) * per_page;

    let (sql, _) = Select::<InvoiceTable, _, _>::new()
        .select_all()
        .from_table()
        .where_clause(col(Invoice::company_id()).eq(company_id))
        .order_by(Invoice::created_at(), false)
        .limit(per_page)
        .offset(offset)
        .build();
    print_sql(&format!("Invoices page {} (25 per page)", page), &sql);

    // -------------------------------------------------------------------------
    // COMPANY QUERIES
    // -------------------------------------------------------------------------
    println!("-- Company Queries");
    println!("-- ---------------");
    println!();

    // 18. Get company by ID
    let (sql, _) = Select::<CompanyTable, _, _>::new()
        .select_all()
        .from_table()
        .where_clause(col(Company::id()).eq(company_id))
        .build();
    print_sql("Get company by ID", &sql);

    // 19. Companies using USD
    let (sql, _) = Select::<CompanyTable, _, _>::new()
        .select::<(
            CompanyColumns::Id,
            CompanyColumns::Name,
            CompanyColumns::DefaultCurrency,
        )>()
        .from_table()
        .where_clause(col(Company::default_currency()).eq("USD"))
        .order_by(Company::name(), true)
        .build();
    print_sql("Companies using USD as default currency", &sql);

    println!("-- =============================================================================");
    println!("-- END OF QUERIES");
    println!("-- =============================================================================");
}