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