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 isRaw to Query annotation #315

Draft
wants to merge 14 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
null safety
  • Loading branch information
joshuatam committed Sep 5, 2021
commit ac05afbcd9d43fbcf1e042a0ea40f574676289aa
10 changes: 5 additions & 5 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.6.1"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -196,7 +196,7 @@ packages:
path: "../floor"
relative: true
source: path
version: "1.0.1"
version: "1.1.0"
floor_annotation:
dependency: transitive
description:
Expand All @@ -210,7 +210,7 @@ packages:
path: "../floor_generator"
relative: true
source: path
version: "1.0.1"
version: "1.1.0"
flutter:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -372,7 +372,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
sqflite:
dependency: transitive
description:
Expand Down Expand Up @@ -449,7 +449,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.3.0"
timing:
dependency: transitive
description:
Expand Down
10 changes: 7 additions & 3 deletions floor/lib/src/adapter/query_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ class QueryAdapter {
Future<T?> query<T>(
final String sql, {
final List<Object>? arguments,
final bool isRaw = false,
required final T Function(Map<String, Object?>) mapper,
}) async {
final rows = await _database.rawQuery(sql, arguments);
final rows = await (isRaw
? _database.rawQuery(sql.replaceAll('?', arguments![0] as String))
: _database.rawQuery(sql, arguments));

if (rows.isEmpty) {
return null;
Expand All @@ -38,20 +41,21 @@ class QueryAdapter {
required final T Function(Map<String, Object?>) mapper,
}) async {
final rows = await (isRaw
? _database.rawQuery(sql.replaceAll('?', arguments[0]))
? _database.rawQuery(sql.replaceAll('?', arguments![0] as String))
: _database.rawQuery(sql, arguments));
return rows.map((row) => mapper(row)).toList();
}

Future<void> queryNoReturn(
final String sql, {
final List<Object>? arguments,
final bool isRaw = false,
}) async {
// TODO #94 differentiate between different query kinds (select, update, delete, insert)
// this enables to notify the observers
// also requires extracting the table name :(
await (isRaw
? _database.rawQuery(sql.replaceAll('?', arguments[0]))
? _database.rawQuery(sql.replaceAll('?', arguments![0] as String))
: _database.rawQuery(sql, arguments));
}

Expand Down
8 changes: 4 additions & 4 deletions floor/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.6.1"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -203,7 +203,7 @@ packages:
path: "../floor_generator"
relative: true
source: path
version: "1.0.1"
version: "1.1.0"
flutter:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -372,7 +372,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
sqflite:
dependency: "direct main"
description:
Expand Down Expand Up @@ -449,7 +449,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.3.0"
timing:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion floor_annotation/lib/src/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Query {
/// The SQLite query.
final String value;

final bool isRaw;
final bool? isRaw;

/// Marks a method as a query method.
const Query(this.value, {this.isRaw});
Expand Down
2 changes: 1 addition & 1 deletion floor_generator/lib/processor/query_method_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class QueryMethodProcessor extends Processor<QueryMethod> {

bool _getIsRaw() {
final isRaw = _methodElement
.getAnnotation(annotations.Query)
.getAnnotation(annotations.Query)!
.getField(AnnotationField.isRaw)
?.toBoolValue();

Expand Down
22 changes: 10 additions & 12 deletions floor_generator/lib/value_object/query_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class QueryMethod {
/// Query where the parameter mapping is stored.
final Query query;

final bool isRaw;
final bool? isRaw;

final DartType rawReturnType;

Expand All @@ -38,17 +38,15 @@ class QueryMethod {
final Set<TypeConverter> typeConverters;

QueryMethod(
this.methodElement,
this.name,
this.query,
this.rawReturnType,
this.flattenedReturnType,
this.parameters,
this.queryable,
this.typeConverters, {
this.isRaw
}
);
this.methodElement,
this.name,
this.query,
this.rawReturnType,
this.flattenedReturnType,
this.parameters,
this.queryable,
this.typeConverters,
{this.isRaw});

bool get returnsList {
final type = returnsStream
Expand Down
4 changes: 2 additions & 2 deletions floor_generator/lib/writer/query_method_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class QueryMethodWriter implements Writer {
String _generateNoReturnQuery(final String query, final String? arguments) {
final parameters = StringBuffer(query);
if (arguments != null) parameters.write(', arguments: $arguments');
if (_queryMethod.isRaw) parameters.write(', isRaw: true');
if (_queryMethod.isRaw!) parameters.write(', isRaw: true');
return 'await _queryAdapter.queryNoReturn($parameters);';
}

Expand All @@ -175,7 +175,7 @@ class QueryMethodWriter implements Writer {
final mapper = _generateMapper(queryable);
final parameters = StringBuffer(query)..write(', mapper: $mapper');
if (arguments != null) parameters.write(', arguments: $arguments');
if (_queryMethod.isRaw) parameters.write(', isRaw: true');
if (_queryMethod.isRaw!) parameters.write(', isRaw: true');

if (_queryMethod.returnsStream) {
// for streamed queries, we need to provide the queryable to know which
Expand Down