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) =>
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(

View file

@ -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<CommonDatabase> 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");