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 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
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
1 change: 1 addition & 0 deletions floor/lib/floor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export 'package:floor/src/adapter/update_adapter.dart';
export 'package:floor/src/callback.dart';
export 'package:floor/src/database.dart';
export 'package:floor/src/migration.dart';
export 'package:floor/src/automigration/auto_migration.dart';
export 'package:floor/src/sqflite_database_factory.dart';
export 'package:floor_annotation/floor_annotation.dart';
3 changes: 2 additions & 1 deletion floor/lib/src/adapter/migration_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ abstract class MigrationAdapter {
..sort((first, second) =>
first.startVersion.compareTo(second.startVersion));

/*
if (relevantMigrations.isEmpty ||
relevantMigrations.last.endVersion != endVersion) {
throw StateError(
'There is no migration supplied to update the database to the current version.'
' Aborting the migration.',
);
}
}*/

for (final migration in relevantMigrations) {
await migration.migrate(migrationDatabase);
Expand Down
15 changes: 12 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('?1', arguments![0] as String))
: _database.rawQuery(sql, arguments));

if (rows.isEmpty) {
return null;
Expand All @@ -34,20 +37,26 @@ class QueryAdapter {
Future<List<T>> queryList<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('?1', 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 _database.rawQuery(sql, arguments);
await (isRaw
? _database.rawQuery(sql.replaceAll('?1', arguments![0] as String))
: _database.rawQuery(sql, arguments));
}

/// Executes a SQLite query that returns a stream of single query results
Expand Down
15 changes: 15 additions & 0 deletions floor/lib/src/automigration/auto_migration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:sqflite/sqlite_api.dart';

class AutoMigration {
static Future<void> migrate(
final Database database,
List<String> createStatements
) async {
/// Auto create new tables
for (String statement in createStatements) {
await database.execute(statement);
}

/// TODO: Auto check new columns
}
}
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
4 changes: 3 additions & 1 deletion floor_annotation/lib/src/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Query {
/// The SQLite query.
final String value;

final bool? isRaw;

/// Marks a method as a query method.
const Query(this.value);
const Query(this.value, {this.isRaw});
}
1 change: 1 addition & 0 deletions floor_generator/lib/misc/constants.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
abstract class AnnotationField {
static const queryValue = 'value';
static const isRaw = 'isRaw';
static const primaryKeyAutoGenerate = 'autoGenerate';
static const onConflict = 'onConflict';

Expand Down
11 changes: 11 additions & 0 deletions floor_generator/lib/processor/query_method_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class QueryMethodProcessor extends Processor<QueryMethod> {
final rawReturnType = _methodElement.returnType;

final query = QueryProcessor(_methodElement, _getQuery()).process();
final isRaw = _getIsRaw();

_getQuery();
final returnsStream = rawReturnType.isStream;
Expand Down Expand Up @@ -85,6 +86,7 @@ class QueryMethodProcessor extends Processor<QueryMethod> {
parameters,
queryable,
allTypeConverters,
isRaw: isRaw,
);
}

Expand All @@ -99,6 +101,15 @@ class QueryMethodProcessor extends Processor<QueryMethod> {
return query;
}

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

return isRaw ?? false;
}

DartType _getFlattenedReturnType(
final DartType rawReturnType,
final bool returnsStream,
Expand Down
20 changes: 11 additions & 9 deletions floor_generator/lib/value_object/query_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class QueryMethod {
/// Query where the parameter mapping is stored.
final Query query;

final bool? isRaw;

final DartType rawReturnType;

/// Flattened return type.
Expand All @@ -36,15 +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.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
29 changes: 23 additions & 6 deletions floor_generator/lib/writer/database_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ class DatabaseWriter implements Writer {
Method _generateOpenMethod(final Database database) {
final createTableStatements =
_generateCreateTableSqlStatements(database.entities)
.map((statement) => "await database.execute('$statement');")
.map((statement) => "'$statement',")
.join('\n');
final createIndexStatements = database.entities
.map((entity) => entity.indices.map((index) => index.createQuery()))
.expand((statements) => statements)
.map((statement) => "await database.execute('$statement');")
.map((statement) => "'$statement',")
.join('\n');
final createViewStatements = database.views
.map((view) => view.getCreateViewStatement())
.map((statement) => "await database.execute('''$statement''');")
.map((statement) => "'''$statement'''")
.join('\n');

final pathParameter = Parameter((builder) => builder
Expand All @@ -99,6 +99,18 @@ class DatabaseWriter implements Writer {
..requiredParameters.addAll([pathParameter, migrationsParameter])
..optionalParameters.add(callbackParameter)
..body = Code('''
final List<String> _createTableStatements = [
$createTableStatements
];

final List<String> _createIndexStatements = [
$createIndexStatements
];

final List<String> _createViewStatements = [
$createViewStatements
];

final databaseOptions = sqflite.OpenDatabaseOptions(
version: ${database.version},
onConfigure: (database) async {
Expand All @@ -111,12 +123,17 @@ class DatabaseWriter implements Writer {
onUpgrade: (database, startVersion, endVersion) async {
await MigrationAdapter.runMigrations(database, startVersion, endVersion, migrations);

await AutoMigration.migrate(database, _createTableStatements);

await callback?.onUpgrade?.call(database, startVersion, endVersion);
},
onCreate: (database, version) async {
$createTableStatements
$createIndexStatements
$createViewStatements
Future.wait(_createTableStatements
.map((statement) async => await database.execute(statement)));
Future.wait(_createIndexStatements
.map((statement) async => await database.execute(statement)));
Future.wait(_createViewStatements
.map((statement) async => await database.execute(statement)));

await callback?.onCreate?.call(database, version);
},
Expand Down
2 changes: 2 additions & 0 deletions floor_generator/lib/writer/query_method_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +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');
return 'await _queryAdapter.queryNoReturn($parameters);';
}

Expand All @@ -174,6 +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.returnsStream) {
// for streamed queries, we need to provide the queryable to know which
Expand Down