Skip to main content

Database

Struct Database 

Source
pub struct Database { /* private fields */ }
Expand description

数据库连接封装

使用 Mutex 包装 Connection 以支持在多线程环境(如 Tauri State)中共享。 rusqlite::Connection 本身不是 Sync 的,因此需要这层包装。

Implementations§

Source§

impl Database

Source

pub fn export_sql_string(&self) -> Result<String, AppError>

导出为 SQL 字符串(内存操作,不写文件)

Source

pub fn export_sql_string_for_sync(&self) -> Result<String, AppError>

Source

pub fn export_sql(&self, target_path: &Path) -> Result<(), AppError>

导出为 SQLite 兼容的 SQL 文本文件

Source

pub fn import_sql_string(&self, sql_raw: &str) -> Result<String, AppError>

从 SQL 字符串导入,返回生成的备份 ID(若无备份则为空字符串)

Source

pub fn import_sql(&self, source_path: &Path) -> Result<String, AppError>

从 SQL 文件导入,返回生成的备份 ID(若无备份则为空字符串)

Source§

impl Database

Source

pub fn get_failover_queue( &self, app_type: &str, ) -> Result<Vec<FailoverQueueItem>, AppError>

获取故障转移队列(按 sort_index 排序)

Source

pub fn get_failover_providers( &self, app_type: &str, ) -> Result<Vec<Provider>, AppError>

获取故障转移队列中的供应商(完整 Provider 信息,按顺序)

Source

pub fn add_to_failover_queue( &self, app_type: &str, provider_id: &str, ) -> Result<(), AppError>

添加供应商到故障转移队列

Source

pub fn remove_from_failover_queue( &self, app_type: &str, provider_id: &str, ) -> Result<(), AppError>

从故障转移队列中移除供应商

Source

pub fn clear_failover_queue(&self, app_type: &str) -> Result<(), AppError>

清空故障转移队列

Source

pub fn is_in_failover_queue( &self, app_type: &str, provider_id: &str, ) -> Result<bool, AppError>

检查供应商是否在故障转移队列中

Source

pub fn get_available_providers_for_failover( &self, app_type: &str, ) -> Result<Vec<Provider>, AppError>

获取可添加到故障转移队列的供应商(不在队列中的)

Source§

impl Database

Source

pub fn get_all_mcp_servers( &self, ) -> Result<IndexMap<String, McpServer>, AppError>

获取所有 MCP 服务器

Source

pub fn save_mcp_server(&self, server: &McpServer) -> Result<(), AppError>

保存 MCP 服务器

Source

pub fn delete_mcp_server(&self, id: &str) -> Result<(), AppError>

删除 MCP 服务器

Source§

impl Database

Source

pub fn get_prompts( &self, app_type: &str, ) -> Result<IndexMap<String, Prompt>, AppError>

获取指定应用类型的所有提示词

Source

pub fn save_prompt( &self, app_type: &str, prompt: &Prompt, ) -> Result<(), AppError>

保存提示词

Source

pub fn delete_prompt(&self, app_type: &str, id: &str) -> Result<(), AppError>

删除提示词

Source§

impl Database

Source

pub fn get_all_providers( &self, app_type: &str, ) -> Result<IndexMap<String, Provider>, AppError>

获取指定应用类型的所有供应商

Source

pub fn get_current_provider( &self, app_type: &str, ) -> Result<Option<String>, AppError>

获取当前激活的供应商 ID

Source

pub fn get_provider_by_id( &self, id: &str, app_type: &str, ) -> Result<Option<Provider>, AppError>

根据 ID 获取单个供应商

Source

pub fn get_provider_ids( &self, app_type: &str, ) -> Result<HashSet<String>, AppError>

仅获取指定 app 下所有 provider 的 id 集合。

Source

pub fn has_non_official_seed_provider( &self, app_type: &str, ) -> Result<bool, AppError>

判断指定 app 下是否存在非官方种子的供应商。

Source

pub fn init_default_official_providers(&self) -> Result<usize, AppError>

启动时补齐上游官方预设供应商(Claude / Codex / Gemini)。

Source

pub fn save_provider( &self, app_type: &str, provider: &Provider, ) -> Result<(), AppError>

保存供应商(新增或更新)

注意:更新模式下不同步 endpoints,因为编辑模式下端点通过单独的 API 管理 (add_custom_endpoint / remove_custom_endpoint),避免覆盖用户的修改。

Source

pub fn delete_provider(&self, app_type: &str, id: &str) -> Result<(), AppError>

删除供应商

Source

pub fn set_current_provider( &self, app_type: &str, id: &str, ) -> Result<(), AppError>

设置当前供应商

Source

pub fn update_provider_settings_config( &self, app_type: &str, provider_id: &str, settings_config: &Value, ) -> Result<(), AppError>

更新供应商的 settings_config(仅更新配置,不改变其他字段)

Source

pub fn add_custom_endpoint( &self, app_type: &str, provider_id: &str, url: &str, ) -> Result<(), AppError>

添加自定义端点

Source

pub fn remove_custom_endpoint( &self, app_type: &str, provider_id: &str, url: &str, ) -> Result<(), AppError>

移除自定义端点

Source§

impl Database

Source

pub async fn get_global_proxy_config( &self, ) -> Result<GlobalProxyConfig, AppError>

获取全局代理配置(统一字段)

从 claude 行读取(三行镜像一致)

Source

pub async fn update_global_proxy_config( &self, config: GlobalProxyConfig, ) -> Result<(), AppError>

更新全局代理配置(镜像写三行)

Source

pub async fn get_default_cost_multiplier( &self, app_type: &str, ) -> Result<String, AppError>

获取默认成本倍率

Source

pub async fn set_default_cost_multiplier( &self, app_type: &str, value: &str, ) -> Result<(), AppError>

设置默认成本倍率

Source

pub async fn get_pricing_model_source( &self, app_type: &str, ) -> Result<String, AppError>

获取计费模式来源

Source

pub async fn set_pricing_model_source( &self, app_type: &str, value: &str, ) -> Result<(), AppError>

设置计费模式来源

Source

pub async fn get_proxy_config_for_app( &self, app_type: &str, ) -> Result<AppProxyConfig, AppError>

获取应用级代理配置

Source

pub async fn update_proxy_config_for_app( &self, config: AppProxyConfig, ) -> Result<(), AppError>

更新应用级代理配置

Source

pub async fn get_proxy_config(&self) -> Result<ProxyConfig, AppError>

获取代理配置(兼容旧接口,返回 claude 行的配置)

Source

pub async fn update_proxy_config( &self, config: ProxyConfig, ) -> Result<(), AppError>

更新代理配置(兼容旧接口,更新所有三行的公共字段)

Source

pub async fn set_live_takeover_active( &self, _active: bool, ) -> Result<(), AppError>

设置 Live 接管状态(兼容旧版本,更新 enabled 字段)

Source

pub async fn is_live_takeover_active(&self) -> Result<bool, AppError>

检查是否处于 Live 接管模式

检查是否有任一 app 的 enabled = true

Source

pub async fn get_provider_health( &self, provider_id: &str, app_type: &str, ) -> Result<ProviderHealth, AppError>

获取Provider健康状态

Source

pub async fn update_provider_health( &self, provider_id: &str, app_type: &str, success: bool, error_msg: Option<String>, ) -> Result<(), AppError>

更新Provider健康状态

使用默认阈值(5)判断是否健康,建议使用 update_provider_health_with_threshold 传入配置的阈值

Source

pub async fn update_provider_health_with_threshold( &self, provider_id: &str, app_type: &str, success: bool, error_msg: Option<String>, failure_threshold: u32, ) -> Result<(), AppError>

更新Provider健康状态(带阈值参数)

§Arguments
  • failure_threshold - 连续失败多少次后标记为不健康
Source

pub async fn reset_provider_health( &self, provider_id: &str, app_type: &str, ) -> Result<(), AppError>

重置Provider健康状态

Source

pub async fn clear_provider_health_for_app( &self, app_type: &str, ) -> Result<(), AppError>

清空指定应用的健康状态(关闭单个代理时使用)

Source

