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

v1.7.6 - selectIDsBy #91

Merged
merged 2 commits into from
May 27, 2024
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## 1.7.6

- `EntityHandler`:
- Added `getIDs`.

- `EntitySource`, `EntityRepository`:
- Added `existIDs`, `selectIDsBy` and `selectIDsByQuery`.

- `DBRelationalAdapter`:
- Added `parseIDs`.

## 1.7.5

- New `IterableEntityReferenceExtension` and `IterableEntityReferenceListExtension`.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/bones_api_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef APILogger = void Function(APIRoot apiRoot, String type, String? message,
/// Bones API Library class.
class BonesAPI {
// ignore: constant_identifier_names
static const String VERSION = '1.7.5';
static const String VERSION = '1.7.6';

static bool _boot = false;

Expand Down
11 changes: 8 additions & 3 deletions lib/src/bones_api_condition_encoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -748,12 +748,15 @@ class EncodingContext {

String? tableName;

String? tableFieldID;

EncodingContext(this.entityName,
{this.parameters,
this.positionalParameters,
this.namedParameters,
this.transaction,
this.tableName});
this.tableName,
this.tableFieldID});

/// The encoded parameters placeholders and values.
final Map<String, dynamic> parametersPlaceholders = <String, dynamic>{};
Expand Down Expand Up @@ -974,13 +977,15 @@ abstract class ConditionEncoder {
List? positionalParameters,
Map<String, Object?>? namedParameters,
Transaction? transaction,
String? tableName}) {
String? tableName,
String? tableFieldID}) {
var context = EncodingContext(entityName,
parameters: parameters,
positionalParameters: positionalParameters,
namedParameters: namedParameters,
transaction: transaction,
tableName: tableName);
tableName: tableName,
tableFieldID: tableFieldID);

if (condition is ConditionANY) {
return context;
Expand Down
98 changes: 98 additions & 0 deletions lib/src/bones_api_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,15 @@
return null;
}

List<V?> getIDs<V>(List<O> os) {
if (os.isEmpty) return [];

Check warning on line 484 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L483-L484

Added lines #L483 - L484 were not covered by tests

var o = os.first;
var fieldID = idFieldName(o);

Check warning on line 487 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L486-L487

Added lines #L486 - L487 were not covered by tests

return os.map((o) => getField<V?>(o, fieldID)).toList();

Check warning on line 489 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L489

Added line #L489 was not covered by tests
}

V? getID<V>(O o) => getField<V?>(o, idFieldName(o));

void setID<V>(O o, V id) => setField<V>(o, idFieldName(o), id);
Expand Down Expand Up @@ -2279,6 +2288,16 @@
return super.encodeObjectListJson(list);
}

@override

Check warning on line 2291 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2291

Added line #L2291 was not covered by tests
List<V?> getIDs<V>(List<O> os) {
if (os.isEmpty) return [];

Check warning on line 2293 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2293

Added line #L2293 was not covered by tests

var o = os.first;
inspectObject(o);

Check warning on line 2296 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2295-L2296

Added lines #L2295 - L2296 were not covered by tests

return os.map((o) => o.getID<V>()).toList();

Check warning on line 2298 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2298

Added line #L2298 was not covered by tests
}

