Problems covered in this chapter |
|
The database tables and their associated Kotlin counterparts are defined in the data classes.
Gender.kt is a data class whose associated database table is called genders. By means of the index to the name column and the addition unique=true, we ensure that the designations for genders must be unique. For example, there cannot be two entries with the same designation ’female’. The column gender_id is our primary key, for which we let the database automatically generate consecutive ID values during insertion using autoGenerate=true. If the column inactive is not set when inserting it into the database, it receives the default value 0 in the database.
Person.kt is another data class whose associated database table is called persons. By means of the index to the name column and the addition unique=true, we ensure that the name must be unique for our persons. For example, there cannot be two entries with the same name ’Anne’. The column person_id is our primary key, for which we let the database automatically generate consecutive ID values when inserting it using autoGenerate=true. The column gender_id is a foreign key that refers to the table genders defined above. birth_date is the optional date of birth of the person. image_path is the file name for an optional image of the person. If the column inactive is not set when it is inserted into the database, it receives the default value 0 in the database. The variable birthDateAsString has the annotation @Ignore - thus there is no corresponding column in the database table persons. It is used to display the date of birth in the user interface, where the date must be displayed as a string.
The data class Tag.kt is another data class that stores our keywords in the database table tags. Its content should be clear based on the descriptions of the previous two data classes.
The data class Preferences.kt stores settings in the associated database table preferences that the user can set in the preferences menu. The column main_user_id is a foreign key to the persons table, which can be used to set the main user of the diary app. The column show_inactive defines whether inactive entries should be displayed in the list view (greyed out).
Our diary entries are stored in the data class DiaryEntry.kt and the associated database table diary_entries. The code should be self-explanatory by now due to the description of the previous data classes.
The data class DiaryEntriesTagsCrossRef.kt stores the assignments of diary entries to keywords and is an m:n relation between the two tables diary_entries and tags.
The data class DiaryEntryWithEverything.kt extends our data class DiaryEntry.kt in that additional properties are defined there with which we can access the person assigned by foreign key and all keywords that are entered in the table diary_entries_tags with the ID entry_id.