we-trust 0.0.1

Core We-Trust binary protocol implementation for high-performance communication within the YYKV ecosystem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! Schema 反射模块
//! 
//! 从数据库读取现有表结构信息。

use std::collections::HashMap;
use crate::{DatabaseBackend, DatabaseConnection, DatabaseResult};

/// 列类型
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColumnType {
    /// 整数类型
    Integer,
    /// 浮点类型
    Real,
    /// 文本类型
    Text,
    /// 二进制类型
    Blob,
}

impl ColumnType {
    /// 从 SQL 类型字符串解析
    pub fn from_sql(sql: &str) -> Self {
        match sql.to_uppercase().as_str() {
            "INTEGER" | "INT" | "BIGINT" | "SMALLINT" | "TINYINT" | "SERIAL" | "BIGSERIAL" => ColumnType::Integer,
            "REAL" | "FLOAT" | "DOUBLE" | "NUMERIC" | "DECIMAL" | "DOUBLE PRECISION" => ColumnType::Real,
            "TEXT" | "VARCHAR" | "CHAR" | "STRING" | "VARCHAR(255)" | "TEXT[]" => ColumnType::Text,
            "BLOB" | "BINARY" | "BYTEA" | "LONGBLOB" => ColumnType::Blob,
            _ => ColumnType::Text,
        }
    }
}

/// 列定义
#[derive(Debug, Clone)]
pub struct ColumnDef {
    /// 列名
    pub name: String,
    /// 列类型
    pub col_type: ColumnType,
    /// 是否可空
    pub nullable: bool,
    /// 是否主键
    pub primary_key: bool,
    /// 是否自增
    pub auto_increment: bool,
    /// 默认值
    pub default_value: Option<String>,
    /// 是否唯一
    pub unique: bool,
}

/// 索引定义
#[derive(Debug, Clone)]
pub struct IndexDef {
    /// 索引名称
    pub name: String,
    /// 表名
    pub table_name: String,
    /// 列名列表
    pub columns: Vec<String>,
    /// 是否唯一索引
    pub unique: bool,
}

/// 外键引用行为
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReferentialAction {
    /// 无操作
    NoAction,
    /// 限制
    Restrict,
    /// 级联
    Cascade,
    /// 设为空
    SetNull,
    /// 设为默认值
    SetDefault,
}

impl ReferentialAction {
    /// 从字符串解析
    pub fn from_str(s: &str) -> Self {
        match s.to_uppercase().as_str() {
            "CASCADE" => ReferentialAction::Cascade,
            "RESTRICT" => ReferentialAction::Restrict,
            "SET NULL" => ReferentialAction::SetNull,
            "SET DEFAULT" => ReferentialAction::SetDefault,
            _ => ReferentialAction::NoAction,
        }
    }
}

/// 外键定义
#[derive(Debug, Clone)]
pub struct ForeignKeyDef {
    /// 外键名称
    pub name: String,
    /// 本表列名
    pub column: String,
    /// 引用表名
    pub ref_table: String,
    /// 引用列名
    pub ref_column: String,
    /// 更新行为
    pub on_update: ReferentialAction,
    /// 删除行为
    pub on_delete: ReferentialAction,
}

/// 表结构定义
#[derive(Debug, Clone)]
pub struct TableSchema {
    /// 表名
    pub name: String,
    /// 列定义列表
    pub columns: Vec<ColumnDef>,
    /// 索引定义列表
    pub indexes: Vec<IndexDef>,
    /// 外键约束列表
    pub foreign_keys: Vec<ForeignKeyDef>,
}

/// Schema 反射器
pub struct SchemaReflector<'a> {
    conn: &'a dyn DatabaseConnection,
}

impl<'a> SchemaReflector<'a> {
    /// 创建新的反射器
    pub fn new(conn: &'a dyn DatabaseConnection) -> Self {
        Self { conn }
    }

    /// 获取所有表名
    pub async fn get_table_names(&self) -> DatabaseResult<Vec<String>> {
        match self.conn.backend() {
            DatabaseBackend::Limbo => self.get_table_names_limbo().await,
            DatabaseBackend::Postgres => self.get_table_names_postgres().await,
            DatabaseBackend::MySql => self.get_table_names_mysql().await,
        }
    }

