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 entity name to RowCountException #761

Merged
Merged
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
10 changes: 5 additions & 5 deletions requery/src/main/java/io/requery/sql/EntityWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void checkRowsAffected(int count, E entity, EntityProxy<E> proxy) {
if (proxy != null && versionAttribute != null && count == 0) {
throw new OptimisticLockException(entity, proxy.get(versionAttribute));
} else if (count != 1) {
throw new RowCountException(1, count);
throw new RowCountException(entity.getClass().getName(), 1, count);
}
}

Expand Down Expand Up @@ -505,7 +505,7 @@ public void upsert(E entity, final EntityProxy<E> proxy) {
}
int rows = upsert.evaluate(element).value();
if (rows <= 0) {
throw new RowCountException(1, rows);
throw new RowCountException(entity.getClass().getName(), 1, rows);
}
proxy.link(context.read(entityClass));
updateAssociations(Cascade.UPSERT, entity, proxy, null);
Expand Down Expand Up @@ -773,7 +773,7 @@ private void updateAssociation(Cascade mode, E entity, EntityProxy<E> proxy,
.and(uKey.equal(otherValue));
int count = query.get().value();
if (count != 1) {
throw new RowCountException(1, count);
throw new RowCountException(entity.getClass().getName(), 1, count);
}
}
changes.clear();
Expand Down Expand Up @@ -953,7 +953,7 @@ private <U extends S> void cascadeWrite(Cascade mode, U entity, EntityProxy<U> p
// If this happens, it means that I'm updating the 'many' side of a
// one-to-many (where the foreign key is) to link it to the 'one' side of
// the relationship, but the 'many' side does not exist.
throw new RowCountException(1, updated);
throw new RowCountException(entity.getClass().getName(), 1, updated);
}
break;
case UPSERT:
Expand Down Expand Up @@ -1022,7 +1022,7 @@ private void batchDelete(Iterable<E> entities) {
}
int rows = deletion.get().value();
if (rows != ids.size()) {
throw new RowCountException(ids.size(), rows);
throw new RowCountException(entityClass.getSimpleName(), ids.size(), rows);
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions requery/src/main/java/io/requery/sql/RowCountException.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
*/
public class RowCountException extends PersistenceException {

private final String entityName;
private final long expected;
private final long actual;

RowCountException(long expected, long actual) {
super("Expected " + expected + " row affected actual " + actual);
RowCountException(String entityName, long expected, long actual) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the usage it would be simpler if this argument was a Class instead of a String. That would simplify the usage in all the calling sites

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the point, updated PR.

super(entityName + ": expected " + expected + " row affected actual " + actual);
this.entityName = entityName;
this.expected = expected;
this.actual = actual;
}
Expand All @@ -46,4 +48,11 @@ public long getExpected() {
public long getActual() {
return actual;
}

/**
* @return entity name affected
*/
public String getEntityName() {
return entityName;
}
}