From 15909da268feb83b026f78b93556e753c43501f6 Mon Sep 17 00:00:00 2001 From: cijiugechu Date: Sat, 26 Oct 2024 23:29:19 +0800 Subject: [PATCH] fix(sql): allow multiple identical connections to be closed --- ...ow-multiple-identical-connections-close.md | 5 ++++ plugins/sql/api-iife.js | 2 +- plugins/sql/guest-js/index.ts | 25 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 .changes/fix-sql-allow-multiple-identical-connections-close.md diff --git a/.changes/fix-sql-allow-multiple-identical-connections-close.md b/.changes/fix-sql-allow-multiple-identical-connections-close.md new file mode 100644 index 00000000..c7818345 --- /dev/null +++ b/.changes/fix-sql-allow-multiple-identical-connections-close.md @@ -0,0 +1,5 @@ +--- +"sql-js": patch +--- + +Allow multiple identical connections to be closed. \ No newline at end of file diff --git a/plugins/sql/api-iife.js b/plugins/sql/api-iife.js index a30f68d9..5fd5aedf 100644 --- a/plugins/sql/api-iife.js +++ b/plugins/sql/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_PLUGIN_SQL__=function(){"use strict";async function e(e,t={},s){return window.__TAURI_INTERNALS__.invoke(e,t,s)}"function"==typeof SuppressedError&&SuppressedError;class t{constructor(e){this.path=e}static async load(s){const n=await e("plugin:sql|load",{db:s});return new t(n)}static get(e){return new t(e)}async execute(t,s){const[n,r]=await e("plugin:sql|execute",{db:this.path,query:t,values:s??[]});return{lastInsertId:r,rowsAffected:n}}async select(t,s){return await e("plugin:sql|select",{db:this.path,query:t,values:s??[]})}async close(t){return await e("plugin:sql|close",{db:t})}}return t}();Object.defineProperty(window.__TAURI__,"sql",{value:__TAURI_PLUGIN_SQL__})} +if("__TAURI__"in window){var __TAURI_PLUGIN_SQL__=function(){"use strict";async function t(t,e={},s){return window.__TAURI_INTERNALS__.invoke(t,e,s)}"function"==typeof SuppressedError&&SuppressedError;const e=new Map;class s{constructor(t){this.path=t,(t=>{e.set(t,(e.get(t)??0)+1)})(t)}static async load(e){const n=await t("plugin:sql|load",{db:e});return new s(n)}static get(t){return new s(t)}async execute(e,s){const[n,r]=await t("plugin:sql|execute",{db:this.path,query:e,values:s??[]});return{lastInsertId:r,rowsAffected:n}}async select(e,s){return await t("plugin:sql|select",{db:this.path,query:e,values:s??[]})}async close(s){if((t=>{const s=e.get(t)??0;s>0&&e.set(t,s-1)})(this.path),n=this.path,0!==e.get(n))return!0;var n;return await t("plugin:sql|close",{db:s})}}return s}();Object.defineProperty(window.__TAURI__,"sql",{value:__TAURI_PLUGIN_SQL__})} diff --git a/plugins/sql/guest-js/index.ts b/plugins/sql/guest-js/index.ts index 05eed0e3..281f971b 100644 --- a/plugins/sql/guest-js/index.ts +++ b/plugins/sql/guest-js/index.ts @@ -4,6 +4,24 @@ import { invoke } from '@tauri-apps/api/core' +const DatabaseConnections = new Map() + +const incrementConnectionCount = (path: string) => { + DatabaseConnections.set(path, (DatabaseConnections.get(path) ?? 0) + 1) +} + +const decrementConnectionCount = (path: string) => { + const count = DatabaseConnections.get(path) ?? 0 + + if (count > 0) { + DatabaseConnections.set(path, count - 1) + } +} + +const shouldCloseActualConnection = (path: string) => { + return DatabaseConnections.get(path) === 0 +} + export interface QueryResult { /** The number of rows affected by the query. */ rowsAffected: number @@ -28,6 +46,7 @@ export default class Database { path: string constructor(path: string) { this.path = path + incrementConnectionCount(path) } /** @@ -160,6 +179,12 @@ export default class Database { * @param db - Optionally state the name of a database if you are managing more than one. Otherwise, all database pools will be in scope. */ async close(db?: string): Promise { + decrementConnectionCount(this.path) + + if (!shouldCloseActualConnection(this.path)) { + return true + } + const success = await invoke('plugin:sql|close', { db })