From c09eca1e3986e869bf912f15ec8eb165ba4b02e2 Mon Sep 17 00:00:00 2001 From: S-Y-rat Date: Fri, 31 Jul 2020 15:59:26 +0300 Subject: [PATCH] Switch from rusqlite to sqlx with sqlite feature enabled --- Cargo.toml | 8 ++++++-- .../dialogue/storage/sqlite_storage.rs | 15 +++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3297f330..baaf9910 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/dispatching/dialogue/storage/sqlite_storage.rs b/src/dispatching/dialogue/storage/sqlite_storage.rs index e497228c..7912caa4 100644 --- a/src/dispatching/dialogue/storage/sqlite_storage.rs +++ b/src/dispatching/dialogue/storage/sqlite_storage.rs @@ -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 { - conn: Mutex, + conn: Mutex, serializer: S, } @@ -35,13 +38,13 @@ impl SqliteStorage { pub async fn open( path: SqliteStorageLocation, serializer: S, - ) -> Result, SqliteStorageError>{ + ) -> Result, Box>{ 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, })) }