mirror of https://github.com/AMT-Cheif/drift.git
Allow running multiple statements on the web
This commit is contained in:
parent
111a2e395a
commit
94bdab0338
|
@ -93,8 +93,15 @@ class SqlJsDatabase {
|
|||
|
||||
/// Calls `run(sql, args)` on the underlying js api
|
||||
void runWithArgs(String sql, List<dynamic> args) {
|
||||
final ar = JsArray.from(args);
|
||||
_obj.callMethod('run', [sql, ar]);
|
||||
if (args.isEmpty) {
|
||||
// Call run without providing arguments. sql.js will then use sqlite3_exec
|
||||
// internally, which supports running multiple statements at once. This
|
||||
// matches the behavior from a `NativeDatabase`.
|
||||
_obj.callMethod('run', [sql]);
|
||||
} else {
|
||||
final ar = JsArray.from(args);
|
||||
_obj.callMethod('run', [sql, ar]);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the amount of rows affected by the most recent INSERT, UPDATE or
|
||||
|
|
|
@ -11,14 +11,12 @@ dev_dependencies:
|
|||
test: ^1.5.0
|
||||
build_runner:
|
||||
build_web_compilers:
|
||||
moor_generator:
|
||||
drift_dev: ^1.0.0
|
||||
|
||||
dependency_overrides:
|
||||
drift:
|
||||
path: ../../../drift
|
||||
moor:
|
||||
path: ../../../moor
|
||||
moor_generator:
|
||||
path: ../../../moor_generator
|
||||
drift_dev:
|
||||
path: ../../../drift_dev
|
||||
sqlparser:
|
||||
path: ../../../sqlparser
|
|
@ -1,7 +1,7 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:moor/moor.dart';
|
||||
import 'package:moor/moor_web.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift/web.dart';
|
||||
|
||||
import 'package:test/test.dart';
|
||||
|
||||
|
@ -184,23 +184,23 @@ AAAAAAAAAAAAAAAAAAAAAAANAQIjaGVsbG8gd29ybGQ=
|
|||
|
||||
void main() {
|
||||
test('can initialize database when absent', () async {
|
||||
await _testWith(const MoorWebStorage('name'));
|
||||
await _testWith(const DriftWebStorage('name'));
|
||||
});
|
||||
|
||||
test('can initialize database when absent - IndexedDB', () async {
|
||||
await _testWith(
|
||||
MoorWebStorage.indexedDb('name', migrateFromLocalStorage: false));
|
||||
DriftWebStorage.indexedDb('name', migrateFromLocalStorage: false));
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _testWith(MoorWebStorage storage) async {
|
||||
Future<void> _testWith(DriftWebStorage storage) async {
|
||||
var didCallInitializer = false;
|
||||
final executor = WebDatabase.withStorage(storage, initializer: () async {
|
||||
didCallInitializer = true;
|
||||
return base64.decode(_rawDataBase64.replaceAll('\n', ''));
|
||||
});
|
||||
|
||||
moorRuntimeOptions.dontWarnAboutMultipleDatabases = true;
|
||||
driftRuntimeOptions.dontWarnAboutMultipleDatabases = true;
|
||||
final attachedDb = _FakeDatabase(executor);
|
||||
|
||||
await executor.ensureOpen(attachedDb);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
@TestOn('browser')
|
||||
import 'dart:html';
|
||||
|
||||
import 'package:moor/moor_web.dart';
|
||||
import 'package:drift/web.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:tests/tests.dart';
|
||||
|
||||
|
@ -30,7 +30,7 @@ class WebExecutorIndexedDb extends TestExecutor {
|
|||
@override
|
||||
DatabaseConnection createConnection() {
|
||||
return DatabaseConnection.fromExecutor(
|
||||
WebDatabase.withStorage(MoorWebStorage.indexedDb('foo')),
|
||||
WebDatabase.withStorage(DriftWebStorage.indexedDb('foo')),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -48,4 +48,15 @@ void main() {
|
|||
group('using IndexedDb', () {
|
||||
runAllTests(WebExecutorIndexedDb());
|
||||
});
|
||||
|
||||
test('can run multiple statements in one call', () async {
|
||||
final db = Database(DatabaseConnection.fromExecutor(
|
||||
WebDatabase.withStorage(DriftWebStorage.volatile())));
|
||||
addTearDown(db.close);
|
||||
|
||||
await db.customStatement(
|
||||
'CREATE TABLE x1 (a INTEGER); INSERT INTO x1 VALUES (1);');
|
||||
final results = await db.customSelect('SELECT * FROM x1;').get();
|
||||
expect(results.length, 1);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
@TestOn('browser')
|
||||
import 'dart:html';
|
||||
|
||||
import 'package:moor/moor.dart';
|
||||
import 'package:moor/moor_web.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift/web.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
part 'saves_after_migration_regression_test.g.dart';
|
||||
|
@ -17,7 +17,7 @@ class Bars extends Table {
|
|||
IntColumn get id => integer().autoIncrement()();
|
||||
}
|
||||
|
||||
@UseMoor(
|
||||
@DriftDatabase(
|
||||
tables: [Foos, Bars],
|
||||
)
|
||||
class _FakeDb extends _$_FakeDb {
|
||||
|
|
|
@ -34,14 +34,14 @@ class Foo extends DataClass implements Insertable<Foo> {
|
|||
|
||||
factory Foo.fromJson(Map<String, dynamic> json,
|
||||
{ValueSerializer serializer}) {
|
||||
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||
serializer ??= driftRuntimeOptions.defaultSerializer;
|
||||
return Foo(
|
||||
id: serializer.fromJson<int>(json['id']),
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||
serializer ??= driftRuntimeOptions.defaultSerializer;
|
||||
return <String, dynamic>{
|
||||
'id': serializer.toJson<int>(id),
|
||||
};
|
||||
|
@ -175,14 +175,14 @@ class Bar extends DataClass implements Insertable<Bar> {
|
|||
|
||||
factory Bar.fromJson(Map<String, dynamic> json,
|
||||
{ValueSerializer serializer}) {
|
||||
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||
serializer ??= driftRuntimeOptions.defaultSerializer;
|
||||
return Bar(
|
||||
id: serializer.fromJson<int>(json['id']),
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||
serializer ??= driftRuntimeOptions.defaultSerializer;
|
||||
return <String, dynamic>{
|
||||
'id': serializer.toJson<int>(id),
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue