overdrive-db 1.4.3

OverDrive-DB — Embeddable hybrid SQL+NoSQL database. Like SQLite for JSON. #OverDriveDB #AFOT
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//! # OverDrive InCode SDK
//! 
//! An embeddable document database — like SQLite for JSON.
//! 
//! Import the package, open a file, query your data. **No server needed.**
//! 
//! ## Quick Start (Rust)
//! 
//! ```no_run
//! use overdrive::OverDriveDB;
//! 
//! let mut db = OverDriveDB::open("myapp.odb").unwrap();
//! db.create_table("users").unwrap();
//! 
//! let id = db.insert("users", &serde_json::json!({
//!     "name": "Alice",
//!     "email": "alice@example.com",
//!     "age": 30
//! })).unwrap();
//! 
//! let results = db.query("SELECT * FROM users WHERE age > 25").unwrap();
//! println!("{:?}", results.rows);
//! ```
//!
//! ## Setup
//!
//! 1. `cargo add overdrive-sdk`
//! 2. Download the native library from [GitHub Releases](https://github.com/ALL-FOR-ONE-TECH/OverDrive-DB_SDK/releases/latest)
//! 3. Place it in your project directory or on your system PATH

pub mod result;
pub mod query_engine;
pub mod ffi;
pub mod shared;
mod dynamic;

use result::{SdkResult, SdkError};
use dynamic::NativeDB;
use serde_json::Value;
use std::path::Path;
use std::time::Instant;
use zeroize::{Zeroize, ZeroizeOnDrop};

// ─────────────────────────────────────────────
// SECURITY: Secret key wrapper
// Zero bytes from RAM automatically on drop
// ─────────────────────────────────────────────

/// A secret key that is automatically zeroed from memory when dropped.
/// Use this to hold AES encryption keys — prevents leak via memory dump.
///
/// ```no_run
/// use overdrive::SecretKey;
/// let key = SecretKey::from_env("ODB_KEY").unwrap();
/// // ...key bytes are wiped from RAM when `key` is dropped
/// ```
#[derive(Zeroize, ZeroizeOnDrop)]
pub struct SecretKey(Vec<u8>);

impl SecretKey {
    /// Create a `SecretKey` from raw bytes.
    pub fn new(bytes: Vec<u8>) -> Self {
        Self(bytes)
    }

    /// Read key bytes from an environment variable.
    ///
    /// Returns `SecurityError` if the env var is not set or is empty.
    pub fn from_env(env_var: &str) -> SdkResult<Self> {
        let val = std::env::var(env_var).map_err(|_| {
            SdkError::SecurityError(format!(
                "Encryption key env var '{}' is not set. \
                 Set it with: $env:{}=\"your-secret-key\" (PowerShell) \
                 or export {}=\"your-secret-key\" (bash)",
                env_var, env_var, env_var
            ))
        })?;
        if val.is_empty() {
            return Err(SdkError::SecurityError(format!(
                "Encryption key env var '{}' is set but empty.", env_var
            )));
        }
        Ok(Self(val.into_bytes()))
    }

    /// Raw key bytes (use sparingly — minimize time in scope).
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }
}

// ─────────────────────────────────────────────
// SECURITY: OS-level file permission hardening
// ─────────────────────────────────────────────

