DB: setup Room database
This commit is contained in:
parent
4046cd16fd
commit
5596d5203b
|
|
@ -0,0 +1,34 @@
|
|||
package com.example.llama.revamp.data.local
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
|
||||
/**
|
||||
* Main database for the application.
|
||||
*/
|
||||
@Database(entities = [SystemPromptEntity::class], version = 1, exportSchema = false)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
|
||||
abstract fun systemPromptDao(): SystemPromptDao
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var INSTANCE: AppDatabase? = null
|
||||
|
||||
fun getDatabase(context: Context): AppDatabase {
|
||||
return INSTANCE ?: synchronized(this) {
|
||||
val instance = Room.databaseBuilder(
|
||||
context.applicationContext,
|
||||
AppDatabase::class.java,
|
||||
"llama_app_database"
|
||||
)
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
INSTANCE = instance
|
||||
instance
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
package com.example.llama.revamp.data.local
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
/**
|
||||
* Data Access Object for System Prompts.
|
||||
*/
|
||||
@Dao
|
||||
interface SystemPromptDao {
|
||||
|
||||
@Query("SELECT * FROM system_prompts ORDER BY timestamp DESC")
|
||||
fun getAllPrompts(): Flow<List<SystemPromptEntity>>
|
||||
|
||||
@Query("SELECT * FROM system_prompts WHERE id = :id")
|
||||
suspend fun getPromptById(id: String): SystemPromptEntity?
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertPrompt(prompt: SystemPromptEntity)
|
||||
|
||||
@Delete
|
||||
suspend fun deletePrompt(prompt: SystemPromptEntity)
|
||||
|
||||
@Query("DELETE FROM system_prompts WHERE id = :id")
|
||||
suspend fun deletePromptById(id: String)
|
||||
|
||||
@Query("DELETE FROM system_prompts")
|
||||
suspend fun deleteAllPrompts()
|
||||
|
||||
@Query("SELECT COUNT(*) FROM system_prompts")
|
||||
suspend fun getPromptCount(): Int
|
||||
|
||||
// Get the most recent prompts, limited by count
|
||||
@Query("SELECT * FROM system_prompts ORDER BY timestamp DESC LIMIT :count")
|
||||
fun getRecentPrompts(count: Int): Flow<List<SystemPromptEntity>>
|
||||
|
||||
// Update the timestamp of an existing prompt to make it the most recent
|
||||
@Query("UPDATE system_prompts SET timestamp = :timestamp WHERE id = :id")
|
||||
suspend fun updatePromptTimestamp(id: String, timestamp: Long)
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
package com.example.llama.revamp.data.local
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import com.example.llama.revamp.data.model.SystemPrompt
|
||||
|
||||
/**
|
||||
* Database entity for storing system prompts.
|
||||
*/
|
||||
@Entity(tableName = "system_prompts")
|
||||
data class SystemPromptEntity(
|
||||
@PrimaryKey
|
||||
val id: String,
|
||||
val content: String,
|
||||
val name: String?,
|
||||
val timestamp: Long,
|
||||
val isPreset: Boolean
|
||||
) {
|
||||
/**
|
||||
* Convert to domain model.
|
||||
*/
|
||||
fun toDomainModel(): SystemPrompt {
|
||||
return if (isPreset) {
|
||||
SystemPrompt.Preset(
|
||||
id = id,
|
||||
content = content,
|
||||
name = name ?: "Unnamed Preset",
|
||||
timestamp = timestamp
|
||||
)
|
||||
} else {
|
||||
SystemPrompt.Custom(
|
||||
id = id,
|
||||
content = content,
|
||||
timestamp = timestamp
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Create an entity from a domain model.
|
||||
*/
|
||||
fun fromDomainModel(prompt: SystemPrompt): SystemPromptEntity {
|
||||
return when (prompt) {
|
||||
is SystemPrompt.Preset -> SystemPromptEntity(
|
||||
id = prompt.id,
|
||||
content = prompt.content,
|
||||
name = prompt.name,
|
||||
timestamp = prompt.timestamp ?: System.currentTimeMillis(),
|
||||
isPreset = true
|
||||
)
|
||||
is SystemPrompt.Custom -> SystemPromptEntity(
|
||||
id = prompt.id,
|
||||
content = prompt.content,
|
||||
name = null,
|
||||
timestamp = prompt.timestamp,
|
||||
isPreset = false
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue