-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:drift/drift.dart'; | ||
import 'package:drift/native.dart'; | ||
|
||
// #docregion class | ||
class LogInterceptor extends QueryInterceptor { | ||
Future<T> _run<T>( | ||
String description, FutureOr<T> Function() operation) async { | ||
final stopwatch = Stopwatch()..start(); | ||
print('Running $description'); | ||
|
||
try { | ||
final result = await operation(); | ||
print(' => succeeded after ${stopwatch.elapsedMilliseconds}ms'); | ||
return result; | ||
} on Object catch (e) { | ||
print(' => failed after ${stopwatch.elapsedMilliseconds}ms ($e)'); | ||
rethrow; | ||
} | ||
} | ||
|
||
@override | ||
TransactionExecutor beginTransaction(QueryExecutor parent) { | ||
print('begin'); | ||
return super.beginTransaction(parent); | ||
} | ||
|
||
@override | ||
Future<void> commitTransaction(TransactionExecutor inner) { | ||
return _run('commit', () => inner.send()); | ||
} | ||
|
||
@override | ||
Future<void> rollbackTransaction(TransactionExecutor inner) { | ||
return _run('rollback', () => inner.rollback()); | ||
} | ||
|
||
@override | ||
Future<void> runBatched( | ||
QueryExecutor executor, BatchedStatements statements) { | ||
return _run( | ||
'batch with $statements', () => executor.runBatched(statements)); | ||
} | ||
|
||
@override | ||
Future<int> runInsert( | ||
QueryExecutor executor, String statement, List<Object?> args) { | ||
return _run( | ||
'$statement with $args', () => executor.runInsert(statement, args)); | ||
} | ||
|
||
@override | ||
Future<int> runUpdate( | ||
QueryExecutor executor, String statement, List<Object?> args) { | ||
return _run( | ||
'$statement with $args', () => executor.runUpdate(statement, args)); | ||
} | ||
|
||
@override | ||
Future<int> runDelete( | ||
QueryExecutor executor, String statement, List<Object?> args) { | ||
return _run( | ||
'$statement with $args', () => executor.runDelete(statement, args)); | ||
} | ||
|
||
@override | ||
Future<void> runCustom( | ||
QueryExecutor executor, String statement, List<Object?> args) { | ||
return _run( | ||
'$statement with $args', () => executor.runCustom(statement, args)); | ||
} | ||
|
||
@override | ||
Future<List<Map<String, Object?>>> runSelect( | ||
QueryExecutor executor, String statement, List<Object?> args) { | ||
return _run( | ||
'$statement with $args', () => executor.runSelect(statement, args)); | ||
} | ||
} | ||
// #enddocregion class | ||
|
||
void use() { | ||
final myDatabaseFile = File('/dev/null'); | ||
|
||
// #docregion use | ||
NativeDatabase.createInBackground( | ||
myDatabaseFile, | ||
).interceptWith(LogInterceptor()); | ||
// #enddocregion use | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
data: | ||
title: "Tracing database operations" | ||
description: Using the `QueryInterceptor` API to log details about database operations. | ||
template: layouts/docs/single | ||
--- | ||
|
||
{% assign snippets = 'package:drift_docs/snippets/log_interceptor.dart.excerpt.json' | readString | json_decode %} | ||
|
||
Drift provides the relatively simple `logStatements` option to print the statements it | ||
executes. | ||
The `QueryInterceptor` API can be used to extend this logging to provide more information, | ||
which this example will show. | ||
|
||
{% include "blocks/snippet" snippets=snippets name="class" %} | ||
|
||
Interceptors can be applied with the `interceptWith` extension on `QueryExecutor` and | ||
`DatabaseConnection`: | ||
|
||
{% include "blocks/snippet" snippets=snippets name="use" %} | ||
|
||
The `QueryInterceptor` class is pretty powerful, as it allows you to fully control the underlying | ||
database connection. You could also use it to retry some failing statements or to aggregate | ||
statistics about query times to an external monitoring service. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters