Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
dickermoshe committed Sep 24, 2024
1 parent 3ca6bb2 commit 16be823
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 50 deletions.
26 changes: 13 additions & 13 deletions docs/lib/snippets/dart_api/manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -334,21 +334,21 @@ Future<void> customOrdering(AppDatabase db) async {
// #enddocregion manager_custom_ordering

void _managerAnnotations(AppDatabase db) async {
// #docregion manager_annotations
// First create an annotation with an expression you want to use
// #docregion manager_annotations
// First create an annotation with an expression you want to use
final titleLengthAnnotation =
db.managers.todoItems.annotation((o) => o.title.length);

/// Create a copy of the manager with the annotations you want to use
final manager =
db.managers.todoItems.withAnnotations([titleLengthAnnotation]);

// Then use the annotation in a filter
// This will filter all items whose title has exactly 10 characters
// Then use the annotation in a filter
// This will filter all items whose title has exactly 10 characters
manager.filter((f) => titleLengthAnnotation.filter(10));

// You can also use the annotation in an ordering
// This will order all items by the length of their title in ascending order
// You can also use the annotation in an ordering
// This will order all items by the length of their title in ascending order
manager.orderBy((o) => titleLengthAnnotation.order.asc());

/// You can read the result of the annotation too
Expand All @@ -360,8 +360,8 @@ void _managerAnnotations(AppDatabase db) async {
}

void _managerReferencedAnnotations(AppDatabase db) async {
// #docregion referenced_annotations
// This annotation will get the name of the user of this todo
// #docregion referenced_annotations
// This annotation will get the name of the user of this todo
final todoUserName =
db.managers.todoItems.annotation((o) => o.category.user.name);

Expand All @@ -373,13 +373,13 @@ void _managerReferencedAnnotations(AppDatabase db) async {
final userName = todoUserName.read(refs);
print('Item ${item.id} has a user with the name $userName');
}
// #enddocregion referenced_annotations
// #enddocregion referenced_annotations
}

void _managerAggregatedAnnotations(AppDatabase db) async {
// #docregion aggregated_annotations
// You can aggregate over multiple rows in a related table
// to perform calculations on them
// #docregion aggregated_annotations
// You can aggregate over multiple rows in a related table
// to perform calculations on them
final todoCountAnnotation = db.managers.todoCategory
.annotation((o) => o.todoItemsRefs((o) => o.id).count());

Expand All @@ -392,5 +392,5 @@ void _managerAggregatedAnnotations(AppDatabase db) async {
final todoCount = todoCountAnnotation.read(refs);
print('Category ${category.id} has $todoCount todos');
}
// #enddocregion aggregated_annotations
// #enddocregion aggregated_annotations
}
10 changes: 3 additions & 7 deletions drift/lib/src/runtime/manager/annotate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Annotation<SqlType extends Object, $Table extends Table>
}

/// Read the result of the annotation from the [BaseReferences] object
SqlType? read(BaseReferences refs) {
SqlType? read(BaseReferences<dynamic, $Table, dynamic> refs) {
try {
return refs.$_typedResult.read(_expression);
} on ArgumentError {
Expand Down Expand Up @@ -62,12 +62,8 @@ class AnnotationWithConverter<DartType, SqlType extends Object,
return ColumnWithTypeConverterFilters(_expression);
}

/// Converter function to convert from [SqlType] to [DartType]
final DartType Function(SqlType) $converter;

/// Create a new annotation with a converter
AnnotationWithConverter(this._expression, super._joinBuilders,
{required this.$converter});
AnnotationWithConverter(this._expression, super._joinBuilders);

@override
bool operator ==(Object other) {
Expand All @@ -87,6 +83,6 @@ class AnnotationWithConverter<DartType, SqlType extends Object,
if (dartType == null) {
return null;
}
return $converter(dartType);
return _expression.converter.fromSql(dartType);
}
}
11 changes: 0 additions & 11 deletions drift/lib/src/runtime/manager/composer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,4 @@ class Composer<Database extends GeneratedDatabase, CurrentTable extends Table> {
$removeJoinBuilderFromRootComposer ??
((JoinBuilder joinBuilder) => $joinBuilders.remove(joinBuilder));
}

/// A helper method for creating a new composer from an existing composer
Composer.fromComposer(Composer<Database, CurrentTable> composer)
: this(
$db: composer.$db,
$table: composer.$table,
joinBuilder: composer.$joinBuilder,
$addJoinBuilderToRootComposer: composer.$addJoinBuilderToRootComposer,
$removeJoinBuilderFromRootComposer:
composer.$removeJoinBuilderFromRootComposer,
);
}
38 changes: 19 additions & 19 deletions drift/lib/src/runtime/manager/manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -306,23 +306,24 @@ class TableManagerState<
.toSet()
..add(joinBuilder);
return TableManagerState(
db: db,
table: table,
createFilteringComposer: createFilteringComposer,
createOrderingComposer: createOrderingComposer,
createAnnotationComposer: createAnnotationComposer,
createCompanionCallback: _createCompanionCallback,
updateCompanionCallback: _updateCompanionCallback,
withReferenceMapper: _withReferenceMapper,
filter: filter,
joinBuilders: newJoinBuilders,
orderingBuilders: orderingBuilders,
distinct: distinct,
limit: limit,
offset: offset,
prefetchHooksCallback: _prefetchHooksCallback,
prefetchedData: _prefetchedData,
addedColumns: addedColumns);
db: db,
table: table,
createFilteringComposer: createFilteringComposer,
createOrderingComposer: createOrderingComposer,
createAnnotationComposer: createAnnotationComposer,
createCompanionCallback: _createCompanionCallback,
updateCompanionCallback: _updateCompanionCallback,
withReferenceMapper: _withReferenceMapper,
filter: filter,
joinBuilders: newJoinBuilders,
orderingBuilders: orderingBuilders,
distinct: distinct,
limit: limit,
offset: offset,
prefetchHooksCallback: _prefetchHooksCallback,
prefetchedData: _prefetchedData,
addedColumns: addedColumns,
);
}

/// Helper for getting the table that's casted as a TableInfo
Expand Down Expand Up @@ -1039,7 +1040,6 @@ abstract class RootTableManager<
) {
final composer = $state.createAnnotationComposer();
final expression = a(composer);
return AnnotationWithConverter(expression, composer.$joinBuilders.toSet(),
$converter: (p0) => expression.converter.fromSql(p0));
return AnnotationWithConverter(expression, composer.$joinBuilders.toSet());
}
}

0 comments on commit 16be823

Please sign in to comment.