-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathupdate_hook.rs
More file actions
98 lines (84 loc) · 2.98 KB
/
update_hook.rs
File metadata and controls
98 lines (84 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use serde::Serialize;
use tauri::{command, AppHandle, Emitter, Runtime, State};
use crate::{DbInstances, DbPool, Error};
#[derive(Clone, Serialize)]
pub struct UpdateHookEvent {
pub operation: String,
pub database: String,
pub table: String,
pub rowid: i64,
}
#[command]
pub(crate) async fn setup_update_hook<R: Runtime>(
app: AppHandle<R>,
db_instances: State<'_, DbInstances>,
db: String,
) -> Result<(), Error> {
#[cfg(feature = "sqlite")]
{
let instances = db_instances.0.read().await;
let db_pool = instances
.get(&db)
.ok_or_else(|| Error::DatabaseNotLoaded(db.clone()))?;
let sqlite_pool = match db_pool {
DbPool::Sqlite(pool) => pool,
_ => return Err(Error::InvalidDbUrl(
format!("Cannot setup update hook for {}: update hooks are only supported for SQLite databases", db)
)),
};
sqlite_pool
.rebuild_pool(Some(move |result: sqlx::sqlite::UpdateHookResult| {
let operation = match result.operation {
sqlx::sqlite::SqliteOperation::Insert => "INSERT",
sqlx::sqlite::SqliteOperation::Update => "UPDATE",
sqlx::sqlite::SqliteOperation::Delete => "DELETE",
sqlx::sqlite::SqliteOperation::Unknown(_) => "UNKNOWN",
};
let event = UpdateHookEvent {
operation: operation.to_string(),
database: result.database.to_string(),
table: result.table.to_string(),
rowid: result.rowid,
};
if let Err(e) = app.emit("sqlite-update-hook", &event) {
log::error!("[tauri-plugin-sql] Failed to emit update hook event: {}", e);
}
}))
.await
.map_err(Error::Sql)?;
Ok(())
}
}
#[command]
pub(crate) async fn remove_update_hook(
db_instances: State<'_, DbInstances>,
db: String,
) -> Result<(), Error> {
#[cfg(feature = "sqlite")]
{
let instances = db_instances.0.read().await;
let db_pool = instances
.get(&db)
.ok_or_else(|| Error::DatabaseNotLoaded(db.clone()))?;
let sqlite_pool = match db_pool {
DbPool::Sqlite(pool) => pool,
_ => return Err(Error::InvalidDbUrl(
format!("Cannot remove update hook for {}: update hooks are only supported for SQLite databases", db)
)),
};
sqlite_pool
.rebuild_pool(None::<fn(sqlx::sqlite::UpdateHookResult)>)
.await
.map_err(Error::Sql)?;
Ok(())
}
#[cfg(not(feature = "sqlite"))]
{
Err(Error::InvalidDbUrl(
"Update hooks are only supported for SQLite".to_string(),
))
}
}