handleCustomRequest method

  1. @override
Future<JSAny?> handleCustomRequest(
  1. ClientConnection connection,
  2. JSAny? request
)
override

Handles a custom request (encoded as any JS value) from the connection.

The response is sent over the channel and completes a Database.customRequest call for clients.

Implementation

@override
Future<JSAny?> handleCustomRequest(
    ClientConnection connection, JSAny? request) async {
  final message = request as CustomDatabaseMessage;

  switch (message.kind) {
    case CustomDatabaseMessageKind.requestSharedLock:
      await mutex.acquireRead();
    case CustomDatabaseMessageKind.requestExclusiveLock:
      await mutex.acquireWrite();
    case CustomDatabaseMessageKind.releaseLock:
      mutex.release();
    case CustomDatabaseMessageKind.lockObtained:
      throw UnsupportedError('This is a response, not a request');
    case CustomDatabaseMessageKind.getAutoCommit:
      return database.autocommit.toJS;
    case CustomDatabaseMessageKind.executeInTransaction:
      final sql = message.rawSql.toDart;
      final parameters = [
        for (final raw in (message.rawParameters).toDart) raw.dartify()
      ];
      if (database.autocommit) {
        throw SqliteException(0,
            "Transaction rolled back by earlier statement. Cannot execute: $sql");
      }
      var res = database.select(sql, parameters);

      var dartMap = resultSetToMap(res);

      var jsObject = js_util.jsify(dartMap);

      return jsObject;
    case CustomDatabaseMessageKind.executeBatchInTransaction:
      final sql = message.rawSql.toDart;
      final parameters = [
        for (final raw in (message.rawParameters).toDart) raw.dartify()
      ];
      if (database.autocommit) {
        throw SqliteException(0,
            "Transaction rolled back by earlier statement. Cannot execute: $sql");
      }
      database.execute(sql, parameters);
  }

  return CustomDatabaseMessage(CustomDatabaseMessageKind.lockObtained);
}