Skip to content

Commit

Permalink
feat: add sqlite close method
Browse files Browse the repository at this point in the history
  • Loading branch information
lzdyes committed Jul 11, 2022
1 parent 125a46d commit 3d09a5a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tauri-plugin-sqlite-api",
"description": "A Tauri plugin for interface to SQLite",
"version": "0.1.0",
"version": "0.1.1",
"author": "lzdyes <[email protected]>",
"repository": {
"type": "git",
Expand Down
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ async fn open(state: State<'_, SqliteMap>, path: String) -> Result<bool> {
Ok(true)
}

#[command]
async fn close(state: State<'_, SqliteMap>, path: String) -> Result<bool> {
let mut map = state.0.lock().unwrap();
let connection = map.get_mut(&path).ok_or(Error::DatabaseNotOpened(path.clone()))?;
drop(connection);
map.remove(&path);
Ok(true)
}

#[command]
async fn execute(state: State<'_, SqliteMap>, path: String, sql: String) -> Result<bool> {
let mut map = state.0.lock().unwrap();
Expand Down Expand Up @@ -148,7 +157,7 @@ async fn select(

pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("sqlite")
.invoke_handler(tauri::generate_handler![open, execute, execute2, select])
.invoke_handler(tauri::generate_handler![open, close, execute, execute2, select])
.setup(|app| {
app.manage(SqliteMap::default());
Ok(())
Expand Down
1 change: 1 addition & 0 deletions webview-dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export default class SQLite {
path: string;
constructor(path: string);
static open(path: string): Promise<SQLite>;
close(): Promise<boolean>;
execute(sql: string, values?: unknown[]): Promise<boolean>;
select<T>(sql: string, values?: unknown[]): Promise<T>;
}
2 changes: 1 addition & 1 deletion webview-dist/index.js

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

4 changes: 4 additions & 0 deletions webview-src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export default class SQLite {
return await invoke<string>('plugin:sqlite|open', { path }).then(() => new SQLite(path))
}

async close(): Promise<boolean> {
return invoke('plugin:sqlite|close', { path: this.path })
}

async execute(sql: string, values?: unknown[]): Promise<boolean> {
return values ? invoke('plugin:sqlite|execute2', { path: this.path, sql, values }) : invoke('plugin:sqlite|execute', { path: this.path, sql })
}
Expand Down

0 comments on commit 3d09a5a

Please sign in to comment.