/// Set restrictive OS-level permissions on the `.odb` file:
/// - **Windows**: `icacls` — removes all inherit ACEs, grants only current user Full Control
/// - **Linux/macOS**: `chmod 600` — owner read/write only
///
/// Called automatically inside `OverDriveDB::open()`.
pub fn set_secure_permissions(path: &str) -> SdkResult<()> {
    #[cfg(target_os = "windows")]
    {
        // Reset all inherited permissions and grant only current user
        let output = std::process::Command::new("icacls")
            .args([path, "/inheritance:r", "/grant:r", "%USERNAME%:F"])
            .output();
        match output {
            Ok(out) if out.status.success() => {}
            Ok(out) => {
                let stderr = String::from_utf8_lossy(&out.stderr);
                // Non-fatal: log but don't fail (icacls may not be available on all setups)
                eprintln!("[overdrive-sdk] WARNING: Could not set file permissions on '{}': {}", path, stderr);
            }
            Err(e) => {
                eprintln!("[overdrive-sdk] WARNING: icacls not available, file permissions not hardened: {}", e);
            }
        }
    }
    #[cfg(not(target_os = "windows"))]
    {
        use std::os::unix::fs::PermissionsExt;
        if Path::new(path).exists() {
            std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))
                .map_err(|e| SdkError::SecurityError(format!(
                    "Failed to chmod 600 '{}': {}", path, e
                )))?;
        }
    }
    Ok(())
}

/// Query result returned by `query()`
#[derive(Debug, Clone)]
pub struct QueryResult {
    /// Result rows (JSON objects)
    pub rows: Vec<Value>,
    /// Column names (for SELECT queries)
    pub columns: Vec<String>,
    /// Number of rows affected (for INSERT/UPDATE/DELETE)
    pub rows_affected: usize,
    /// Query execution time in milliseconds
    pub execution_time_ms: f64,
}

impl QueryResult {
    fn empty() -> Self {
        Self {
            rows: Vec::new(),
            columns: Vec::new(),
            rows_affected: 0,
            execution_time_ms: 0.0,
        }
    }
}

/// Database statistics (expanded for Phase 5)
#[derive(Debug, Clone)]
pub struct Stats {
    pub tables: usize,
    pub total_records: usize,
    pub file_size_bytes: u64,
    pub path: String,
    /// Number of active MVCC versions in memory
    pub mvcc_active_versions: usize,
    /// Database page size in bytes (typically 4096)
    pub page_size: usize,
    /// SDK version string
    pub sdk_version: String,
}

/// MVCC Isolation level for transactions
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum IsolationLevel {
    /// Read uncommitted data (fastest, least safe)
    ReadUncommitted = 0,
    /// Read only committed data (default)
    ReadCommitted = 1,
    /// Repeatable reads within the transaction
    RepeatableRead = 2,
    /// Full serializable isolation (slowest, safest)
    Serializable = 3,
}

impl IsolationLevel {
    pub fn from_i32(val: i32) -> Self {
        match val {
            0 => IsolationLevel::ReadUncommitted,
            1 => IsolationLevel::ReadCommitted,
            2 => IsolationLevel::RepeatableRead,
            3 => IsolationLevel::Serializable,
            _ => IsolationLevel::ReadCommitted,
        }
    }
}

/// A handle for an active MVCC transaction
#[derive(Debug, Clone)]
pub struct TransactionHandle {
    /// Unique transaction ID
    pub txn_id: u64,
    /// Isolation level of this transaction
    pub isolation: IsolationLevel,
    /// Whether this transaction is still active
    pub active: bool,
}

/// Result of an integrity verification check
#[derive(Debug, Clone)]
pub struct IntegrityReport {
    /// Whether the database passed all checks
    pub is_valid: bool,
    /// Total pages checked
    pub pages_checked: usize,
    /// Total tables verified
    pub tables_verified: usize,
    /// List of issues found (empty if valid)
    pub issues: Vec<String>,
}

/// OverDrive InCode SDK — Embeddable document database
/// 
/// Use this struct to create, open, and interact with OverDrive databases
/// directly in your application. No server required.
pub struct OverDriveDB {
    native: NativeDB,
    path: String,
}

impl OverDriveDB {
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // DATABASE LIFECYCLE
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    /// Open an existing database or create a new one.
    ///
    /// File permissions are automatically hardened on open (chmod 600 / Windows ACL).
    pub fn open(path: &str) -> SdkResult<Self> {
        let native = NativeDB::open(path)?;
        // Fix 6: Harden file permissions immediately on open
        let _ = set_secure_permissions(path);
        Ok(Self {
            native,
            path: path.to_string(),
        })
    }

