discontinuedPackage connect_database has been discontinued.

execute method

Future<Result<Unit>> execute()

Implementation

Future<Result<Unit>> execute() async {
  final stopwatch = Stopwatch()..start();
  final query = commandText ?? '';
  final queryType = _extractQueryType(query);

  try {
    SqlValidCommand.validateExecute(_isConnected, commandText);
  } catch (e) {
    stopwatch.stop();
    _recordMetrics(
        query, queryType, stopwatch.elapsed, 0, 0, false, e.toString());
    return Failure(e is Exception ? e : Exception(e.toString()));
  }

  return _substituteParameters(commandText!).fold(
    (prepared) async {
      if (transaction != null &&
          !transaction!.autoCommit &&
          !transaction!.isOpen()) {
        final startResult = await transaction!.start(isSelect: false);
        if (startResult case Failure()) return startResult;
      }

      final execResult =
          await odbc.execute(prepared.sql, params: prepared.params);

      stopwatch.stop();

      if (execResult case Failure()) {
        await transaction?.doAutoRollback();
        final exception = execResult.exceptionOrNull();
        _recordMetrics(
          query,
          queryType,
          stopwatch.elapsed,
          0,
          0,
          false,
          exception.toString(),
        );
        return execResult.map((_) => unit);
      }

      await transaction?.doAutoCommit();

      final rows = execResult.getOrElse((_) => []);
      _recordMetrics(
        query,
        queryType,
        stopwatch.elapsed,
        rows.length,
        _estimateResultSize(rows),
        true,
        null,
      );

      return Success.unit();
    },
    (error) {
      stopwatch.stop();
      _recordMetrics(
          query, queryType, stopwatch.elapsed, 0, 0, false, error.toString());
      return Future.value(Failure(error));
    },
  );
}