From 7f0d5c34fcb229a16ca7b203cad3d10078c78073 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 27 Nov 2023 07:09:22 +0100 Subject: [PATCH] draft: database --- app/src/main/AndroidManifest.xml | 3 +- .../MemoApplication.kt | 8 ++++++ .../memorisationapplication/data/MemoDao.kt | 13 +++++++++ .../memorisationapplication/data/Question.kt | 22 +++++++++++++++ .../data/QuestionsDB.kt | 28 +++++++++++++++++++ .../data/SetOfQuestions.kt | 13 +++++++++ .../data/SetQuestions.kt | 11 ++++++++ .../memorisationapplication/data/Sets.kt | 25 +++++++++++++++++ 8 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MemoApplication.kt create mode 100644 app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/MemoDao.kt create mode 100644 app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Question.kt create mode 100644 app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/QuestionsDB.kt create mode 100644 app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/SetOfQuestions.kt create mode 100644 app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/SetQuestions.kt create mode 100644 app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Sets.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 06bd76a..7766a47 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ xmlns:tools="http://schemas.android.com/tools"> - \ No newline at end of file + diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MemoApplication.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MemoApplication.kt new file mode 100644 index 0000000..5880500 --- /dev/null +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MemoApplication.kt @@ -0,0 +1,8 @@ +package fr.uparis.diamantkennel.memorisationapplication + +import android.app.Application +import fr.uparis.diamantkennel.memorisationapplication.data.QuestionsDB + +class MemoApplication : Application() { + val database: QuestionsDB by lazy { QuestionsDB.getDataBase(this) } +} diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/MemoDao.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/MemoDao.kt new file mode 100644 index 0000000..62d3783 --- /dev/null +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/MemoDao.kt @@ -0,0 +1,13 @@ +package fr.uparis.diamantkennel.memorisationapplication.data + +import androidx.room.Dao +import androidx.room.Query +import androidx.room.Transaction +import kotlinx.coroutines.flow.Flow + +@Dao +interface MemoDao { + @Transaction + @Query("SELECT * FROM SetQuestions") + fun loadAllSets(): Flow> +} diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Question.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Question.kt new file mode 100644 index 0000000..cea9686 --- /dev/null +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Question.kt @@ -0,0 +1,22 @@ +package fr.uparis.diamantkennel.memorisationapplication.data + +import androidx.room.Entity +import androidx.room.ForeignKey +import androidx.room.PrimaryKey + +@Entity( + foreignKeys = [ + ForeignKey( + entity = SetQuestions::class, + parentColumns = ["idSet"], + childColumns = ["setId"], + onDelete = ForeignKey.CASCADE + ) + ] +) +data class Question( + @PrimaryKey(autoGenerate = true) val idQuestion: Int = 0, + val setId: Int, // Foreign key linking to SetQuestions + val enonce: String, + val reponse: String +) diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/QuestionsDB.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/QuestionsDB.kt new file mode 100644 index 0000000..1e5705f --- /dev/null +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/QuestionsDB.kt @@ -0,0 +1,28 @@ +package fr.uparis.diamantkennel.memorisationapplication.data + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase + +@Database( + entities = [SetQuestions::class, Question::class, Sets::class], + version = 1, +) +abstract class QuestionsDB : RoomDatabase() { + abstract fun memoDao(): MemoDao + + companion object { + @Volatile + private var instance: QuestionsDB? = null + + fun getDataBase(c: Context): QuestionsDB { + if (instance != null) return instance!! + val db = + Room.databaseBuilder(c.applicationContext, QuestionsDB::class.java, "memo") + .fallbackToDestructiveMigration().build() + instance = db + return instance!! + } + } +} diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/SetOfQuestions.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/SetOfQuestions.kt new file mode 100644 index 0000000..9df2560 --- /dev/null +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/SetOfQuestions.kt @@ -0,0 +1,13 @@ +package fr.uparis.diamantkennel.memorisationapplication.data + +import androidx.room.Embedded +import androidx.room.Relation + +data class SetOfQuestions( + @Embedded val set: SetQuestions, + @Relation( + parentColumn = "idSet", + entityColumn = "setId" + ) + val questions: List +) diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/SetQuestions.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/SetQuestions.kt new file mode 100644 index 0000000..5636104 --- /dev/null +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/SetQuestions.kt @@ -0,0 +1,11 @@ +package fr.uparis.diamantkennel.memorisationapplication.data + +import androidx.room.Entity +import androidx.room.Index +import androidx.room.PrimaryKey + +@Entity(indices = [Index(value = ["name"])]) +data class SetQuestions( + @PrimaryKey(autoGenerate = true) val idSet: Int = 0, + val name: String, +) diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Sets.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Sets.kt new file mode 100644 index 0000000..d1d335a --- /dev/null +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Sets.kt @@ -0,0 +1,25 @@ +package fr.uparis.diamantkennel.memorisationapplication.data + +import androidx.room.Entity +import androidx.room.ForeignKey +import androidx.room.Index + +@Entity( + indices = [Index(value = ["idQuestion"])], + primaryKeys = ["idSet", "idQuestion"], + foreignKeys = [ + ForeignKey( + entity = SetQuestions::class, + parentColumns = ["idSet"], + childColumns = ["idSet"], + onDelete = ForeignKey.CASCADE + ), + ForeignKey( + entity = Question::class, + parentColumns = ["idQuestion"], + childColumns = ["idQuestion"], + onDelete = ForeignKey.CASCADE + ) + ] +) +data class Sets(val idSet: Int, val idQuestion: Int)