Skip to content

Commit

Permalink
Add entity class to RowCountException (#761)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnm authored and npurushe committed Mar 21, 2018
1 parent e09143e commit 1bc0717
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
10 changes: 5 additions & 5 deletions requery/src/main/java/io/requery/sql/EntityWriter.java
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
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;
}
}

0 comments on commit 1bc0717

Please sign in to comment.