pub async fn clear_all_provider_health(&self) -> Result<(), AppError>

清空所有Provider健康状态(代理停止时调用)

Source

pub async fn get_circuit_breaker_config( &self, ) -> Result<CircuitBreakerConfig, AppError>

获取熔断器配置(兼容旧接口,从 claude 行读取)

熔断器配置已合并到 proxy_config 表,每 app 独立 此方法保留用于兼容旧代码,建议使用 get_proxy_config_for_app

Source

pub async fn update_circuit_breaker_config( &self, config: &CircuitBreakerConfig, ) -> Result<(), AppError>

更新熔断器配置(兼容旧接口,更新所有三行)

熔断器配置已合并到 proxy_config 表 此方法保留用于兼容旧代码,建议使用 update_proxy_config_for_app

Source

pub async fn save_live_backup( &self, app_type: &str, config_json: &str, ) -> Result<(), AppError>

保存 Live 配置备份

Source

pub async fn has_any_live_backup(&self) -> Result<bool, AppError>

检查是否存在任意 Live 配置备份

Source

pub async fn get_live_backup( &self, app_type: &str, ) -> Result<Option<LiveBackup>, AppError>

获取 Live 配置备份

Source

pub async fn delete_live_backup(&self, app_type: &str) -> Result<(), AppError>

删除 Live 配置备份

Source

pub async fn delete_all_live_backups(&self) -> Result<(), AppError>

删除所有 Live 配置备份

Source

pub fn get_proxy_flags_sync(&self, app_type: &str) -> (bool, bool)

同步获取应用的 proxy 启用状态和自动故障转移状态

用于托盘菜单构建等同步场景 返回 (enabled, auto_failover_enabled)

Source

pub fn set_proxy_flags_sync( &self, app_type: &str, enabled: bool, auto_failover_enabled: bool, ) -> Result<(), AppError>

同步设置应用的 proxy 启用状态和自动故障转移状态

用于托盘菜单点击等同步场景

Source§

impl Database

Source

pub fn get_setting(&self, key: &str) -> Result<Option<String>, AppError>

获取设置值

Source

pub fn set_setting(&self, key: &str, value: &str) -> Result<(), AppError>

设置值

Source

pub fn get_bool_flag(&self, key: &str) -> Result<bool, AppError>

获取一次性布尔 flag。兼容历史写法 "1"

Source

pub fn delete_setting(&self, key: &str) -> Result<(), AppError>

删除设置值

Source

pub fn get_config_snippet( &self, app_type: &str, ) -> Result<Option<String>, AppError>

获取通用配置片段

Source

pub fn set_config_snippet( &self, app_type: &str, snippet: Option<String>, ) -> Result<(), AppError>

设置通用配置片段

Source

pub fn get_global_proxy_url(&self) -> Result<Option<String>, AppError>

获取全局出站代理 URL

返回 None 表示未配置或已清除代理(直连) 返回 Some(url) 表示已配置代理

Source

pub fn set_global_proxy_url(&self, url: Option<&str>) -> Result<(), AppError>

设置全局出站代理 URL

  • 传入非空字符串:启用代理
  • 传入空字符串或 None:清除代理设置(直连)
Source

pub fn get_proxy_takeover_enabled( &self, app_type: &str, ) -> Result<bool, AppError>

👎Deprecated since 3.9.0:

使用 get_proxy_config_for_app().enabled 替代

获取指定应用的代理接管状态

已废弃: 请使用 proxy_config.enabled 字段替代 此方法仅用于数据库迁移时读取旧数据

Source

pub fn set_proxy_takeover_enabled( &self, app_type: &str, enabled: bool, ) -> Result<(), AppError>

👎Deprecated since 3.9.0:

使用 update_proxy_config_for_app() 修改 enabled 字段

设置指定应用的代理接管状态

已废弃: 请使用 proxy_config.enabled 字段替代

Source

pub fn has_any_proxy_takeover(&self) -> Result<bool, AppError>

👎Deprecated since 3.9.0:

使用 is_live_takeover_active() 替代

检查是否有任一应用开启了代理接管

已废弃: 请使用 is_live_takeover_active() 替代

