Fix sqlite3 compatibility

This commit is contained in:
Ming Ming 2024-05-20 22:31:40 +08:00
parent 7331e5a24d
commit e18ffce8a3
2 changed files with 14 additions and 15 deletions

View file

@ -11,17 +11,17 @@ Future<Map<String, dynamic>> getSqliteConnectionArgs() async => {};
QueryExecutor openSqliteConnectionWithArgs(Map<String, dynamic> args) => QueryExecutor openSqliteConnectionWithArgs(Map<String, dynamic> args) =>
openSqliteConnection(); 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() { QueryExecutor openSqliteConnection() {
return LazyDatabase(() async { return LazyDatabase(() async {
// Load wasm bundle // Load wasm bundle
final response = await http.get(Uri.parse("sqlite3.wasm")); final response = await http.get(Uri.parse("sqlite3.wasm"));
// Create a virtual file system backed by IndexedDb with everything in // Create a virtual file system backed by IndexedDb with everything in
// `/drift/my_app/` being persisted. // `/drift/my_app/` being persisted.
final sqlite3 = await WasmSqlite3.load(response.bodyBytes);
final fs = await IndexedDbFileSystem.open(dbName: "nc-photos"); final fs = await IndexedDbFileSystem.open(dbName: "nc-photos");
final sqlite3 = await WasmSqlite3.load( sqlite3.registerVirtualFileSystem(fs, makeDefault: true);
response.bodyBytes,
SqliteEnvironment(fileSystem: fs),
);
// Then, open a database inside that persisted folder. // Then, open a database inside that persisted folder.
return WasmDatabase( return WasmDatabase(

View file

@ -2,28 +2,27 @@ import 'package:flutter/services.dart' show rootBundle;
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:sqlite3/wasm.dart'; 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<CommonDatabase> openRawSqliteDbFromAsset( Future<CommonDatabase> openRawSqliteDbFromAsset(
String assetRelativePath, String assetRelativePath,
String outputFilename, { String outputFilename, {
bool isReadOnly = false, bool isReadOnly = false,
}) async { }) async {
final response = await http.get(Uri.parse("sqlite3.wasm")); 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 fs = await IndexedDbFileSystem.open(dbName: "nc-photos");
final sqlite3 = await WasmSqlite3.load( sqlite3.registerVirtualFileSystem(fs, makeDefault: true);
response.bodyBytes,
SqliteEnvironment(fileSystem: fs),
);
if (!fs.exists("/app-file/$outputFilename")) { if (fs.xAccess("/app-file/$outputFilename", SqlFlag.SQLITE_OPEN_READONLY) ==
0) {
// copy file from assets // copy file from assets
final blob = await rootBundle.load("assets/$assetRelativePath"); final blob = await rootBundle.load("assets/$assetRelativePath");
final buffer = blob.buffer; final buffer = blob.buffer;
fs.createFile("/app-file/$outputFilename"); final f = fs.xOpen(Sqlite3Filename("/app-file/$outputFilename"),
fs.write( SqlFlag.SQLITE_OPEN_CREATE | SqlFlag.SQLITE_OPEN_READWRITE);
"/app-file/$outputFilename", f.file
buffer.asUint8List(blob.offsetInBytes, blob.lengthInBytes), .xWrite(buffer.asUint8List(blob.offsetInBytes, blob.lengthInBytes), 0);
0,
);
await fs.flush(); await fs.flush();
} }
return sqlite3.open("/app-file/$outputFilename"); return sqlite3.open("/app-file/$outputFilename");