    /// 获取表的完整结构
    pub async fn get_table_schema(&self, table_name: &str) -> DatabaseResult<TableSchema> {
        let columns = self.get_columns(table_name).await?;
        let indexes = self.get_indexes(table_name).await?;
        let foreign_keys = self.get_foreign_keys(table_name).await.unwrap_or_default();

        Ok(TableSchema {
            name: table_name.to_string(),
            columns,
            indexes,
            foreign_keys,
        })
    }

    /// 获取表的所有外键约束
    pub async fn get_foreign_keys(&self, table_name: &str) -> DatabaseResult<Vec<ForeignKeyDef>> {
        match self.conn.backend() {
            DatabaseBackend::Limbo => self.get_foreign_keys_limbo(table_name).await,
            DatabaseBackend::Postgres => self.get_foreign_keys_postgres(table_name).await,
            DatabaseBackend::MySql => self.get_foreign_keys_mysql(table_name).await,
        }
    }

    /// 获取表的所有列
    pub async fn get_columns(&self, table_name: &str) -> DatabaseResult<Vec<ColumnDef>> {
        match self.conn.backend() {
            DatabaseBackend::Limbo => self.get_columns_limbo(table_name).await,
            DatabaseBackend::Postgres => self.get_columns_postgres(table_name).await,
            DatabaseBackend::MySql => self.get_columns_mysql(table_name).await,
        }
    }

    /// 获取表的所有索引
    pub async fn get_indexes(&self, table_name: &str) -> DatabaseResult<Vec<IndexDef>> {
        match self.conn.backend() {
            DatabaseBackend::Limbo => self.get_indexes_limbo(table_name).await,
            DatabaseBackend::Postgres => self.get_indexes_postgres(table_name).await,
            DatabaseBackend::MySql => self.get_indexes_mysql(table_name).await,
        }
    }

    /// 获取所有表的 Schema
    pub async fn get_all_schemas(&self) -> DatabaseResult<HashMap<String, TableSchema>> {
        let table_names = self.get_table_names().await?;
        let mut schemas = HashMap::new();

        for name in table_names {
            let schema = self.get_table_schema(&name).await?;
            schemas.insert(name, schema);
        }

        Ok(schemas)
    }

    /// 检查表是否存在
    pub async fn table_exists(&self, table_name: &str) -> DatabaseResult<bool> {
        match self.conn.backend() {
            DatabaseBackend::Limbo => self.table_exists_limbo(table_name).await,
            DatabaseBackend::Postgres => self.table_exists_postgres(table_name).await,
            DatabaseBackend::MySql => self.table_exists_mysql(table_name).await,
        }
    }

    async fn get_table_names_limbo(&self) -> DatabaseResult<Vec<String>> {
        let sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name != '_migrations'";
        let mut rows = self.conn.query(sql).await?;

        let mut tables = Vec::new();
        while let Some(row) = rows.next().await? {
            tables.push(row.get_string(0)?);
        }

        Ok(tables)
    }

    async fn get_table_names_postgres(&self) -> DatabaseResult<Vec<String>> {
        let sql = "SELECT tablename FROM pg_tables WHERE schemaname = 'public' AND tablename != '_migrations'";
        let mut rows = self.conn.query(sql).await?;

        let mut tables = Vec::new();
        while let Some(row) = rows.next().await? {
            tables.push(row.get_string(0)?);
        }

        Ok(tables)
    }

    async fn get_table_names_mysql(&self) -> DatabaseResult<Vec<String>> {
        let sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name != '_migrations'";
        let mut rows = self.conn.query(sql).await?;

        let mut tables = Vec::new();
        while let Some(row) = rows.next().await? {
            tables.push(row.get_string(0)?);
        }

        Ok(tables)
    }

