From e18ffce8a3e5a4dee82b6f25f08ee2f396b576b0 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Mon, 20 May 2024 22:31:40 +0800 Subject: [PATCH] Fix sqlite3 compatibility --- np_db_sqlite/lib/src/web/util.dart | 8 ++++---- np_geocoder/lib/src/web/db_util.dart | 21 ++++++++++----------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/np_db_sqlite/lib/src/web/util.dart b/np_db_sqlite/lib/src/web/util.dart index 8e5a8c41..50be0333 100644 --- a/np_db_sqlite/lib/src/web/util.dart +++ b/np_db_sqlite/lib/src/web/util.dart @@ -11,17 +11,17 @@ Future> getSqliteConnectionArgs() async => {}; QueryExecutor openSqliteConnectionWithArgs(Map args) => openSqliteConnection(); +// Web is no longer supported. The code here has been updated to make it build +// with the latest sqlite3 package, but they are untested QueryExecutor openSqliteConnection() { return LazyDatabase(() async { // Load wasm bundle final response = await http.get(Uri.parse("sqlite3.wasm")); // Create a virtual file system backed by IndexedDb with everything in // `/drift/my_app/` being persisted. + final sqlite3 = await WasmSqlite3.load(response.bodyBytes); final fs = await IndexedDbFileSystem.open(dbName: "nc-photos"); - final sqlite3 = await WasmSqlite3.load( - response.bodyBytes, - SqliteEnvironment(fileSystem: fs), - ); + sqlite3.registerVirtualFileSystem(fs, makeDefault: true); // Then, open a database inside that persisted folder. return WasmDatabase( diff --git a/np_geocoder/lib/src/web/db_util.dart b/np_geocoder/lib/src/web/db_util.dart index f6bfcdee..c4198d15 100644 --- a/np_geocoder/lib/src/web/db_util.dart +++ b/np_geocoder/lib/src/web/db_util.dart @@ -2,28 +2,27 @@ import 'package:flutter/services.dart' show rootBundle; import 'package:http/http.dart' as http; import 'package:sqlite3/wasm.dart'; +// Web is no longer supported. The code here has been updated to make it build +// with the latest sqlite3 package, but they are untested Future openRawSqliteDbFromAsset( String assetRelativePath, String outputFilename, { bool isReadOnly = false, }) async { final response = await http.get(Uri.parse("sqlite3.wasm")); + final sqlite3 = await WasmSqlite3.load(response.bodyBytes); final fs = await IndexedDbFileSystem.open(dbName: "nc-photos"); - final sqlite3 = await WasmSqlite3.load( - response.bodyBytes, - SqliteEnvironment(fileSystem: fs), - ); + sqlite3.registerVirtualFileSystem(fs, makeDefault: true); - if (!fs.exists("/app-file/$outputFilename")) { + if (fs.xAccess("/app-file/$outputFilename", SqlFlag.SQLITE_OPEN_READONLY) == + 0) { // copy file from assets final blob = await rootBundle.load("assets/$assetRelativePath"); final buffer = blob.buffer; - fs.createFile("/app-file/$outputFilename"); - fs.write( - "/app-file/$outputFilename", - buffer.asUint8List(blob.offsetInBytes, blob.lengthInBytes), - 0, - ); + final f = fs.xOpen(Sqlite3Filename("/app-file/$outputFilename"), + SqlFlag.SQLITE_OPEN_CREATE | SqlFlag.SQLITE_OPEN_READWRITE); + f.file + .xWrite(buffer.asUint8List(blob.offsetInBytes, blob.lengthInBytes), 0); await fs.flush(); } return sqlite3.open("/app-file/$outputFilename");