desc:创建数据库
parent
76a09ce971
commit
5dbe3fef20
@ -0,0 +1,33 @@
|
|||||||
|
package com.yinuo.safetywatcher.watcher.db
|
||||||
|
|
||||||
|
import androidx.room.Database
|
||||||
|
import androidx.room.Room
|
||||||
|
import androidx.room.RoomDatabase
|
||||||
|
import com.common.commonlib.CommonApplication
|
||||||
|
import com.yinuo.safetywatcher.watcher.db.dao.GasDao
|
||||||
|
import com.yinuo.safetywatcher.watcher.db.dao.WarningDao
|
||||||
|
import com.yinuo.safetywatcher.watcher.db.entity.Gas
|
||||||
|
import com.yinuo.safetywatcher.watcher.db.entity.Warning
|
||||||
|
|
||||||
|
@Database(entities = [Warning::class, Gas::class], version = 1)
|
||||||
|
abstract class AppDatabase : RoomDatabase() {
|
||||||
|
abstract fun warningDao(): WarningDao
|
||||||
|
abstract fun gasDao(): GasDao
|
||||||
|
}
|
||||||
|
|
||||||
|
object DBUtils {
|
||||||
|
val db by lazy {
|
||||||
|
Room.databaseBuilder(
|
||||||
|
CommonApplication.getContext()!!,
|
||||||
|
AppDatabase::class.java, "watcher_db"
|
||||||
|
).build()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun warningDao(): WarningDao {
|
||||||
|
return db.warningDao()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun gasDao(): GasDao {
|
||||||
|
return db.gasDao()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.yinuo.safetywatcher.watcher.db.dao
|
||||||
|
|
||||||
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Delete
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.Query
|
||||||
|
import com.yinuo.safetywatcher.watcher.db.entity.Gas
|
||||||
|
import com.yinuo.safetywatcher.watcher.db.entity.Warning
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
interface GasDao {
|
||||||
|
@Query("SELECT * FROM gas")
|
||||||
|
suspend fun getAll(): List<Gas>
|
||||||
|
|
||||||
|
@Query("SELECT * FROM gas WHERE gas_name IS :name AND time BETWEEN :startTime AND :endTime")
|
||||||
|
suspend fun findByName(name: String, startTime: Long, endTime: Long): List<Warning>
|
||||||
|
|
||||||
|
@Insert
|
||||||
|
suspend fun insertAll(vararg gases: Gas)
|
||||||
|
|
||||||
|
@Insert
|
||||||
|
suspend fun insert(gas: Gas)
|
||||||
|
|
||||||
|
@Delete
|
||||||
|
suspend fun delete(gas: Gas)
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.yinuo.safetywatcher.watcher.db.dao
|
||||||
|
|
||||||
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Delete
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.Query
|
||||||
|
import com.yinuo.safetywatcher.watcher.db.entity.Warning
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
interface WarningDao {
|
||||||
|
@Query("SELECT * FROM warning")
|
||||||
|
suspend fun getAll(): List<Warning>
|
||||||
|
|
||||||
|
@Query("SELECT * FROM warning WHERE gas_name IS :name AND start_time BETWEEN :startTime AND :endTime")
|
||||||
|
suspend fun findByName(name: String, startTime: Long, endTime: Long): List<Warning>
|
||||||
|
|
||||||
|
@Insert
|
||||||
|
suspend fun insertAll(vararg warnings: Warning)
|
||||||
|
|
||||||
|
@Insert
|
||||||
|
suspend fun insert(warning: Warning)
|
||||||
|
|
||||||
|
@Delete
|
||||||
|
suspend fun delete(warning: Warning)
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.yinuo.safetywatcher.watcher.db.entity
|
||||||
|
|
||||||
|
import androidx.room.ColumnInfo
|
||||||
|
import androidx.room.Entity
|
||||||
|
import androidx.room.PrimaryKey
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
data class Gas(
|
||||||
|
@PrimaryKey val id: Int,
|
||||||
|
@ColumnInfo(name = "time") val time: Long,
|
||||||
|
@ColumnInfo(name = "gas_name") val gasName: String,
|
||||||
|
@ColumnInfo(name = "gas_value") val gasValue: Double,
|
||||||
|
@ColumnInfo(name = "unit") val unit: String,
|
||||||
|
@ColumnInfo(name = "threshold_low") val thresholdLow: Double,
|
||||||
|
@ColumnInfo(name = "threshold_high") val thresholdHigh: Double,
|
||||||
|
)
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.yinuo.safetywatcher.watcher.db.entity
|
||||||
|
|
||||||
|
import androidx.room.ColumnInfo
|
||||||
|
import androidx.room.Entity
|
||||||
|
import androidx.room.PrimaryKey
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
data class Warning(
|
||||||
|
@PrimaryKey val id: Int,
|
||||||
|
@ColumnInfo(name = "gas_name") val gasName: String,
|
||||||
|
@ColumnInfo(name = "gas_value") val gasValue: Double,
|
||||||
|
@ColumnInfo(name = "unit") val unit: String,
|
||||||
|
@ColumnInfo(name = "threshold_low") val thresholdLow: Double,
|
||||||
|
@ColumnInfo(name = "threshold_high") val thresholdHigh: Double,
|
||||||
|
@ColumnInfo(name = "start_time") val startTime: Long,
|
||||||
|
@ColumnInfo(name = "end_time") val endTime: Long,
|
||||||
|
)
|
Loading…
Reference in New Issue