    async fn get_columns_limbo(&self, table_name: &str) -> DatabaseResult<Vec<ColumnDef>> {
        let sql = format!("PRAGMA table_info({})", table_name);
        let mut rows = self.conn.query(&sql).await?;

        let mut columns = Vec::new();
        while let Some(row) = rows.next().await? {
            let name = row.get_string(1)?;
            let type_str = row.get_string(2)?;
            let not_null = row.get_i64(3)? != 0;
            let default_value = row.get_option_string(4)?;
            let is_pk = row.get_i64(5)? != 0;

            let col_type = ColumnType::from_sql(&type_str);

            columns.push(ColumnDef {
                name,
                col_type,
                nullable: !not_null,
                primary_key: is_pk,
                auto_increment: false,
                default_value,
                unique: false,
            });
        }

        Ok(columns)
    }

    async fn get_columns_postgres(&self, table_name: &str) -> DatabaseResult<Vec<ColumnDef>> {
        let sql = format!(
            "SELECT column_name, data_type, is_nullable, column_default, \
             EXISTS (SELECT 1 FROM information_schema.key_column_usage k \
             JOIN information_schema.table_constraints t ON k.constraint_name = t.constraint_name \
             WHERE k.table_name = '{}' AND k.column_name = c.column_name AND t.constraint_type = 'PRIMARY KEY') AS is_pk, \
             EXISTS (SELECT 1 FROM information_schema.key_column_usage k \
             JOIN information_schema.table_constraints t ON k.constraint_name = t.constraint_name \
             WHERE k.table_name = '{}' AND k.column_name = c.column_name AND t.constraint_type = 'UNIQUE') AS is_unique \
             FROM information_schema.columns c \
             WHERE table_name = '{}'",
            table_name, table_name, table_name
        );
        let mut rows = self.conn.query(&sql).await?;

        let mut columns = Vec::new();
        while let Some(row) = rows.next().await? {
            let name = row.get_string(0)?;
            let type_str = row.get_string(1)?;
            let is_nullable = row.get_string(2)? == "YES";
            let default_value = row.get_option_string(3)?;
            let is_pk = row.get_bool(4)?;
            let is_unique = row.get_bool(5)?;

            let col_type = ColumnType::from_sql(&type_str);

            columns.push(ColumnDef {
                name,
                col_type,
                nullable: is_nullable,
                primary_key: is_pk,
                auto_increment: false,
                default_value,
                unique: is_unique,
            });
        }

        Ok(columns)
    }

    async fn get_columns_mysql(&self, table_name: &str) -> DatabaseResult<Vec<ColumnDef>> {
        let sql = format!(
            "SELECT column_name, data_type, is_nullable, column_default, column_key = 'PRI' AS is_pk, \
             column_key = 'UNI' AS is_unique, extra LIKE '%auto_increment%' AS is_auto_inc \
             FROM information_schema.columns WHERE table_name = '{}' AND table_schema = DATABASE()",
            table_name
        );
        let mut rows = self.conn.query(&sql).await?;

        let mut columns = Vec::new();
        while let Some(row) = rows.next().await? {
            let name = row.get_string(0)?;
            let type_str = row.get_string(1)?;
            let is_nullable = row.get_string(2)? == "YES";
            let default_value = row.get_option_string(3)?;
            let is_pk = row.get_bool(4)?;
            let is_unique = row.get_bool(5)?;
            let is_auto_inc = row.get_bool(6)?;

            let col_type = ColumnType::from_sql(&type_str);

            columns.push(ColumnDef {
                name,
                col_type,
                nullable: is_nullable,
                primary_key: is_pk,
                auto_increment: is_auto_inc,
                default_value,
                unique: is_unique,
            });
        }

        Ok(columns)
    }

    async fn get_indexes_limbo(&self, table_name: &str) -> DatabaseResult<Vec<IndexDef>> {
        let sql = format!("PRAGMA index_list({})", table_name);
        let mut rows = self.conn.query(&sql).await?;

        let mut indexes = Vec::new();
        while let Some(row) = rows.next().await? {
            let index_name = row.get_string(1)?;
            let unique = row.get_i64(2)? != 0;

            let columns = self.get_index_columns_limbo(&index_name).await?;

            indexes.push(IndexDef {
                name: index_name,
                table_name: table_name.to_string(),
                columns,
                unique,
            });
        }

        Ok(indexes)
    }

