From 518ade3165c9ff6747f25682a3b3e00717e94c65 Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Sun, 18 Sep 2016 08:08:13 -0400 Subject: [PATCH] Add some database checks --- .../isoron/uhabits/models/CheckmarkList.java | 2 ++ .../sqlite/InconsistentDatabaseException.java | 28 +++++++++++++++++++ .../models/sqlite/SQLiteCheckmarkList.java | 10 +++++++ 3 files changed, 40 insertions(+) create mode 100644 app/src/main/java/org/isoron/uhabits/models/sqlite/InconsistentDatabaseException.java diff --git a/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java b/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java index 2b23199e7..86488a8d1 100644 --- a/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java +++ b/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java @@ -130,6 +130,8 @@ public abstract class CheckmarkList */ public final int[] getValues(long from, long to) { + if(from > to) return new int[0]; + List checkmarks = getByInterval(from, to); int values[] = new int[checkmarks.size()]; diff --git a/app/src/main/java/org/isoron/uhabits/models/sqlite/InconsistentDatabaseException.java b/app/src/main/java/org/isoron/uhabits/models/sqlite/InconsistentDatabaseException.java new file mode 100644 index 000000000..2a3d862ca --- /dev/null +++ b/app/src/main/java/org/isoron/uhabits/models/sqlite/InconsistentDatabaseException.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.models.sqlite; + +public class InconsistentDatabaseException extends RuntimeException +{ + public InconsistentDatabaseException(String message) + { + super(message); + } +} diff --git a/app/src/main/java/org/isoron/uhabits/models/sqlite/SQLiteCheckmarkList.java b/app/src/main/java/org/isoron/uhabits/models/sqlite/SQLiteCheckmarkList.java index ea42a846b..7c9b552e0 100644 --- a/app/src/main/java/org/isoron/uhabits/models/sqlite/SQLiteCheckmarkList.java +++ b/app/src/main/java/org/isoron/uhabits/models/sqlite/SQLiteCheckmarkList.java @@ -28,6 +28,7 @@ import com.activeandroid.query.*; import org.isoron.uhabits.models.*; import org.isoron.uhabits.models.sqlite.records.*; +import org.isoron.uhabits.utils.*; import org.jetbrains.annotations.*; import java.util.*; @@ -99,6 +100,15 @@ public class SQLiteCheckmarkList extends CheckmarkList List records = sqlite.query(query, params); for (CheckmarkRecord record : records) record.habit = habitRecord; + + int nDays = DateUtils.getDaysBetween(fromTimestamp, toTimestamp) + 1; + if (records.size() != nDays) + { + throw new InconsistentDatabaseException( + String.format("habit=%s, %d expected, %d found", + habit.getName(), nDays, records.size())); + } + return toCheckmarks(records); }