    /// Create a new database. Returns an error if the file already exists.
    pub fn create(path: &str) -> SdkResult<Self> {
        if Path::new(path).exists() {
            return Err(SdkError::DatabaseAlreadyExists(path.to_string()));
        }
        Self::open(path)
    }

    /// Open an existing database. Returns an error if the file doesn't exist.
    pub fn open_existing(path: &str) -> SdkResult<Self> {
        if !Path::new(path).exists() {
            return Err(SdkError::DatabaseNotFound(path.to_string()));
        }
        Self::open(path)
    }

    /// Force sync all data to disk.
    pub fn sync(&self) -> SdkResult<()> {
        self.native.sync();
        Ok(())
    }

    /// Close the database and release all resources.
    pub fn close(mut self) -> SdkResult<()> {
        self.native.close();
        Ok(())
    }

    /// Delete a database file from disk.
    pub fn destroy(path: &str) -> SdkResult<()> {
        if Path::new(path).exists() {
            std::fs::remove_file(path)?;
        }
        Ok(())
    }

    /// Get the database file path.
    pub fn path(&self) -> &str {
        &self.path
    }

    /// Get the SDK version.
    pub fn version() -> String {
        NativeDB::version()
    }

    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // SECURITY: Encrypted open, backup, WAL cleanup
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    /// Open a database with an encryption key loaded securely from an environment variable.
    ///
    /// **Never hardcode** the key — always read from env or a secrets manager.
    ///
    /// ```no_run
    /// // In your shell: $env:ODB_KEY="my-secret-32-char-aes-key!!!!"
    /// use overdrive::OverDriveDB;
    /// let mut db = OverDriveDB::open_encrypted("app.odb", "ODB_KEY").unwrap();
    /// ```
    pub fn open_encrypted(path: &str, key_env_var: &str) -> SdkResult<Self> {
        let key = SecretKey::from_env(key_env_var)?;
        // Pass key to the engine via a dedicated env var the engine reads internally.
        // This avoids passing the key as a command-line argument (visible in process list).
        std::env::set_var("__OVERDRIVE_KEY", std::str::from_utf8(key.as_bytes())
            .map_err(|_| SdkError::SecurityError("Key contains non-UTF8 bytes".to_string()))?);
        let db = Self::open(path)?;
        // Immediately remove from env after handoff
        std::env::remove_var("__OVERDRIVE_KEY");
        // SecretKey is dropped here — bytes are zeroed
        Ok(db)
    }

    /// Create an encrypted backup of the database to `dest_path`.
    ///
    /// Syncs all in-memory data to disk first, then copies the `.odb` and `.wal` files.
    /// Store the backup in a separate physical location or cloud storage.
    ///
    /// ```no_run
    /// # use overdrive::OverDriveDB;
    /// # let db = OverDriveDB::open("app.odb").unwrap();
    /// db.backup("backups/app_2026-03-04.odb").unwrap();
    /// ```
    pub fn backup(&self, dest_path: &str) -> SdkResult<()> {
        // Flush all in-memory pages to disk first
        self.sync()?;

        // Copy the main .odb file
        std::fs::copy(&self.path, dest_path)
            .map_err(|e| SdkError::BackupError(format!(
                "Failed to copy '{}' -> '{}': {}", self.path, dest_path, e
            )))?;

        // Also copy the WAL file if it exists (crash consistency)
        let wal_src = format!("{}.wal", self.path);
        let wal_dst = format!("{}.wal", dest_path);
        if Path::new(&wal_src).exists() {
            std::fs::copy(&wal_src, &wal_dst)
                .map_err(|e| SdkError::BackupError(format!(
                    "Failed to copy WAL '{}' -> '{}': {}", wal_src, wal_dst, e
                )))?;
        }

        // Harden permissions on the backup file too
        let _ = set_secure_permissions(dest_path);
        Ok(())
    }

