Skip to content

Commit

Permalink
add batch support
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Mar 7, 2023
1 parent 4c3c624 commit a949bae
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 25 deletions.
17 changes: 12 additions & 5 deletions flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ Future<void> setupSentry(AppRunner appRunner, String dsn) async {
options.attachScreenshot = true;
options.screenshotQuality = SentryScreenshotQuality.low;
options.attachViewHierarchy = true;
options.environment = 'dev';
// We can enable Sentry debug logging during development. This is likely
// going to log too much for your app, but can be useful when figuring out
// configuration issues, e.g. finding out why your events are not uploaded.
Expand Down Expand Up @@ -410,18 +409,22 @@ class MainScaffold extends StatelessWidget {
bindToScope: true,
);

var db = await openDatabase(inMemoryDatabasePath);
final db = await openDatabase(inMemoryDatabasePath);
// final batch = db.batch();
await db.execute('''
CREATE TABLE Product (
id INTEGER PRIMARY KEY,
title TEXT
)
''');
final dbTitles = <String>[];
for (int i = 1; i <= 20; i++) {
await db.insert('Product', <String, Object?>{'title': 'Product $i'});
final title = 'Product $i';
dbTitles.add(title);
await db.insert('Product', <String, Object?>{'title': title});
}

await db.query('Product');
// await db.query('Product');

await db.transaction((txn) async {
await txn
Expand All @@ -430,7 +433,11 @@ class MainScaffold extends StatelessWidget {
where: 'title = ?', whereArgs: ['Product Another one']);
});

await db.delete('Product', where: 'title = ?', whereArgs: ['Product 1']);
// await db.delete('Product', where: 'title = ?', whereArgs: ['Product 1']);

// final batch = db.batch();
// batch.delete('Product', where: 'title = ?', whereArgs: dbTitles);
// await batch.commit();

await db.close();

Expand Down
2 changes: 2 additions & 0 deletions sqflite/lib/sentry_sqflite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ library sentry_sqflite;
export 'src/sentry_database.dart';
export 'src/sentry_sqflite.dart';
export 'src/sentry_sqflite_database_factory.dart';
export 'src/sentry_batch.dart';
export 'src/sentry_sqflite_transaction.dart';
243 changes: 243 additions & 0 deletions sqflite/lib/src/sentry_batch.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
import 'package:meta/meta.dart';
import 'package:sentry/sentry.dart';
import 'package:sqflite/sqflite.dart';

// ignore: implementation_imports, depend_on_referenced_packages
import 'package:sqflite_common/src/sql_builder.dart';

/// A [Batch] wrapper that adds Sentry support.
///
/// ```dart
/// import 'package:sqflite/sqflite.dart';
/// import 'package:sentry_sqflite/sentry_sqflite.dart';
///
/// final database = await openDatabase('path/to/db');
/// final sentryDatabase = SentryDatabase(database);
/// final batch = sentryDatabase.batch();
/// ```
class SentryBatch implements Batch {
final Batch _batch;
final Hub _hub;

// we don't clear the buffer because SqfliteBatch don't either
final _buffer = StringBuffer();

/// ```dart
/// import 'package:sqflite/sqflite.dart';
/// import 'package:sentry_sqflite/sentry_sqflite.dart';
///
/// final database = await openDatabase('path/to/db');
/// final sentryDatabase = SentryDatabase(database);
/// final batch = sentryDatabase.batch();
/// ```
SentryBatch(
this._batch, {
@internal Hub? hub,
}) : _hub = hub ?? HubAdapter();

@override
Future<List<Object?>> apply({bool? noResult, bool? continueOnError}) {
Future<List<Object?>> future() async {
final currentSpan = _hub.getSpan();

final span = currentSpan?.startChild(
'db.sql.execute',
description: _buffer.toString(),
);

try {
final result = await _batch.apply(
noResult: noResult,
continueOnError: continueOnError,
);

span?.status = SpanStatus.ok();

return result;
} catch (exception) {
span?.throwable = exception;
span?.status = SpanStatus.internalError();

rethrow;
} finally {
await span?.finish();
}
}

return future();
}

@override
Future<List<Object?>> commit({
bool? exclusive,
bool? noResult,
bool? continueOnError,
}) {
Future<List<Object?>> future() async {
final currentSpan = _hub.getSpan();

final span = currentSpan?.startChild(
'db.sql.execute',
description: _buffer.toString(),
);

try {
final result = await _batch.commit(
exclusive: exclusive,
noResult: noResult,
continueOnError: continueOnError,
);

span?.status = SpanStatus.ok();

return result;
} catch (exception) {
span?.throwable = exception;
span?.status = SpanStatus.internalError();

rethrow;
} finally {
await span?.finish();
}
}

return future();
}

@override
void delete(String table, {String? where, List<Object?>? whereArgs}) {
final builder =
SqlBuilder.delete(table, where: where, whereArgs: whereArgs);
_buffer.writeln(builder.sql);

_batch.delete(table, where: where, whereArgs: whereArgs);
}

@override
void execute(String sql, [List<Object?>? arguments]) {
_buffer.writeln(sql);

_batch.execute(sql, arguments);
}

@override
void insert(
String table,
Map<String, Object?> values, {
String? nullColumnHack,
ConflictAlgorithm? conflictAlgorithm,
}) {
final builder = SqlBuilder.insert(
table,
values,
nullColumnHack: nullColumnHack,
conflictAlgorithm: conflictAlgorithm,
);
_buffer.writeln(builder.sql);

_batch.insert(
table,
values,
nullColumnHack: nullColumnHack,
conflictAlgorithm: conflictAlgorithm,
);
}

@override
int get length => _batch.length;

@override
void query(
String table, {
bool? distinct,
List<String>? columns,
String? where,
List<Object?>? whereArgs,
String? groupBy,
String? having,
String? orderBy,
int? limit,
int? offset,
}) {
final builder = SqlBuilder.query(
table,
distinct: distinct,
columns: columns,
where: where,
groupBy: groupBy,
having: having,
orderBy: orderBy,
limit: limit,
offset: offset,
whereArgs: whereArgs,
);
_buffer.writeln(builder.sql);

_batch.query(
table,
distinct: distinct,
columns: columns,
where: where,
whereArgs: whereArgs,
groupBy: groupBy,
having: having,
orderBy: orderBy,
limit: limit,
offset: offset,
);
}

@override
void rawDelete(String sql, [List<Object?>? arguments]) {
_buffer.writeln(sql);

_batch.rawDelete(sql, arguments);
}

@override
void rawInsert(String sql, [List<Object?>? arguments]) {
_buffer.writeln(sql);

_batch.rawInsert(sql, arguments);
}

@override
void rawQuery(String sql, [List<Object?>? arguments]) {
_buffer.writeln(sql);

_batch.rawQuery(sql, arguments);
}

@override
void rawUpdate(String sql, [List<Object?>? arguments]) {
_buffer.writeln(sql);

_batch.rawUpdate(sql, arguments);
}

@override
void update(
String table,
Map<String, Object?> values, {
String? where,
List<Object?>? whereArgs,
ConflictAlgorithm? conflictAlgorithm,
}) {
final builder = SqlBuilder.update(
table,
values,
where: where,
whereArgs: whereArgs,
conflictAlgorithm: conflictAlgorithm,
);
_buffer.writeln(builder.sql);

_batch.update(
table,
where: where,
values,
whereArgs: whereArgs,
conflictAlgorithm: conflictAlgorithm,
);
}
}
21 changes: 15 additions & 6 deletions sqflite/lib/src/sentry_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@ import 'sentry_database_executor.dart';
import 'sentry_sqflite_transaction.dart';

/// A [Database] wrapper that adds Sentry support.
///
///
/// ```dart
/// import 'package:sqflite/sqflite.dart';
/// import 'package:sentry_sqflite/sentry_sqflite.dart';
///
///
/// final database = await openDatabase('path/to/db');
/// final sentryDatabase = SentryDatabase(database);
/// ```
class SentryDatabase extends SentryDatabaseExecutor implements Database {
final Database _database;
final Hub _hub;

// ignore: public_member_api_docs
/// ```dart
/// import 'package:sqflite/sqflite.dart';
/// import 'package:sentry_sqflite/sentry_sqflite.dart';
///
/// final database = await openDatabase('path/to/db');
/// final sentryDatabase = SentryDatabase(database);
/// ```
SentryDatabase(
this._database, {
@internal Hub? hub,
Expand Down Expand Up @@ -58,8 +64,11 @@ class SentryDatabase extends SentryDatabaseExecutor implements Database {
}

@override
Future<T> devInvokeSqlMethod<T>(String method, String sql,
[List<Object?>? arguments]) {
Future<T> devInvokeSqlMethod<T>(
String method,
String sql, [
List<Object?>? arguments,
]) {
// ignore: deprecated_member_use
return _database.devInvokeSqlMethod(method, sql);
}
Expand All @@ -86,7 +95,7 @@ class SentryDatabase extends SentryDatabaseExecutor implements Database {
final sentryExecutor =
SentryDatabaseExecutor(txn, parentSpan: span, hub: _hub);
final sentrySqfliteTransaction =
SentrySqfliteTransaction(sentryExecutor);
SentrySqfliteTransaction(sentryExecutor, hub: _hub);

return await action(sentrySqfliteTransaction);
}
Expand Down
7 changes: 3 additions & 4 deletions sqflite/lib/src/sentry_database_executor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:sqflite/sqflite.dart';
// ignore: implementation_imports, depend_on_referenced_packages
import 'package:sqflite_common/src/sql_builder.dart';

import 'sentry_batch.dart';

@internal
// ignore: public_member_api_docs
class SentryDatabaseExecutor implements DatabaseExecutor {
Expand All @@ -21,10 +23,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
final Hub _hub;

@override
Batch batch() {
// TODO: implement batch, needs a wrapper
return _executor.batch();
}
Batch batch() => SentryBatch(_executor.batch(), hub: _hub);

@override
Database get database => _executor.database;
Expand Down
8 changes: 4 additions & 4 deletions sqflite/lib/src/sentry_sqflite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import 'package:sqflite/sqflite.dart';
import 'sentry_database.dart';

/// Opens a database with Sentry support.
///
///
/// ```dart
/// import 'package:sqflite/sqflite.dart';
/// import 'package:sentry_sqflite/sentry_sqflite.dart';
///
///
/// final database = await openDatabaseWithSentry('path/to/db');
/// ```
Future<Database> openDatabaseWithSentry(
Expand Down Expand Up @@ -41,11 +41,11 @@ Future<Database> openDatabaseWithSentry(
}

/// Opens a database with Sentry support.
///
///
/// ```dart
/// import 'package:sqflite/sqflite.dart';
/// import 'package:sentry_sqflite/sentry_sqflite.dart';
///
///
/// final database = await openReadOnlyDatabaseWithSentry('path/to/db');
/// ```
Future<Database> openReadOnlyDatabaseWithSentry(String path) {
Expand Down
Loading

0 comments on commit a949bae

Please sign in to comment.