@override
V? getID<V>(O o) {
inspectObject(o);
Expand Down Expand Up @@ -2714,6 +2733,9 @@

FutureOr<bool> existsID(dynamic id, {Transaction? transaction});

FutureOr<Iterable<I>> existIDs<I extends Object>(List<I?> ids,
{Transaction? transaction});

FutureOr<O?> selectByID(dynamic id, {Transaction? transaction}) {
return select(ConditionID(id)).resolveMapped((sel) {
return sel.isNotEmpty ? sel.first : null;
Expand Down Expand Up @@ -2789,6 +2811,29 @@
int? limit,
EntityResolutionRules? resolutionRules});

FutureOr<Iterable<I>> selectIDsByQuery<I extends Object>(String query,

Check warning on line 2814 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2814

Added line #L2814 was not covered by tests
{Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters,
Transaction? transaction,
int? limit}) {
var condition = _parseCache.parseQuery(query);

Check warning on line 2820 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2820

Added line #L2820 was not covered by tests

return selectIDsBy(condition,

Check warning on line 2822 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2822

Added line #L2822 was not covered by tests
parameters: parameters,
positionalParameters: positionalParameters,
namedParameters: namedParameters,
transaction: transaction,
limit: limit);
}

FutureOr<Iterable<I>> selectIDsBy<I extends Object>(EntityMatcher<O> matcher,
{Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters,
Transaction? transaction,
int? limit});

FutureOr<Iterable<O>> selectAll(
{Transaction? transaction,
int? limit,
Expand Down Expand Up @@ -4352,6 +4397,25 @@
resolutionRules: resolutionRules);
}

@override
FutureOr<Iterable<I>> selectIDsByQuery<I extends Object>(String query,
{Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters,
Transaction? transaction,
int? limit}) {
checkNotClosed();

var condition = _parseCache.parseQuery(query);

return selectIDsBy(condition,
parameters: parameters,
positionalParameters: positionalParameters,
namedParameters: namedParameters,
transaction: transaction,
limit: limit);
}

@override
FutureOr<O?> deleteEntity(O o, {Transaction? transaction}) =>
deleteByID(getEntityID(o), transaction: transaction);
Expand Down Expand Up @@ -5989,6 +6053,20 @@
return o != null;
}

@override

Check warning on line 6056 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L6056

Added line #L6056 was not covered by tests
FutureOr<Iterable<I>> existIDs<I extends Object>(List<I?> ids,
{Transaction? transaction}) {
checkNotClosed();

Check warning on line 6059 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L6059

Added line #L6059 was not covered by tests

var osIDs = iterable().map((o) {
var oId = getID(o, entityHandler: entityHandler);
var matches = ids.contains(oId);

Check warning on line 6063 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L6061-L6063

Added lines #L6061 - L6063 were not covered by tests
return matches ? oId as I : null;
}).whereNotNull();

Check warning on line 6065 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L6065

Added line #L6065 was not covered by tests

return osIDs;
}

@override
Iterable<O> select(EntityMatcher<O> matcher,
{Object? parameters,
Expand All @@ -6008,6 +6086,26 @@
return trackEntities(os);
}

@override

Check warning on line 6089 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L6089

Added line #L6089 was not covered by tests
Iterable<I> selectIDsBy<I extends Object>(EntityMatcher<O> matcher,
{Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters,
Transaction? transaction,
int? limit}) {
checkNotClosed();

Check warning on line 6096 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L6096

Added line #L6096 was not covered by tests

var os = matches(matcher,

Check warning on line 6098 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L6098

Added line #L6098 was not covered by tests
parameters: parameters,
positionalParameters: positionalParameters,
namedParameters: namedParameters,
limit: limit);

var ids = entityHandler.getIDs<I>(os);

Check warning on line 6104 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L6104

Added line #L6104 was not covered by tests

return ids.whereNotNull().toList();

Check warning on line 6106 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L6106

Added line #L6106 was not covered by tests
}

@override
FutureOr<Iterable<O>> selectAll(
{Transaction? transaction,
Expand Down
77 changes: 74 additions & 3 deletions lib/src/bones_api_entity_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,9 @@
Map<String, Object?>? namedParameters,
PreFinishDBOperation<int, int>? preFinish});

FutureOr<List<I>> doExistIDs<I extends Object>(
TransactionOperation op, String entityName, String table, List<I> ids);

FutureOr<R?> doSelectByID<R>(
TransactionOperation op, String entityName, String table, Object id,
{PreFinishDBOperation<Map<String, dynamic>?, R?>? preFinish});
Expand Down Expand Up @@ -1129,6 +1132,10 @@
namedParameters: namedParameters,
preFinish: preFinish);

FutureOr<List<I>> doExistIDs<I extends Object>(
TransactionOperation op, List<I> ids) =>
databaseAdapter.doExistIDs<I>(op, name, tableName, ids);

FutureOr<R?> doSelectByID<R>(TransactionOperation op, Object id,
{PreFinishDBOperation<Map<String, dynamic>?, R?>? preFinish}) =>
databaseAdapter.doSelectByID<R>(op, name, tableName, id,
Expand Down Expand Up @@ -1216,13 +1223,55 @@

@override
FutureOr<bool> existsID(dynamic id, {Transaction? transaction}) {
var cachedEntityByID = transaction?.getCachedEntityByID(id, type: type);
if (cachedEntityByID != null) return false;

return count(matcher: ConditionID(id), transaction: transaction)
.resolveMapped((count) => count > 0);
}

@override
FutureOr<Iterable<I>> existIDs<I extends Object>(List<I?> ids,
{Transaction? transaction}) {
var idsNotNull = ids is List<I> ? ids : ids.whereNotNull().toList();
if (idsNotNull.isEmpty) return <I>[];

var cachedEntityByIDs =
transaction?.getCachedEntitiesByIDs(idsNotNull, type: type);

Check warning on line 1237 in lib/src/bones_api_entity_db.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db.dart#L1237

Added line #L1237 was not covered by tests

var cachedIDs = cachedEntityByIDs?.keys.whereType<I>().toList();

Check warning on line 1239 in lib/src/bones_api_entity_db.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db.dart#L1239

Added line #L1239 was not covered by tests

List<I> notCachedIDs;
if (cachedIDs != null) {
notCachedIDs = idsNotNull.whereNotIn(cachedIDs).toList();
if (notCachedIDs.isEmpty) {

Check warning on line 1244 in lib/src/bones_api_entity_db.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db.dart#L1243-L1244

Added lines #L1243 - L1244 were not covered by tests
return idsNotNull;
}
} else {
notCachedIDs = idsNotNull;
}

checkNotClosed();

var op = TransactionOperationCount(name, operationExecutor,
matcher: ConditionIdIN(notCachedIDs), transaction: transaction);

try {
return repositoryAdapter
.doExistIDs(op, notCachedIDs)
.resolveMapped((ids) {
return [
...?cachedIDs,

Check warning on line 1261 in lib/src/bones_api_entity_db.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db.dart#L1261

Added line #L1261 was not covered by tests
...ids,
];
});
} catch (e, s) {
var message = 'existIDs> '

Check warning on line 1266 in lib/src/bones_api_entity_db.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db.dart#L1266

Added line #L1266 was not covered by tests
'cachedIDs: $cachedIDs ; '
'notCachedIDs: $notCachedIDs ; '
'op: $op';
_log.severe(message, e, s);

Check warning on line 1270 in lib/src/bones_api_entity_db.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db.dart#L1270

Added line #L1270 was not covered by tests
rethrow;
}
}

@override
FutureOr<dynamic> ensureStored(o,
{Transaction? transaction, TransactionOperation? operation}) {
Expand Down Expand Up @@ -1337,6 +1386,28 @@
"Relationship select not supported for: (${matcher.runtimeTypeNameUnsafe}) $matcher @ $tableName ($this)");
}

@override

Check warning on line 1389 in lib/src/bones_api_entity_db.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db.dart#L1389

Added line #L1389 was not covered by tests
FutureOr<Iterable<I>> selectIDsBy<I extends Object>(EntityMatcher matcher,
{Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters,
Transaction? transaction,
int? limit}) {
if (matcher is ConditionID) {
var id = matcher.idValue;
return existsID(id, transaction: transaction)
.resolveMapped((exists) => exists ? [id] : []);

Check warning on line 1399 in lib/src/bones_api_entity_db.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db.dart#L1396-L1399

Added lines #L1396 - L1399 were not covered by tests
}

if (matcher is ConditionIdIN) {
var ids = matcher.idsValues.whereType<I>().toList();
return existIDs(ids, transaction: transaction);

Check warning on line 1404 in lib/src/bones_api_entity_db.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db.dart#L1402-L1404

Added lines #L1402 - L1404 were not covered by tests
}

throw UnsupportedError(
"Relationship select not supported for: (${matcher.runtimeTypeNameUnsafe}) $matcher @ $tableName ($this)");

Check warning on line 1408 in lib/src/bones_api_entity_db.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db.dart#L1407-L1408

Added lines #L1407 - L1408 were not covered by tests
}

FutureOr<O?> _selectByID(Transaction? transaction, ConditionID matcher,
Object? parameters, EntityResolutionRules? resolutionRules) {
var id = matcher.idValue ?? matcher.getID(parameters);
Expand Down
23 changes: 23 additions & 0 deletions lib/src/bones_api_entity_db_memory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import 'package:statistics/statistics.dart';

import 'bones_api_condition_encoder.dart';
import 'bones_api_condition.dart';
import 'bones_api_entity.dart';
import 'bones_api_entity_annotation.dart';
import 'bones_api_entity_db_sql.dart';
Expand Down Expand Up @@ -412,6 +413,28 @@
}
}

