이번엔 앱을 만들면서 미리쓸 DAO를 만들 예정입니다
JPA에서의 Repository생각하면서 만들니 나름 편했습니다.
@Dao
interface MemoDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertMemo(memo: Memo)
@Update
suspend fun updateMemo(memo: Memo)
@Delete
suspend fun deleteMemo(memo: Memo)
@Query("SELECT * FROM memo ORDER BY createdAt DESC")
fun getAllMemos(): Flow<List<Memo>>
@Query("SELECT * FROM memo WHERE memoId = :id")
suspend fun getMemoById(id: Int): Memo?
}
해당 문법은 데이터 삽입(@Insert)시, 이미 동일한 기본 키(Primary Key)를 가진 데이터가 존재할 경우 발생하는 충돌을 처리하는 전략 입니다.
IGNORE: 충돌 발시 무시하고 아무 작업도 하지 않습니다.ABORT: (기본값) 충돌이 발생하면 트랜잭션을 중단하고 예외를 발생시킵니다.FAIL: 충돌이 발생하면 작업을 시패로 처리되지만 트랜잭션은 유지됩니다.REPLACE: 충돌 시 데이터를 교체합니다.ROLLBACK: 충돌 발생시 트랜잭션을 롤백합니다.@Dao
interface MemoCategotyDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertCategory(category: MemoCategory)
@Update
suspend fun updateCategory(category: MemoCategory)
@Delete
suspend fun deleteCategory(category: MemoCategory)
@Query("SELECT * FROM memo_category ORDER BY name ASC")
fun getAllCategories(): Flow<List<MemoCategory>>
}
@Dao
interface CartListDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertCartList(cartList: CartList)
@Update
suspend fun updateCartList(cartList: CartList)
@Delete
suspend fun deleteCartList(cartList: CartList)
@Query("SELECT * FROM cart_list WHERE cartId = :cartId ORDER BY cartListId ASC")
fun getCartListsByCartId(cartId: Int): Flow<List<CartList>>
}
@Dao
interface CartDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertCart(cart: Cart)
@Update
suspend fun updateCart(cart: Cart)
@Delete
suspend fun deleteCart(cart: Cart)
@Query("SELECT * FROM cart ORDER BY startDate DESC")
fun getALLCarts(): Flow<List<Cart>>
}
@Dao
interface ToDoListDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertToDo(toDo: ToDoList)
@Update
suspend fun updateToDo(toDo: ToDoList)
@Delete
suspend fun deleteToDo(toDo: ToDoList)
@Query("SELECT * FROM to_do_list ORDER BY priority ASC, createdAt DESC")
fun getAllToDos(): Flow<List<ToDoList>>
@Query("SELECT * FROM to_do_list WHERE isCompleted = :isCompleted ORDER BY priority ASC")
fun getToDosByCompletionStatus(isCompleted: Boolean): Flow<List<ToDoList>>
}
해당 DAO들 작업 요약