Skip to main content

clm_rust_core/layer2/
portable_sqlite.rs

1//! Layer 2: Portable SQLite Container Compilation and Parsing.
2//! Canonical Path: kernel/clm_rust_core/src/layer2/portable_sqlite.rs
3//! Parity: TypeScript `src/layer2/portableSqlite.ts`, Python `clm/layer2/portable_sqlite.py`
4
5#[cfg(any(feature = "native", feature = "db"))]
6pub use inner::*;
7
8#[cfg(any(feature = "native", feature = "db"))]
9mod inner {
10    use std::io::Write;
11    use rusqlite::{params, Connection};
12    use serde::{Deserialize, Serialize};
13
14    pub const SQLITE_HEADER: &[u8] = b"SQLite format 3\0";
15
16    pub const CANONICAL_SCHEMA: &str = r#"
17PRAGMA foreign_keys = OFF;
18
19CREATE TABLE IF NOT EXISTS card (
20    hash TEXT PRIMARY KEY,
21    content BLOB NOT NULL,
22    g_time TEXT NOT NULL
23);
24
25CREATE TABLE IF NOT EXISTS handle_registry (
26    handle TEXT PRIMARY KEY,
27    current_hash TEXT NOT NULL,
28    updated_at TEXT NOT NULL
29);
30
31CREATE TABLE IF NOT EXISTS handle_history (
32    id INTEGER PRIMARY KEY AUTOINCREMENT,
33    handle TEXT NOT NULL,
34    previous_hash TEXT NOT NULL,
35    changed_at TEXT NOT NULL
36);
37
38CREATE TABLE IF NOT EXISTS mcard_meta (
39    key TEXT PRIMARY KEY,
40    value TEXT NOT NULL
41);
42"#;
43
44    #[derive(Debug, Clone, Serialize, Deserialize)]
45    pub struct PortableSqliteCard {
46        pub hash: String,
47        pub content: Vec<u8>,
48        pub g_time: String,
49        pub handle: Option<String>,
50    }
51
52    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
53    pub struct ParsedCard {
54        pub hash: String,
55        pub content: Vec<u8>,
56        pub g_time: String,
57    }
58
59    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
60    pub struct ParsedHandle {
61        pub handle: String,
62        pub current_hash: String,
63        pub updated_at: String,
64    }
65
66    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
67    pub struct ParsedHistory {
68        pub handle: String,
69        pub previous_hash: String,
70        pub changed_at: String,
71    }
72
73    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
74    pub struct ParsedMeta {
75        pub key: String,
76        pub value: String,
77    }
78
79    #[derive(Debug, Clone, Serialize, Deserialize)]
80    pub struct ParsedPortableSqlite {
81        pub tables: Vec<String>,
82        pub cards: Vec<ParsedCard>,
83        pub handles: Vec<ParsedHandle>,
84        pub history: Vec<ParsedHistory>,
85        pub meta: Vec<ParsedMeta>,
86    }
87
88    pub fn compile_portable_sqlite(
89        cards: &[PortableSqliteCard],
90        schema_profile: Option<&str>,
91        exported_at: Option<&str>,
92        exported_by: Option<&str>,
93    ) -> Result<Vec<u8>, String> {
94        let temp_file = tempfile::NamedTempFile::new().map_err(|e| e.to_string())?;
95        let temp_path = temp_file.path();
96
97        {
98            let conn = Connection::open(temp_path).map_err(|e| e.to_string())?;
99            conn.execute_batch(CANONICAL_SCHEMA).map_err(|e| e.to_string())?;
100
101            // Virtual column compatibility
102            let _ = conn.execute_batch(
103                "ALTER TABLE handle_registry ADD COLUMN hash TEXT GENERATED ALWAYS AS (current_hash) VIRTUAL;
104                 ALTER TABLE handle_history ADD COLUMN hash TEXT GENERATED ALWAYS AS (previous_hash) VIRTUAL;
105                 ALTER TABLE handle_history ADD COLUMN created_at TEXT GENERATED ALWAYS AS (changed_at) VIRTUAL;"
106            );
107
108            let profile = schema_profile.unwrap_or("portable-3.0.3");
109            let author = exported_by.unwrap_or("RustCLM");
110            let now = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true);
111            let ts = exported_at.unwrap_or(&now);
112
113            conn.execute(
114                "INSERT OR REPLACE INTO mcard_meta (key, value) VALUES (?1, ?2), (?3, ?4), (?5, ?6), (?7, ?8);",
115                params![
116                    "schema_profile", profile,
117                    "exported_at", ts,
118                    "card_count", cards.len().to_string(),
119                    "exported_by", author
120                ],
121            ).map_err(|e| e.to_string())?;
122
123            let mut insert_card = conn.prepare(
124                "INSERT OR REPLACE INTO card (hash, content, g_time) VALUES (?1, ?2, ?3);"
125            ).map_err(|e| e.to_string())?;
126
127            let mut insert_handle = conn.prepare(
128                "INSERT OR REPLACE INTO handle_registry (handle, current_hash, updated_at) VALUES (?1, ?2, ?3);"
129            ).map_err(|e| e.to_string())?;
130
131            let mut insert_history = conn.prepare(
132                "INSERT INTO handle_history (handle, previous_hash, changed_at) VALUES (?1, ?2, ?3);"
133            ).map_err(|e| e.to_string())?;
134
135            for card in cards {
136                insert_card.execute(params![card.hash, card.content, card.g_time]).map_err(|e| e.to_string())?;
137                if let Some(ref h) = card.handle {
138                    insert_handle.execute(params![h, card.hash, card.g_time]).map_err(|e| e.to_string())?;
139                    insert_history.execute(params![h, card.hash, card.g_time]).map_err(|e| e.to_string())?;
140                }
141            }
142        }
143
144        std::fs::read(temp_path).map_err(|e| e.to_string())
145    }
146
147    pub fn parse_portable_sqlite(bytes: &[u8]) -> Result<ParsedPortableSqlite, String> {
148        if bytes.len() < 16 {
149            return Err("File too small to be a valid SQLite database".to_string());
150        }
151        if !bytes.starts_with(SQLITE_HEADER) {
152            return Err("Invalid SQLite 3 binary header (magic bytes mismatch)".to_string());
153        }
154
155        let mut temp_file = tempfile::NamedTempFile::new().map_err(|e| e.to_string())?;
156        temp_file.write_all(bytes).map_err(|e| e.to_string())?;
157        let temp_path = temp_file.path();
158
159        let conn = Connection::open(temp_path).map_err(|e| e.to_string())?;
160
161        let mut stmt = conn.prepare("SELECT name FROM sqlite_master WHERE type='table';").map_err(|e| e.to_string())?;
162        let table_iter = stmt.query_map([], |r| r.get::<_, String>(0)).map_err(|e| e.to_string())?;
163        let mut tables = Vec::new();
164        for t in table_iter {
165            tables.push(t.map_err(|e| e.to_string())?);
166        }
167
168        let mut cards = Vec::new();
169        if tables.contains(&"card".to_string()) {
170            let mut s = conn.prepare("SELECT hash, content, g_time FROM card;").map_err(|e| e.to_string())?;
171            let rows = s.query_map([], |r| {
172                Ok(ParsedCard {
173                    hash: r.get(0)?,
174                    content: r.get(1)?,
175                    g_time: r.get(2)?,
176                })
177            }).map_err(|e| e.to_string())?;
178            for c in rows {
179                cards.push(c.map_err(|e| e.to_string())?);
180            }
181        }
182
183        let mut handles = Vec::new();
184        if tables.contains(&"handle_registry".to_string()) {
185            let mut s = conn.prepare("SELECT handle, current_hash, updated_at FROM handle_registry;").map_err(|e| e.to_string())?;
186            let rows = s.query_map([], |r| {
187                Ok(ParsedHandle {
188                    handle: r.get(0)?,
189                    current_hash: r.get(1)?,
190                    updated_at: r.get(2)?,
191                })
192            }).map_err(|e| e.to_string())?;
193            for h in rows {
194                handles.push(h.map_err(|e| e.to_string())?);
195            }
196        }
197
198        let mut history = Vec::new();
199        if tables.contains(&"handle_history".to_string()) {
200            let mut s = conn.prepare("SELECT handle, previous_hash, changed_at FROM handle_history;").map_err(|e| e.to_string())?;
201            let rows = s.query_map([], |r| {
202                Ok(ParsedHistory {
203                    handle: r.get(0)?,
204                    previous_hash: r.get(1)?,
205                    changed_at: r.get(2)?,
206                })
207            }).map_err(|e| e.to_string())?;
208            for h in rows {
209                history.push(h.map_err(|e| e.to_string())?);
210            }
211        }
212
213        let mut meta = Vec::new();
214        if tables.contains(&"mcard_meta".to_string()) {
215            let mut s = conn.prepare("SELECT key, value FROM mcard_meta;").map_err(|e| e.to_string())?;
216            let rows = s.query_map([], |r| {
217                Ok(ParsedMeta {
218                    key: r.get(0)?,
219                    value: r.get(1)?,
220                })
221            }).map_err(|e| e.to_string())?;
222            for m in rows {
223                meta.push(m.map_err(|e| e.to_string())?);
224            }
225        }
226
227        Ok(ParsedPortableSqlite {
228            tables,
229            cards,
230            handles,
231            history,
232            meta,
233        })
234    }
235}