Skip to content

Allow profiling queries #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## 2025-05-19

### Changes

---

Packages with breaking changes:

- There are no breaking changes in this release.

Packages with other changes:

- [`sqlite_async` - `v0.11.5`](#sqlite_async---v0115)
- [`drift_sqlite_async` - `v0.2.2+1`](#drift_sqlite_async---v0221)

Packages with dependency updates only:

> Packages listed below depend on other packages in this workspace that have had changes. Their versions have been incremented to bump the minimum dependency versions of the packages they depend upon in this project.

- `drift_sqlite_async` - `v0.2.2+1`

---

#### `sqlite_async` - `v0.11.5`

- **FEAT**: Allow profiling queries.


## 2024-11-06

### Changes
Expand Down
4 changes: 4 additions & 0 deletions packages/drift_sqlite_async/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.2+1

- Update a dependency to the latest release.

## 0.2.2

- Fix write detection when using UPDATE/INSERT/DELETE with RETURNING in raw queries.
Expand Down
4 changes: 2 additions & 2 deletions packages/drift_sqlite_async/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: drift_sqlite_async
version: 0.2.2
version: 0.2.2+1
homepage: https://github.com/powersync-ja/sqlite_async.dart
repository: https://github.com/powersync-ja/sqlite_async.dart
description: Use Drift with a sqlite_async database, allowing both to be used in the same application.
Expand All @@ -15,7 +15,7 @@ environment:
sdk: ">=3.0.0 <4.0.0"
dependencies:
drift: ">=2.19.0 <3.0.0"
sqlite_async: ^0.11.0
sqlite_async: ^0.11.5

dev_dependencies:
build_runner: ^2.4.8
Expand Down
4 changes: 4 additions & 0 deletions packages/sqlite_async/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.11.5

- **FEAT**: Allow profiling queries.

## 0.11.4

- Add `SqliteConnection.synchronousWrapper` and `SqliteDatabase.singleConnection`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'dart:developer';

import 'package:sqlite3/common.dart';
import 'package:sqlite_async/src/common/mutex.dart';
import 'package:sqlite_async/src/sqlite_connection.dart';
import 'package:sqlite_async/src/sqlite_queries.dart';
import 'package:sqlite_async/src/update_notification.dart';
import 'package:sqlite_async/src/utils/profiler.dart';

/// A simple "synchronous" connection which provides the async SqliteConnection
/// implementation using a synchronous SQLite connection
Expand All @@ -14,7 +17,12 @@ class SyncSqliteConnection extends SqliteConnection with SqliteQueries {

bool _closed = false;

SyncSqliteConnection(this.db, Mutex m) {
/// Whether queries should be added to the `dart:developer` timeline.
///
/// This is disabled by default.
final bool profileQueries;

SyncSqliteConnection(this.db, Mutex m, {this.profileQueries = false}) {
mutex = m.open();
updates = db.updates.map(
(event) {
Expand All @@ -26,15 +34,31 @@ class SyncSqliteConnection extends SqliteConnection with SqliteQueries {
@override
Future<T> readLock<T>(Future<T> Function(SqliteReadContext tx) callback,
{Duration? lockTimeout, String? debugContext}) {
return mutex.lock(() => callback(SyncReadContext(db)),
timeout: lockTimeout);
final task = profileQueries ? TimelineTask() : null;
task?.start('mutex_lock');

return mutex.lock(
() {
task?.finish();
return callback(SyncReadContext(db, parent: task));
},
timeout: lockTimeout,
);
}

@override
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout, String? debugContext}) {
return mutex.lock(() => callback(SyncWriteContext(db)),
timeout: lockTimeout);
final task = profileQueries ? TimelineTask() : null;
task?.start('mutex_lock');

return mutex.lock(
() {
task?.finish();
return callback(SyncWriteContext(db, parent: task));
},
timeout: lockTimeout,
);
}

@override
Expand All @@ -53,9 +77,12 @@ class SyncSqliteConnection extends SqliteConnection with SqliteQueries {
}

class SyncReadContext implements SqliteReadContext {
final TimelineTask? task;

CommonDatabase db;

SyncReadContext(this.db);
SyncReadContext(this.db, {TimelineTask? parent})
: task = TimelineTask(parent: parent);

@override
Future<T> computeWithDatabase<T>(
Expand All @@ -65,13 +92,21 @@ class SyncReadContext implements SqliteReadContext {

@override
Future<Row> get(String sql, [List<Object?> parameters = const []]) async {
return db.select(sql, parameters).first;
return task.timeSync(
'get',
() => db.select(sql, parameters).first,
arguments: timelineArgs(sql, parameters),
);
}

@override
Future<ResultSet> getAll(String sql,
[List<Object?> parameters = const []]) async {
return db.select(sql, parameters);
return task.timeSync(
'getAll',
() => db.select(sql, parameters),
arguments: timelineArgs(sql, parameters),
);
}

@override
Expand All @@ -91,26 +126,31 @@ class SyncReadContext implements SqliteReadContext {
}

class SyncWriteContext extends SyncReadContext implements SqliteWriteContext {
SyncWriteContext(super.db);
SyncWriteContext(super.db, {super.parent});

@override
Future<ResultSet> execute(String sql,
[List<Object?> parameters = const []]) async {
return db.select(sql, parameters);
return task.timeSync(
'execute',
() => db.select(sql, parameters),
arguments: timelineArgs(sql, parameters),
);
}

@override
Future<void> executeBatch(
String sql, List<List<Object?>> parameterSets) async {
return computeWithDatabase((db) async {
task.timeSync('executeBatch', () {
final statement = db.prepare(sql, checkNoTail: true);
try {
for (var parameters in parameterSets) {
statement.execute(parameters);
task.timeSync('iteration', () => statement.execute(parameters),
arguments: parameterArgs(parameters));
}
} finally {
statement.dispose();
}
});
}, arguments: {'sql': sql});
}
}
Loading
Loading