mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-25 13:45:03 +08:00
c3f2f0798d
Split the 1788-line database.rs into a well-organized module directory: - mod.rs (142 lines): Database struct and initialization - schema.rs (341 lines): Table creation and schema migrations - backup.rs (324 lines): SQL import/export and snapshot backup - migration.rs (240 lines): JSON to SQLite data migration - tests.rs (280 lines): Unit tests - dao/providers.rs (254 lines): Provider CRUD operations - dao/mcp.rs (97 lines): MCP server CRUD operations - dao/prompts.rs (88 lines): Prompt CRUD operations - dao/skills.rs (124 lines): Skills CRUD operations - dao/settings.rs (65 lines): Settings key-value storage Benefits: - Max file size reduced from 1788 to 341 lines - Clear separation of concerns (schema, backup, migration, DAOs) - Easier to navigate and maintain - All 107 tests passing with zero API changes
89 lines
2.9 KiB
Rust
89 lines
2.9 KiB
Rust
//! 提示词数据访问对象
|
|
//!
|
|
//! 提供提示词(Prompt)的 CRUD 操作。
|
|
|
|
use crate::database::{lock_conn, Database};
|
|
use crate::error::AppError;
|
|
use crate::prompt::Prompt;
|
|
use indexmap::IndexMap;
|
|
use rusqlite::params;
|
|
|
|
impl Database {
|
|
/// 获取指定应用类型的所有提示词
|
|
pub fn get_prompts(&self, app_type: &str) -> Result<IndexMap<String, Prompt>, AppError> {
|
|
let conn = lock_conn!(self.conn);
|
|
let mut stmt = conn
|
|
.prepare(
|
|
"SELECT id, name, content, description, enabled, created_at, updated_at
|
|
FROM prompts WHERE app_type = ?1
|
|
ORDER BY created_at ASC, id ASC",
|
|
)
|
|
.map_err(|e| AppError::Database(e.to_string()))?;
|
|
|
|
let prompt_iter = stmt
|
|
.query_map(params![app_type], |row| {
|
|
let id: String = row.get(0)?;
|
|
let name: String = row.get(1)?;
|
|
let content: String = row.get(2)?;
|
|
let description: Option<String> = row.get(3)?;
|
|
let enabled: bool = row.get(4)?;
|
|
let created_at: Option<i64> = row.get(5)?;
|
|
let updated_at: Option<i64> = row.get(6)?;
|
|
|
|
Ok((
|
|
id.clone(),
|
|
Prompt {
|
|
id,
|
|
name,
|
|
content,
|
|
description,
|
|
enabled,
|
|
created_at,
|
|
updated_at,
|
|
},
|
|
))
|
|
})
|
|
.map_err(|e| AppError::Database(e.to_string()))?;
|
|
|
|
let mut prompts = IndexMap::new();
|
|
for prompt_res in prompt_iter {
|
|
let (id, prompt) = prompt_res.map_err(|e| AppError::Database(e.to_string()))?;
|
|
prompts.insert(id, prompt);
|
|
}
|
|
Ok(prompts)
|
|
}
|
|
|
|
/// 保存提示词
|
|
pub fn save_prompt(&self, app_type: &str, prompt: &Prompt) -> Result<(), AppError> {
|
|
let conn = lock_conn!(self.conn);
|
|
conn.execute(
|
|
"INSERT OR REPLACE INTO prompts (
|
|
id, app_type, name, content, description, enabled, created_at, updated_at
|
|
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
|
|
params![
|
|
prompt.id,
|
|
app_type,
|
|
prompt.name,
|
|
prompt.content,
|
|
prompt.description,
|
|
prompt.enabled,
|
|
prompt.created_at,
|
|
prompt.updated_at,
|
|
],
|
|
)
|
|
.map_err(|e| AppError::Database(e.to_string()))?;
|
|
Ok(())
|
|
}
|
|
|
|
/// 删除提示词
|
|
pub fn delete_prompt(&self, app_type: &str, id: &str) -> Result<(), AppError> {
|
|
let conn = lock_conn!(self.conn);
|
|
conn.execute(
|
|
"DELETE FROM prompts WHERE id = ?1 AND app_type = ?2",
|
|
params![id, app_type],
|
|
)
|
|
.map_err(|e| AppError::Database(e.to_string()))?;
|
|
Ok(())
|
|
}
|
|
}
|