Skip to content

Commit

Permalink
Detect if any duplicates in fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyeh committed Nov 23, 2023
1 parent 6fc9171 commit 79a7d35
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/postgresql2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
library entity.postgresql2;

import "dart:async";
import "dart:collection" show HashMap;
import "dart:collection";

import "package:postgresql2/postgresql.dart" show Connection, Row;
import "package:rikulo_commons/util.dart";
Expand Down
19 changes: 16 additions & 3 deletions lib/src/postgresql/postgresql.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,10 @@ class PostgresqlAccessAgent implements AccessAgent {
} else if (fields.isEmpty) {
sql.write("1");
} else {
assert(fields is Set || fields.toSet().length == fields.length,
'dup? $fields'); //Remove duplicated items for better performance
assert(_assertNoDup(fields));

bool first = true;
for (final String fd in fields) {
for (final fd in fields) {
if (first) first = false;
else sql.write(',');

Expand Down Expand Up @@ -114,6 +113,20 @@ class PostgresqlAccessAgent implements AccessAgent {
return null;
}

/// Detects if any duplication in [fields] for better performance
static bool _assertNoDup(Iterable<String> fields) {
if (fields is! Set<String>) {
final found = HashSet<String>(),
dups = <String>[];
for (final fd in fields)
if (!found.add(fd))
dups.add(fd);
if (dups.isNotEmpty)
print("Dup: $dups in $fields\n${StackTrace.current}");
}
return true;
}

@override
Future update(Entity entity, Map data, Iterable<String>? fields) {
final sql = StringBuffer('update "')
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: entity
version: 3.0.0
version: 3.0.0+1
description: A simple ORM for Relational and NoSQL database, such as Couchbase and PostgreSQL.
homepage: https://github.com/rikulo/entity
documentation: https://github.com/rikulo/entity
Expand Down

0 comments on commit 79a7d35

Please sign in to comment.