Switch from rusqlite to sqlx with sqlite feature enabled

This commit is contained in:
S-Y-rat 2020-07-31 15:59:26 +03:00
parent d0ab14d593
commit c09eca1e39
2 changed files with 15 additions and 8 deletions

View file

@ -24,7 +24,7 @@ authors = [
maintenance = { status = "actively-developed" }
[features]
sqlite-storage = ["rusqlite"]
sqlite-storage = ["sqlx"]
redis-storage = ["redis"]
cbor-serializer = ["serde_cbor"]
bincode-serializer = ["bincode"]
@ -51,7 +51,11 @@ futures = "0.3.5"
pin-project = "0.4.22"
serde_with_macros = "1.1.0"
rusqlite = { version = "0.23", optional = true }
sqlx = { version = "0.4.0-beta.1", optional = true, default-features = false, features = [
"runtime-tokio",
"macros",
"sqlite"
] }
redis = { version = "0.16.0", optional = true }
serde_cbor = { version = "0.11.1", optional = true }
bincode = { version = "1.3.1", optional = true }

View file

@ -4,10 +4,13 @@ use std::{
fmt::{Debug, Display},
sync::Arc,
};
use rusqlite::{params, Connection, Error, Result};
use sqlx::{SqliteConnection, Connection, sqlite::SqliteError};
use serde::{de::DeserializeOwned, Serialize};
use thiserror::Error;
use tokio::sync::Mutex;
use tokio::{
sync::Mutex,
task::block_in_place,
};
pub enum SqliteStorageLocation {
InMemory,
@ -23,11 +26,11 @@ where
#[error("parsing/serializing error: {0}")]
SerdeError(SE),
#[error("error from Sqlite: {0}")]
SqliteError(#[from] Error),
SqliteError(#[from] SqliteError),
}
pub struct SqliteStorage<S> {
conn: Mutex<Connection>,
conn: Mutex<SqliteConnection>,
serializer: S,
}
@ -35,13 +38,13 @@ impl <S> SqliteStorage<S> {
pub async fn open(
path: SqliteStorageLocation,
serializer: S,
) -> Result<Arc<Self>, SqliteStorageError<Infallible>>{
) -> Result<Arc<Self>, Box<dyn std::error::Error>>{
let url = match path {
SqliteStorageLocation::InMemory => String::from("sqlite::memory:"),
SqliteStorageLocation::Path(p) => p,
};
Ok(Arc::new(Self {
conn: Mutex::new(Connection::open(&url[..])?),
conn: Mutex::new(SqliteConnection::connect(&url[..]).await?),
serializer,
}))
}