Source

pub fn clear_all_proxy_takeover(&self) -> Result<(), AppError>

👎Deprecated since 3.9.0:

使用 update_proxy_config_for_app() 清除各应用的 enabled 字段

清除所有代理接管状态(将所有 proxy_takeover_* 设置为 false)

已废弃: settings 表不再用于存储代理状态

Source

pub fn get_rectifier_config(&self) -> Result<RectifierConfig, AppError>

获取整流器配置

返回整流器配置,如果不存在则返回默认值(全部启用)

Source

pub fn set_rectifier_config( &self, config: &RectifierConfig, ) -> Result<(), AppError>

更新整流器配置

Source

pub fn get_optimizer_config(&self) -> Result<OptimizerConfig, AppError>

Source

pub fn set_optimizer_config( &self, config: &OptimizerConfig, ) -> Result<(), AppError>

Source

pub fn get_log_config(&self) -> Result<LogConfig, AppError>

获取日志配置

Source

pub fn set_log_config(&self, config: &LogConfig) -> Result<(), AppError>

更新日志配置

Source§

impl Database

Source

pub fn get_all_installed_skills( &self, ) -> Result<IndexMap<String, InstalledSkill>, AppError>

获取所有已安装的 Skills

Source

pub fn get_installed_skill( &self, id: &str, ) -> Result<Option<InstalledSkill>, AppError>

获取单个已安装的 Skill

Source

pub fn save_skill(&self, skill: &InstalledSkill) -> Result<(), AppError>

保存 Skill(添加或更新)

Source

pub fn delete_skill(&self, id: &str) -> Result<bool, AppError>

删除 Skill

Source

pub fn clear_skills(&self) -> Result<(), AppError>

清空所有 Skills(用于迁移)

Source

pub fn update_skill_apps( &self, id: &str, apps: &SkillApps, ) -> Result<bool, AppError>

更新 Skill 的应用启用状态

Source

pub fn get_skill_repos(&self) -> Result<Vec<SkillRepo>, AppError>

获取所有 Skill 仓库

Source

pub fn save_skill_repo(&self, repo: &SkillRepo) -> Result<(), AppError>

保存 Skill 仓库

Source

pub fn delete_skill_repo(&self, owner: &str, name: &str) -> Result<(), AppError>

删除 Skill 仓库

Source

pub fn init_default_skill_repos(&self) -> Result<usize, AppError>

初始化默认的 Skill 仓库(启动时调用,补充缺失的默认仓库)

Source§

impl Database

Source

pub fn save_stream_check_log( &self, provider_id: &str, provider_name: &str, app_type: &str, result: &StreamCheckResult, ) -> Result<i64, AppError>

保存流式检查日志

Source

pub fn get_stream_check_config(&self) -> Result<StreamCheckConfig, AppError>

获取流式检查配置

Source

pub fn save_stream_check_config( &self, config: &StreamCheckConfig, ) -> Result<(), AppError>

保存流式检查配置

Source§

impl Database

Source

pub fn migrate_from_json(&self, config: &MultiAppConfig) -> Result<(), AppError>

从 MultiAppConfig 迁移数据到数据库

Source

pub fn migrate_from_json_dry_run( config: &MultiAppConfig, ) -> Result<(), AppError>

运行迁移的 dry-run 模式(在内存数据库中验证,不写入磁盘)

用于部署前验证迁移逻辑是否正确。

Source§

impl Database

Source

pub fn ensure_model_pricing_seeded(&self) -> Result<(), AppError>

确保模型定价表具备默认数据

Source§

impl Database

Source

pub fn init() -> Result<Self, AppError>

初始化数据库连接并创建表

数据库文件位于 ~/.cc-switch-tui/cc-switch.db

Source

pub fn memory() -> Result<Self, AppError>

创建内存数据库(用于测试)

Source

pub fn is_mcp_table_empty(&self) -> Result<bool, AppError>

检查 MCP 服务器表是否为空

Source

pub fn is_prompts_table_empty(&self) -> Result<bool, AppError>

检查提示词表是否为空

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ParallelSend for T

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ThreadSafetyMarker for T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more