Generate null variables

This commit is contained in:
Simon Binder 2022-09-19 22:43:16 +02:00
parent cd89379627
commit 944725dafb
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
9 changed files with 63 additions and 23 deletions

View File

@ -84,11 +84,9 @@ class Variable<T extends Object> extends Expression<T> {
..write(mark)
..write(explicitStart + context.amountOfVariables);
context.introduceVariable(this, mapToSimpleValue(context));
} else if (value != null) {
} else {
context.buffer.write(mark);
context.introduceVariable(this, mapToSimpleValue(context));
} else {
context.buffer.write('NULL');
}
}

View File

@ -16,6 +16,10 @@ void main() {
expect(ctx.boundVariables, [1551297563]);
});
test('maps null to null', () {
expect(Variable<Object>(null), generates('?', [null]));
});
group('can write variables with wrong type parameter', () {
test('true', () {
expect(const Variable<Object>(true), generates('?', [1]));
@ -52,16 +56,6 @@ void main() {
});
});
test('writes null directly for null values', () {
const variable = Variable<String>(null);
final ctx = GenerationContext.fromDb(TodoDb());
variable.writeInto(ctx);
expect(ctx.sql, 'NULL');
expect(ctx.boundVariables, isEmpty);
});
test('writes constants when variables are not supported', () {
const variable = Variable("hello world'");
final ctx = GenerationContext.fromDb(TodoDb(), supportsVariables: false);

View File

@ -1,4 +1,4 @@
import 'package:drift/drift.dart';
import 'package:drift/drift.dart' hide isNull;
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';
@ -37,8 +37,8 @@ void main() {
verify(executor.runInsert(
'INSERT INTO table_without_p_k '
'(not_really_an_id, some_float, web_safe_int, custom) '
'VALUES (?, ?, NULL, ?)',
[42, 3.1415, anything]));
'VALUES (?, ?, ?, ?)',
[42, 3.1415, isNull, anything]));
});
test('can insert BigInt values', () async {
@ -156,7 +156,8 @@ void main() {
TodosTableCompanion.insert(content: 'content', title: Value(null)));
verify(executor.runInsert(
'INSERT INTO todos (title, content) VALUES (NULL, ?)', ['content']));
'INSERT INTO todos (title, content) VALUES (?, ?)',
[null, 'content']));
});
});

View File

@ -61,8 +61,8 @@ void main() {
verify(executor.runUpdate(
'UPDATE todos SET title = ?, content = ?, '
'target_date = NULL, category = NULL WHERE id = ?;',
['Title', 'Updated content', 3]));
'target_date = ?, category = ? WHERE id = ?;',
['Title', 'Updated content', null, null, 3]));
});
test('applies default values', () async {

View File

@ -1,3 +1,7 @@
## 2.0.1
- Fix batch implementation to not start a second transaction.
## 2.0.0
- Support `drift` version `2.0.0`.

View File

@ -74,7 +74,7 @@ class _SqfliteDelegate extends DatabaseDelegate {
batch.execute(statements.statements[arg.statementIndex], arg.arguments);
}
await batch.commit(noResult: true);
await batch.apply(noResult: true);
}
@override

View File

@ -1,6 +1,6 @@
name: drift_sqflite
description: A Flutter-only implementation of a drift database, based on the `sqflite` package.
version: 2.0.0
version: 2.0.1
repository: https://github.com/simolus3/drift
homepage: https://drift.simonbinder.eu/
issue_tracker: https://github.com/simolus3/drift/issues
@ -10,7 +10,7 @@ environment:
dependencies:
drift: ^2.0.0
sqflite: ^2.0.0+3
sqflite: ^2.1.0
path: ^1.8.0
flutter:
sdk: flutter

View File

@ -111,4 +111,47 @@ void crudTests(TestExecutor executor) {
expect(await db.friendsOf(1).get(), isNotEmpty);
await executor.clearDatabaseAndClose(db);
});
group('bind variable', () {
late Database database;
setUp(() => database = Database(executor.createConnection()));
tearDown(() => executor.clearDatabaseAndClose(database));
Future<T?> evaluate<T extends Object>(Expression<T> expr) async {
final query = database.selectOnly(database.users)
..addColumns([expr])
..limit(1);
final row = await query.getSingle();
return row.read(expr);
}
test('null', () {
expect(evaluate(Variable<String>(null)), completion(isNull));
});
test('string', () {
expect(evaluate(Variable<String>('foo bar')), completion('foo bar'));
expect(evaluate(Variable<String>('')), completion(''));
});
test('boolean', () {
expect(evaluate(Variable<bool>(true)), completion(isTrue));
expect(evaluate(Variable<bool>(false)), completion(isFalse));
});
test('int', () {
expect(evaluate(Variable<double>(42)), completion(42));
});
test('double', () {
expect(evaluate(Variable<double>(3.14)), completion(3.14));
});
test('Uint8List', () {
final list = Uint8List.fromList(List.generate(12, (index) => index));
expect(evaluate(Variable<Uint8List>(list)), completion(list));
});
});
}

View File

@ -1,6 +1,6 @@
library tests;
export 'package:drift/drift.dart';
export 'package:drift/drift.dart' hide isNull, isNotNull;
export 'data/sample_data.dart';
export 'database/database.dart';