diff options
Diffstat (limited to 'src/storage.rs')
-rw-r--r-- | src/storage.rs | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/src/storage.rs b/src/storage.rs index fef8194..51a418e 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,4 +1,8 @@ -use color_eyre::{eyre::{bail, WrapErr}, Result}; +use color_eyre::{ + eyre::{bail, WrapErr}, + Result, +}; +use rusqlite::{params, Connection}; use std::{ fs, io::ErrorKind, @@ -65,3 +69,53 @@ impl Storage for Folder { Ok(()) } } + +#[derive(Debug)] +pub struct Sqlite { + connection: Connection, +} + +impl Sqlite { + pub fn connect<P: AsRef<Path>>(file: P) -> Result<Self> { + let path = file.as_ref(); + if fs::metadata(path).is_ok() { + bail!("Path {path:?} already exists, refusing to open") + } + let connection = Connection::open(path)?; + Ok(Sqlite { connection }) + } +} + +impl Storage for Sqlite { + fn prepare(&mut self) -> Result<()> { + self.connection.execute( + "CREATE TABLE tiles ( + zoom INTEGER, + x INTEGER, + y INTEGER, + data BLOB, + PRIMARY KEY (zoom, x, y) + );", + (), + )?; + self.connection.execute("BEGIN;", ())?; + Ok(()) + } + + fn prepare_zoom(&mut self, _zoom: u32) -> Result<()> { + Ok(()) + } + + fn store(&mut self, zoom: u32, x: u64, y: u64, data: &[u8]) -> Result<()> { + self.connection.execute( + "INSERT INTO tiles (zoom, x, y, data) VALUES (?, ?, ?, ?)", + params![zoom, x, y, data], + )?; + Ok(()) + } + + fn finish(&mut self) -> Result<()> { + self.connection.execute("COMMIT;", ())?; + Ok(()) + } +} |