drizzle_sqlite/pragma.rs
1//! SQLite PRAGMA statements for database configuration and introspection
2//!
3//! This module provides type-safe, ergonomic access to SQLite's PRAGMA statements.
4//! PRAGMA statements are SQL extension specific to SQLite and are used to modify
5//! the operation of the SQLite library or to query the SQLite library for internal
6//! (non-table) data.
7//!
8//! [SQLite PRAGMA Documentation](https://sqlite.org/pragma.html)
9//!
10//! ## Features
11//!
12//! - **Type Safety**: Enums for all pragma values (no string literals needed)
13//! - **Ergonomic API**: Uses `&'static str` instead of `String` - no `.to_string()` calls
14//! - **Documentation Links**: Each pragma links to official SQLite documentation
15//! - **ToSQL Integration**: Seamless integration with the query builder
16//!
17//! ## Categories
18//!
19//! - **Configuration**: `foreign_keys`, `journal_mode`, `synchronous`, etc.
20//! - **Introspection**: `table_info`, `index_list`, `compile_options`, etc.
21//! - **Maintenance**: `integrity_check`, `optimize`, `quick_check`, etc.
22//!
23//! ## Examples
24//!
25//! ```
26//! use drizzle_sqlite::pragma::{Pragma, JournalMode, AutoVacuum};
27//! use drizzle_core::ToSQL;
28//!
29//! // Enable foreign key constraints
30//! let pragma = Pragma::foreign_keys(true);
31//! assert_eq!(pragma.to_sql().sql(), "PRAGMA foreign_keys = ON");
32//!
33//! // Set journal mode to WAL
34//! let pragma = Pragma::journal_mode(JournalMode::Wal);
35//! assert_eq!(pragma.to_sql().sql(), "PRAGMA journal_mode = WAL");
36//!
37//! // Get table schema information
38//! let pragma = Pragma::table_info("users");
39//! assert_eq!(pragma.to_sql().sql(), "PRAGMA table_info(users)");
40//!
41//! // Check database integrity
42//! let pragma = Pragma::integrity_check(None);
43//! assert_eq!(pragma.to_sql().sql(), "PRAGMA integrity_check");
44//! ```
45
46use crate::{traits::SQLiteSQL, values::SQLiteValue};
47use drizzle_core::{ToSQL, sql::SQL};
48
49/// Auto-vacuum modes for SQLite databases
50///
51/// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_auto_vacuum)
52#[derive(Debug, Clone, PartialEq)]
53pub enum AutoVacuum {
54 /// Disable auto-vacuum
55 ///
56 /// # Example
57 /// ```
58 /// # use drizzle_sqlite::pragma::AutoVacuum;
59 /// # use drizzle_core::ToSQL;
60 /// assert_eq!(AutoVacuum::None.to_sql().sql(), "NONE");
61 /// ```
62 None,
63
64 /// Enable full auto-vacuum
65 ///
66 /// # Example
67 /// ```
68 /// # use drizzle_sqlite::pragma::AutoVacuum;
69 /// # use drizzle_core::ToSQL;
70 /// assert_eq!(AutoVacuum::Full.to_sql().sql(), "FULL");
71 /// ```
72 Full,
73
74 /// Enable incremental auto-vacuum
75 ///
76 /// # Example
77 /// ```
78 /// # use drizzle_sqlite::pragma::AutoVacuum;
79 /// # use drizzle_core::ToSQL;
80 /// assert_eq!(AutoVacuum::Incremental.to_sql().sql(), "INCREMENTAL");
81 /// ```
82 Incremental,
83}
84
85/// Journal modes for SQLite databases
86///
87/// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_journal_mode)
88#[derive(Debug, Clone, PartialEq)]
89pub enum JournalMode {
90 /// Delete journal file after each transaction
91 ///
92 /// # Example
93 /// ```
94 /// # use drizzle_sqlite::pragma::JournalMode;
95 /// # use drizzle_core::ToSQL;
96 /// assert_eq!(JournalMode::Delete.to_sql().sql(), "DELETE");
97 /// ```
98 Delete,
99
100 /// Truncate journal file after each transaction
101 ///
102 /// # Example
103 /// ```
104 /// # use drizzle_sqlite::pragma::JournalMode;
105 /// # use drizzle_core::ToSQL;
106 /// assert_eq!(JournalMode::Truncate.to_sql().sql(), "TRUNCATE");
107 /// ```
108 Truncate,
109
110 /// Keep journal file persistent
111 ///
112 /// # Example
113 /// ```
114 /// # use drizzle_sqlite::pragma::JournalMode;
115 /// # use drizzle_core::ToSQL;
116 /// assert_eq!(JournalMode::Persist.to_sql().sql(), "PERSIST");
117 /// ```
118 Persist,
119
120 /// Store journal in memory
121 ///
122 /// # Example
123 /// ```
124 /// # use drizzle_sqlite::pragma::JournalMode;
125 /// # use drizzle_core::ToSQL;
126 /// assert_eq!(JournalMode::Memory.to_sql().sql(), "MEMORY");
127 /// ```
128 Memory,
129
130 /// Write-Ahead Logging mode
131 ///
132 /// # Example
133 /// ```
134 /// # use drizzle_sqlite::pragma::JournalMode;
135 /// # use drizzle_core::ToSQL;
136 /// assert_eq!(JournalMode::Wal.to_sql().sql(), "WAL");
137 /// ```
138 Wal,
139
140 /// Disable journaling
141 ///
142 /// # Example
143 /// ```
144 /// # use drizzle_sqlite::pragma::JournalMode;
145 /// # use drizzle_core::ToSQL;
146 /// assert_eq!(JournalMode::Off.to_sql().sql(), "OFF");
147 /// ```
148 Off,
149}
150
151/// Synchronous modes for SQLite databases
152///
153/// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_synchronous)
154#[derive(Debug, Clone, PartialEq)]
155pub enum Synchronous {
156 /// No syncing - fastest but least safe
157 ///
158 /// # Example
159 /// ```
160 /// # use drizzle_sqlite::pragma::Synchronous;
161 /// # use drizzle_core::ToSQL;
162 /// assert_eq!(Synchronous::Off.to_sql().sql(), "OFF");
163 /// ```
164 Off,
165
166 /// Sync at critical moments - good balance
167 ///
168 /// # Example
169 /// ```
170 /// # use drizzle_sqlite::pragma::Synchronous;
171 /// # use drizzle_core::ToSQL;
172 /// assert_eq!(Synchronous::Normal.to_sql().sql(), "NORMAL");
173 /// ```
174 Normal,
175
176 /// Sync frequently - safest but slower
177 ///
178 /// # Example
179 /// ```
180 /// # use drizzle_sqlite::pragma::Synchronous;
181 /// # use drizzle_core::ToSQL;
182 /// assert_eq!(Synchronous::Full.to_sql().sql(), "FULL");
183 /// ```
184 Full,
185
186 /// Like FULL with additional syncing
187 ///
188 /// # Example
189 /// ```
190 /// # use drizzle_sqlite::pragma::Synchronous;
191 /// # use drizzle_core::ToSQL;
192 /// assert_eq!(Synchronous::Extra.to_sql().sql(), "EXTRA");
193 /// ```
194 Extra,
195}
196
197/// Storage modes for temporary tables and indices
198///
199/// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_temp_store)
200#[derive(Debug, Clone, PartialEq)]
201pub enum TempStore {
202 /// Use default storage mode
203 ///
204 /// # Example
205 /// ```
206 /// # use drizzle_sqlite::pragma::TempStore;
207 /// # use drizzle_core::ToSQL;
208 /// assert_eq!(TempStore::Default.to_sql().sql(), "DEFAULT");
209 /// ```
210 Default,
211
212 /// Store temporary tables in files
213 ///
214 /// # Example
215 /// ```
216 /// # use drizzle_sqlite::pragma::TempStore;
217 /// # use drizzle_core::ToSQL;
218 /// assert_eq!(TempStore::File.to_sql().sql(), "FILE");
219 /// ```
220 File,
221
222 /// Store temporary tables in memory
223 ///
224 /// # Example
225 /// ```
226 /// # use drizzle_sqlite::pragma::TempStore;
227 /// # use drizzle_core::ToSQL;
228 /// assert_eq!(TempStore::Memory.to_sql().sql(), "MEMORY");
229 /// ```
230 Memory,
231}
232
233/// Database locking modes
234///
235/// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_locking_mode)
236#[derive(Debug, Clone, PartialEq)]
237pub enum LockingMode {
238 /// Normal locking mode - allows multiple readers
239 ///
240 /// # Example
241 /// ```
242 /// # use drizzle_sqlite::pragma::LockingMode;
243 /// # use drizzle_core::ToSQL;
244 /// assert_eq!(LockingMode::Normal.to_sql().sql(), "NORMAL");
245 /// ```
246 Normal,
247
248 /// Exclusive locking mode - single connection only
249 ///
250 /// # Example
251 /// ```
252 /// # use drizzle_sqlite::pragma::LockingMode;
253 /// # use drizzle_core::ToSQL;
254 /// assert_eq!(LockingMode::Exclusive.to_sql().sql(), "EXCLUSIVE");
255 /// ```
256 Exclusive,
257}
258
259/// Secure delete modes for SQLite
260///
261/// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_secure_delete)
262#[derive(Debug, Clone, PartialEq)]
263pub enum SecureDelete {
264 /// Disable secure delete
265 ///
266 /// # Example
267 /// ```
268 /// # use drizzle_sqlite::pragma::SecureDelete;
269 /// # use drizzle_core::ToSQL;
270 /// assert_eq!(SecureDelete::Off.to_sql().sql(), "OFF");
271 /// ```
272 Off,
273
274 /// Enable secure delete - overwrite deleted data
275 ///
276 /// # Example
277 /// ```
278 /// # use drizzle_sqlite::pragma::SecureDelete;
279 /// # use drizzle_core::ToSQL;
280 /// assert_eq!(SecureDelete::On.to_sql().sql(), "ON");
281 /// ```
282 On,
283
284 /// Fast secure delete - partial overwriting
285 ///
286 /// # Example
287 /// ```
288 /// # use drizzle_sqlite::pragma::SecureDelete;
289 /// # use drizzle_core::ToSQL;
290 /// assert_eq!(SecureDelete::Fast.to_sql().sql(), "FAST");
291 /// ```
292 Fast,
293}
294
295/// Encoding types for SQLite databases
296///
297/// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_encoding)
298#[derive(Debug, Clone, PartialEq)]
299pub enum Encoding {
300 /// UTF-8 encoding
301 ///
302 /// # Example
303 /// ```
304 /// # use drizzle_sqlite::pragma::Encoding;
305 /// # use drizzle_core::ToSQL;
306 /// assert_eq!(Encoding::Utf8.to_sql().sql(), "UTF-8");
307 /// ```
308 Utf8,
309
310 /// UTF-16 little endian encoding
311 ///
312 /// # Example
313 /// ```
314 /// # use drizzle_sqlite::pragma::Encoding;
315 /// # use drizzle_core::ToSQL;
316 /// assert_eq!(Encoding::Utf16Le.to_sql().sql(), "UTF-16LE");
317 /// ```
318 Utf16Le,
319
320 /// UTF-16 big endian encoding
321 ///
322 /// # Example
323 /// ```
324 /// # use drizzle_sqlite::pragma::Encoding;
325 /// # use drizzle_core::ToSQL;
326 /// assert_eq!(Encoding::Utf16Be.to_sql().sql(), "UTF-16BE");
327 /// ```
328 Utf16Be,
329}
330
331/// SQLite pragma statements for database configuration and introspection
332#[derive(Debug, Clone, PartialEq)]
333pub enum Pragma {
334 // Read/Write Configuration Pragmas
335 /// Set or query the 32-bit signed big-endian application ID
336 ///
337 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_application_id)
338 ///
339 /// # Example
340 /// ```
341 /// # use drizzle_sqlite::pragma::Pragma;
342 /// # use drizzle_core::ToSQL;
343 /// let pragma = Pragma::ApplicationId(12345);
344 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA application_id = 12345");
345 /// ```
346 ApplicationId(i32),
347
348 /// Query or set the auto-vacuum status in the database
349 ///
350 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_auto_vacuum)
351 ///
352 /// # Example
353 /// ```
354 /// # use drizzle_sqlite::pragma::{Pragma, AutoVacuum};
355 /// # use drizzle_core::ToSQL;
356 /// let pragma = Pragma::AutoVacuum(AutoVacuum::Full);
357 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA auto_vacuum = FULL");
358 /// ```
359 AutoVacuum(AutoVacuum),
360
361 /// Suggest maximum number of database disk pages in memory
362 ///
363 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_cache_size)
364 ///
365 /// # Example
366 /// ```
367 /// # use drizzle_sqlite::pragma::Pragma;
368 /// # use drizzle_core::ToSQL;
369 /// let pragma = Pragma::CacheSize(-2000);
370 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA cache_size = -2000");
371 /// ```
372 CacheSize(i32),
373
374 /// Query, set, or clear the enforcement of foreign key constraints
375 ///
376 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_foreign_keys)
377 ///
378 /// # Example
379 /// ```
380 /// # use drizzle_sqlite::pragma::Pragma;
381 /// # use drizzle_core::ToSQL;
382 /// let pragma = Pragma::ForeignKeys(true);
383 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA foreign_keys = ON");
384 ///
385 /// let pragma = Pragma::ForeignKeys(false);
386 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA foreign_keys = OFF");
387 /// ```
388 ForeignKeys(bool),
389
390 /// Query or set the journal mode for databases
391 ///
392 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_journal_mode)
393 ///
394 /// # Example
395 /// ```
396 /// # use drizzle_sqlite::pragma::{Pragma, JournalMode};
397 /// # use drizzle_core::ToSQL;
398 /// let pragma = Pragma::JournalMode(JournalMode::Wal);
399 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA journal_mode = WAL");
400 /// ```
401 JournalMode(JournalMode),
402
403 /// Control how aggressively SQLite will write data
404 ///
405 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_synchronous)
406 ///
407 /// # Example
408 /// ```
409 /// # use drizzle_sqlite::pragma::{Pragma, Synchronous};
410 /// # use drizzle_core::ToSQL;
411 /// let pragma = Pragma::Synchronous(Synchronous::Normal);
412 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA synchronous = NORMAL");
413 /// ```
414 Synchronous(Synchronous),
415
416 /// Query or set the storage mode used by temporary tables and indices
417 ///
418 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_temp_store)
419 ///
420 /// # Example
421 /// ```
422 /// # use drizzle_sqlite::pragma::{Pragma, TempStore};
423 /// # use drizzle_core::ToSQL;
424 /// let pragma = Pragma::TempStore(TempStore::Memory);
425 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA temp_store = MEMORY");
426 /// ```
427 TempStore(TempStore),
428
429 /// Query or set the database connection locking-mode
430 ///
431 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_locking_mode)
432 ///
433 /// # Example
434 /// ```
435 /// # use drizzle_sqlite::pragma::{Pragma, LockingMode};
436 /// # use drizzle_core::ToSQL;
437 /// let pragma = Pragma::LockingMode(LockingMode::Exclusive);
438 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA locking_mode = EXCLUSIVE");
439 /// ```
440 LockingMode(LockingMode),
441
442 /// Query or set the secure-delete setting
443 ///
444 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_secure_delete)
445 ///
446 /// # Example
447 /// ```
448 /// # use drizzle_sqlite::pragma::{Pragma, SecureDelete};
449 /// # use drizzle_core::ToSQL;
450 /// let pragma = Pragma::SecureDelete(SecureDelete::Fast);
451 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA secure_delete = FAST");
452 /// ```
453 SecureDelete(SecureDelete),
454
455 /// Set or get the user-version integer
456 ///
457 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_user_version)
458 ///
459 /// # Example
460 /// ```
461 /// # use drizzle_sqlite::pragma::Pragma;
462 /// # use drizzle_core::ToSQL;
463 /// let pragma = Pragma::UserVersion(42);
464 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA user_version = 42");
465 /// ```
466 UserVersion(i32),
467
468 /// Query or set the text encoding used by the database
469 ///
470 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_encoding)
471 ///
472 /// # Example
473 /// ```
474 /// # use drizzle_sqlite::pragma::{Pragma, Encoding};
475 /// # use drizzle_core::ToSQL;
476 /// let pragma = Pragma::Encoding(Encoding::Utf8);
477 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA encoding = UTF-8");
478 /// ```
479 Encoding(Encoding),
480
481 /// Query or set the database page size in bytes
482 ///
483 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_page_size)
484 ///
485 /// # Example
486 /// ```
487 /// # use drizzle_sqlite::pragma::Pragma;
488 /// # use drizzle_core::ToSQL;
489 /// let pragma = Pragma::PageSize(4096);
490 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA page_size = 4096");
491 /// ```
492 PageSize(i32),
493
494 /// Query or set the maximum memory map size
495 ///
496 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_mmap_size)
497 ///
498 /// # Example
499 /// ```
500 /// # use drizzle_sqlite::pragma::Pragma;
501 /// # use drizzle_core::ToSQL;
502 /// let pragma = Pragma::MmapSize(268435456);
503 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA mmap_size = 268435456");
504 /// ```
505 MmapSize(i64),
506
507 /// Enable or disable recursive trigger firing
508 ///
509 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_recursive_triggers)
510 ///
511 /// # Example
512 /// ```
513 /// # use drizzle_sqlite::pragma::Pragma;
514 /// # use drizzle_core::ToSQL;
515 /// let pragma = Pragma::RecursiveTriggers(true);
516 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA recursive_triggers = ON");
517 /// ```
518 RecursiveTriggers(bool),
519
520 // Read-Only Query Pragmas
521 /// Return a list of collating sequences
522 ///
523 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_collation_list)
524 ///
525 /// # Example
526 /// ```
527 /// # use drizzle_sqlite::pragma::Pragma;
528 /// # use drizzle_core::ToSQL;
529 /// let pragma = Pragma::CollationList;
530 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA collation_list");
531 /// ```
532 CollationList,
533
534 /// Return compile-time options used when building SQLite
535 ///
536 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_compile_options)
537 ///
538 /// # Example
539 /// ```
540 /// # use drizzle_sqlite::pragma::Pragma;
541 /// # use drizzle_core::ToSQL;
542 /// let pragma = Pragma::CompileOptions;
543 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA compile_options");
544 /// ```
545 CompileOptions,
546
547 /// Return information about attached databases
548 ///
549 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_database_list)
550 ///
551 /// # Example
552 /// ```
553 /// # use drizzle_sqlite::pragma::Pragma;
554 /// # use drizzle_core::ToSQL;
555 /// let pragma = Pragma::DatabaseList;
556 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA database_list");
557 /// ```
558 DatabaseList,
559
560 /// Return a list of SQL functions
561 ///
562 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_function_list)
563 ///
564 /// # Example
565 /// ```
566 /// # use drizzle_sqlite::pragma::Pragma;
567 /// # use drizzle_core::ToSQL;
568 /// let pragma = Pragma::FunctionList;
569 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA function_list");
570 /// ```
571 FunctionList,
572
573 /// Return information about tables and views in the schema
574 ///
575 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_table_list)
576 ///
577 /// # Example
578 /// ```
579 /// # use drizzle_sqlite::pragma::Pragma;
580 /// # use drizzle_core::ToSQL;
581 /// let pragma = Pragma::TableList;
582 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA table_list");
583 /// ```
584 TableList,
585
586 /// Return extended table information including hidden columns
587 ///
588 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_table_xinfo)
589 ///
590 /// # Example
591 /// ```
592 /// # use drizzle_sqlite::pragma::Pragma;
593 /// # use drizzle_core::ToSQL;
594 /// let pragma = Pragma::TableXInfo("users");
595 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA table_xinfo(users)");
596 /// ```
597 TableXInfo(&'static str),
598
599 /// Return a list of available virtual table modules
600 ///
601 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_module_list)
602 ///
603 /// # Example
604 /// ```
605 /// # use drizzle_sqlite::pragma::Pragma;
606 /// # use drizzle_core::ToSQL;
607 /// let pragma = Pragma::ModuleList;
608 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA module_list");
609 /// ```
610 ModuleList,
611
612 // Utility Pragmas
613 /// Perform database integrity check
614 ///
615 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_integrity_check)
616 ///
617 /// # Example
618 /// ```
619 /// # use drizzle_sqlite::pragma::Pragma;
620 /// # use drizzle_core::ToSQL;
621 /// // Check entire database
622 /// let pragma = Pragma::IntegrityCheck(None);
623 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA integrity_check");
624 ///
625 /// // Check specific table
626 /// let pragma = Pragma::IntegrityCheck(Some("users"));
627 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA integrity_check(users)");
628 /// ```
629 IntegrityCheck(Option<&'static str>),
630
631 /// Perform faster database integrity check
632 ///
633 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_quick_check)
634 ///
635 /// # Example
636 /// ```
637 /// # use drizzle_sqlite::pragma::Pragma;
638 /// # use drizzle_core::ToSQL;
639 /// let pragma = Pragma::QuickCheck(None);
640 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA quick_check");
641 ///
642 /// let pragma = Pragma::QuickCheck(Some("users"));
643 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA quick_check(users)");
644 /// ```
645 QuickCheck(Option<&'static str>),
646
647 /// Attempt to optimize the database
648 ///
649 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_optimize)
650 ///
651 /// # Example
652 /// ```
653 /// # use drizzle_sqlite::pragma::Pragma;
654 /// # use drizzle_core::ToSQL;
655 /// let pragma = Pragma::Optimize(None);
656 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA optimize");
657 ///
658 /// let pragma = Pragma::Optimize(Some(0x10002));
659 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA optimize(65538)");
660 /// ```
661 Optimize(Option<u32>),
662
663 /// Check foreign key constraints for a table
664 ///
665 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_foreign_key_check)
666 ///
667 /// # Example
668 /// ```
669 /// # use drizzle_sqlite::pragma::Pragma;
670 /// # use drizzle_core::ToSQL;
671 /// let pragma = Pragma::ForeignKeyCheck(None);
672 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA foreign_key_check");
673 ///
674 /// let pragma = Pragma::ForeignKeyCheck(Some("orders"));
675 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA foreign_key_check(orders)");
676 /// ```
677 ForeignKeyCheck(Option<&'static str>),
678
679 // Table-specific Pragmas
680 /// Return information about table columns
681 ///
682 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_table_info)
683 ///
684 /// # Example
685 /// ```
686 /// # use drizzle_sqlite::pragma::Pragma;
687 /// # use drizzle_core::ToSQL;
688 /// let pragma = Pragma::TableInfo("users");
689 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA table_info(users)");
690 /// ```
691 TableInfo(&'static str),
692
693 /// Return information about table indexes
694 ///
695 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_index_list)
696 ///
697 /// # Example
698 /// ```
699 /// # use drizzle_sqlite::pragma::Pragma;
700 /// # use drizzle_core::ToSQL;
701 /// let pragma = Pragma::IndexList("users");
702 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA index_list(users)");
703 /// ```
704 IndexList(&'static str),
705
706 /// Return information about index columns
707 ///
708 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_index_info)
709 ///
710 /// # Example
711 /// ```
712 /// # use drizzle_sqlite::pragma::Pragma;
713 /// # use drizzle_core::ToSQL;
714 /// let pragma = Pragma::IndexInfo("idx_users_email");
715 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA index_info(idx_users_email)");
716 /// ```
717 IndexInfo(&'static str),
718
719 /// Return foreign key information for a table
720 ///
721 /// [SQLite Documentation](https://sqlite.org/pragma.html#pragma_foreign_key_list)
722 ///
723 /// # Example
724 /// ```
725 /// # use drizzle_sqlite::pragma::Pragma;
726 /// # use drizzle_core::ToSQL;
727 /// let pragma = Pragma::ForeignKeyList("orders");
728 /// assert_eq!(pragma.to_sql().sql(), "PRAGMA foreign_key_list(orders)");
729 /// ```
730 ForeignKeyList(&'static str),
731}
732
733impl<'a> ToSQL<'a, SQLiteValue<'a>> for AutoVacuum {
734 fn to_sql(&self) -> SQLiteSQL<'a> {
735 match self {
736 AutoVacuum::None => SQL::raw("NONE"),
737 AutoVacuum::Full => SQL::raw("FULL"),
738 AutoVacuum::Incremental => SQL::raw("INCREMENTAL"),
739 }
740 }
741}
742
743impl<'a> ToSQL<'a, SQLiteValue<'a>> for JournalMode {
744 fn to_sql(&self) -> SQLiteSQL<'a> {
745 match self {
746 JournalMode::Delete => SQL::raw("DELETE"),
747 JournalMode::Truncate => SQL::raw("TRUNCATE"),
748 JournalMode::Persist => SQL::raw("PERSIST"),
749 JournalMode::Memory => SQL::raw("MEMORY"),
750 JournalMode::Wal => SQL::raw("WAL"),
751 JournalMode::Off => SQL::raw("OFF"),
752 }
753 }
754}
755
756impl<'a> ToSQL<'a, SQLiteValue<'a>> for Synchronous {
757 fn to_sql(&self) -> SQLiteSQL<'a> {
758 match self {
759 Synchronous::Off => SQL::raw("OFF"),
760 Synchronous::Normal => SQL::raw("NORMAL"),
761 Synchronous::Full => SQL::raw("FULL"),
762 Synchronous::Extra => SQL::raw("EXTRA"),
763 }
764 }
765}
766
767impl<'a> ToSQL<'a, SQLiteValue<'a>> for TempStore {
768 fn to_sql(&self) -> SQLiteSQL<'a> {
769 match self {
770 TempStore::Default => SQL::raw("DEFAULT"),
771 TempStore::File => SQL::raw("FILE"),
772 TempStore::Memory => SQL::raw("MEMORY"),
773 }
774 }
775}
776
777impl<'a> ToSQL<'a, SQLiteValue<'a>> for LockingMode {
778 fn to_sql(&self) -> SQLiteSQL<'a> {
779 match self {
780 LockingMode::Normal => SQL::raw("NORMAL"),
781 LockingMode::Exclusive => SQL::raw("EXCLUSIVE"),
782 }
783 }
784}
785
786impl<'a> ToSQL<'a, SQLiteValue<'a>> for SecureDelete {
787 fn to_sql(&self) -> SQLiteSQL<'a> {
788 match self {
789 SecureDelete::Off => SQL::raw("OFF"),
790 SecureDelete::On => SQL::raw("ON"),
791 SecureDelete::Fast => SQL::raw("FAST"),
792 }
793 }
794}
795
796impl<'a> ToSQL<'a, SQLiteValue<'a>> for Encoding {
797 fn to_sql(&self) -> SQLiteSQL<'a> {
798 match self {
799 Encoding::Utf8 => SQL::raw("UTF-8"),
800 Encoding::Utf16Le => SQL::raw("UTF-16LE"),
801 Encoding::Utf16Be => SQL::raw("UTF-16BE"),
802 }
803 }
804}
805
806impl<'a> ToSQL<'a, SQLiteValue<'a>> for Pragma {
807 fn to_sql(&self) -> SQLiteSQL<'a> {
808 match self {
809 // Read/Write Configuration Pragmas
810 Pragma::ApplicationId(id) => SQL::raw(format!("PRAGMA application_id = {}", id)),
811 Pragma::AutoVacuum(mode) => SQL::raw("PRAGMA auto_vacuum = ").append(mode.to_sql()),
812 Pragma::CacheSize(size) => SQL::raw(format!("PRAGMA cache_size = {}", size)),
813 Pragma::ForeignKeys(enabled) => SQL::raw("PRAGMA foreign_keys = ")
814 .append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
815 Pragma::JournalMode(mode) => SQL::raw("PRAGMA journal_mode = ").append(mode.to_sql()),
816 Pragma::Synchronous(mode) => SQL::raw("PRAGMA synchronous = ").append(mode.to_sql()),
817 Pragma::TempStore(store) => SQL::raw("PRAGMA temp_store = ").append(store.to_sql()),
818 Pragma::LockingMode(mode) => SQL::raw("PRAGMA locking_mode = ").append(mode.to_sql()),
819 Pragma::SecureDelete(mode) => SQL::raw("PRAGMA secure_delete = ").append(mode.to_sql()),
820 Pragma::UserVersion(version) => SQL::raw(format!("PRAGMA user_version = {}", version)),
821 Pragma::Encoding(encoding) => SQL::raw("PRAGMA encoding = ").append(encoding.to_sql()),
822 Pragma::PageSize(size) => SQL::raw(format!("PRAGMA page_size = {}", size)),
823 Pragma::MmapSize(size) => SQL::raw(format!("PRAGMA mmap_size = {}", size)),
824 Pragma::RecursiveTriggers(enabled) => SQL::raw("PRAGMA recursive_triggers = ")
825 .append(SQL::raw(if *enabled { "ON" } else { "OFF" })),
826
827 // Read-Only Query Pragmas
828 Pragma::CollationList => SQL::raw("PRAGMA collation_list"),
829 Pragma::CompileOptions => SQL::raw("PRAGMA compile_options"),
830 Pragma::DatabaseList => SQL::raw("PRAGMA database_list"),
831 Pragma::FunctionList => SQL::raw("PRAGMA function_list"),
832 Pragma::TableList => SQL::raw("PRAGMA table_list"),
833 Pragma::TableXInfo(table) => SQL::raw(format!("PRAGMA table_xinfo({})", table)),
834 Pragma::ModuleList => SQL::raw("PRAGMA module_list"),
835
836 // Utility Pragmas
837 Pragma::IntegrityCheck(table) => match table {
838 Some(t) => SQL::raw(format!("PRAGMA integrity_check({})", t)),
839 None => SQL::raw("PRAGMA integrity_check"),
840 },
841 Pragma::QuickCheck(table) => match table {
842 Some(t) => SQL::raw(format!("PRAGMA quick_check({})", t)),
843 None => SQL::raw("PRAGMA quick_check"),
844 },
845 Pragma::Optimize(mask) => match mask {
846 Some(m) => SQL::raw(format!("PRAGMA optimize({})", m)),
847 None => SQL::raw("PRAGMA optimize"),
848 },
849 Pragma::ForeignKeyCheck(table) => match table {
850 Some(t) => SQL::raw(format!("PRAGMA foreign_key_check({})", t)),
851 None => SQL::raw("PRAGMA foreign_key_check"),
852 },
853
854 // Table-specific Pragmas
855 Pragma::TableInfo(table) => SQL::raw(format!("PRAGMA table_info({})", table)),
856 Pragma::IndexList(table) => SQL::raw(format!("PRAGMA index_list({})", table)),
857 Pragma::IndexInfo(index) => SQL::raw(format!("PRAGMA index_info({})", index)),
858 Pragma::ForeignKeyList(table) => {
859 SQL::raw(format!("PRAGMA foreign_key_list({})", table))
860 }
861 }
862 }
863}
864
865impl Pragma {
866 /// Create a PRAGMA query to get the current value (read-only operation)
867 pub fn query(pragma_name: &str) -> SQL<'static, SQLiteValue<'static>> {
868 SQL::raw(format!("PRAGMA {}", pragma_name))
869 }
870
871 /// Convenience constructor for foreign_keys pragma
872 pub fn foreign_keys(enabled: bool) -> Self {
873 Self::ForeignKeys(enabled)
874 }
875
876 /// Convenience constructor for journal_mode pragma
877 pub fn journal_mode(mode: JournalMode) -> Self {
878 Self::JournalMode(mode)
879 }
880
881 /// Convenience constructor for table_info pragma
882 pub fn table_info(table: &'static str) -> Self {
883 Self::TableInfo(table)
884 }
885
886 /// Convenience constructor for index_list pragma
887 pub fn index_list(table: &'static str) -> Self {
888 Self::IndexList(table)
889 }
890
891 /// Convenience constructor for foreign_key_list pragma
892 pub fn foreign_key_list(table: &'static str) -> Self {
893 Self::ForeignKeyList(table)
894 }
895
896 /// Convenience constructor for integrity_check pragma
897 pub fn integrity_check(table: Option<&'static str>) -> Self {
898 Self::IntegrityCheck(table)
899 }
900
901 /// Convenience constructor for foreign_key_check pragma
902 pub fn foreign_key_check(table: Option<&'static str>) -> Self {
903 Self::ForeignKeyCheck(table)
904 }
905
906 /// Convenience constructor for table_xinfo pragma
907 pub fn table_xinfo(table: &'static str) -> Self {
908 Self::TableXInfo(table)
909 }
910
911 /// Convenience constructor for encoding pragma
912 pub fn encoding(encoding: Encoding) -> Self {
913 Self::Encoding(encoding)
914 }
915}
916
917#[cfg(test)]
918mod tests {
919 use super::*;
920
921 #[test]
922 fn test_query_pragma_helper() {
923 // Test the static query helper function - not covered in doc tests
924 assert_eq!(Pragma::query("foreign_keys").sql(), "PRAGMA foreign_keys");
925 assert_eq!(Pragma::query("custom_pragma").sql(), "PRAGMA custom_pragma");
926 }
927
928 #[test]
929 fn test_convenience_constructor_integration() {
930 // Test that convenience constructors work the same as direct construction
931 assert_eq!(
932 Pragma::foreign_keys(true).to_sql().sql(),
933 Pragma::ForeignKeys(true).to_sql().sql()
934 );
935 assert_eq!(
936 Pragma::table_info("users").to_sql().sql(),
937 Pragma::TableInfo("users").to_sql().sql()
938 );
939 assert_eq!(
940 Pragma::encoding(Encoding::Utf8).to_sql().sql(),
941 Pragma::Encoding(Encoding::Utf8).to_sql().sql()
942 );
943 }
944}