    /// Delete the WAL (Write-Ahead Log) file after a confirmed commit.
    ///
    /// **Call this after `commit_transaction()`** to prevent attackers from replaying
    /// the WAL file to restore deleted data.
    ///
    /// ```no_run
    /// # use overdrive::{OverDriveDB, IsolationLevel};
    /// # let mut db = OverDriveDB::open("app.odb").unwrap();
    /// let txn = db.begin_transaction(IsolationLevel::ReadCommitted).unwrap();
    /// // ... writes ...
    /// db.commit_transaction(&txn).unwrap();
    /// db.cleanup_wal().unwrap(); // Remove stale WAL
    /// ```
    pub fn cleanup_wal(&self) -> SdkResult<()> {
        let wal_path = format!("{}.wal", self.path);
        if Path::new(&wal_path).exists() {
            std::fs::remove_file(&wal_path)
                .map_err(|e| SdkError::IoError(e))?;
        }
        Ok(())
    }

    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // TABLE MANAGEMENT
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    /// Create a new table (schemaless, NoSQL mode).
    pub fn create_table(&mut self, name: &str) -> SdkResult<()> {
        self.native.create_table(name)?;
        Ok(())
    }

    /// Drop (delete) a table and all its data.
    pub fn drop_table(&mut self, name: &str) -> SdkResult<()> {
        self.native.drop_table(name)?;
        Ok(())
    }

    /// List all tables in the database.
    pub fn list_tables(&self) -> SdkResult<Vec<String>> {
        Ok(self.native.list_tables()?)
    }

    /// Check if a table exists.
    pub fn table_exists(&self, name: &str) -> SdkResult<bool> {
        Ok(self.native.table_exists(name))
    }

    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // CRUD OPERATIONS
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    /// Insert a JSON document into a table. Returns the auto-generated `_id`.
    /// 
    /// ```ignore
    /// let id = db.insert("users", &serde_json::json!({
    ///     "name": "Alice",
    ///     "age": 30,
    ///     "tags": ["admin", "developer"]
    /// })).unwrap();
    /// println!("Inserted: {}", id);
    /// ```
    pub fn insert(&mut self, table: &str, doc: &Value) -> SdkResult<String> {
        let json_str = serde_json::to_string(doc)?;
        let id = self.native.insert(table, &json_str)?;
        Ok(id)
    }

    /// Insert multiple documents in a batch. Returns a list of generated `_id`s.
    pub fn insert_batch(&mut self, table: &str, docs: &[Value]) -> SdkResult<Vec<String>> {
        let mut ids = Vec::with_capacity(docs.len());
        for doc in docs {
            let id = self.insert(table, doc)?;
            ids.push(id);
        }
        Ok(ids)
    }

    /// Get a document by its `_id`.
    pub fn get(&self, table: &str, id: &str) -> SdkResult<Option<Value>> {
        match self.native.get(table, id)? {
            Some(json_str) => {
                let value: Value = serde_json::from_str(&json_str)?;
                Ok(Some(value))
            }
            None => Ok(None),
        }
    }

    /// Update a document by its `_id`. Returns `true` if the document was found and updated.
    /// 
    /// ```ignore
    /// db.update("users", &id, &serde_json::json!({
    ///     "age": 31,
    ///     "email": "alice@newmail.com"
    /// })).unwrap();
    /// ```
    pub fn update(&mut self, table: &str, id: &str, updates: &Value) -> SdkResult<bool> {
        let json_str = serde_json::to_string(updates)?;
        Ok(self.native.update(table, id, &json_str)?)
    }

    /// Delete a document by its `_id`. Returns `true` if found and deleted.
    pub fn delete(&mut self, table: &str, id: &str) -> SdkResult<bool> {
        Ok(self.native.delete(table, id)?)
    }

