fix some database operations

This commit is contained in:
f43nd1r 2018-06-01 17:09:23 +02:00
parent 289debf88d
commit 4d4938d3ab
2 changed files with 8 additions and 4 deletions

View file

@ -15,14 +15,14 @@ import static com.faendir.acra.model.QReport.report;
*/
public abstract class Queries {
private static final JPAQuery<VBug> V_BUG = new JPAQuery<>().from(bug)
.join(report)
.leftJoin(report)
.on(report.bug.eq(bug))
.select(new QVBug(bug, report.date.max(), report.count()))
.groupBy(bug);
private static final JPAQuery<VApp> V_APP = new JPAQuery<>().from(app)
.join(bug)
.leftJoin(bug)
.on(bug.app.eq(app))
.join(report)
.leftJoin(report)
.on(report.bug.eq(bug))
.select(new QVApp(app, bug.countDistinct(), report.count()))
.groupBy(app);

View file

@ -222,7 +222,7 @@ public class DataService implements Serializable {
}
private void deleteOrphanBugs() {
new JPADeleteClause(entityManager, bug).where(JPAExpressions.selectFrom(report).where(report.bug.eq(bug)).notExists()).execute();
new JPADeleteClause(entityManager, bug).where(bug.notIn(JPAExpressions.select(report.bug).from(report).distinct())).execute();
}
private Optional<Bug> findBug(@NonNull App app, @NonNull String stacktrace) {
@ -241,24 +241,28 @@ public class DataService implements Serializable {
if (!bug.isPresent()) {
report.setBug(new Bug(app, stacktrace, report.getVersionCode()));
store(report);
entityManager.flush();
} else if (!bug.get().equals(report.getBug())) {
report.setBug(bug.get());
store(report);
}
}
iterator.close();
entityManager.flush();
deleteOrphanBugs();
}
@Transactional
public void deleteReportsOlderThanDays(@NonNull App app, @NonNull int days) {
new JPADeleteClause(entityManager, report).where(report.bug.app.eq(app).and(report.date.before(LocalDateTime.now().minus(days, ChronoUnit.DAYS))));
entityManager.flush();
deleteOrphanBugs();
}
@Transactional
public void deleteReportsBeforeVersion(@NonNull App app, int versionCode) {
new JPADeleteClause(entityManager, report).where(report.bug.app.eq(app).and(report.versionCode.lt(versionCode)));
entityManager.flush();
deleteOrphanBugs();
}