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 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 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(), 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(), 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(), 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(), 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, ids.size(), rows);
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions requery/src/main/java/io/requery/sql/RowCountException.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@

import io.requery.PersistenceException;

import javax.annotation.Nonnull;

/**
* Exception thrown when the affected row count of a executed statement doesn't match the value
* expected.
*/
public class RowCountException extends PersistenceException {

@Nonnull
private final Class<?> entityClass;
private final long expected;
private final long actual;

RowCountException(long expected, long actual) {
super("Expected " + expected + " row affected actual " + actual);
RowCountException(@Nonnull Class<?> entityClass, long expected, long actual) {
super(entityClass.getSimpleName() + ": expected " + expected + " row affected actual " + actual);
this.entityClass = entityClass;
this.expected = expected;
this.actual = actual;
}
Expand All @@ -46,4 +51,12 @@ public long getExpected() {
public long getActual() {
return actual;
}

/**
* @return entity class affected
*/
@Nonnull
public Class<?> getEntityClass() {
return entityClass;
}
}