mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-30 14:12:07 +02:00
Queries now use AppHandle instead of Window (#189)
This commit is contained in:
@@ -4,10 +4,16 @@ use thiserror::Error;
|
||||
pub enum Error {
|
||||
#[error("SQL error: {0}")]
|
||||
SqlError(#[from] rusqlite::Error),
|
||||
|
||||
#[error("SQL Pool error: {0}")]
|
||||
SqlPoolError(#[from] r2d2::Error),
|
||||
|
||||
#[error("JSON error: {0}")]
|
||||
JsonError(#[from] serde_json::Error),
|
||||
|
||||
#[error("Model not found {0}")]
|
||||
ModelNotFound(String),
|
||||
|
||||
#[error("unknown error")]
|
||||
Unknown,
|
||||
}
|
||||
|
||||
@@ -4,3 +4,4 @@ pub mod queries;
|
||||
|
||||
pub mod plugin;
|
||||
pub mod render;
|
||||
pub mod manager;
|
||||
|
||||
40
src-tauri/yaak-models/src/manager.rs
Normal file
40
src-tauri/yaak-models/src/manager.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use crate::error::Result;
|
||||
use crate::models::{Workspace, WorkspaceIden};
|
||||
use crate::plugin::SqliteConnection;
|
||||
use r2d2::{Pool, PooledConnection};
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use rusqlite::Connection;
|
||||
use sea_query::{Asterisk, Order, Query, SqliteQueryBuilder};
|
||||
use sea_query_rusqlite::RusqliteBinder;
|
||||
use std::future::Future;
|
||||
use std::ops::Deref;
|
||||
use tauri::{AppHandle, Manager, Runtime};
|
||||
|
||||
pub struct QueryManager {
|
||||
pool: Pool<SqliteConnectionManager>,
|
||||
}
|
||||
|
||||
pub trait DBConnection {
|
||||
fn connect(
|
||||
&self,
|
||||
) -> impl Future<Output = Result<PooledConnection<SqliteConnectionManager>>> + Send;
|
||||
}
|
||||
|
||||
impl<R: Runtime> DBConnection for AppHandle<R> {
|
||||
async fn connect(&self) -> Result<PooledConnection<SqliteConnectionManager>> {
|
||||
let dbm = &*self.state::<SqliteConnection>();
|
||||
let db = dbm.0.lock().await.get()?;
|
||||
Ok(db)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn list_workspaces<T: Deref<Target = Connection>>(c: &T) -> Result<Vec<Workspace>> {
|
||||
let (sql, params) = Query::select()
|
||||
.from(WorkspaceIden::Table)
|
||||
.column(Asterisk)
|
||||
.order_by(WorkspaceIden::Name, Order::Asc)
|
||||
.build_rusqlite(SqliteQueryBuilder);
|
||||
let mut stmt = c.prepare(sql.as_str())?;
|
||||
let items = stmt.query_map(&*params.as_params(), |row| row.try_into())?;
|
||||
Ok(items.map(|v| v.unwrap()).collect())
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user