-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(sql): Allow multiple drivers at the same time (#1838)
* refactor(sql): Allow multiple drivers at the same time * fmt * remove default feature comment [skip ci] * what was that doing there [skip ci] * disable public methods for now
- Loading branch information
1 parent
6857993
commit 30bcf5d
Showing
11 changed files
with
615 additions
and
393 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
sql: patch | ||
--- | ||
|
||
It is now possible to enable multiple SQL backends at the same time. There will be no compile error anymore if no backends are enabled! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
use indexmap::IndexMap; | ||
use serde_json::Value as JsonValue; | ||
use sqlx::migrate::Migrator; | ||
use tauri::{command, AppHandle, Runtime, State}; | ||
|
||
use crate::{DbInstances, DbPool, Error, LastInsertId, Migrations}; | ||
|
||
#[command] | ||
pub(crate) async fn load<R: Runtime>( | ||
app: AppHandle<R>, | ||
db_instances: State<'_, DbInstances>, | ||
migrations: State<'_, Migrations>, | ||
db: String, | ||
) -> Result<String, crate::Error> { | ||
let pool = DbPool::connect(&db, &app).await?; | ||
|
||
if let Some(migrations) = migrations.0.lock().await.remove(&db) { | ||
let migrator = Migrator::new(migrations).await?; | ||
pool.migrate(&migrator).await?; | ||
} | ||
|
||
db_instances.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( | ||
db_instances: State<'_, DbInstances>, | ||
db: Option<String>, | ||
) -> Result<bool, crate::Error> { | ||
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( | ||
db_instances: State<'_, DbInstances>, | ||
db: String, | ||
query: String, | ||
values: Vec<JsonValue>, | ||
) -> Result<(u64, LastInsertId), crate::Error> { | ||
let mut instances = db_instances.0.lock().await; | ||
|
||
let db = instances.get_mut(&db).ok_or(Error::DatabaseNotLoaded(db))?; | ||
db.execute(query, values).await | ||
} | ||
|
||
#[command] | ||
pub(crate) async fn select( | ||
db_instances: State<'_, DbInstances>, | ||
db: String, | ||
query: String, | ||
values: Vec<JsonValue>, | ||
) -> Result<Vec<IndexMap<String, JsonValue>>, crate::Error> { | ||
let mut instances = db_instances.0.lock().await; | ||
|
||
let db = instances.get_mut(&db).ok_or(Error::DatabaseNotLoaded(db))?; | ||
db.select(query, values).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
use serde::{Serialize, Serializer}; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[error(transparent)] | ||
Sql(#[from] sqlx::Error), | ||
#[error(transparent)] | ||
Migration(#[from] sqlx::migrate::MigrateError), | ||
#[error("invalid connection url: {0}")] | ||
InvalidDbUrl(String), | ||
#[error("database {0} not loaded")] | ||
DatabaseNotLoaded(String), | ||
#[error("unsupported datatype: {0}")] | ||
UnsupportedDatatype(String), | ||
} | ||
|
||
impl Serialize for Error { | ||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serializer.serialize_str(self.to_string().as_ref()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.