draft: database
This commit is contained in:
parent
e01ee18251
commit
7f0d5c34fc
8 changed files with 122 additions and 1 deletions
|
@ -3,6 +3,7 @@
|
|||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<application
|
||||
android:name=".MemoApplication"
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
|
|
|
@ -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) }
|
||||
}
|
|
@ -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>>
|
||||
}
|
|
@ -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
|
||||
)
|
|
@ -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!!
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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>
|
||||
)
|
|
@ -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,
|
||||
)
|
|
@ -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)
|
Reference in a new issue