Download packages/tauri-plugins/plugins/sql/src/wrapper.rs from echodict/readest: direct link, hf CLI and curl.
- Browser
- Download file 12.5 kB
-
https://huggingface.co/datasets/echodict/readest/resolve/main/packages/tauri-plugins/plugins/sql/src/wrapper.rs
- Command line
-
hf download hf://datasets/echodict/readest/packages/tauri-plugins/plugins/sql/src/wrapper.rs
-
curl -L -o wrapper.rs https://huggingface.co/datasets/echodict/readest/resolve/main/packages/tauri-plugins/plugins/sql/src/wrapper.rs
12.5 kB
| // Copyright 2019-2023 Tauri Programme within The Commons Conservancy | |
| // SPDX-License-Identifier: Apache-2.0 | |
| // SPDX-License-Identifier: MIT | |
| use std::fs::create_dir_all; | |
| use indexmap::IndexMap; | |
| use serde_json::Value as JsonValue; | |
| use sqlx::{migrate::MigrateDatabase, Column, Executor, Pool, Row}; | |
| use tauri::Manager; | |
| use tauri::{AppHandle, Runtime}; | |
| use sqlx::MySql; | |
| use sqlx::Postgres; | |
| use sqlx::Sqlite; | |
| use crate::LastInsertId; | |
| pub enum DbPool { | |
| Sqlite(Pool<Sqlite>), | |
| MySql(Pool<MySql>), | |
| Postgres(Pool<Postgres>), | |
| None, | |
| } | |
| // public methods | |
| /* impl DbPool { | |
| /// Get the inner Sqlite Pool. Returns None for MySql and Postgres pools. | |
| #[cfg(feature = "sqlite")] | |
| pub fn sqlite(&self) -> Option<&Pool<Sqlite>> { | |
| match self { | |
| DbPool::Sqlite(pool) => Some(pool), | |
| _ => None, | |
| } | |
| } | |
| /// Get the inner MySql Pool. Returns None for Sqlite and Postgres pools. | |
| #[cfg(feature = "mysql")] | |
| pub fn mysql(&self) -> Option<&Pool<MySql>> { | |
| match self { | |
| DbPool::MySql(pool) => Some(pool), | |
| _ => None, | |
| } | |
| } | |
| /// Get the inner Postgres Pool. Returns None for MySql and Sqlite pools. | |
| #[cfg(feature = "postgres")] | |
| pub fn postgres(&self) -> Option<&Pool<Postgres>> { | |
| match self { | |
| DbPool::Postgres(pool) => Some(pool), | |
| _ => None, | |
| } | |
| } | |
| } */ | |
| // private methods | |
| impl DbPool { | |
| pub(crate) async fn connect<R: Runtime>( | |
| conn_url: &str, | |
| _app: &AppHandle<R>, | |
| ) -> Result<Self, crate::Error> { | |
| match conn_url | |
| .split_once(':') | |
| .ok_or_else(|| crate::Error::InvalidDbUrl(conn_url.to_string()))? | |
| .0 | |
| { | |
| "sqlite" => { | |
| let app_path = _app | |
| .path() | |
| .app_config_dir() | |
| .expect("No App config path was found!"); | |
| create_dir_all(&app_path).expect("Couldn't create app config dir"); | |
| let conn_url = &path_mapper(app_path, conn_url); | |
| if !Sqlite::database_exists(conn_url).await.unwrap_or(false) { | |
| Sqlite::create_database(conn_url).await?; | |
| } | |
| Ok(Self::Sqlite(Pool::connect(conn_url).await?)) | |
| } | |
| "mysql" => { | |
| if !MySql::database_exists(conn_url).await.unwrap_or(false) { | |
| MySql::create_database(conn_url).await?; | |
| } | |
| Ok(Self::MySql(Pool::connect(conn_url).await?)) | |
| } | |
| "postgres" => { | |
| if !Postgres::database_exists(conn_url).await.unwrap_or(false) { | |
| Postgres::create_database(conn_url).await?; | |
| } | |
| Ok(Self::Postgres(Pool::connect(conn_url).await?)) | |
| } | |
| _ => Err(crate::Error::InvalidDbUrl(format!( | |
| "{conn_url} - No database driver enabled!" | |
| ))), | |
| _ => Err(crate::Error::InvalidDbUrl(conn_url.to_string())), | |
| } | |
| } | |
| pub(crate) async fn migrate( | |
| &self, | |
| _migrator: &sqlx::migrate::Migrator, | |
| ) -> Result<(), crate::Error> { | |
| match self { | |
| DbPool::Sqlite(pool) => _migrator.run(pool).await?, | |
| DbPool::MySql(pool) => _migrator.run(pool).await?, | |
| DbPool::Postgres(pool) => _migrator.run(pool).await?, | |
| DbPool::None => (), | |
| } | |
| Ok(()) | |
| } | |
| pub(crate) async fn close(&self) { | |
| match self { | |
| DbPool::Sqlite(pool) => pool.close().await, | |
| DbPool::MySql(pool) => pool.close().await, | |
| DbPool::Postgres(pool) => pool.close().await, | |
| DbPool::None => (), | |
| } | |
| } | |
| pub(crate) async fn execute( | |
| &self, | |
| _query: String, | |
| _values: Vec<JsonValue>, | |
| ) -> Result<(u64, LastInsertId), crate::Error> { | |
| Ok(match self { | |
| DbPool::Sqlite(pool) => { | |
| let mut query = sqlx::query(&_query); | |
| for value in _values { | |
| if value.is_null() { | |
| query = query.bind(None::<JsonValue>); | |
| } else if value.is_string() { | |
| query = query.bind(value.as_str().unwrap().to_owned()) | |
| } else if let Some(number) = value.as_number() { | |
| query = query.bind(number.as_f64().unwrap_or_default()) | |
| } else { | |
| query = query.bind(value); | |
| } | |
| } | |
| let result = pool.execute(query).await?; | |
| ( | |
| result.rows_affected(), | |
| LastInsertId::Sqlite(result.last_insert_rowid()), | |
| ) | |
| } | |
| DbPool::MySql(pool) => { | |
| let mut query = sqlx::query(&_query); | |
| for value in _values { | |
| if value.is_null() { | |
| query = query.bind(None::<JsonValue>); | |
| } else if value.is_string() { | |
| query = query.bind(value.as_str().unwrap().to_owned()) | |
| } else if let Some(number) = value.as_number() { | |
| query = query.bind(number.as_f64().unwrap_or_default()) | |
| } else { | |
| query = query.bind(value); | |
| } | |
| } | |
| let result = pool.execute(query).await?; | |
| ( | |
| result.rows_affected(), | |
| LastInsertId::MySql(result.last_insert_id()), | |
| ) | |
| } | |
| DbPool::Postgres(pool) => { | |
| let mut query = sqlx::query(&_query); | |
| for value in _values { | |
| if value.is_null() { | |
| query = query.bind(None::<JsonValue>); | |
| } else if value.is_string() { | |
| query = query.bind(value.as_str().unwrap().to_owned()) | |
| } else if let Some(number) = value.as_number() { | |
| query = query.bind(number.as_f64().unwrap_or_default()) | |
| } else { | |
| query = query.bind(value); | |
| } | |
| } | |
| let result = pool.execute(query).await?; | |
| (result.rows_affected(), LastInsertId::Postgres(())) | |
| } | |
| DbPool::None => (0, LastInsertId::None), | |
| }) | |
| } | |
| pub(crate) async fn select( | |
| &self, | |
| _query: String, | |
| _values: Vec<JsonValue>, | |
| ) -> Result<Vec<IndexMap<String, JsonValue>>, crate::Error> { | |
| Ok(match self { | |
| DbPool::Sqlite(pool) => { | |
| let mut query = sqlx::query(&_query); | |
| for value in _values { | |
| if value.is_null() { | |
| query = query.bind(None::<JsonValue>); | |
| } else if value.is_string() { | |
| query = query.bind(value.as_str().unwrap().to_owned()) | |
| } else if let Some(number) = value.as_number() { | |
| query = query.bind(number.as_f64().unwrap_or_default()) | |
| } else { | |
| query = query.bind(value); | |
| } | |
| } | |
| let rows = pool.fetch_all(query).await?; | |
| let mut values = Vec::new(); | |
| for row in rows { | |
| let mut value = IndexMap::default(); | |
| for (i, column) in row.columns().iter().enumerate() { | |
| let v = row.try_get_raw(i)?; | |
| let v = crate::decode::sqlite::to_json(v)?; | |
| value.insert(column.name().to_string(), v); | |
| } | |
| values.push(value); | |
| } | |
| values | |
| } | |
| DbPool::MySql(pool) => { | |
| let mut query = sqlx::query(&_query); | |
| for value in _values { | |
| if value.is_null() { | |
| query = query.bind(None::<JsonValue>); | |
| } else if value.is_string() { | |
| query = query.bind(value.as_str().unwrap().to_owned()) | |
| } else if let Some(number) = value.as_number() { | |
| query = query.bind(number.as_f64().unwrap_or_default()) | |
| } else { | |
| query = query.bind(value); | |
| } | |
| } | |
| let rows = pool.fetch_all(query).await?; | |
| let mut values = Vec::new(); | |
| for row in rows { | |
| let mut value = IndexMap::default(); | |
| for (i, column) in row.columns().iter().enumerate() { | |
| let v = row.try_get_raw(i)?; | |
| let v = crate::decode::mysql::to_json(v)?; | |
| value.insert(column.name().to_string(), v); | |
| } | |
| values.push(value); | |
| } | |
| values | |
| } | |
| DbPool::Postgres(pool) => { | |
| let mut query = sqlx::query(&_query); | |
| for value in _values { | |
| if value.is_null() { | |
| query = query.bind(None::<JsonValue>); | |
| } else if value.is_string() { | |
| query = query.bind(value.as_str().unwrap().to_owned()) | |
| } else if let Some(number) = value.as_number() { | |
| query = query.bind(number.as_f64().unwrap_or_default()) | |
| } else { | |
| query = query.bind(value); | |
| } | |
| } | |
| let rows = pool.fetch_all(query).await?; | |
| let mut values = Vec::new(); | |
| for row in rows { | |
| let mut value = IndexMap::default(); | |
| for (i, column) in row.columns().iter().enumerate() { | |
| let v = row.try_get_raw(i)?; | |
| let v = crate::decode::postgres::to_json(v)?; | |
| value.insert(column.name().to_string(), v); | |
| } | |
| values.push(value); | |
| } | |
| values | |
| } | |
| DbPool::None => Vec::new(), | |
| }) | |
| } | |
| } | |
| /// Maps the user supplied DB connection string to a connection string | |
| /// with a fully qualified file path to the App's designed "app_path" | |
| fn path_mapper(mut app_path: std::path::PathBuf, connection_string: &str) -> String { | |
| app_path.push( | |
| connection_string | |
| .split_once(':') | |
| .expect("Couldn't parse the connection string for DB!") | |
| .1, | |
| ); | |
| format!( | |
| "sqlite:{}", | |
| app_path | |
| .to_str() | |
| .expect("Problem creating fully qualified path to Database file!") | |
| ) | |
| } | |