Skip to content

Prototype for impl with sqlite3_web package #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix more web tests
  • Loading branch information
simolus3 committed Apr 7, 2024
commit ba23fb995b35b4beba454580fd322c18ea822eee
39 changes: 17 additions & 22 deletions lib/src/web/database/web_sqlite_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,38 +80,33 @@ class SqliteDatabaseImpl
});
}

T _runZoned<T>(T Function() callback, {required String debugContext}) {
if (Zone.current[this] != null) {
throw LockError(
'Recursive lock is not allowed. Use `tx.$debugContext` instead of `db.$debugContext`.');
}
var zone = Zone.current.fork(zoneValues: {this: true});
return zone.run(callback);
}

@override
Future<T> readLock<T>(Future<T> Function(SqliteReadContext tx) callback,
{Duration? lockTimeout, String? debugContext}) async {
await isInitialized;
return _connection.readLock(callback,
lockTimeout: lockTimeout, debugContext: debugContext);
return _runZoned(() {
return _connection.readLock(callback,
lockTimeout: lockTimeout, debugContext: debugContext);
}, debugContext: debugContext ?? 'execute()');
}

@override
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout, String? debugContext}) async {
await isInitialized;
return _connection.writeLock(callback,
lockTimeout: lockTimeout, debugContext: debugContext);
}

@override
Future<T> readTransaction<T>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This test currently is failing due to not throwing an exception if a SQL statement is executed after the transaction has been rolled back. This used to work by checking the state of autoCommit here. This does unfortunately perform a call to getAutoCommit for each executed statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the semantics you need are "execute if in transaction, otherwise throw", this should be possible with custom requests. _AsyncSqliteDatabase.handleCustomRequest has access to the raw database and could implement this request within a single round-trip if necessary.

Future<T> Function(SqliteReadContext tx) callback,
{Duration? lockTimeout,
String? debugContext}) async {
await isInitialized;
return _connection.readTransaction(callback, lockTimeout: lockTimeout);
}

@override
Future<T> writeTransaction<T>(
Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout,
String? debugContext}) async {
await isInitialized;
return _connection.writeTransaction(callback, lockTimeout: lockTimeout);
return _runZoned(() {
return _connection.writeLock(callback,
lockTimeout: lockTimeout, debugContext: debugContext);
}, debugContext: debugContext ?? 'execute()');
}

@override
Expand Down
2 changes: 1 addition & 1 deletion test/server/worker_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Future<void> hybridMain(StreamChannel<Object?> channel) async {
'-o',
driftWorkerPath,
'-O0',
'lib/src/web/worker/drift_worker.dart',
'lib/src/web/worker/worker.dart',
]);
if (process.exitCode != 0) {
fail('Could not compile worker');
Expand Down