    async fn get_indexes_postgres(&self, table_name: &str) -> DatabaseResult<Vec<IndexDef>> {
        let sql = format!(
            "SELECT i.relname AS index_name, ix.indisunique AS is_unique, \
             array_agg(a.attname ORDER BY array_position(ix.indkey, a.attnum)) AS columns \
             FROM pg_index ix \
             JOIN pg_class i ON i.oid = ix.indexrelid \
             JOIN pg_class t ON t.oid = ix.indrelid \
             JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(ix.indkey) \
             WHERE t.relname = '{}' \
             GROUP BY i.relname, ix.indisunique",
            table_name
        );
        let mut rows = self.conn.query(&sql).await?;

        let mut indexes = Vec::new();
        while let Some(row) = rows.next().await? {
            let index_name = row.get_string(0)?;
            let unique = row.get_bool(1)?;
            let columns_str = row.get_string(2)?;
            let columns: Vec<String> = columns_str
                .trim_matches(|c| c == '{' || c == '}')
                .split(',')
                .map(|s| s.trim().trim_matches('"').to_string())
                .collect();

            indexes.push(IndexDef {
                name: index_name,
                table_name: table_name.to_string(),
                columns,
                unique,
            });
        }

        Ok(indexes)
    }

    async fn get_indexes_mysql(&self, table_name: &str) -> DatabaseResult<Vec<IndexDef>> {
        let sql = format!(
            "SELECT index_name, non_unique = 0 AS is_unique, \
             GROUP_CONCAT(column_name ORDER BY seq_in_index SEPARATOR ',') AS columns \
             FROM information_schema.statistics \
             WHERE table_name = '{}' AND table_schema = DATABASE() \
             GROUP BY index_name, non_unique",
            table_name
        );
        let mut rows = self.conn.query(&sql).await?;

        let mut indexes = Vec::new();
        while let Some(row) = rows.next().await? {
            let index_name = row.get_string(0)?;
            let unique = row.get_bool(1)?;
            let columns_str = row.get_string(2)?;
            let columns: Vec<String> = columns_str.split(',').map(|s| s.trim().to_string()).collect();

            indexes.push(IndexDef {
                name: index_name,
                table_name: table_name.to_string(),
                columns,
                unique,
            });
        }

        Ok(indexes)
    }

    async fn get_foreign_keys_limbo(&self, table_name: &str) -> DatabaseResult<Vec<ForeignKeyDef>> {
        let sql = format!("PRAGMA foreign_key_list({})", table_name);
        let mut rows = self.conn.query(&sql).await?;

        let mut foreign_keys = Vec::new();
        while let Some(row) = rows.next().await? {
            let id = row.get_i64(0)?;
            let seq = row.get_i64(1)?;
            if seq != 0 {
                continue;
            }
            let ref_table = row.get_string(2)?;
            let column = row.get_string(3)?;
            let ref_column = row.get_string(4)?;
            let on_update_str = row.get_string(5)?;
            let on_delete_str = row.get_string(6)?;

            let on_update = ReferentialAction::from_str(&on_update_str);
            let on_delete = ReferentialAction::from_str(&on_delete_str);

            let fk_name = format!("fk_{}_{}", table_name, column);

            foreign_keys.push(ForeignKeyDef {
                name: fk_name,
                column,
                ref_table,
                ref_column,
                on_update,
                on_delete,
            });
        }

        Ok(foreign_keys)
    }

