draft: database

This commit is contained in:
Mylloon 2023-11-27 07:09:22 +01:00
parent e01ee18251
commit 7f0d5c34fc
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
8 changed files with 122 additions and 1 deletions

View file

@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<application <application
android:name=".MemoApplication"
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules" android:fullBackupContent="@xml/backup_rules"
@ -25,4 +26,4 @@
</activity> </activity>
</application> </application>
</manifest> </manifest>

View file

@ -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) }
}

View file

@ -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<List<SetOfQuestions>>
}

View file

@ -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
)

View file

@ -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!!
}
}
}

View file

@ -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<Question>
)

View file

@ -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,
)

View file

@ -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)