    /// Count all documents in a table.
    pub fn count(&self, table: &str) -> SdkResult<usize> {
        let count = self.native.count(table)?;
        Ok(count.max(0) as usize)
    }

    /// Scan all documents in a table (no filter).
    pub fn scan(&self, table: &str) -> SdkResult<Vec<Value>> {
        let result_str = self.native.query(&format!("SELECT * FROM {}", table))?;
        let result: Value = serde_json::from_str(&result_str)?;
        let rows = result.get("rows")
            .and_then(|r| r.as_array())
            .cloned()
            .unwrap_or_default();
        Ok(rows)
    }

    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // QUERY ENGINE
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    /// Execute an SQL query and return results.
    /// 
    /// Supports: SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, DROP TABLE, SHOW TABLES
    /// 
    /// ```ignore
    /// let result = db.query("SELECT * FROM users WHERE age > 25 ORDER BY name LIMIT 10").unwrap();
    /// for row in &result.rows {
    ///     println!("{}", row);
    /// }
    /// ```
    pub fn query(&mut self, sql: &str) -> SdkResult<QueryResult> {
        let start = Instant::now();
        let result_str = self.native.query(sql)?;
        let elapsed = start.elapsed().as_secs_f64() * 1000.0;

        let result: Value = serde_json::from_str(&result_str)?;
        let rows = result.get("rows")
            .and_then(|r| r.as_array())
            .cloned()
            .unwrap_or_default();
        let columns = result.get("columns")
            .and_then(|c| c.as_array())
            .map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect())
            .unwrap_or_default();
        let rows_affected = result.get("rows_affected")
            .and_then(|r| r.as_u64())
            .unwrap_or(0) as usize;

