Skip to content

Commit

Permalink
Clean up the codebase. Update the example project by the RawQuery sam…
Browse files Browse the repository at this point in the history
…ple.
  • Loading branch information
dkaera committed Aug 6, 2022
1 parent 225a298 commit b7a1f6c
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 147 deletions.
39 changes: 1 addition & 38 deletions example/lib/database.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class TasksListView extends StatelessWidget {
Widget build(BuildContext context) {
return Expanded(
child: StreamBuilder<List<Task>>(
stream: dao.findAllTasksAsStream(),
stream: dao.findYesterdaysTasksByMessageAsStream('test'),
builder: (_, snapshot) {
if (!snapshot.hasData) return Container();

Expand Down
28 changes: 15 additions & 13 deletions example/lib/task_dao.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ abstract class TaskDao {
Stream<List<Task>> findAllTasksAsStream();

@rawQuery
Stream<List<Task>> rawFindAllTasksAsStream(SQLiteQuery query);

@rawQuery
Future<List<Task>> rawFindAllTasks(SQLiteQuery query);

@rawQuery
Stream<Task?> rawFindTaskAsStream(SQLiteQuery query);

@rawQuery
Future<Task?> rawFindTask(SQLiteQuery query);

@rawQuery
Future<void> rawInsertionTask(SQLiteQuery query);
Stream<List<Task>> rawQueryTasksAsStream(SQLiteQuery query);

Stream<List<Task>> findYesterdaysTasksByMessageAsStream(String message) {
final timestamp = DateTime.now()
.subtract(
const Duration(days: 1),
)
.millisecondsSinceEpoch;
return rawQueryTasksAsStream(SQLiteQuery(
'SELECT * FROM task WHERE timestamp > ?1 AND message == ?2',
arguments: [
timestamp,
message,
]));
}

@insert
Future<void> insertTask(Task task);
Expand Down
15 changes: 8 additions & 7 deletions floor_generator/lib/processor/base_query_method_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:collection/collection.dart';
import 'package:floor_generator/misc/extension/dart_type_extension.dart';
import 'package:floor_generator/misc/type_utils.dart';
import 'package:floor_generator/processor/error/base_query_method_processor_error.dart';
import 'package:floor_generator/processor/processor.dart';
import 'package:floor_generator/value_object/query_method.dart';
import 'package:floor_generator/value_object/queryable.dart';

abstract class BaseQueryMethodProcessor extends Processor<QueryMethod> {
final MethodElement _methodElement;
final List<Queryable> _queryables;
final BaseQueryMethodProcessorError _processorError;

BaseQueryMethodProcessor(
final MethodElement methodElement,
final List<Queryable> queryables,
) : _methodElement = methodElement,
_queryables = queryables;
_queryables = queryables,
_processorError = BaseQueryMethodProcessorError(methodElement);

@override
QueryMethod process() {
Expand Down Expand Up @@ -76,12 +79,10 @@ abstract class BaseQueryMethodProcessor extends Processor<QueryMethod> {
final bool returnsStream,
) {
if (!rawReturnType.isDartAsyncFuture && !returnsStream) {
onDoesNotReturnFutureNorStream();
throw _processorError.doesNotReturnFutureNorStream;
}
}

void onDoesNotReturnFutureNorStream();

void _assertReturnsNullableSingle(
final bool returnsStream,
final bool returnsList,
Expand All @@ -90,7 +91,9 @@ abstract class BaseQueryMethodProcessor extends Processor<QueryMethod> {
if (!returnsList &&
!flattenedReturnType.isVoid &&
!flattenedReturnType.isNullable) {
onAssertReturnsNullableSingle(returnsStream);
returnsStream
? throw _processorError.doesNotReturnNullableStream
: throw _processorError.doesNotReturnNullableFuture;
}
}

Expand All @@ -102,6 +105,4 @@ abstract class BaseQueryMethodProcessor extends Processor<QueryMethod> {
List<ParameterElement> parameters,
Queryable? queryable,
);

void onAssertReturnsNullableSingle(bool returnsStream);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:floor_generator/processor/error/processor_error.dart';
import 'package:source_gen/source_gen.dart';

class BaseQueryMethodProcessorError {
final MethodElement _methodElement;

BaseQueryMethodProcessorError(final MethodElement methodElement)
: _methodElement = methodElement;

InvalidGenerationSourceError get doesNotReturnFutureNorStream {
return InvalidGenerationSourceError(
'All queries have to return a Future or Stream.',
todo: 'Define the return type as Future or Stream.',
element: _methodElement,
);
}

ProcessorError get doesNotReturnNullableStream {
return ProcessorError(
message: 'Queries returning streams of single elements might emit null.',
todo:
'Make the method return a Stream of a nullable type e.g. Stream<Person?>.',
element: _methodElement,
);
}

ProcessorError get doesNotReturnNullableFuture {
return ProcessorError(
message: 'Queries returning single elements might return null.',
todo:
'Make the method return a Future of a nullable type e.g. Future<Person?>.',
element: _methodElement,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,4 @@ class QueryMethodProcessorError {
element: _methodElement,
);
}

ProcessorError get doesNotReturnNullableStream {
return ProcessorError(
message: 'Queries returning streams of single elements might emit null.',
todo:
'Make the method return a Stream of a nullable type e.g. Stream<Person?>.',
element: _methodElement,
);
}

ProcessorError get doesNotReturnNullableFuture {
return ProcessorError(
message: 'Queries returning single elements might return null.',
todo:
'Make the method return a Future of a nullable type e.g. Future<Person?>.',
element: _methodElement,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@ class RawQueryMethodProcessorError {
);
}

InvalidGenerationSourceError get queryArgumentsAndMethodParametersDoNotMatch {
return InvalidGenerationSourceError(
'SQL query arguments and method parameters have to match.',
todo: 'Make sure to supply one parameter per SQL query argument.',
element: _methodElement,
);
}

InvalidGenerationSourceError get doesNotReturnFutureNorStream {
return InvalidGenerationSourceError(
'All queries have to return a Future or Stream.',
todo: 'Define the return type as Future or Stream.',
element: _methodElement,
);
}

ProcessorError queryMethodParameterIsNullable(
final ParameterElement parameterElement,
) {
Expand All @@ -43,22 +27,4 @@ class RawQueryMethodProcessorError {
element: _methodElement,
);
}

ProcessorError get doesNotReturnNullableStream {
return ProcessorError(
message: 'Queries returning streams of single elements might emit null.',
todo:
'Make the method return a Stream of a nullable type e.g. Stream<Person?>.',
element: _methodElement,
);
}

ProcessorError get doesNotReturnNullableFuture {
return ProcessorError(
message: 'Queries returning single elements might return null.',
todo:
'Make the method return a Future of a nullable type e.g. Future<Person?>.',
element: _methodElement,
);
}
}
23 changes: 0 additions & 23 deletions floor_generator/lib/processor/query_method_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,6 @@ class QueryMethodProcessor extends BaseQueryMethodProcessor {
return query;
}

DartType _getFlattenedReturnType(
final DartType rawReturnType,
final bool returnsStream,
final bool returnsList,
) {
final type = returnsStream
? _methodElement.returnType.flatten()
: _methodElement.library.typeSystem.flatten(rawReturnType);
return returnsList ? type.flatten() : type;
}

@override
void onAssertReturnsNullableSingle(bool returnsStream) {
returnsStream
? throw _processorError.doesNotReturnNullableStream
: throw _processorError.doesNotReturnNullableFuture;
}

@override
void onDoesNotReturnFutureNorStream() {
throw _processorError.doesNotReturnFutureNorStream;
}

@override
QueryMethod onProcess(
MethodElement methodElement,
Expand Down
13 changes: 0 additions & 13 deletions floor_generator/lib/processor/raw_query_method_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:floor_generator/value_object/queryable.dart';

class RawQueryMethodProcessor extends BaseQueryMethodProcessor {
final RawQueryMethodProcessorError _processorError;

final MethodElement _methodElement;

RawQueryMethodProcessor(
Expand Down Expand Up @@ -37,18 +36,6 @@ class RawQueryMethodProcessor extends BaseQueryMethodProcessor {
}
}

@override
void onAssertReturnsNullableSingle(bool returnsStream) {
returnsStream
? throw _processorError.doesNotReturnNullableStream
: throw _processorError.doesNotReturnNullableFuture;
}

@override
void onDoesNotReturnFutureNorStream() {
throw _processorError.doesNotReturnFutureNorStream;
}

@override
QueryMethod onProcess(
MethodElement methodElement,
Expand Down

0 comments on commit b7a1f6c

Please sign in to comment.