Skip to content

Commit

Permalink
fix: use _ as word boundary in IIFE var names
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianLars committed Aug 7, 2024
1 parent 901dd56 commit 396ed3f
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 6 deletions.
9 changes: 9 additions & 0 deletions .changes/iife-varname-spacing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
barcode-scanner: patch
clipboard-manager: patch
deep-link: patch
global-shortcut: patch
window-state: patch
---

Fixed an issue that caused multi-word IIFE names to not be formatted correctly. For example the `barcode-scanner` was defined as `window.__TAURI_PLUGIN_CLIPBOARDMANAGER__` instead of `window.__TAURI_PLUGIN_CLIPBOARD_MANAGER__`.
2 changes: 1 addition & 1 deletion plugins/barcode-scanner/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/clipboard-manager/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/deep-link/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/global-shortcut/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

181 changes: 181 additions & 0 deletions plugins/sql/src/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#[cfg(feature = "sqlite")]
use std::{fs::create_dir_all, path::PathBuf};

use indexmap::IndexMap;
use serde_json::Value as JsonValue;
use sqlx::{
migrate::{MigrateDatabase, Migrator},
Any, MySql, Pool, Postgres, Sqlite,
};
use tauri::{command, AppHandle, Manager, Runtime, State};

use crate::{
path_mapper, DbType, Error, Migrations, MySqlInstances, PostgresInstances, Result,
SqliteInstances,
};

#[command]
pub(crate) async fn load<R: Runtime>(
app: AppHandle<R>,
migrations: State<'_, Migrations>,
db: String,
) -> Result<String> {
let dbtype = DbType::try_from(&*db)?;
let fqdb = path_mapper(app.path().app_config_dir()?, &db)?;

#[cfg(feature = "sqlite")]
create_dir_all(fqdb).expect("Problem creating App directory!");

match dbtype {
DbType::Sqlite => {
if !Sqlite::database_exists(&fqdb).await.unwrap_or(false) {
Sqlite::create_database(&fqdb).await?;
}

let pool = Pool::connect(&fqdb).await?;

if let Some(migrations) = migrations.0.lock().await.remove(&db) {
let migrator = Migrator::new(migrations).await?;
migrator.run(&pool).await?;
}

app.state::<SqliteInstances>()
.0
.lock()
.await
.insert(db.clone(), pool);
}
DbType::Postgres => {
if !Postgres::database_exists(&fqdb).await.unwrap_or(false) {
Postgres::create_database(&fqdb).await?;
}

let pool = Pool::connect(&fqdb).await?;

if let Some(migrations) = migrations.0.lock().await.remove(&db) {
let migrator = Migrator::new(migrations).await?;
migrator.run(&pool).await?;
}

app.state::<PostgresInstances>()
.0
.lock()
.await
.insert(db.clone(), pool);
}
DbType::MySql => {
if !MySql::database_exists(&fqdb).await.unwrap_or(false) {
MySql::create_database(&fqdb).await?;
}

let pool = Pool::connect(&fqdb).await?;

if let Some(migrations) = migrations.0.lock().await.remove(&db) {
let migrator = Migrator::new(migrations).await?;
migrator.run(&pool).await?;
}

app.state::<MySqlInstances>()
.0
.lock()
.await
.insert(db.clone(), pool);
}
}

Ok(db)
}

/// Allows the database connection(s) to be closed; if no database
/// name is passed in then _all_ database connection pools will be
/// shut down.
#[command]
pub(crate) async fn close<R: Runtime>(app: AppHandle<R>, db: Option<String>) -> Result<bool> {
let mut instances = db_instances.0.lock().await;

let pools = if let Some(db) = db {
vec![db]
} else {
instances.keys().cloned().collect()
};

for pool in pools {
let db = instances
.get_mut(&pool) //
.ok_or(Error::DatabaseNotLoaded(pool))?;
db.close().await;
}

Ok(true)
}

/// Execute a command against the database
#[command]
pub(crate) async fn execute<R: Runtime>(
app: AppHandle<R>,
db: String,
query: String,
values: Vec<JsonValue>,
) -> Result<(u64, i64)> {
let mut instances = db_instances.0.lock().await;

let db = instances.get_mut(&db).ok_or(Error::DatabaseNotLoaded(db))?;
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 = query.execute(&*db).await?;
#[cfg(feature = "sqlite")]
let r = Ok((result.rows_affected(), result.last_insert_rowid()));
#[cfg(feature = "mysql")]
let r = Ok((result.rows_affected(), result.last_insert_id()));
#[cfg(feature = "postgres")]
let r = Ok((result.rows_affected(), 0));
r
}

#[command]
pub(crate) async fn select<R: Runtime>(
app: AppHandle<R>,
db: String,
query: String,
values: Vec<JsonValue>,
) -> Result<Vec<IndexMap<String, JsonValue>>> {
let db = instances.get_mut(&db).ok_or(Error::DatabaseNotLoaded(db))?;
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 = query.fetch_all(&*db).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::to_json(v)?;

value.insert(column.name().to_string(), v);
}

values.push(value);
}

Ok(values)
}
Loading

0 comments on commit 396ed3f

Please sign in to comment.