|
| 1 | +//! 笔记手动排序数据层(REQ-287,v0.19.7)。 |
| 2 | +//! |
| 3 | +//! @ai-context: 交互矩阵裁决——组内位置移动=该组切"手动排序"(首次拖拽自动 |
| 4 | +//! 快照);与自动排序(更新时间/固定/创建)互斥,scope 级生效: |
| 5 | +//! `g:{groupId}`=具体组,`none`=未分组区。独立表免改 notes 列与 |
| 6 | +//! 全库行映射(35 表重建涟漪零承担);排序仅在树视图(无搜索/ |
| 7 | +//! 标签/非手动排序筛选)由前端消费(前端先拉全量再按 scope 排)。 |
| 8 | +//! 空序=未启用(清表即回自动);数据量 ≤2000/scope(命令层守卫)。 |
| 9 | +
|
| 10 | +use crate::db::Db; |
| 11 | +use crate::error::Result; |
| 12 | + |
| 13 | +/// scope 合法性(纯函数可单测):`g:` + 纯数字,或 `none`。 |
| 14 | +pub fn is_valid_scope(scope: &str) -> bool { |
| 15 | + if scope == "none" { |
| 16 | + return true; |
| 17 | + } |
| 18 | + scope |
| 19 | + .strip_prefix("g:") |
| 20 | + .is_some_and(|rest| !rest.is_empty() && rest.chars().all(|c| c.is_ascii_digit())) |
| 21 | +} |
| 22 | + |
| 23 | +impl Db { |
| 24 | + /// 全量拉取手动序(scope, note_id, ord 升序)——前端树视图消费(量小)。 |
| 25 | + pub fn load_note_orders(&self) -> Result<Vec<(String, i64, i64)>> { |
| 26 | + self.with_conn(|conn| { |
| 27 | + let mut stmt = |
| 28 | + conn.prepare("SELECT scope, note_id, ord FROM note_orders ORDER BY scope, ord")?; |
| 29 | + let rows = stmt.query_map([], |row| { |
| 30 | + Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?, row.get::<_, i64>(2)?)) |
| 31 | + })?; |
| 32 | + rows.collect::<rusqlite::Result<Vec<_>>>().map_err(Into::into) |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + /// 保存某 scope 的完整手动序(事务内先删后插=快照语义;ids 保序编号)。 |
| 37 | + /// |
| 38 | + /// @ai-context: 首次启用=前端把当前可见序全量写入;后续移动=前端重算后整 |
| 39 | + /// 表覆写(单命令单事务,无逐位移动的复杂度)。with_conn 仅给 |
| 40 | + /// &Connection 无法开事务(db_fragments 同款:直接锁 + tx)。 |
| 41 | + pub fn save_note_order(&self, scope: &str, note_ids: &[i64]) -> Result<()> { |
| 42 | + let mut conn = self |
| 43 | + .conn |
| 44 | + .lock() |
| 45 | + .unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 46 | + let tx = conn.transaction()?; |
| 47 | + tx.execute("DELETE FROM note_orders WHERE scope = ?1", [scope])?; |
| 48 | + { |
| 49 | + let mut stmt = |
| 50 | + tx.prepare("INSERT INTO note_orders (scope, note_id, ord) VALUES (?1, ?2, ?3)")?; |
| 51 | + for (i, id) in note_ids.iter().enumerate() { |
| 52 | + stmt.execute([scope, &id.to_string(), &(i as i64).to_string()])?; |
| 53 | + } |
| 54 | + } |
| 55 | + tx.commit()?; |
| 56 | + Ok(()) |
| 57 | + } |
| 58 | + |
| 59 | + /// 清除某 scope 手动序(=回自动排序;幂等——无记录时零动作)。 |
| 60 | + pub fn clear_note_order(&self, scope: &str) -> Result<bool> { |
| 61 | + self.with_conn(|conn| { |
| 62 | + let affected = conn.execute("DELETE FROM note_orders WHERE scope = ?1", [scope])?; |
| 63 | + Ok(affected > 0) |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[cfg(test)] |
| 69 | +mod tests { |
| 70 | + use super::is_valid_scope; |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn scope_validation() { |
| 74 | + assert!(is_valid_scope("g:3")); |
| 75 | + assert!(is_valid_scope("g:123456789")); |
| 76 | + assert!(is_valid_scope("none")); |
| 77 | + assert!(!is_valid_scope("")); |
| 78 | + assert!(!is_valid_scope("g:")); |
| 79 | + assert!(!is_valid_scope("G:1")); |
| 80 | + assert!(!is_valid_scope("g:1a")); |
| 81 | + assert!(!is_valid_scope("notes")); |
| 82 | + } |
| 83 | +} |
0 commit comments