/******************************************************************************
* *
* Copyright (C) 2024 dyhkwong *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see
. *
* *
******************************************************************************/
package io.nekohasekai.sagernet.database
import android.os.Parcelable
import androidx.room.*
import kotlinx.parcelize.Parcelize
@Entity(tableName = "assets")
@Parcelize
class AssetEntity(
@PrimaryKey(autoGenerate = true) var id: Long = 0L,
var url: String = "",
var name: String = ""
) : Parcelable {
@androidx.room.Dao
interface Dao {
@Query("SELECT * FROM assets")
fun getAll(): List
@Query("SELECT * FROM assets WHERE name = :name")
fun getAllByName(name: String): List
fun get(name: String): AssetEntity? {
val assets = getAllByName(name)
if (assets.isEmpty()) return null
return assets.last()
}
@Query("SELECT * FROM assets WHERE id = :id")
fun getById(id: Long): AssetEntity?
@Query("DELETE FROM assets WHERE name = :name")
fun delete(name: String): Int
@Insert
fun create0(asset: AssetEntity)
@Update
fun update0(asset: AssetEntity)
fun create(asset: AssetEntity) {
if (getAllByName(asset.name).isNotEmpty()) {
delete(asset.name)
}
create0(asset)
}
fun update(asset: AssetEntity) {
if (getById(asset.id) != null) {
update0(asset)
} else {
create(asset)
}
}
@Query("DELETE FROM assets")
fun reset()
@Insert
fun insert(assets: List)
}
}