Make ids unique across both habits and groups

pull/2020/head
Dharanish 1 year ago
parent 30b124ae6e
commit 32c69772ae

@ -22,8 +22,6 @@ import org.apache.commons.lang3.StringUtils
import org.apache.commons.lang3.tuple.ImmutablePair import org.apache.commons.lang3.tuple.ImmutablePair
import org.apache.commons.lang3.tuple.Pair import org.apache.commons.lang3.tuple.Pair
import java.lang.reflect.Field import java.lang.reflect.Field
import java.util.ArrayList
import java.util.HashMap
import java.util.LinkedList import java.util.LinkedList
class Repository<T>( class Repository<T>(
@ -130,6 +128,26 @@ class Repository<T>(
} }
} }
/**
* Gets the shared id for tables that need to maintain unique ids across tables
* like habits and habit groups
*/
fun getNextAvailableId(name: String): Long {
val query = "SELECT next_id FROM SharedIds WHERE name = ?"
val cursor = db.query(query, name)
val nextId: Long
cursor.use { c ->
if (cursor.moveToNext()) {
nextId = cursor.getLong(0) ?: throw IllegalStateException("Cannot fetch shared ID for $name")
execSQL("UPDATE SharedIds SET next_id = next_id + 1 WHERE name = ?", name)
} else {
throw IllegalStateException("Cannot fetch shared ID for $name")
}
}
return nextId
}
private fun cursorToMultipleRecords(c: Cursor): List<T> { private fun cursorToMultipleRecords(c: Cursor): List<T> {
val records: MutableList<T> = LinkedList() val records: MutableList<T> = LinkedList()
while (c.moveToNext()) records.add(cursorToSingleRecord(c)) while (c.moveToNext()) records.add(cursorToSingleRecord(c))

@ -36,10 +36,10 @@ class SQLiteHabitGroupList @Inject constructor(private val modelFactory: ModelFa
override fun add(habitGroup: HabitGroup) { override fun add(habitGroup: HabitGroup) {
loadRecords() loadRecords()
habitGroup.position = size() habitGroup.position = size()
habitGroup.id = repository.getNextAvailableId("habitandgroup")
val record = HabitGroupRecord() val record = HabitGroupRecord()
record.copyFrom(habitGroup) record.copyFrom(habitGroup)
repository.save(record) repository.save(record)
habitGroup.id = record.id
habitGroup.habitList.groupID = record.id habitGroup.habitList.groupID = record.id
list.add(habitGroup) list.add(habitGroup)
observable.notifyListeners() observable.notifyListeners()

@ -52,10 +52,10 @@ class SQLiteHabitList @Inject constructor(private val modelFactory: ModelFactory
override fun add(habit: Habit) { override fun add(habit: Habit) {
loadRecords() loadRecords()
habit.position = size() habit.position = size()
habit.id = repository.getNextAvailableId("habitandgroup")
val record = HabitRecord() val record = HabitRecord()
record.copyFrom(habit) record.copyFrom(habit)
repository.save(record) repository.save(record)
habit.id = record.id
(habit.originalEntries as SQLiteEntryList).habitId = record.id (habit.originalEntries as SQLiteEntryList).habitId = record.id
list.add(habit) list.add(habit)
observable.notifyListeners() observable.notifyListeners()

@ -1,5 +1,41 @@
create table SharedIds (
name text primary key,
next_id integer not null
);
insert into SharedIds (name, next_id) values ('habitandgroup', (select coalesce(max(id),0) from Habits) + 1 );
alter table Habits rename to HabitsOld;
create table Habits (
id integer primary key,
archived integer,
color integer,
description text,
freq_den integer,
freq_num integer,
highlight integer,
name text,
position integer,
reminder_hour integer,
reminder_min integer,
reminder_days integer not null default 127,
type integer not null default 0,
target_type integer not null default 0,
target_value real not null default 0,
unit text not null default "",
question text,
uuid text
);
insert into Habits (id, archived, color, description, freq_den, freq_num, highlight, name, position, reminder_min, reminder_days, type, target_type, target_value, unit, question, uuid)
select id, archived, color, description, freq_den, freq_num, highlight, name, position, reminder_min, reminder_days, type, target_type, target_value, unit, question, uuid
from HabitsOld;
drop table HabitsOld;
create table HabitGroups ( create table HabitGroups (
id integer primary key autoincrement, id integer primary key,
archived integer, archived integer,
color integer, color integer,
description text not null default "", description text not null default "",

Loading…
Cancel
Save