    async fn get_foreign_keys_postgres(&self, table_name: &str) -> DatabaseResult<Vec<ForeignKeyDef>> {
        let sql = format!(
            "SELECT tc.constraint_name, kcu.column_name, ccu.table_name AS foreign_table_name, \
             ccu.column_name AS foreign_column_name, rc.update_rule, rc.delete_rule \
             FROM information_schema.table_constraints tc \
             JOIN information_schema.key_column_usage kcu \
             ON tc.constraint_name = kcu.constraint_name \
             JOIN information_schema.constraint_column_usage ccu \
             ON ccu.constraint_name = tc.constraint_name \
             JOIN information_schema.referential_constraints rc \
             ON tc.constraint_name = rc.constraint_name \
             WHERE tc.table_name = '{}' AND tc.constraint_type = 'FOREIGN KEY'",
            table_name
        );
        let mut rows = self.conn.query(&sql).await?;

        let mut foreign_keys = Vec::new();
        while let Some(row) = rows.next().await? {
            let name = row.get_string(0)?;
            let column = row.get_string(1)?;
            let ref_table = row.get_string(2)?;
            let ref_column = row.get_string(3)?;
            let on_update_str = row.get_string(4)?;
            let on_delete_str = row.get_string(5)?;

            let on_update = ReferentialAction::from_str(&on_update_str);
            let on_delete = ReferentialAction::from_str(&on_delete_str);

            foreign_keys.push(ForeignKeyDef {
                name,
                column,
                ref_table,
                ref_column,
                on_update,
                on_delete,
            });
        }

        Ok(foreign_keys)
    }

    async fn get_foreign_keys_mysql(&self, table_name: &str) -> DatabaseResult<Vec<ForeignKeyDef>> {
        let sql = format!(
            "SELECT kcu.constraint_name, kcu.column_name, kcu.referenced_table_name, kcu.referenced_column_name, \
             rc.update_rule, rc.delete_rule \
             FROM information_schema.key_column_usage kcu \
             JOIN information_schema.referential_constraints rc \
             ON kcu.constraint_name = rc.constraint_name \
             WHERE kcu.table_name = '{}' AND kcu.table_schema = DATABASE() AND kcu.referenced_table_name IS NOT NULL",
            table_name
        );
        let mut rows = self.conn.query(&sql).await?;

        let mut foreign_keys = Vec::new();
        while let Some(row) = rows.next().await? {
            let name = row.get_string(0)?;
            let column = row.get_string(1)?;
            let ref_table = row.get_string(2)?;
            let ref_column = row.get_string(3)?;
            let on_update_str = row.get_string(4)?;
            let on_delete_str = row.get_string(5)?;

            let on_update = ReferentialAction::from_str(&on_update_str);
            let on_delete = ReferentialAction::from_str(&on_delete_str);

            foreign_keys.push(ForeignKeyDef {
                name,
                column,
                ref_table,
                ref_column,
                on_update,
                on_delete,
            });
        }

        Ok(foreign_keys)
    }

    async fn table_exists_limbo(&self, table_name: &str) -> DatabaseResult<bool> {
        let sql = format!("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='{}'", table_name);
        let mut rows = self.conn.query(&sql).await?;

        if let Some(row) = rows.next().await? {
            let count = row.get_i64(0)?;
            Ok(count > 0)
        }
        else {
            Ok(false)
        }
    }

    async fn table_exists_postgres(&self, table_name: &str) -> DatabaseResult<bool> {
        let sql = format!("SELECT COUNT(*) FROM pg_tables WHERE schemaname = 'public' AND tablename = '{}'", table_name);
        let mut rows = self.conn.query(&sql).await?;

        if let Some(row) = rows.next().await? {
            let count = row.get_i64(0)?;
            Ok(count > 0)
        }
        else {
            Ok(false)
        }
    }

    async fn table_exists_mysql(&self, table_name: &str) -> DatabaseResult<bool> {
        let sql = format!(
            "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = '{}'",
            table_name
        );
        let mut rows = self.conn.query(&sql).await?;

        if let Some(row) = rows.next().await? {
            let count = row.get_i64(0)?;
            Ok(count > 0)
        }
        else {
            Ok(false)
        }
    }

    async fn get_index_columns_limbo(&self, index_name: &str) -> DatabaseResult<Vec<String>> {
        let sql = format!("PRAGMA index_info({})", index_name);
        let mut rows = self.conn.query(&sql).await?;

        let mut columns = Vec::new();
        while let Some(row) = rows.next().await? {
            columns.push(row.get_string(2)?);
        }

        Ok(columns)
    }
}