diff --git a/floor_generator/lib/writer/delete_method_body_writer.dart b/floor_generator/lib/writer/delete_method_body_writer.dart index 0a2c5948..b29bed00 100644 --- a/floor_generator/lib/writer/delete_method_body_writer.dart +++ b/floor_generator/lib/writer/delete_method_body_writer.dart @@ -1,4 +1,5 @@ import 'package:code_builder/code_builder.dart'; +import 'package:floor_generator/model/column.dart'; import 'package:floor_generator/model/delete_method.dart'; import 'package:floor_generator/writer/writer.dart'; import 'package:source_gen/source_gen.dart'; @@ -15,23 +16,83 @@ class DeleteMethodBodyWriter implements Writer { } String _generateMethodBody() { + _assertMethodReturnsNoList(); + final entity = method.getEntity(library); + final entityName = entity.name; final primaryKeyColumn = entity.primaryKeyColumn; - final methodHeadParameterName = method.parameter.name; + final methodSignatureParameterName = method.parameter.name; + + if (method.returnsInt) { + return _generateIntReturnMethodBody( + methodSignatureParameterName, + entityName, + primaryKeyColumn, + ); + } else if (method.returnsVoid) { + return _generateVoidReturnMethodBody( + methodSignatureParameterName, + entityName, + primaryKeyColumn, + ); + } else { + throw InvalidGenerationSourceError( + 'Delete methods have to return a Future of either void or int.', + element: method.method, + ); + } + } + String _generateVoidReturnMethodBody( + final String methodSignatureParameterName, + final String entityName, + final Column primaryKeyColumn, + ) { if (method.changesMultipleItems) { return ''' final batch = database.batch(); - for (final item in $methodHeadParameterName) { - batch.delete('${entity.name}', where: '${primaryKeyColumn.name} = ?', whereArgs: [item.${primaryKeyColumn.field.displayName}]); + for (final item in $methodSignatureParameterName) { + batch.delete('$entityName', where: '${primaryKeyColumn.name} = ?', whereArgs: [item.${primaryKeyColumn.field.displayName}]); } await batch.commit(noResult: true); '''; } else { return ''' - final item = $methodHeadParameterName; - await database.delete('${entity.name}', where: '${primaryKeyColumn.name} = ?', whereArgs: [item.${primaryKeyColumn.field.displayName}]); + final item = $methodSignatureParameterName; + await database.delete('$entityName', where: '${primaryKeyColumn.name} = ?', whereArgs: [item.${primaryKeyColumn.field.displayName}]); '''; } } + + String _generateIntReturnMethodBody( + final String methodSignatureParameterName, + final String entityName, + final Column primaryKeyColumn, + ) { + if (method.changesMultipleItems) { + return ''' + final batch = database.batch(); + for (final item in $methodSignatureParameterName) { + batch.delete('$entityName', where: '${primaryKeyColumn.name} = ?', whereArgs: [item.${primaryKeyColumn.field.displayName}]); + } + return (await batch.commit(noResult: false)) + .cast() + .reduce((first, second) => first + second); + '''; + } else { + return ''' + final item = $methodSignatureParameterName; + return database.delete('$entityName', where: '${primaryKeyColumn.name} = ?', whereArgs: [item.${primaryKeyColumn.field.displayName}]); + '''; + } + } + + void _assertMethodReturnsNoList() { + if (method.returnsList) { + throw InvalidGenerationSourceError( + 'Delete methods have to return a Future of either void or int but not a list.', + element: method.method, + ); + } + } } diff --git a/floor_test/test/database.dart b/floor_test/test/database.dart index 4226ae04..7f9baf89 100644 --- a/floor_test/test/database.dart +++ b/floor_test/test/database.dart @@ -33,13 +33,13 @@ abstract class TestDatabase extends FloorDatabase { Future updatePerson(Person person); @update - Future updatePersonWithReturn(Person person); + Future updatePersons(List persons); @update - Future updatePersonsWithReturn(List persons); + Future updatePersonWithReturn(Person person); @update - Future updatePersons(List persons); + Future updatePersonsWithReturn(List persons); @delete Future deletePerson(Person person); @@ -47,6 +47,12 @@ abstract class TestDatabase extends FloorDatabase { @delete Future deletePersons(List person); + @delete + Future deletePersonWithReturn(Person person); + + @delete + Future deletePersonsWithReturn(List persons); + @transaction Future replacePersons(List persons) async { await database.execute('DELETE FROM person'); diff --git a/floor_test/test/database_test.dart b/floor_test/test/database_test.dart index 369011b6..71f8e5aa 100644 --- a/floor_test/test/database_test.dart +++ b/floor_test/test/database_test.dart @@ -136,6 +136,24 @@ void main() { expect(persistentPersons, equals(updatedPersons)); expect(actual, equals(2)); }); + + test('delete person and return 1 (affected row count)', () async { + final person = Person(1, 'Simon'); + await database.insertPerson(person); + + final actual = await database.deletePersonWithReturn(person); + + expect(actual, equals(1)); + }); + + test('delete persons and return affected rows count', () async { + final persons = [Person(1, 'Simon'), Person(2, 'Frank')]; + await database.insertPersons(persons); + + final actual = await database.deletePersonsWithReturn(persons); + + expect(actual, equals(2)); + }); }); }