//! SQLite helpers for `# db` CRUD and W2 data-layer capabilities. //! //! - **连接池**:进程级 `Pool` 复用连接(WAL + busy_timeout + foreign_keys), //! 避免每请求重连与并发写 `database is locked`。 //! - **事务**:`begin` 把连接从池中借出并独占,`commit` / `rollback` 后归还。 //! - **查询表达力**:where 支持 OR 组、`in` / `between` / `is null`。 //! - **分页**:`select` 支持 `offset`,返回 `{rows, total}`。 use rusqlite::{types::ValueRef, Connection, OptionalExtension, params_from_iter}; use serde_json::{json, Map, Value}; use std::collections::HashMap; use std::path::PathBuf; use std::sync::{Arc, Mutex}; /// Process-wide pool: url → shared connection. Connections are reused across /// calls instead of reopening per request. static POOL: Mutex>>>> = Mutex::new(None); /// Process-wide active transactions: txn id → (url, borrowed connection). static TXNS: Mutex>)>>> = Mutex::new(None); /// Reset pools (used between listens / tests). #[allow(dead_code)] pub fn reset_pool() { let mut p = POOL.lock().unwrap_or_else(|e| e.into_inner()); *p = Some(HashMap::new()); let mut t = TXNS.lock().unwrap_or_else(|e| e.into_inner()); *t = Some(HashMap::new()); } fn next_txn_id() -> String { use std::time::{SystemTime, UNIX_EPOCH}; let nanos = SystemTime::now() .duration_since(UNIX_EPOCH) .map(|d| d.subsec_nanos()) .unwrap_or(0); let pid = std::process::id(); static COUNTER: Mutex = Mutex::new(0x243f_6a88_85a3_08d3); let mut c = COUNTER.lock().unwrap_or_else(|e| e.into_inner()); let mut x = *c; x ^= x << 13; x ^= x >> 7; x ^= x << 17; *c = x; format!("txn-{pid:x}{nanos:x}{x:x}") } /// Open (or reuse) a pooled connection. WAL + busy_timeout + foreign_keys are /// applied once per physical connection. fn pooled(url: &str) -> Result>, String> { let mut guard = POOL.lock().unwrap_or_else(|e| e.into_inner()); let pool = guard.get_or_insert_with(HashMap::new); if let Some(c) = pool.get(url) { return Ok(c.clone()); } let conn = open_conn(url)?; let arc = Arc::new(Mutex::new(conn)); pool.insert(url.to_string(), arc.clone()); Ok(arc) } /// Open a fresh physical connection to `url`, creating parent dirs as needed. fn open_conn(url: &str) -> Result { let path = url .strip_prefix("sqlite:") .or_else(|| url.strip_prefix("SQLITE:")) .unwrap_or(url); if let Some(parent) = PathBuf::from(path).parent() { if !parent.as_os_str().is_empty() { std::fs::create_dir_all(parent).map_err(|e| e.to_string())?; } } let conn = Connection::open(path).map_err(|e| e.to_string())?; conn.execute_batch( "PRAGMA foreign_keys = ON; PRAGMA journal_mode = WAL; PRAGMA busy_timeout = 5000;", ) .map_err(|e| e.to_string())?; Ok(conn) } /// Borrow a connection for a single statement. If `txn_id` is set, the txn's /// borrowed connection is used (transactional reads/writes); otherwise the pool. fn conn_for<'a>( url: &str, txn_id: Option<&'a str>, ) -> Result>, String> { if let Some(tid) = txn_id { let guard = TXNS.lock().unwrap_or_else(|e| e.into_inner()); match guard.as_ref().and_then(|m| m.get(tid)) { Some((_, c)) => Ok(c.clone()), None => Err(format!("unknown transaction `{tid}`")), } } else { pooled(url) } } fn ident(s: &str) -> Result<&str, String> { let s = s.trim(); if s.is_empty() || !s .chars() .all(|c| c.is_ascii_alphanumeric() || c == '_') { return Err(format!("bad identifier `{s}`")); } Ok(s) } fn sql_type(t: &str) -> &'static str { match t.trim().to_ascii_lowercase().as_str() { "int" | "integer" => "INTEGER", "real" | "float" | "double" => "REAL", "blob" => "BLOB", "timestamp" | "datetime" | "timestamptz" | "审计" => "TEXT", _ => "TEXT", } } /// Parse `posts.id` / `posts(id)` / `posts` → (`posts`, `id`). fn parse_fk(raw: &str) -> Result<(String, String), String> { let s = raw.trim(); if s.is_empty() { return Err("empty foreign key".into()); } if let Some((t, rest)) = s.split_once('(') { let col = rest.trim().trim_end_matches(')').trim(); let t = ident(t)?.to_string(); let col = if col.is_empty() { "id".into() } else { ident(col)?.to_string() }; return Ok((t, col)); } if let Some((t, c)) = s.split_once('.') { return Ok((ident(t)?.to_string(), ident(c)?.to_string())); } Ok((ident(s)?.to_string(), "id".into())) } fn utc_now_iso() -> String { use std::time::{SystemTime, UNIX_EPOCH}; let secs = SystemTime::now() .duration_since(UNIX_EPOCH) .map(|d| d.as_secs()) .unwrap_or(0); // Keep it SQL-friendly ISO without extra deps. let days = secs / 86400; let tod = secs % 86400; let h = tod / 3600; let m = (tod % 3600) / 60; let s = tod % 60; // Civil date from days since 1970-01-01 (Howard Hinnant algorithm). let z = days as i64 + 719468; let era = if z >= 0 { z } else { z - 146096 } / 146097; let doe = (z - era * 146097) as u64; let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; let y = yoe as i64 + era * 400; let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); let mp = (5 * doy + 2) / 153; let d = doy - (153 * mp + 2) / 5 + 1; let mo = if mp < 10 { mp + 3 } else { mp - 9 }; let y = if mo <= 2 { y + 1 } else { y }; format!("{y:04}-{mo:02}-{d:02}T{h:02}:{m:02}:{s:02}Z") } /// Column names present on `table` (SQLite). fn column_names(url: &str, table: &str, txn_id: Option<&str>) -> Result, String> { let table = ident(table)?; let conn = conn_for(url, txn_id)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let sql = format!("PRAGMA table_info(\"{table}\")"); let mut stmt = c.prepare(&sql).map_err(|e| e.to_string())?; let rows = stmt .query_map([], |row| row.get::<_, String>(1)) .map_err(|e| e.to_string())?; let mut out = Vec::new(); for r in rows { out.push(r.map_err(|e| e.to_string())?); } Ok(out) } fn cell_str(v: &Value) -> String { match v { Value::Null => String::new(), Value::String(s) => s.clone(), Value::Bool(b) => b.to_string(), Value::Number(n) => n.to_string(), other => other.to_string(), } } fn to_sql(v: &Value) -> rusqlite::types::Value { match v { Value::Null => rusqlite::types::Value::Null, Value::Bool(b) => rusqlite::types::Value::Integer(if *b { 1 } else { 0 }), Value::Number(n) => { if let Some(i) = n.as_i64() { rusqlite::types::Value::Integer(i) } else if let Some(f) = n.as_f64() { rusqlite::types::Value::Real(f) } else { rusqlite::types::Value::Text(n.to_string()) } } Value::String(s) => { let t = s.trim(); if let Ok(i) = t.parse::() { rusqlite::types::Value::Integer(i) } else if let Ok(f) = t.parse::() { if !t.contains('e') && !t.contains('E') && t.contains('.') { rusqlite::types::Value::Real(f) } else { rusqlite::types::Value::Text(s.clone()) } } else { rusqlite::types::Value::Text(s.clone()) } } other => rusqlite::types::Value::Text(other.to_string()), } } fn from_sql(v: ValueRef<'_>) -> Value { match v { ValueRef::Null => Value::Null, ValueRef::Integer(i) => json!(i), ValueRef::Real(f) => json!(f), ValueRef::Text(t) => Value::String(String::from_utf8_lossy(t).into_owned()), ValueRef::Blob(b) => Value::String(format!("blob:{}b", b.len())), } } /// `fields` = list of {name,type,nullable?} or columnar schema table already normalized. pub fn init(url: &str, table: &str, fields: &Value) -> Result { if crate::db_pg::is_postgres(url) { return crate::db_pg::init(url, table, fields); } let table = ident(table)?; let cols = crate::table::as_fields(fields); let arr = cols.as_array().ok_or("fields must be a list")?; if arr.is_empty() { return Err("empty fields".into()); } let mut parts = Vec::new(); let mut has_pk = false; let mut index_sql = Vec::new(); for c in arr { let name = c .get("name") .or_else(|| c.get("字段")) .map(cell_str) .unwrap_or_default(); let name = ident(&name)?; let ty = c .get("type") .or_else(|| c.get("类型")) .map(cell_str) .unwrap_or_else(|| "text".into()); let nullable = c .get("nullable") .or_else(|| c.get("可空")) .and_then(|v| match v { Value::Bool(b) => Some(*b), Value::String(s) => Some(matches!(s.as_str(), "true" | "True" | "1" | "是" | "可")), _ => None, }) .unwrap_or(true); let unique = c .get("unique") .or_else(|| c.get("唯一")) .and_then(|v| match v { Value::Bool(b) => Some(*b), Value::String(s) => { Some(matches!(s.as_str(), "true" | "True" | "1" | "yes" | "是" | "唯一")) } _ => None, }) .unwrap_or(false); let index = c .get("index") .or_else(|| c.get("索引")) .and_then(|v| match v { Value::Bool(b) => Some(*b), Value::String(s) => { Some(matches!(s.as_str(), "true" | "True" | "1" | "yes" | "是" | "索引")) } _ => None, }) .unwrap_or(false); let mut col = format!("\"{name}\" {}", sql_type(&ty)); if name == "id" && !has_pk { col.push_str(" PRIMARY KEY"); if sql_type(&ty) == "INTEGER" { col.push_str(" AUTOINCREMENT"); } has_pk = true; } else if !nullable { col.push_str(" NOT NULL"); } if unique && name != "id" { col.push_str(" UNIQUE"); } let fk_raw = c .get("fk") .or_else(|| c.get("外键")) .map(cell_str) .unwrap_or_default(); if !fk_raw.is_empty() { let (ref_table, ref_col) = parse_fk(&fk_raw)?; col.push_str(&format!(" REFERENCES \"{ref_table}\"(\"{ref_col}\")")); } parts.push(col); if (index || unique) && name != "id" { let kind = if unique { "UNIQUE " } else { "" }; index_sql.push(format!( "CREATE {kind}INDEX IF NOT EXISTS \"idx_{table}_{name}\" ON \"{table}\" (\"{name}\")" )); } } let sql = format!( "CREATE TABLE IF NOT EXISTS \"{table}\" ({})", parts.join(", ") ); let conn = pooled(url)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); c.execute_batch(&sql).map_err(|e| e.to_string())?; for stmt in index_sql { c.execute_batch(&stmt).map_err(|e| e.to_string())?; } Ok(json!({ "_type": "db_table", "name": table, "url": url })) } pub fn insert(url: &str, table: &str, rows: &Value, txn_id: Option<&str>) -> Result { if crate::db_pg::is_postgres(url) { return crate::db_pg::insert(url, table, rows, txn_id); } let table = ident(table)?; let cols_present = column_names(url, table, txn_id)?; let has_created = cols_present.iter().any(|c| c == "created_at"); let has_updated = cols_present.iter().any(|c| c == "updated_at"); let rows = crate::table::as_rows(rows); let arr = rows.as_array().ok_or("rows must be a list")?; let conn = conn_for(url, txn_id)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let mut n = 0i64; let now = utc_now_iso(); for row in arr { let obj = row.as_object().ok_or("row must be a map")?; let mut cols = Vec::new(); let mut placeholders = Vec::new(); let mut vals = Vec::new(); let mut seen_created = false; let mut seen_updated = false; for (k, v) in obj { if k == "@" || k == "行" || k == "row" { continue; } let _ = ident(k)?; if k == "created_at" { seen_created = true; } if k == "updated_at" { seen_updated = true; } cols.push(format!("\"{k}\"")); placeholders.push("?"); vals.push(to_sql(v)); } if has_created && !seen_created { cols.push("\"created_at\"".into()); placeholders.push("?"); vals.push(rusqlite::types::Value::Text(now.clone())); } if has_updated && !seen_updated { cols.push("\"updated_at\"".into()); placeholders.push("?"); vals.push(rusqlite::types::Value::Text(now.clone())); } if cols.is_empty() { continue; } let sql = format!( "INSERT INTO \"{table}\" ({}) VALUES ({})", cols.join(", "), placeholders.join(", ") ); c.execute(&sql, params_from_iter(vals.iter().cloned())) .map_err(|e| e.to_string())?; n += 1; } Ok(json!({ "ok": true, "inserted": n })) } fn skip_where_key(k: &str) -> bool { matches!(k, "@" | "行" | "row") || k.starts_with('_') } fn where_op(raw: &str) -> Result<&'static str, String> { match raw.trim().to_ascii_lowercase().as_str() { "" | "=" | "eq" | "==" | "等于" => Ok("="), "!=" | "<>" | "ne" | "不等于" => Ok("!="), ">" | "gt" | "大于" => Ok(">"), ">=" | "gte" | "大于等于" => Ok(">="), "<" | "lt" | "小于" => Ok("<"), "<=" | "lte" | "小于等于" => Ok("<="), "like" | "contains" | "包含" | "匹配" => Ok("LIKE"), "in" | "在" => Ok("IN"), "between" | "介于" | "在之间" => Ok("BETWEEN"), "is null" | "isnull" | "为空" | "空" => Ok("IS NULL"), other => Err(format!("unsupported where op `{other}`")), } } /// One parsed filter clause: column, op, bound values, and whether the clause /// joins the previous one with `OR` (false ⇒ AND). struct Clause { expr: String, vals: Vec, or: bool, } fn in_values(v: &Value) -> Vec { match v { Value::Array(a) => a.iter().map(to_sql).collect(), Value::String(s) => s .split(',') .map(|p| to_sql(&Value::String(p.trim().to_string()))) .collect(), other => vec![to_sql(other)], } } fn between_values(v: &Value) -> Result, String> { match v { Value::Array(a) if a.len() == 2 => Ok(vec![to_sql(&a[0]), to_sql(&a[1])]), Value::String(s) => { let mut it = s.split(','); let lo = it.next().unwrap_or("").trim().to_string(); let hi = it.next().unwrap_or("").trim().to_string(); Ok(vec![ to_sql(&Value::String(lo)), to_sql(&Value::String(hi)), ]) } _ => Err("between expects two bounds (\"lo,hi\" or [lo,hi])".into()), } } /// Parse a filter table row into a `Clause`. fn clause_from_row(m: &Map) -> Result, String> { let field = m .get("field") .or_else(|| m.get("字段")) .or_else(|| m.get("列")) .map(cell_str) .unwrap_or_default(); if field.is_empty() { return Ok(None); } let col = ident(&field)?; let op = where_op( &m.get("op") .or_else(|| m.get("操作")) .map(cell_str) .unwrap_or_default(), )?; let val = m .get("value") .or_else(|| m.get("值")) .cloned() .unwrap_or(Value::Null); let or = m .get("or") .or_else(|| m.get("或")) .and_then(|v| match v { Value::Bool(b) => Some(*b), Value::String(s) => Some(matches!(s.trim().to_ascii_lowercase().as_str(), "1" | "true" | "是" | "or" | "或者")), _ => None, }) .unwrap_or(false); let (expr, vals) = match op { "IS NULL" => (format!("\"{col}\" IS NULL"), vec![]), "IN" => { let vs = in_values(&val); let ph: Vec<&str> = vs.iter().map(|_| "?").collect(); ( format!("\"{col}\" IN ({})", ph.join(", ")), vs, ) } "BETWEEN" => { let vs = between_values(&val)?; (format!("\"{col}\" BETWEEN ? AND ?"), vs) } _ => (format!("\"{col}\" {op} ?"), vec![to_sql(&val)]), }; Ok(Some(Clause { expr, vals, or })) } /// Parse simple filters: map of column→value (AND `=`), or rows /// `{field|字段, op|操作|=, value|值, or|或?}` / `{列, 值}`. /// Supports `in` / `between` / `is null` and OR groups via the `或` column. /// /// GFM tables reach the plugin in columnar form (`{字段:[…], 值:[…]}`), so the /// value is first normalized via `as_rows` into a row list. fn parse_where(where_v: Option<&Value>) -> Result<(Vec, Vec), String> { let mut clauses: Vec = Vec::new(); let Some(w) = where_v else { return Ok((vec![], vec![])); }; // Normalize GFM tables (columnar object) into row arrays; a single map // (column→value filter) becomes a one-element row list. let w = crate::table::as_rows(w); match &w { Value::Null => {} Value::Array(arr) => { for item in arr { match item { Value::Object(m) => { let field = m .get("field") .or_else(|| m.get("字段")) .or_else(|| m.get("列")) .map(cell_str) .unwrap_or_default(); if !field.is_empty() && (m.contains_key("value") || m.contains_key("值") || m.contains_key("op") || m.contains_key("操作")) { if let Some(cl) = clause_from_row(m)? { clauses.push(cl); } } else { for (k, v) in m { if skip_where_key(k) { continue; } let col = ident(k)?; clauses.push(Clause { expr: format!("\"{col}\" = ?"), vals: vec![to_sql(v)], or: false, }); } } } other => { return Err(format!( "where must be a map or filter table, got {}", match other { Value::String(_) => "string", Value::Number(_) => "number", Value::Bool(_) => "bool", _ => "value", } )); } } } } other => { return Err(format!( "where must be a map or filter table, got {}", match other { Value::String(_) => "string", Value::Number(_) => "number", Value::Bool(_) => "bool", _ => "value", } )); } } // Assemble: AND by default, OR where the clause is marked. let mut exprs = Vec::new(); let mut vals = Vec::new(); let mut group: Vec = Vec::new(); for cl in clauses { if cl.or { group.push(cl.expr); } else { if !group.is_empty() { exprs.push(format!("({})", group.join(" OR "))); group.clear(); } group.push(cl.expr); } vals.extend(cl.vals); } if !group.is_empty() { exprs.push(format!("({})", group.join(" OR "))); } Ok((exprs, vals)) } pub fn select( url: &str, table: &str, limit: i64, where_v: Option<&Value>, ) -> Result { select_order(url, table, limit, where_v, None, None, None) } /// Like `select`, with an optional `ORDER BY`, `OFFSET` and a transaction. /// /// - `order` is a column name, optionally prefixed with `-` for descending /// (`"created_at"`, `"-created_at"`). Comma-separated lists are allowed. /// - `offset` (页偏移/跳过) enables pagination: `LIMIT … OFFSET …`. /// - When `offset` is provided, the result carries `total` (row count without /// limit/offset) so pages can render `上一页 / 下一页`. /// - `txn_id` routes the read through an open transaction (事务内一致性读). pub fn select_order( url: &str, table: &str, limit: i64, where_v: Option<&Value>, order: Option<&str>, offset: Option, txn_id: Option<&str>, ) -> Result { if crate::db_pg::is_postgres(url) { return crate::db_pg::select_order(url, table, limit, where_v, order, offset, txn_id); } let table = ident(table)?; let (exprs, mut vals) = parse_where(where_v)?; let conn = conn_for(url, txn_id)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let mut sql = format!("SELECT * FROM \"{table}\""); let where_sql = if exprs.is_empty() { String::new() } else { format!(" WHERE {}", exprs.join(" AND ")) }; sql.push_str(&where_sql); if let Some(order) = order { let order = order.trim(); if !order.is_empty() { let parts: Vec<&str> = order .split(',') .map(|p| p.trim()) .filter(|p| !p.is_empty()) .collect(); let mut order_exprs = Vec::new(); for part in parts { let (col, dir) = match part.strip_prefix('-') { Some(col) => (col, " DESC"), None => (part, ""), }; order_exprs.push(format!("\"{}\"{}", ident(col)?, dir)); } if !order_exprs.is_empty() { sql.push_str(" ORDER BY "); sql.push_str(&order_exprs.join(", ")); } } } sql.push_str(&format!(" LIMIT ?{}", vals.len() + 1)); vals.push(rusqlite::types::Value::Integer(limit)); if let Some(off) = offset { if off > 0 { sql.push_str(&format!(" OFFSET ?{}", vals.len() + 1)); vals.push(rusqlite::types::Value::Integer(off)); } } let rows = query_rows(&c, &sql, &vals)?; // Total count (ignoring limit/offset) for pagination. let total = if offset.is_some() { let (_exprs2, where_vals) = parse_where(where_v)?; let n_where = where_vals.len(); let count_sql = format!("SELECT COUNT(*) FROM \"{table}\"{where_sql}"); count_rows(&c, &count_sql, &vals[..n_where]).unwrap_or(rows.len() as i64) } else { rows.len() as i64 }; if offset.is_some() { Ok(json!({ "rows": rows, "total": total })) } else { Ok(json!({ "rows": rows })) } } /// Run `SELECT …` and collect rows as a list of column→value maps. fn query_rows( c: &Connection, sql: &str, vals: &[rusqlite::types::Value], ) -> Result, String> { let mut stmt = c.prepare(sql).map_err(|e| e.to_string())?; let names: Vec = (0..stmt.column_count()) .map(|i| stmt.column_name(i).unwrap_or("?").to_string()) .collect(); let mut rows_iter = stmt .query(params_from_iter(vals.iter().cloned())) .map_err(|e| e.to_string())?; let mut rows = Vec::new(); while let Some(r) = rows_iter.next().map_err(|e| e.to_string())? { let mut m = Map::new(); for (i, name) in names.iter().enumerate() { m.insert(name.clone(), from_sql(r.get_ref(i).map_err(|e| e.to_string())?)); } rows.push(Value::Object(m)); } Ok(rows) } /// Run `SELECT COUNT(*) …` and return the scalar. fn count_rows( c: &Connection, sql: &str, vals: &[rusqlite::types::Value], ) -> Result { c.query_row(sql, params_from_iter(vals.iter().cloned()), |r| r.get(0)) .map_err(|e| e.to_string()) } pub fn get(url: &str, table: &str, id: &str, txn_id: Option<&str>) -> Result { if crate::db_pg::is_postgres(url) { return crate::db_pg::get(url, table, id, txn_id); } let table = ident(table)?; let conn = conn_for(url, txn_id)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let sql = format!("SELECT * FROM \"{table}\" WHERE \"id\" = ?1 LIMIT 1"); let mut stmt = c.prepare(&sql).map_err(|e| e.to_string())?; let names: Vec = (0..stmt.column_count()) .map(|i| stmt.column_name(i).unwrap_or("?").to_string()) .collect(); let row = stmt .query_row(rusqlite::params![id], |r| { let mut m = Map::new(); for (i, name) in names.iter().enumerate() { m.insert(name.clone(), from_sql(r.get_ref(i)?)); } Ok(Value::Object(m)) }) .optional() .map_err(|e| e.to_string())?; Ok(row.unwrap_or(Value::Null)) } pub fn update( url: &str, table: &str, id: &str, row: &Value, txn_id: Option<&str>, ) -> Result { if crate::db_pg::is_postgres(url) { return crate::db_pg::update(url, table, id, row, txn_id); } let table = ident(table)?; let cols_present = column_names(url, table, txn_id)?; let has_updated = cols_present.iter().any(|c| c == "updated_at"); let obj = row.as_object().ok_or("row must be a map")?; let mut sets = Vec::new(); let mut vals = Vec::new(); let mut seen_updated = false; for (k, v) in obj { if k == "id" { continue; } let _ = ident(k)?; if k == "updated_at" { seen_updated = true; } sets.push(format!("\"{k}\" = ?")); vals.push(to_sql(v)); } if has_updated && !seen_updated { sets.push("\"updated_at\" = ?".into()); vals.push(rusqlite::types::Value::Text(utc_now_iso())); } if sets.is_empty() { return Err("nothing to update".into()); } vals.push(rusqlite::types::Value::Text(id.to_string())); let sql = format!( "UPDATE \"{table}\" SET {} WHERE \"id\" = ?", sets.join(", ") ); let conn = conn_for(url, txn_id)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let n = c .execute(&sql, params_from_iter(vals.iter().cloned())) .map_err(|e| e.to_string())?; Ok(json!({ "ok": true, "updated": n })) } pub fn delete(url: &str, table: &str, id: &str, txn_id: Option<&str>) -> Result { if crate::db_pg::is_postgres(url) { return crate::db_pg::delete(url, table, id, txn_id); } let table = ident(table)?; let conn = conn_for(url, txn_id)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let sql = format!("DELETE FROM \"{table}\" WHERE \"id\" = ?1"); let n = c .execute(&sql, rusqlite::params![id]) .map_err(|e| e.to_string())?; Ok(json!({ "ok": true, "deleted": n })) } pub fn exec(url: &str, sql: &str, args: Option<&Value>, txn_id: Option<&str>) -> Result { if crate::db_pg::is_postgres(url) { return crate::db_pg::exec(url, sql, args, txn_id); } let conn = conn_for(url, txn_id)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let vals: Vec = match args { Some(Value::Array(a)) => a.iter().map(to_sql).collect(), _ => Vec::new(), }; let n = c .execute(sql, params_from_iter(vals.iter().cloned())) .map_err(|e| e.to_string())?; Ok(json!({ "ok": true, "changes": n })) } /// Run a `SELECT …` and return the result set (count / join / group / subquery). /// Bare SQL — the result carries `{ rows, count }`. pub fn query(url: &str, sql: &str, args: Option<&Value>, txn_id: Option<&str>) -> Result { if crate::db_pg::is_postgres(url) { return crate::db_pg::query(url, sql, args, txn_id); } let conn = conn_for(url, txn_id)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let vals: Vec = match args { Some(Value::Array(a)) => a.iter().map(to_sql).collect(), _ => Vec::new(), }; let rows = query_rows(&c, sql, &vals)?; Ok(json!({ "rows": rows, "count": rows.len() })) } /// Count rows matching a `where` filter (aggregation helper). pub fn count(url: &str, table: &str, where_v: Option<&Value>, txn_id: Option<&str>) -> Result { if crate::db_pg::is_postgres(url) { return crate::db_pg::count(url, table, where_v, txn_id); } let table = ident(table)?; let (exprs, vals) = parse_where(where_v)?; let conn = conn_for(url, txn_id)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let mut sql = format!("SELECT COUNT(*) FROM \"{table}\""); if !exprs.is_empty() { sql.push_str(&format!(" WHERE {}", exprs.join(" AND "))); } let n = count_rows(&c, &sql, &vals)?; Ok(json!({ "count": n })) } /// Begin a transaction. Borrows the pooled connection exclusively and records /// it under a fresh txn id; returns `{ txn, url }` for `commit` / `rollback`. pub fn begin(url: &str) -> Result { if crate::db_pg::is_postgres(url) { return crate::db_pg::begin(url); } let conn = pooled(url)?; { let c = conn.lock().unwrap_or_else(|e| e.into_inner()); c.execute_batch("BEGIN IMMEDIATE").map_err(|e| e.to_string())?; } let id = next_txn_id(); { let mut guard = TXNS.lock().unwrap_or_else(|e| e.into_inner()); guard .get_or_insert_with(HashMap::new) .insert(id.clone(), (url.to_string(), conn)); } Ok(json!({ "_type": "txn", "txn": id, "url": url, "事务": id, "地址": url })) } /// Commit a transaction and return its connection to the pool. pub fn commit(txn_id: &str) -> Result { if txn_id.starts_with("pg-txn-") { return crate::db_pg::commit(txn_id); } let (_, conn) = take_txn(txn_id)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); c.execute_batch("COMMIT").map_err(|e| e.to_string())?; Ok(json!({ "ok": true })) } /// Roll back a transaction and return its connection to the pool. pub fn rollback(txn_id: &str) -> Result { if txn_id.starts_with("pg-txn-") { return crate::db_pg::rollback(txn_id); } let (_, conn) = take_txn(txn_id)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); c.execute_batch("ROLLBACK").map_err(|e| e.to_string())?; Ok(json!({ "ok": true })) } /// Remove a txn from the registry, returning its connection for reuse. fn take_txn(txn_id: &str) -> Result<(String, Arc>), String> { let mut guard = TXNS.lock().unwrap_or_else(|e| e.into_inner()); guard .as_mut() .and_then(|m| m.remove(txn_id)) .ok_or_else(|| format!("unknown transaction `{txn_id}`")) } pub fn list_tables(url: &str) -> Result, String> { if crate::db_pg::is_postgres(url) { return crate::db_pg::list_tables(url); } let conn = pooled(url)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let mut stmt = c .prepare( "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '_marqdo%' ORDER BY name", ) .map_err(|e| e.to_string())?; let rows = stmt .query_map([], |r| r.get::<_, String>(0)) .map_err(|e| e.to_string())? .collect::, _>>() .map_err(|e| e.to_string())?; Ok(rows) } #[derive(Debug, Clone)] pub struct ColumnInfo { pub name: String, pub sql_type: String, pub notnull: bool, pub pk: bool, } /// `PRAGMA table_info` → column metadata for admin forms. pub fn table_info(url: &str, table: &str) -> Result, String> { if crate::db_pg::is_postgres(url) { return crate::db_pg::table_info(url, table); } let table = ident(table)?; let conn = pooled(url)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let sql = format!("PRAGMA table_info(\"{table}\")"); let mut stmt = c.prepare(&sql).map_err(|e| e.to_string())?; let rows = stmt .query_map([], |r| { Ok(ColumnInfo { name: r.get::<_, String>(1)?, sql_type: r.get::<_, String>(2).unwrap_or_else(|_| "TEXT".into()), notnull: r.get::<_, i64>(3).unwrap_or(0) != 0, pk: r.get::<_, i64>(5).unwrap_or(0) != 0, }) }) .map_err(|e| e.to_string())? .collect::, _>>() .map_err(|e| e.to_string())?; Ok(rows) } /// Apply versioned SQL steps. Table `_marqdo_migrations` tracks applied versions. /// /// `steps` is a list of `{version|版本, sql|SQL}` (GFM columnar tables ok via `as_rows`). pub fn migrate(url: &str, steps: &Value) -> Result { if crate::db_pg::is_postgres(url) { return Err( "db.migrate is SQLite-only in this wave (use postgres migration tooling)".into(), ); } let rows = crate::table::as_rows(steps); let arr = rows.as_array().ok_or("migrate steps must be a list")?; let mut parsed: Vec<(i64, String)> = Vec::new(); for row in arr { let obj = row.as_object().ok_or("migrate step must be a map")?; let ver = obj .get("version") .or_else(|| obj.get("版本")) .or_else(|| obj.get("ver")) .ok_or("migrate step missing `version`/`版本`")?; let version = match ver { Value::Number(n) => n .as_i64() .ok_or_else(|| "migrate version must be an integer".to_string())?, Value::String(s) => s .trim() .parse::() .map_err(|_| format!("bad migrate version `{s}`"))?, _ => return Err("migrate version must be an integer".into()), }; if version <= 0 { return Err(format!("migrate version must be positive, got {version}")); } let sql = obj .get("sql") .or_else(|| obj.get("SQL")) .or_else(|| obj.get("Sql")) .map(cell_str) .unwrap_or_default(); if sql.trim().is_empty() { return Err(format!("migrate version {version} has empty SQL")); } parsed.push((version, sql)); } parsed.sort_by_key(|(v, _)| *v); for w in parsed.windows(2) { if w[0].0 == w[1].0 { return Err(format!("duplicate migrate version {}", w[0].0)); } } let conn = pooled(url)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); c.execute_batch( "CREATE TABLE IF NOT EXISTS \"_marqdo_migrations\" ( version INTEGER PRIMARY KEY, applied_at TEXT NOT NULL )", ) .map_err(|e| e.to_string())?; let current: i64 = c .query_row( "SELECT COALESCE(MAX(version), 0) FROM \"_marqdo_migrations\"", [], |r| r.get(0), ) .unwrap_or(0); let mut applied = Vec::new(); for (version, sql) in parsed { if version <= current { continue; } c.execute_batch("BEGIN IMMEDIATE") .map_err(|e| e.to_string())?; match c.execute_batch(&sql) { Ok(()) => { if let Err(e) = c.execute( "INSERT INTO \"_marqdo_migrations\" (version, applied_at) VALUES (?1, datetime('now'))", rusqlite::params![version], ) { let _ = c.execute_batch("ROLLBACK"); return Err(e.to_string()); } c.execute_batch("COMMIT").map_err(|e| e.to_string())?; applied.push(version); } Err(e) => { let _ = c.execute_batch("ROLLBACK"); return Err(format!("migrate version {version} failed: {e}")); } } } let to = applied.last().copied().unwrap_or(current); Ok(json!({ "ok": true, "from": current, "to": to, "applied": applied, })) } fn parse_column_list(columns: &Value) -> Result, String> { match columns { Value::String(s) => { let cols: Vec = s .split(',') .map(|p| p.trim().to_string()) .filter(|p| !p.is_empty()) .collect(); if cols.is_empty() { return Err("fts columns is empty".into()); } for c in &cols { let _ = ident(c)?; } Ok(cols) } Value::Array(a) => { let mut cols = Vec::new(); for v in a { let name = cell_str(v); let _ = ident(&name)?; cols.push(name); } if cols.is_empty() { return Err("fts columns is empty".into()); } Ok(cols) } Value::Object(_) => { let rows = crate::table::as_rows(columns); if let Some(arr) = rows.as_array() { let mut cols = Vec::new(); for row in arr { if let Some(obj) = row.as_object() { let name = obj .get("name") .or_else(|| obj.get("字段")) .or_else(|| obj.get("column")) .or_else(|| obj.values().next()) .map(cell_str) .unwrap_or_default(); if !name.is_empty() { let _ = ident(&name)?; cols.push(name); } } else { let name = cell_str(row); if !name.is_empty() { let _ = ident(&name)?; cols.push(name); } } } if cols.is_empty() { return Err("fts columns is empty".into()); } return Ok(cols); } Err("fts columns must be a list or CSV string".into()) } _ => Err("fts columns must be a list or CSV string".into()), } } /// Create an FTS5 virtual table synced to `table` via content= + triggers. pub fn fts_create( url: &str, table: &str, columns: &Value, name: Option<&str>, ) -> Result { if crate::db_pg::is_postgres(url) { return Err("db.fts is SQLite-only in this wave".into()); } let table = ident(table)?; let cols = parse_column_list(columns)?; let fts = match name { Some(n) if !n.trim().is_empty() => ident(n.trim())?.to_string(), _ => format!("{table}_fts"), }; let col_sql = cols .iter() .map(|c| format!("\"{c}\"")) .collect::>() .join(", "); let conn = pooled(url)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let create = format!( "CREATE VIRTUAL TABLE IF NOT EXISTS \"{fts}\" USING fts5({col_sql}, content=\"{table}\", content_rowid=\"id\")" ); c.execute_batch(&create).map_err(|e| e.to_string())?; let ai = format!("{fts}_ai"); let ad = format!("{fts}_ad"); let au = format!("{fts}_au"); c.execute_batch(&format!( "DROP TRIGGER IF EXISTS \"{ai}\"; DROP TRIGGER IF EXISTS \"{ad}\"; DROP TRIGGER IF EXISTS \"{au}\";" )) .map_err(|e| e.to_string())?; let new_cols = cols .iter() .map(|c| format!("new.\"{c}\"")) .collect::>() .join(", "); let old_cols = cols .iter() .map(|c| format!("old.\"{c}\"")) .collect::>() .join(", "); c.execute_batch(&format!( "CREATE TRIGGER \"{ai}\" AFTER INSERT ON \"{table}\" BEGIN INSERT INTO \"{fts}\"(rowid, {col_sql}) VALUES (new.id, {new_cols}); END; CREATE TRIGGER \"{ad}\" AFTER DELETE ON \"{table}\" BEGIN INSERT INTO \"{fts}\"(\"{fts}\", rowid, {col_sql}) VALUES('delete', old.id, {old_cols}); END; CREATE TRIGGER \"{au}\" AFTER UPDATE ON \"{table}\" BEGIN INSERT INTO \"{fts}\"(\"{fts}\", rowid, {col_sql}) VALUES('delete', old.id, {old_cols}); INSERT INTO \"{fts}\"(rowid, {col_sql}) VALUES (new.id, {new_cols}); END;" )) .map_err(|e| e.to_string())?; c.execute( &format!("INSERT INTO \"{fts}\"(\"{fts}\") VALUES('rebuild')"), [], ) .map_err(|e| e.to_string())?; Ok(json!({ "ok": true, "table": table, "fts": fts, "columns": cols, })) } /// Full-text search against an FTS5 table created by `fts_create`. pub fn search( url: &str, table: &str, q: &str, limit: i64, name: Option<&str>, ) -> Result { if crate::db_pg::is_postgres(url) { return Err("db.search is SQLite-only in this wave".into()); } let table = ident(table)?; let fts = match name { Some(n) if !n.trim().is_empty() => ident(n.trim())?.to_string(), _ => format!("{table}_fts"), }; let q = q.trim(); if q.is_empty() { return Ok(json!({ "rows": [], "count": 0 })); } let limit = if limit <= 0 { 20 } else { limit.min(500) }; let sql = format!( "SELECT t.*, bm25(\"{fts}\") AS rank FROM \"{fts}\" JOIN \"{table}\" t ON t.id = \"{fts}\".rowid WHERE \"{fts}\" MATCH ?1 ORDER BY rank LIMIT ?2" ); let conn = pooled(url)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let vals = vec![ rusqlite::types::Value::Text(q.to_string()), rusqlite::types::Value::Integer(limit), ]; let rows = query_rows(&c, &sql, &vals)?; Ok(json!({ "rows": rows, "count": rows.len() })) }