Room
안드로이드 내부 데이터베이스 Room을 사용하여 "나만의 타이머"에서 사용될 데이터를 관리하도록 하였습니다.
Room DB를 사용한 과정
Table의 역활을 할 Model
//외래키 설정
@Entity(foreignKeys = [ForeignKey(
entity = Title::class,
parentColumns = ["title"],
childColumns = ["todo_title"],
onDelete = CASCADE,
onUpdate = CASCADE
)]
)
//Table 데이터 정보
data class Todo(
val todo_title : String?,
var todo : String?,
val success : Boolean? = false,
@PrimaryKey(autoGenerate = true)
var id : Int? = 0
)
위 모델은 목표에 대한 Todo(할 일)을 저장하는 Table입니다.
데이터베이스에 SQL 명령어(Insert, Select...)을 포함된 Dao
@Dao
interface DAO {
//Room DB Insert SQL
@Insert
suspend fun titleAdd(title: Title)
@Query("INSERT INTO Todo(todo_title, todo, success) VALUES(:title, :todo, :success)")
suspend fun todoAdd(title : String,todo : String, success:Boolean)
@Query("INSERT INTO CalendarTime(calendar_title,time_date) VALUES(:title,:date)")
suspend fun calendarTimeAdd(title : String, date : String)
.....
//Room DB Update SQL
@Query("UPDATE Todo SET success = :success WHERE todo = :todo AND todo_title = :title")
suspend fun todoSuccessUpdate(title : String, todo : String, success: Boolean)
@Query("UPDATE CalendarTodo SET success = :success Where todo = :todo AND todo_date = :date AND calendar_title = :title")
suspend fun calendarTodoSuccessUpdate(title: String, todo: String, success: Boolean, date: String)
@Query("UPDATE CalendarTime SET time = time + :time Where calendar_title = :title AND time_date = :date")
suspend fun timeUpdate(time : Long, title : String, date: String)
.....
//Room DB SELECT SQL
@Query("SELECT COUNT(title) FROM Title")
suspend fun getTitleCount() : Int
@Query("SELECT * FROM Title LEFT OUTER JOIN Todo ON Title.title == Todo.todo_title")
suspend fun getInfo() : List<TitleAndTodo>?
@Query("SELECT * FROM Title")
suspend fun getTitle() : List<Title>
....
//Room DB DELETE SQL
@Query("DELETE FROM Todo WHERE todo_title = :title AND todo = :todo")
suspend fun deleteTodo(title : String, todo : String)
@Query("DELETE FROM CalendarTodo WHERE calendar_title = :title AND todo = :todo AND todo_date = :date")
suspend fun deleteCalendarTodo(title : String, todo : String, date : String)
@Query("DELETE FROM Title WHERE title = :title")
suspend fun deleteTitle(title : String)
저는 Room DB를 Build를 진행하는 것을 Application에서 한 번 진행하여 Build가 반복되는 것을 방지하도록 하였습니다.
class GlobalApplication : Application() {
companion object{
....
lateinit var databaseInstance : DB
private set
}
override fun onCreate() {
super.onCreate()
...
databaseInstance = Room.databaseBuilder(
appInstance.applicationContext,
DB::class.java,
"DB"
)
.fallbackToDestructiveMigration()
.allowMainThreadQueries()
.build()
}
}
Dao에 저장된 내용들을 Room에 실행하여 데이터를 가져오거나 저장하는 것을 수행하는 Repository
class Repo{
private val dbInstance = GlobalApplication.databaseInstance.dao()
//Insert Call
suspend fun titleInsert(title: Title){
dbInstance.titleAdd(title)
}
suspend fun todoInsert(title : String, todo : String, success : Boolean ){
dbInstance.todoAdd(title,todo,success)
}
...
//Update Call
suspend fun todoSuccessUpdate(title : String, todo : String, success: Boolean){
dbInstance.todoSuccessUpdate(title, todo, success)
}
suspend fun calendarTodoSuccessUpdate(title : String, todo: String, success: Boolean, date: String){
dbInstance.calendarTodoSuccessUpdate(title, todo, success, date)
}
...
//Delete Call
suspend fun deleteTodo(todo :String, title : String){
dbInstance.deleteTodo(title, todo)
}
suspend fun deleteCalendarTodo(todo:String, title : String, date: String){
dbInstance.deleteCalendarTodo(title, todo, date)
}
...
//Select Call
suspend fun getInfo() = dbInstance.getInfo()
suspend fun getTitleCount() = dbInstance.getTitleCount()
E-R Diagram
댓글