Skip to main content

we_trust_sqlite/reader/
schema.rs

1/// SQLite 表结构信息
2#[derive(Debug, Clone)]
3pub struct TableSchema {
4    pub table_type: String,
5    pub name: String,
6    pub tbl_name: String,
7    pub rootpage: u32,
8    pub sql: String,
9}
10
11impl TableSchema {
12    /// 从 SQL 中解析列名 (极简实现)
13    pub fn get_columns(&self) -> Vec<String> {
14        if self.sql.is_empty() {
15            return vec!["column".to_string()];
16        }
17
18        // 寻找括号内的内容: CREATE TABLE x (col1 type, col2 type)
19        if let Some(start) = self.sql.find('(') {
20            if let Some(end) = self.sql.rfind(')') {
21                let content = &self.sql[start + 1..end];
22                return content
23                    .split(',')
24                    .map(|s| {
25                        s.trim()
26                            .split_whitespace()
27                            .next()
28                            .unwrap_or("unknown")
29                            .trim_matches(|c| c == '"' || c == '`' || c == '[')
30                            .to_string()
31                    })
32                    .collect();
33            }
34        }
35        vec!["column".to_string()]
36    }
37}