Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,20 @@ final class _UnsafeSyncContext extends UnscopedContext {
}
}, sql: sql);
}

@override
Future<void> executeMultiple(String sql) async {
task.timeSync('executeBatch', () {
final statements = db.prepareMultiple(sql);
try {
for (var statement in statements) {
task.timeSync('iteration', () => statement.execute());
}
} finally {
for (var statement in statements) {
statement.dispose();
}
}
}, sql: sql);
}
}
7 changes: 7 additions & 0 deletions packages/sqlite_async/lib/src/impl/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import '../sqlite_connection.dart';
abstract base class UnscopedContext implements SqliteReadContext {
Future<ResultSet> execute(String sql, List<Object?> parameters);
Future<void> executeBatch(String sql, List<List<Object?>> parameterSets);
Future<void> executeMultiple(String sql);

/// Returns an [UnscopedContext] useful as the outermost transaction.
///
Expand Down Expand Up @@ -143,6 +144,12 @@ final class ScopedWriteContext extends ScopedReadContext
return await _context.executeBatch(sql, parameterSets);
}

@override
Future<void> executeMultiple(String sql) {
_checkNotLocked();
return _context.executeMultiple(sql);
}

@override
Future<T> writeTransaction<T>(
Future<T> Function(SqliteWriteContext tx) callback) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,22 @@ final class _UnsafeContext extends UnscopedContext {
}
});
}

@override
Future<void> executeMultiple(String sql) async {
return computeWithDatabase((db) async {
final statements = db.prepareMultiple(sql);
try {
for (var statement in statements) {
statement.execute();
}
} finally {
for (var statement in statements) {
statement.dispose();
}
}
});
}
}

void _sqliteConnectionIsolate(_SqliteConnectionParams params) async {
Expand Down
3 changes: 3 additions & 0 deletions packages/sqlite_async/lib/src/sqlite_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ abstract interface class SqliteWriteContext extends SqliteReadContext {
/// parameter set.
Future<void> executeBatch(String sql, List<List<Object?>> parameterSets);

// Execute multiple SQL statements from a single string, sequentially.
Future<void> executeMultiple(String sql);

/// Open a read-write transaction on this write context.
///
/// When called on a [SqliteConnection], this takes a global lock - only one
Expand Down
7 changes: 7 additions & 0 deletions packages/sqlite_async/lib/src/sqlite_queries.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ mixin SqliteQueries implements SqliteWriteContext, SqliteConnection {
});
}

@override
Future<void> executeMultiple(String sql) {
return writeTransaction((tx) async {
return tx.executeMultiple(sql);
});
}

@override
Future<void> refreshSchema() {
return getAll("PRAGMA table_info('sqlite_master')");
Expand Down
13 changes: 13 additions & 0 deletions packages/sqlite_async/lib/src/web/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,19 @@ final class _UnscopedContext extends UnscopedContext {
});
}

@override
Future<void> executeMultiple(String sql) {
return _task.timeAsync('executeMultiple', sql: sql, () {
return wrapSqliteException(() async {
await _database._database.executeMultiple(
sql,
token: _lock,
checkInTransaction: _checkInTransaction,
);
});
});
}

@override
UnscopedContext interceptOutermostTransaction() {
// All execute calls done in the callback will be checked for the
Expand Down
9 changes: 9 additions & 0 deletions packages/sqlite_async/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ repository: https://github.com/powersync-ja/sqlite_async.dart
environment:
sdk: ">=3.5.0 <4.0.0"

# TODO : Remove this override when the changes are merged and published.
dependency_overrides:
sqlite3_web:
git:
url: https://github.com/stevenctl/sqlite3.dart
ref: stevenctl/execute-multi-support
path: sqlite3_web


topics:
- sqlite
- async
Expand Down
17 changes: 17 additions & 0 deletions packages/sqlite_async/test/basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,23 @@ void main() {
)
});

test('executeMultiple inserts multiple rows', () async {
final db = await testUtils.setupDatabase(path: path);
await createTables(db);

await db.executeMultiple('''
INSERT INTO test_data(description) VALUES('row1');
INSERT INTO test_data(description) VALUES('row2');
''');

final results = await db.getAll('SELECT description FROM test_data ORDER BY id');
expect(results.length, equals(2));
expect(results.rows[0], equals(['row1']));
expect(results.rows[1], equals(['row2']));

await db.close();
}, skip: _isWeb ? 'executeMultiple is not supported on web' : null);

test('with all connections', () async {
final maxReaders = _isWeb ? 0 : 3;

Expand Down