-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add delete methods code generation and fix update methods (#22)
- Loading branch information
1 parent
0649b90
commit 4d74404
Showing
9 changed files
with
174 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:analyzer/dart/element/type.dart'; | ||
import 'package:floor_generator/misc/type_utils.dart'; | ||
import 'package:floor_generator/model/entity.dart'; | ||
import 'package:source_gen/source_gen.dart'; | ||
|
||
class DeleteMethod { | ||
final MethodElement method; | ||
|
||
DeleteMethod(this.method); | ||
|
||
DartType get returnType => method.returnType; | ||
|
||
String get name => method.displayName; | ||
|
||
ParameterElement get parameter { | ||
final parameters = method.parameters; | ||
if (parameters.isEmpty) { | ||
throw InvalidGenerationSourceError( | ||
'There is no parameter supplied for an update method. Please add one.', | ||
element: method, | ||
); | ||
} else if (parameters.length > 1) { | ||
throw InvalidGenerationSourceError( | ||
'Only one parameter is allowed on update methods.', | ||
element: method, | ||
); | ||
} | ||
return parameters.first; | ||
} | ||
|
||
Entity getEntity(LibraryReader library) { | ||
final entityClass = _getEntities(library).firstWhere( | ||
(entity) => entity.displayName == parameter.type.displayName); | ||
|
||
return Entity(entityClass); | ||
} | ||
|
||
bool insertsEntity(LibraryReader library) { | ||
return _getEntities(library) | ||
.map((clazz) => clazz.displayName) | ||
.any((entity) => entity == parameter.type.displayName); | ||
} | ||
|
||
List<ClassElement> _getEntities(LibraryReader library) { | ||
return library.classes | ||
.where((clazz) => | ||
!clazz.isAbstract && clazz.metadata.any(isEntityAnnotation)) | ||
.toList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import 'package:code_builder/code_builder.dart'; | ||
import 'package:floor_generator/misc/annotation_expression.dart'; | ||
import 'package:floor_generator/model/delete_method.dart'; | ||
import 'package:floor_generator/writer/writer.dart'; | ||
import 'package:source_gen/source_gen.dart'; | ||
|
||
class DeleteMethodWriter implements Writer { | ||
final LibraryReader library; | ||
final DeleteMethod deleteMethod; | ||
|
||
DeleteMethodWriter(this.library, this.deleteMethod); | ||
|
||
@override | ||
Method write() { | ||
return _generateDeleteMethod(); | ||
} | ||
|
||
Method _generateDeleteMethod() { | ||
// TODO assert deletes entity | ||
|
||
return Method((builder) => builder | ||
..annotations.add(overrideAnnotationExpression) | ||
..returns = refer(deleteMethod.returnType.displayName) | ||
..name = deleteMethod.name | ||
..requiredParameters.add(_generateParameter()) | ||
..modifier = MethodModifier.async | ||
..body = Code(_generateMethodBody())); | ||
} | ||
|
||
Parameter _generateParameter() { | ||
final parameter = deleteMethod.parameter; | ||
|
||
return Parameter((builder) => builder | ||
..name = parameter.name | ||
..type = refer(parameter.type.displayName)); | ||
} | ||
|
||
String _generateMethodBody() { | ||
final entity = deleteMethod.getEntity(library); | ||
final primaryKeyColumn = | ||
entity.columns.firstWhere((column) => column.isPrimaryKey); | ||
final methodHeadParameterName = deleteMethod.parameter.name; | ||
|
||
return ''' | ||
await this.database.rawDelete('DELETE FROM ${entity.name} WHERE ${primaryKeyColumn.name} = \${$methodHeadParameterName.${primaryKeyColumn.field.displayName}}'); | ||
'''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters