Skip to content
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

Allow void-return queries with arguments #132

Merged
merged 3 commits into from
May 4, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions floor/lib/src/adapter/query_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ class QueryAdapter {
return rows.map((row) => mapper(row)).toList();
}

Future<void> queryNoReturn(final String sql) async {
Future<void> queryNoReturn(
final String sql, {
final List<dynamic> arguments,
}) async {
// TODO differentiate between different query kinds (select, update, delete, insert)
// this enables to notify the observers
// also requires extracting the table name :(
await _database.rawQuery(sql);
await _database.rawQuery(sql, arguments);
}

/// Executes a SQLite query that returns a stream of single query results.
Expand Down
8 changes: 8 additions & 0 deletions floor/test/adapter/query_adapter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ void main() {

verify(mockDatabaseExecutor.rawQuery(sql));
});

test('executes query with argument', () async {
final arguments = [123];

await underTest.queryNoReturn(sql, arguments: arguments);

verify(mockDatabaseExecutor.rawQuery(sql, arguments));
});
});
});

Expand Down
19 changes: 14 additions & 5 deletions floor_generator/lib/writer/query_method_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ class QueryMethodWriter implements Writer {
}

String _generateMethodBody() {
if (_queryMethod.returnsVoid) {
return "await _queryAdapter.queryNoReturn('${_queryMethod.query}');";
}

final parameters =
_queryMethod.parameters.map((parameter) => parameter.displayName);
final arguments =
parameters.isNotEmpty ? '<dynamic>[${parameters.join(', ')}]' : null;

if (_queryMethod.returnsVoid) {
return _generateNoReturnQuery(arguments);
}

final mapper = '_${decapitalize(_queryMethod.entity.name)}Mapper';

if (_queryMethod.returnsStream) {
Expand All @@ -68,9 +68,18 @@ class QueryMethodWriter implements Writer {
}
}

@nonNull
String _generateNoReturnQuery(String arguments) {
final parameters = StringBuffer()..write("'${_queryMethod.query}'");
if (arguments != null) parameters.write(', arguments: $arguments');
return 'await _queryAdapter.queryNoReturn($parameters);';
}

@nonNull
String _generateQuery(
@nullable final String arguments, @nonNull final String mapper) {
@nullable final String arguments,
@nonNull final String mapper,
) {
final parameters = StringBuffer()..write("'${_queryMethod.query}', ");
if (arguments != null) parameters.write('arguments: $arguments, ');
parameters.write('mapper: $mapper');
Expand Down
16 changes: 16 additions & 0 deletions floor_generator/test/writer/query_method_writer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ void main() {
'''));
});

test('query no return with parameter', () async {
final queryMethod = await _createQueryMethod('''
@Query('DELETE FROM Person WHERE id = :id')
Future<void> deletePersonById(int id);
''');

final actual = QueryMethodWriter(queryMethod).write();

expect(actual, equalsDart(r'''
@override
Future<void> deletePersonById(int id) async {
await _queryAdapter.queryNoReturn('DELETE FROM Person WHERE id = ?', arguments: <dynamic>[id]);
}
'''));
});

test('query item', () async {
final queryMethod = await _createQueryMethod('''
@Query('SELECT * FROM Person WHERE id = :id')
Expand Down