From d72b787610f0774efd676d735b62eeb8581fcc95 Mon Sep 17 00:00:00 2001 From: Visal In Date: Sun, 28 Jul 2024 18:53:30 +0900 Subject: [PATCH] feat: add refresh sqlite file --- src/app/playground/client/page-client.tsx | 59 ++++++++++++++++++++++- src/drivers/sqljs-driver.ts | 7 ++- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/app/playground/client/page-client.tsx b/src/app/playground/client/page-client.tsx index 372f12c6..d98ff72a 100644 --- a/src/app/playground/client/page-client.tsx +++ b/src/app/playground/client/page-client.tsx @@ -3,13 +3,18 @@ import { saveAs } from "file-saver"; import MyStudio from "@/components/my-studio"; import { Button } from "@/components/ui/button"; import SqljsDriver from "@/drivers/sqljs-driver"; -import { LucideFile, LucideLoader } from "lucide-react"; +import { LucideFile, LucideLoader, LucideRefreshCw } from "lucide-react"; import Script from "next/script"; import { useCallback, useEffect, useMemo, useState } from "react"; import { Database, SqlJsStatic } from "sql.js"; import ScreenDropZone from "@/components/screen-dropzone"; import { toast } from "sonner"; import downloadFileFromUrl from "@/lib/download-file"; +import { + Tooltip, + TooltipContent, + TooltipTrigger, +} from "@/components/ui/tooltip"; export default function PlaygroundEditorBody({ preloadDatabase, @@ -62,6 +67,30 @@ export default function PlaygroundEditorBody({ } }, [handler, sqlInit]); + const onReloadDatabase = useCallback(() => { + if (db && db.hasChanged()) { + if ( + !confirm( + "You have some changes. Refresh will lose your change. Do you want to refresh" + ) + ) { + return; + } + } + + if (handler && sqlInit) { + handler.getFile().then((file) => { + file.arrayBuffer().then((buffer) => { + const sqljsDatabase = new sqlInit.Database(new Uint8Array(buffer)); + setRawDb(sqljsDatabase); + if (db) { + db.reload(sqljsDatabase); + } + }); + }); + } + }, [handler, sqlInit, db]); + const sidebarMenu = useMemo(() => { return (
@@ -143,6 +172,34 @@ export default function PlaygroundEditorBody({ > Open + {handler && ( + + + + + +

+ Refresh +

+ +

+ LibSQL Studio loads data into memory. If the file changes, it + is unaware of the update. +

+ +

+ To reflect the changes, use the refresh option to reload the + data from the file. +

+
+
+ )}
); diff --git a/src/drivers/sqljs-driver.ts b/src/drivers/sqljs-driver.ts index d50647f8..80b3e40c 100644 --- a/src/drivers/sqljs-driver.ts +++ b/src/drivers/sqljs-driver.ts @@ -10,8 +10,6 @@ import { BindParams, Database } from "sql.js"; export default class SqljsDriver extends SqliteLikeBaseDriver { protected db: Database; - protected username?: string; - protected password?: string; protected hasRowsChanged: boolean = false; constructor(sqljs: Database) { @@ -19,6 +17,11 @@ export default class SqljsDriver extends SqliteLikeBaseDriver { this.db = sqljs; } + reload(sqljs: Database) { + this.db = sqljs; + this.hasRowsChanged = false; + } + async transaction(stmts: InStatement[]): Promise { const r: DatabaseResultSet[] = [];