@override
FutureOr<List<I>> doExistIDsSQL<I extends Object>(
String entityName,
String table,
SQL sql,
Transaction transaction,
DBSQLMemoryAdapterContext connection) {
var map = _getTableMap(table, false);
if (map == null) return [];

var condition = sql.condition;
if (condition is! ConditionIdIN) {
return [];

Check warning on line 428 in lib/src/bones_api_entity_db_memory.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db_memory.dart#L428

Added line #L428 was not covered by tests
}

var ids = condition.idsValues;

var existIDs = map.keys.whereIn(ids).whereType<I>().toList();

return existIDs;
}

@override
FutureOr doInsertRelationshipSQL(String entityName, String table, SQL sql,
Transaction transaction, DBSQLMemoryAdapterContext connection) {
Expand Down
21 changes: 21 additions & 0 deletions lib/src/bones_api_entity_db_mysql.dart
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,27 @@ class DBMySQLAdapter extends DBSQLAdapter<DBMySqlConnectionWrapper>
});
}

@override
FutureOr<List<I>> doExistIDsSQL<I extends Object>(
String entityName,
String table,
SQL sql,
Transaction transaction,
DBMySqlConnectionWrapper connection) {
if (sql.isDummy) return <I>[];

return connection
.query(sql.sqlPositional, sql.parametersValuesByPosition)
.resolveMapped((results) {
var ids = results
.map((e) => e.fields)
.whereType<Map<String, dynamic>>()
.map((e) => e['id']);

return parseIDs<I>(ids);
});
}

@override
FutureOr<Iterable<Map<String, dynamic>>> doSelectSQL(
String entityName,
Expand Down
Loading
Loading