ignis-db 0.2.0

A comprehensive PostgreSQL ORM with macro-driven schema management
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
514
515
516
517
518
519
520
521
522
use ignis_db::builders::SequenceBuilder;
use ignis_db::builders::{ConditionalBuilder, InspectableBuilder, SchemaBuilder};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== SequenceBuilder Demo ===\n");

    // 1. Basic sequences
    println!("1. Basic Sequences:");
    println!("-------------------");

    let user_id_seq = SequenceBuilder::new()
        .name("user_id_seq")
        .data_type("BIGINT")
        .start_value(1)
        .increment(1)
        .build()?;

    println!("User ID Sequence: {:?}", user_id_seq);

    let order_id_seq = SequenceBuilder::new()
        .name("order_id_seq")
        .data_type("INTEGER")
        .start_value(1000)
        .increment(10)
        .build()?;

    println!("Order ID Sequence: {:?}", order_id_seq);

    // 2. Auto-increment sequences
    println!("\n2. Auto-Increment Sequences:");
    println!("------------------------------");

    let auto_seq = SequenceBuilder::auto_increment()
        .name("auto_increment_seq")
        .build()?;

    let small_auto_seq = SequenceBuilder::small_auto_increment()
        .name("small_auto_seq")
        .build()?;

    let large_auto_seq = SequenceBuilder::large_auto_increment()
        .name("large_auto_seq")
        .build()?;

    println!("Auto-Increment Sequence: {:?}", auto_seq);
    println!("Small Auto-Increment Sequence: {:?}", small_auto_seq);
    println!("Large Auto-Increment Sequence: {:?}", large_auto_seq);

    // 3. Specialized sequence types
    println!("\n3. Specialized Sequence Types:");
    println!("--------------------------------");

    let timestamp_seq = SequenceBuilder::timestamp().name("timestamp_seq").build()?;

    let counter_seq = SequenceBuilder::counter().name("counter_seq").build()?;

    let cycling_seq = SequenceBuilder::cycling().name("cycling_seq").build()?;

    let cached_seq = SequenceBuilder::cached().name("cached_seq").build()?;

    println!("Timestamp Sequence: {:?}", timestamp_seq);
    println!("Counter Sequence: {:?}", counter_seq);
    println!("Cycling Sequence: {:?}", cycling_seq);
    println!("Cached Sequence: {:?}", cached_seq);

    // 4. Sequences with schema
    println!("\n4. Sequences with Schema:");
    println!("---------------------------");

    let schema_seq = SequenceBuilder::new()
        .name("audit_id_seq")
        .in_schema("audit")
        .data_type("BIGINT")
        .start_value(1)
        .increment(1)
        .build()?;

    let public_seq = SequenceBuilder::new()
        .name("public_seq")
        .in_schema("public")
        .data_type("INTEGER")
        .start_value(0)
        .increment(1)
        .build()?;

    println!("Schema Sequence: {:?}", schema_seq);
    println!("Public Sequence: {:?}", public_seq);

    // 5. Sequences with constraints
    println!("\n5. Sequences with Constraints:");
    println!("--------------------------------");

    let constrained_seq = SequenceBuilder::new()
        .name("constrained_seq")
        .data_type("INTEGER")
        .start_value(1)
        .minimum_value(1)
        .maximum_value(1000)
        .increment(1)
        .cycle(false)
        .build()?;

    let negative_seq = SequenceBuilder::new()
        .name("negative_seq")
        .data_type("INTEGER")
        .start_value(-100)
        .minimum_value(-1000)
        .maximum_value(0)
        .increment(10)
        .build()?;

    println!("Constrained Sequence: {:?}", constrained_seq);
    println!("Negative Sequence: {:?}", negative_seq);

    // 6. Sequences with cycle behavior
    println!("\n6. Sequences with Cycle Behavior:");
    println!("-----------------------------------");

    let cycling_positive_seq = SequenceBuilder::new()
        .name("cycling_positive")
        .data_type("INTEGER")
        .start_value(1)
        .minimum_value(1)
        .maximum_value(100)
        .increment(1)
        .cycle(true)
        .build()?;

    let no_cycle_seq = SequenceBuilder::new()
        .name("no_cycle_seq")
        .data_type("BIGINT")
        .start_value(1)
        .minimum_value(1)
        .maximum_value(9223372036854775807)
        .increment(1)
        .cycle(false)
        .build()?;

    println!("Cycling Positive Sequence: {:?}", cycling_positive_seq);
    println!("No Cycle Sequence: {:?}", no_cycle_seq);

    // 7. Sequences with cache settings
    println!("\n7. Sequences with Cache Settings:");
    println!("-----------------------------------");

    let small_cache_seq = SequenceBuilder::new()
        .name("small_cache_seq")
        .data_type("INTEGER")
        .start_value(1)
        .increment(1)
        .cache(1)
        .build()?;

    let large_cache_seq = SequenceBuilder::new()
        .name("large_cache_seq")
        .data_type("BIGINT")
        .start_value(1)
        .increment(1)
        .cache(100)
        .build()?;

    let no_cache_seq = SequenceBuilder::new()
        .name("no_cache_seq")
        .data_type("INTEGER")
        .start_value(1)
        .increment(1)
        .build()?;

    println!("Small Cache Sequence: {:?}", small_cache_seq);
    println!("Large Cache Sequence: {:?}", large_cache_seq);
    println!("No Cache Sequence: {:?}", no_cache_seq);

    // 8. Sequences with owners
    println!("\n8. Sequences with Owners:");
    println!("---------------------------");

    let owned_seq = SequenceBuilder::new()
        .name("owned_sequence")
        .data_type("BIGINT")
        .owner("app_user")
        .build()?;

    let system_owned_seq = SequenceBuilder::new()
        .name("system_sequence")
        .data_type("INTEGER")
        .owner("postgres")
        .build()?;

    println!("Owned Sequence: {:?}", owned_seq);
    println!("System Owned Sequence: {:?}", system_owned_seq);

    // 9. Sequences with comments
    println!("\n9. Sequences with Comments:");
    println!("------------------------------");

    let commented_seq = SequenceBuilder::new()
        .name("commented_sequence")
        .data_type("BIGINT")
        .comment("This sequence generates unique user IDs")
        .build()?;

    let detailed_seq = SequenceBuilder::new()
        .name("detailed_sequence")
        .data_type("INTEGER")
        .comment("Sequence for order numbers, starts at 1000, increments by 1")
        .start_value(1000)
        .increment(1)
        .build()?;

    println!("Commented Sequence: {:?}", commented_seq);
    println!("Detailed Sequence: {:?}", detailed_seq);

    // 10. Complex sequences with all properties
    println!("\n10. Complex Sequences with All Properties:");
    println!("---------------------------------------------");

    let complex_seq = SequenceBuilder::new()
        .name("complex_sequence")
        .in_schema("analytics")
        .data_type("BIGINT")
        .start_value(1000000)
        .minimum_value(1)
        .maximum_value(999999999)
        .increment(1000)
        .cycle(false)
        .cache(50)
        .owner("analytics_user")
        .comment("High-performance sequence for analytics data")
        .build()?;

    let business_seq = SequenceBuilder::new()
        .name("business_sequence")
        .in_schema("business")
        .data_type("INTEGER")
        .start_value(50000)
        .minimum_value(10000)
        .maximum_value(99999)
        .increment(1)
        .cycle(true)
        .cache(10)
        .owner("business_user")
        .comment("Cycling sequence for business transaction IDs")
        .build()?;

    println!("Complex Sequence: {:?}", complex_seq);
    println!("Business Sequence: {:?}", business_seq);

    // 11. Conditional sequence building
    println!("\n11. Conditional Sequence Building:");
    println!("-----------------------------------");

    let is_production = std::env::var("ENV").unwrap_or_default() == "production";

    let conditional_seq = SequenceBuilder::new()
        .when(is_production, |s| {
            s.set_name("prod_sequence");
            s.set_data_type("BIGINT");
            s.set_start_value(1000000);
            s.set_increment(1);
            s.set_cache(100);
            s.set_owner("prod_user");
        })
        .when(!is_production, |s| {
            s.set_name("dev_sequence");
            s.set_data_type("INTEGER");
            s.set_start_value(1);
            s.set_increment(1);
            s.set_cache(1);
            s.set_owner("dev_user");
        })
        .build()?;

    println!("Conditional Sequence: {:?}", conditional_seq);

    // 12. Sequence validation
    println!("\n12. Sequence Validation:");
    println!("-------------------------");

    // This should fail validation
    let invalid_result = SequenceBuilder::new().data_type("BIGINT").build();
    match invalid_result {
        Ok(_) => println!("❌ Unexpected success for invalid sequence"),
        Err(e) => println!("✅ Correctly caught validation error: {}", e),
    }

    // This should pass validation
    let valid_result = SequenceBuilder::new()
        .name("valid_sequence")
        .data_type("INTEGER")
        .build();

    match valid_result {
        Ok(_) => println!("✅ Valid sequence built successfully"),
        Err(e) => println!("❌ Unexpected error for valid sequence: {}", e),
    }

    // 13. Sequence inspection
    println!("\n13. Sequence Inspection:");
    println!("-------------------------");

    let builder = SequenceBuilder::new()
        .name("inspect_sequence")
        .in_schema("public")
        .data_type("BIGINT")
        .start_value(1)
        .minimum_value(1)
        .maximum_value(1000000)
        .increment(1)
        .cycle(false)
        .cache(10)
        .owner("postgres")
        .comment("This is a test sequence");

    println!("Debug info: {}", builder.debug_info());
    println!("Summary: {}", builder.summary());
    println!("Has name: {}", builder.has_name());
    println!("Has schema: {}", builder.has_schema());
    println!("Has data type: {}", builder.has_data_type());
    println!("Has start value: {}", builder.has_start_value());
    println!("Has minimum value: {}", builder.has_minimum_value());
    println!("Has maximum value: {}", builder.has_maximum_value());
    println!("Has increment: {}", builder.has_increment());
    println!("Has cycle: {}", builder.has_cycle());
    println!("Has cache: {}", builder.has_cache());
    println!("Has owner: {}", builder.has_owner());
    println!("Has comment: {}", builder.has_comment());

    // 14. Performance optimization sequences
    println!("\n14. Performance Optimization Sequences:");
    println!("----------------------------------------");

    let high_perf_seq = SequenceBuilder::new()
        .name("high_perf_seq")
        .data_type("BIGINT")
        .start_value(1)
        .increment(1)
        .cache(1000)
        .comment("High-performance sequence with large cache")
        .build()?;

    let batch_seq = SequenceBuilder::new()
        .name("batch_seq")
        .data_type("INTEGER")
        .start_value(1)
        .increment(100)
        .cache(100)
        .comment("Batch processing sequence")
        .build()?;

    println!("High Performance Sequence: {:?}", high_perf_seq);
    println!("Batch Sequence: {:?}", batch_seq);

    // 15. Business logic sequences
    println!("\n15. Business Logic Sequences:");
    println!("--------------------------------");

    let invoice_seq = SequenceBuilder::new()
        .name("invoice_seq")
        .data_type("INTEGER")
        .start_value(10000)
        .minimum_value(10000)
        .maximum_value(99999)
        .increment(1)
        .cycle(true)
        .comment("Invoice numbering sequence (5 digits, cycles)")
        .build()?;

    let sku_seq = SequenceBuilder::new()
        .name("sku_seq")
        .data_type("BIGINT")
        .start_value(1000000000)
        .increment(1)
        .comment("SKU generation sequence")
        .build()?;

    println!("Invoice Sequence: {:?}", invoice_seq);
    println!("SKU Sequence: {:?}", sku_seq);

    // 16. Data migration sequences
    println!("\n16. Data Migration Sequences:");
    println!("--------------------------------");

    let migration_seq = SequenceBuilder::new()
        .name("migration_seq")
        .data_type("BIGINT")
        .start_value(1)
        .increment(1)
        .cache(1)
        .comment("Sequence for tracking data migration progress")
        .build()?;

    let sync_seq = SequenceBuilder::new()
        .name("sync_seq")
        .data_type("INTEGER")
        .start_value(0)
        .increment(1)
        .comment("Synchronization sequence for data sync operations")
        .build()?;

    println!("Migration Sequence: {:?}", migration_seq);
    println!("Sync Sequence: {:?}", sync_seq);

    // 17. Monitoring and maintenance sequences
    println!("\n17. Monitoring and Maintenance Sequences:");
    println!("------------------------------------------");

    let monitoring_seq = SequenceBuilder::new()
        .name("monitoring_seq")
        .data_type("BIGINT")
        .start_value(1)
        .increment(1)
        .cache(10)
        .comment("Sequence for monitoring system events")
        .build()?;

    let maintenance_seq = SequenceBuilder::new()
        .name("maintenance_seq")
        .data_type("INTEGER")
        .start_value(1)
        .increment(1)
        .cycle(true)
        .maximum_value(100)
        .comment("Maintenance task sequence (cycles every 100)")
        .build()?;

    println!("Monitoring Sequence: {:?}", monitoring_seq);
    println!("Maintenance Sequence: {:?}", maintenance_seq);

    // 18. Security and compliance sequences
    println!("\n18. Security and Compliance Sequences:");
    println!("---------------------------------------");

    let audit_seq = SequenceBuilder::new()
        .name("audit_seq")
        .data_type("BIGINT")
        .start_value(1)
        .increment(1)
        .owner("audit_user")
        .comment("Audit trail sequence for compliance tracking")
        .build()?;

    let security_seq = SequenceBuilder::new()
        .name("security_seq")
        .data_type("INTEGER")
        .start_value(1000)
        .increment(1)
        .owner("security_user")
        .comment("Security event sequence")
        .build()?;

    println!("Audit Sequence: {:?}", audit_seq);
    println!("Security Sequence: {:?}", security_seq);

    // 19. Integration sequences
    println!("\n19. Integration Sequences:");
    println!("----------------------------");

    let api_seq = SequenceBuilder::new()
        .name("api_seq")
        .data_type("BIGINT")
        .start_value(1)
        .increment(1)
        .cache(100)
        .comment("API request tracking sequence")
        .build()?;

    let webhook_seq = SequenceBuilder::new()
        .name("webhook_seq")
        .data_type("INTEGER")
        .start_value(1)
        .increment(1)
        .comment("Webhook delivery tracking sequence")
        .build()?;

    println!("API Sequence: {:?}", api_seq);
    println!("Webhook Sequence: {:?}", webhook_seq);

    // 20. Comprehensive sequence with all properties
    println!("\n20. Comprehensive Sequence with All Properties:");
    println!("-------------------------------------------------");

    let comprehensive_seq = SequenceBuilder::new()
        .name("comprehensive_sequence")
        .in_schema("enterprise")
        .data_type("BIGINT")
        .start_value(1000000000)
        .minimum_value(1)
        .maximum_value(9223372036854775807)
        .increment(1)
        .cycle(false)
        .cache(1000)
        .owner("enterprise_user")
        .comment("Enterprise-grade sequence for high-volume applications with comprehensive configuration")
        .build()?;

    println!("Comprehensive Sequence: {:?}", comprehensive_seq);

    // 21. Sequence DDL generation
    println!("\n21. Sequence DDL Generation:");
    println!("-------------------------------");

    let ddl_seq = SequenceBuilder::new()
        .name("ddl_test_seq")
        .in_schema("test")
        .data_type("INTEGER")
        .start_value(1)
        .minimum_value(1)
        .maximum_value(1000)
        .increment(1)
        .cycle(true)
        .cache(10)
        .owner("test_user")
        .comment("Test sequence for DDL generation")
        .build()?;

    println!("Sequence: {:?}", ddl_seq);
    println!("DDL: {}", ddl_seq.generate_create_ddl());

    println!("\n=== SequenceBuilder Demo Complete ===");

    Ok(())
}