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

Add entity classes to database annotation #86

Merged
merged 3 commits into from
Mar 9, 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_annotation/lib/src/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import 'package:meta/meta.dart';

/// Marks a class as a FloorDatabase.
class Database {
/// The database version
/// The database version.
final int version;

/// The entities the database manages-
final List<Type> entities;

/// Marks a class as a FloorDatabase.
const Database({@required this.version});
const Database({@required this.version, @required this.entities});
}
35 changes: 26 additions & 9 deletions floor_generator/lib/generator.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:async';

import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:code_builder/code_builder.dart';
import 'package:floor_generator/misc/annotations.dart';
import 'package:floor_generator/misc/constants.dart';
import 'package:floor_generator/misc/type_utils.dart';
import 'package:floor_generator/processor/database_processor.dart';
import 'package:floor_generator/processor/entity_processor.dart';
Expand Down Expand Up @@ -52,20 +54,35 @@ class FloorGenerator implements Generator {
' There are too many classes annotated with @Database.',
element: databaseClasses[2]);
} else {
final databaseClassElement = databaseClasses.first;
final entities = _getEntities(databaseClassElement);

if (entities == null || entities.isEmpty) {
throw InvalidGenerationSourceError(
'There are no entities added to the database annotation.',
element: databaseClassElement);
}

return DatabaseProcessor(
databaseClasses.first,
_getEntities(library),
databaseClassElement,
entities,
).process();
}
}

@nonNull
List<Entity> _getEntities(final LibraryReader library) {
return library.classes
.where((clazz) =>
!clazz.isAbstract && clazz.metadata.any(isEntityAnnotation))
.map((entity) => EntityProcessor(entity).process())
.toList();
List<Entity> _getEntities(final ClassElement databaseClassElement) {
return databaseClassElement.metadata
.firstWhere(isDatabaseAnnotation)
.computeConstantValue()
.getField(AnnotationField.DATABASE_ENTITIES)
?.toListValue()
?.map((object) => object.toTypeValue().element)
?.whereType<ClassElement>()
?.where((classElement) =>
!classElement.isAbstract &&
classElement.metadata.any(isEntityAnnotation))
?.map((classElement) => EntityProcessor(classElement).process())
?.toList();
}

@nonNull
Expand Down
2 changes: 2 additions & 0 deletions floor_generator/lib/misc/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ abstract class AnnotationField {
static const QUERY_VALUE = 'value';
static const PRIMARY_KEY_AUTO_GENERATE = 'autoGenerate';
static const ON_CONFLICT = 'onConflict';

static const DATABASE_VERSION = 'version';
static const DATABASE_ENTITIES = 'entities';

static const COLUMN_INFO_NAME = 'name';
static const COLUMN_INFO_NULLABLE = 'nullable';
Expand Down
13 changes: 0 additions & 13 deletions floor_generator/lib/processor/database_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,6 @@ class DatabaseProcessor extends Processor<Database> {
}

List<DaoGetter> _getDaoGetters(final String databaseName) {
// TODO decide for either implementing this approach (include adding
// entities to the database annotation) or keep it like it is now
// another option would be to get the libraryElement form the classElement
// e.g. _classElement.library;

// final entities = _classElement.metadata
// .firstWhere(isDatabaseAnnotation)
// .computeConstantValue()
// .getField(AnnotationField.DATABASE_ENTITIES)
// ?.toListValue()
// ?.map((object) => object.toTypeValue().element)
// ?.whereType<ClassElement>();

return _classElement.fields.where(_isDao).map((field) {
final classElement = field.type.element as ClassElement;
final name = field.displayName;
Expand Down
4 changes: 3 additions & 1 deletion floor_test/test/dao/dog_dao.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
part of '../database.dart';
import 'package:floor/floor.dart';

import '../model/dog.dart';

@dao
abstract class DogDao {
Expand Down
4 changes: 3 additions & 1 deletion floor_test/test/dao/person_dao.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
part of '../database.dart';
import 'package:floor/floor.dart';

import '../model/person.dart';

@dao
abstract class PersonDao {
Expand Down
12 changes: 6 additions & 6 deletions floor_test/test/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import 'package:floor/floor.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart' as sqflite;

part 'dao/dog_dao.dart';
part 'dao/person_dao.dart';
import 'dao/dog_dao.dart';
import 'dao/person_dao.dart';
import 'model/dog.dart';
import 'model/person.dart';

part 'database.g.dart';
part 'model/address.dart';
part 'model/dog.dart';
part 'model/person.dart';

@Database(version: 2)
@Database(version: 2, entities: [Person, Dog])
abstract class TestDatabase extends FloorDatabase {
static Future<TestDatabase> openDatabase(List<Migration> migrations) async =>
_$open(migrations);
Expand Down
4 changes: 4 additions & 0 deletions floor_test/test/database_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:matcher/matcher.dart';
import 'package:sqflite/sqflite.dart';

import 'dao/dog_dao.dart';
import 'dao/person_dao.dart';
import 'database.dart';
import 'model/dog.dart';
import 'model/person.dart';

// run test with 'flutter run test/database_test.dart'
void main() {
Expand Down
24 changes: 0 additions & 24 deletions floor_test/test/model/address.dart

This file was deleted.

4 changes: 3 additions & 1 deletion floor_test/test/model/dog.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
part of '../database.dart';
import 'package:floor/floor.dart';

import 'person.dart';

@Entity(
tableName: 'dog',
Expand Down
2 changes: 1 addition & 1 deletion floor_test/test/model/person.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../database.dart';
import 'package:floor/floor.dart';

@Entity(tableName: 'person')
class Person {
Expand Down