-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use our own logger to log the SQL statements for consistency
- Loading branch information
1 parent
c53f290
commit fa62569
Showing
3 changed files
with
137 additions
and
20 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
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
105 changes: 105 additions & 0 deletions
105
turms-chat-demo-flutter/lib/infra/sqlite/core/sql_logging_query_executor.dart
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,105 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:drift/drift.dart'; | ||
|
||
import '../../logging/logger.dart'; | ||
|
||
class SqlLoggingQueryInterceptor extends QueryInterceptor { | ||
SqlLoggingQueryInterceptor(); | ||
|
||
@override | ||
TransactionExecutor beginTransaction(QueryExecutor parent) { | ||
logger.debug('[Drift] Start transaction'); | ||
return super.beginTransaction(parent); | ||
} | ||
|
||
@override | ||
Future<void> commitTransaction(TransactionExecutor inner) => | ||
_run('Commit transaction', () => inner.send()); | ||
|
||
@override | ||
Future<void> rollbackTransaction(TransactionExecutor inner) => | ||
_run('Rollback transaction', () => inner.rollback()); | ||
|
||
@override | ||
Future<void> runBatched( | ||
QueryExecutor executor, | ||
BatchedStatements statements, | ||
) => | ||
_run( | ||
'Run ${statements.statements}. Args: ${statements.arguments}}', | ||
() => executor.runBatched(statements), | ||
); | ||
|
||
@override | ||
Future<int> runInsert( | ||
QueryExecutor executor, | ||
String statement, | ||
List<Object?> args, | ||
) => | ||
_run( | ||
'Run `$statement`. Args $args', | ||
() => executor.runInsert(statement, args), | ||
); | ||
|
||
@override | ||
Future<int> runUpdate( | ||
QueryExecutor executor, | ||
String statement, | ||
List<Object?> args, | ||
) => | ||
_run( | ||
'Run `$statement`. Args $args', | ||
() => executor.runUpdate(statement, args), | ||
); | ||
|
||
@override | ||
Future<int> runDelete( | ||
QueryExecutor executor, | ||
String statement, | ||
List<Object?> args, | ||
) => | ||
_run( | ||
'Run `$statement`. Args $args', | ||
() => executor.runDelete(statement, args), | ||
); | ||
|
||
@override | ||
Future<void> runCustom( | ||
QueryExecutor executor, | ||
String statement, | ||
List<Object?> args, | ||
) => | ||
_run( | ||
'Run `$statement`. Args $args', | ||
() => executor.runCustom(statement, args), | ||
); | ||
|
||
@override | ||
Future<List<Map<String, Object?>>> runSelect( | ||
QueryExecutor executor, | ||
String statement, | ||
List<Object?> args, | ||
) => | ||
_run( | ||
'Run `$statement`. Args $args', | ||
() => executor.runSelect(statement, args), | ||
); | ||
|
||
Future<T> _run<T>( | ||
String description, | ||
FutureOr<T> Function() operation, | ||
) async { | ||
final stopwatch = Stopwatch()..start(); | ||
try { | ||
final result = await operation(); | ||
logger.debug( | ||
'[Drift] $description. Took: ${stopwatch.elapsedMilliseconds}'); | ||
return result; | ||
} catch (e) { | ||
logger.error( | ||
'[Drift] $description. Took: ${stopwatch.elapsedMilliseconds}. Cause: ($e)'); | ||
rethrow; | ||
} | ||
} | ||
} |