Compare commits
1 commit
ophelia_ma
...
azea/Room_
Author | SHA1 | Date | |
---|---|---|---|
e3516a07cc |
10 changed files with 33 additions and 191 deletions
|
@ -0,0 +1,13 @@
|
|||
package com.menagerie.ophelia.database
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "candidate_table")
|
||||
data class Candidate(
|
||||
@PrimaryKey(autoGenerate = true) val id: Int,
|
||||
@ColumnInfo (name = "name") val word: String
|
||||
){
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.menagerie.ophelia.database
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface CandidateDao {
|
||||
|
||||
@Query("SELECT * FROM candidate_table ORDER BY name ASC")
|
||||
fun getAlphabetisedList(): Flow<List<Candidate>>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||
suspend fun insert(candidate: Candidate)
|
||||
|
||||
@Query("DELETE FROM candidate_table")
|
||||
suspend fun deleteAll()
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
package com.menagerie.ophelia.database.polycule
|
||||
|
||||
import android.util.LruCache
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
|
||||
abstract class CachedDao<K, V> (capacity: Int = 100) {
|
||||
|
||||
private val cache: LruCache<K, V> = LruCache(capacity)
|
||||
|
||||
private val cacheMutex: Mutex = Mutex()
|
||||
|
||||
protected suspend fun <R> withDaoCache(cacheBlock: suspend LruCache<K, V>.()->R): R {
|
||||
return cacheMutex.withLock {
|
||||
cacheBlock(cache)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
package com.menagerie.ophelia.database.polycule
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import com.menagerie.ophelia.database.polycule.entity.Bio
|
||||
import com.menagerie.ophelia.database.polycule.entity.BioDao
|
||||
|
||||
|
||||
@Database(
|
||||
version = 1,
|
||||
exportSchema = false,
|
||||
entities = [
|
||||
Bio::class
|
||||
],
|
||||
)
|
||||
abstract class PolyculeDatabase : RoomDatabase() {
|
||||
|
||||
abstract fun bioDao(): BioDao
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var INSTANCE: PolyculeDatabase? = null
|
||||
|
||||
fun getDatabase(context: Context): PolyculeDatabase {
|
||||
return INSTANCE ?: synchronized(this)
|
||||
{
|
||||
val instance = Room.databaseBuilder(
|
||||
context.applicationContext,
|
||||
PolyculeDatabase::class.java,
|
||||
"polycule.db"
|
||||
)
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
INSTANCE = instance
|
||||
instance
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
package com.menagerie.ophelia.database.polycule
|
||||
|
||||
import com.menagerie.ophelia.database.polycule.entity.Bio
|
||||
|
||||
object PolyculeDatabaseManager {
|
||||
|
||||
lateinit var polyculeRepository : PolyculeRepository
|
||||
|
||||
suspend fun post(bio: Bio) {
|
||||
polyculeRepository.upsertBio(bio)
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.menagerie.ophelia.database.polycule
|
||||
|
||||
import com.menagerie.ophelia.database.polycule.entity.Bio
|
||||
import com.menagerie.ophelia.database.polycule.entity.BioDao
|
||||
|
||||
|
||||
class PolyculeRepository(
|
||||
private val bioDao: BioDao
|
||||
) {
|
||||
|
||||
suspend fun upsertBio(bio: Bio): Long {
|
||||
return bioDao.findOrInsert(bio)
|
||||
}
|
||||
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package com.menagerie.ophelia.database.polycule.entity
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = Identity::class,
|
||||
parentColumns = ["id"],
|
||||
childColumns = ["uniqueId"]
|
||||
)
|
||||
]
|
||||
)
|
||||
data class Bio(
|
||||
@PrimaryKey(autoGenerate = true) val id: Long,
|
||||
/*FK*/ val uniqueId: Long
|
||||
)
|
|
@ -1,45 +0,0 @@
|
|||
package com.menagerie.ophelia.database.polycule.entity
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import androidx.room.Transaction
|
||||
import androidx.room.Update
|
||||
import com.menagerie.ophelia.database.polycule.CachedDao
|
||||
|
||||
@Dao
|
||||
abstract class BioDao : CachedDao<Long, Bio>() {
|
||||
|
||||
@Insert
|
||||
abstract suspend fun insert(newBio: Bio): Long
|
||||
|
||||
@Update
|
||||
abstract suspend fun update(existingBio: Bio)
|
||||
|
||||
@Query("SELECT * FROM Bio WHERE uniqueId = :uniqueId")
|
||||
abstract suspend fun findByUniqueId(uniqueId: Long): Bio?
|
||||
|
||||
@Transaction
|
||||
open suspend fun findOrInsert(bio: Bio): Long {
|
||||
return withDaoCache {
|
||||
val key = bio.uniqueId
|
||||
val returnVal: Bio =
|
||||
get(key)
|
||||
|
||||
?: findByUniqueId(key)
|
||||
?: run {
|
||||
val newId: Long = insert(bio)
|
||||
bio.copy(id = newId)
|
||||
}
|
||||
if (bio != returnVal) {
|
||||
if (returnVal.id == 0L) {
|
||||
insert(bio)
|
||||
} else {
|
||||
update(bio)
|
||||
}
|
||||
}
|
||||
put(key, returnVal)
|
||||
returnVal.id
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
package com.menagerie.ophelia.database.polycule.entity
|
||||
|
||||
import android.graphics.drawable.Icon
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(
|
||||
foreignKeys = [
|
||||
|
||||
],
|
||||
indices = [
|
||||
|
||||
]
|
||||
)
|
||||
class Identity (
|
||||
@PrimaryKey(autoGenerate = true) val id: Long = 0,
|
||||
val uniqueId: Long = 0,
|
||||
val name: String = "",
|
||||
val icon: Icon,
|
||||
)
|
|
@ -1,21 +0,0 @@
|
|||
package com.menagerie.ophelia.database.polycule.entity
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import androidx.room.Update
|
||||
import com.menagerie.ophelia.database.polycule.entity.Identity
|
||||
|
||||
@Dao
|
||||
abstract class IdentityDao {
|
||||
@Insert
|
||||
abstract suspend fun insert(identity: Identity): Long
|
||||
|
||||
@Update
|
||||
abstract suspend fun update(idenity: Identity)
|
||||
|
||||
@Query("SELECT * FROM Identity WHERE uniqueId = :uniqueId")
|
||||
abstract suspend fun findByIdentity(uniqueId : Long): Identity?
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue