diff options
author | Daniel Schadt <kingdread@gmx.de> | 2023-01-18 18:46:39 +0100 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2023-01-18 18:46:39 +0100 |
commit | ac3afadba547b4b9a4063da567acd6d2f4f74554 (patch) | |
tree | ac6bf655b90d56a0431c04322cf3f5bdc92caf8b /src/storage.rs | |
parent | 160aba6258e1979ef85d66b2c27c33f6f28a7e38 (diff) | |
download | hittekaart-ac3afadba547b4b9a4063da567acd6d2f4f74554.tar.gz hittekaart-ac3afadba547b4b9a4063da567acd6d2f4f74554.tar.bz2 hittekaart-ac3afadba547b4b9a4063da567acd6d2f4f74554.zip |
add support for SQLite output
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(()) + } +} |