        Ok(QueryResult {
            rows,
            columns,
            rows_affected,
            execution_time_ms: elapsed,
        })
    }

    /// Execute a **parameterized** SQL query — the safe way to include user input.
    ///
    /// Use `?` as placeholders in the SQL template; values are sanitized and
    /// escaped before substitution. Any param containing SQL injection patterns
    /// (`DROP`, `DELETE`, `--`, `;`) is rejected with `SecurityError`.
    ///
    /// ```ignore
    /// // SAFE: user input via params, never via string concat
    /// let name: &str = get_user_input(); // could be "Alice'; DROP TABLE users--"
    /// let result = db.query_safe(
    ///     "SELECT * FROM users WHERE name = ?",
    ///     &[name],
    /// ).unwrap(); // Blocked: SecurityError if injection detected
    /// ```
    pub fn query_safe(&mut self, sql_template: &str, params: &[&str]) -> SdkResult<QueryResult> {
        /// Dangerous SQL keywords/tokens that signal injection attempts
        const DANGEROUS: &[&str] = &[
            "DROP", "TRUNCATE", "ALTER", "EXEC", "EXECUTE",
            "--", ";--", "/*", "*/", "xp_", "UNION",
        ];

        // Sanitize each param
        let mut sanitized: Vec<String> = Vec::with_capacity(params.len());
        for &param in params {
            let upper = param.to_uppercase();
            for &danger in DANGEROUS {
                if upper.contains(danger) {
                    return Err(SdkError::SecurityError(format!(
                        "SQL injection detected in parameter: '{}' contains forbidden token '{}'",
                        param, danger
                    )));
                }
            }
            // Escape single quotes by doubling them (SQL standard)
            let escaped = param.replace('\'', "''");
            sanitized.push(format!("'{}'", escaped));
        }

        // Replace ? placeholders in order
        let mut sql = sql_template.to_string();
        for value in &sanitized {
            if let Some(pos) = sql.find('?') {
                sql.replace_range(pos..pos + 1, value);
            } else {
                return Err(SdkError::SecurityError(
                    "More params than '?' placeholders in SQL template".to_string()
                ));
            }
        }

        // Check no unresolved placeholders remain
        let remaining = params.len();
        let placeholder_count = sql_template.chars().filter(|&c| c == '?').count();
        if remaining < placeholder_count {
            return Err(SdkError::SecurityError(format!(
                "SQL template has {} '?' placeholders but only {} params were provided",
                placeholder_count, remaining
            )));
        }

        self.query(&sql)
    }

    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // SEARCH
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    /// Full-text search across a table.
    pub fn search(&self, table: &str, text: &str) -> SdkResult<Vec<Value>> {
        let result_str = self.native.search(table, text)?;
        let values: Vec<Value> = serde_json::from_str(&result_str).unwrap_or_default();
        Ok(values)
    }

    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // STATISTICS
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    /// Get database statistics (expanded with MVCC info).
    pub fn stats(&self) -> SdkResult<Stats> {
        let file_size = std::fs::metadata(&self.path)
            .map(|m| m.len())
            .unwrap_or(0);
        let tables = self.list_tables().unwrap_or_default();
        let mut total_records = 0;
        for table in &tables {
            total_records += self.count(table).unwrap_or(0);
        }
        Ok(Stats {
            tables: tables.len(),
            total_records,
            file_size_bytes: file_size,
            path: self.path.clone(),
            mvcc_active_versions: 0, // Populated by engine when available
            page_size: 4096,
            sdk_version: Self::version(),
        })
    }

    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // MVCC TRANSACTIONS (Phase 5)
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    /// Begin a new MVCC transaction with the specified isolation level.
    ///
    /// ```ignore
    /// let txn = db.begin_transaction(IsolationLevel::ReadCommitted).unwrap();
    /// // ... perform reads/writes ...
    /// db.commit_transaction(&txn).unwrap();
    /// ```
    pub fn begin_transaction(&mut self, isolation: IsolationLevel) -> SdkResult<TransactionHandle> {
        let txn_id = self.native.begin_transaction(isolation as i32)?;
        Ok(TransactionHandle {
            txn_id,
            isolation,
            active: true,
        })
    }

    /// Commit a transaction, making all its changes permanent.
    pub fn commit_transaction(&mut self, txn: &TransactionHandle) -> SdkResult<()> {
        self.native.commit_transaction(txn.txn_id)?;
        Ok(())
    }

    /// Abort (rollback) a transaction, discarding all its changes.
    pub fn abort_transaction(&mut self, txn: &TransactionHandle) -> SdkResult<()> {
        self.native.abort_transaction(txn.txn_id)?;
        Ok(())
    }

    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    // INTEGRITY VERIFICATION (Phase 5)
    // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    /// Verify the integrity of the database.
    ///
    /// Checks B-Tree consistency, page checksums, and MVCC version chains.
    /// Returns a detailed report.
    ///
    /// ```ignore
    /// let report = db.verify_integrity().unwrap();
    /// assert!(report.is_valid);
    /// println!("Checked {} pages across {} tables", report.pages_checked, report.tables_verified);
    /// ```
    pub fn verify_integrity(&self) -> SdkResult<IntegrityReport> {
        let result_str = self.native.verify_integrity()?;
        let result: Value = serde_json::from_str(&result_str).unwrap_or_default();

        let is_valid = result.get("valid")
            .and_then(|v| v.as_bool())
            .unwrap_or(true);
        let pages_checked = result.get("pages_checked")
            .and_then(|v| v.as_u64())
            .unwrap_or(0) as usize;
        let tables_verified = result.get("tables_verified")
            .and_then(|v| v.as_u64())
            .unwrap_or(0) as usize;
        let issues = result.get("issues")
            .and_then(|v| v.as_array())
            .map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect())
            .unwrap_or_default();

        Ok(IntegrityReport {
            is_valid,
            pages_checked,
            tables_verified,
            issues,
        })
    }
}

impl Drop for OverDriveDB {
    fn drop(&mut self) {
        self.native.close();
    }
}