rustf-cli 1.0.0

CLI tool for analyzing RustF projects with MCP server support for AI agents
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
//! MySQL database introspection implementation

use super::{common::*, DatabaseIntrospector};
use anyhow::Result;
use async_trait::async_trait;
use serde_json::{Map, Value};
use sqlx::{Column, MySql, Pool, Row, TypeInfo, ValueRef};
use std::collections::HashMap;
use std::path::PathBuf;
use tokio::fs;

pub struct MySqlIntrospector {
    pool: Pool<MySql>,
    db_name: String,
}

impl MySqlIntrospector {
    pub async fn new(database_url: &str) -> Result<Self> {
        let pool = sqlx::mysql::MySqlPoolOptions::new()
            .max_connections(5)
            .connect(database_url)
            .await?;
            
        // Get database name
        let db_name_row = sqlx::query("SELECT DATABASE() as db_name")
            .fetch_one(&pool)
            .await?;
        let db_name: String = db_name_row.try_get("db_name")?;
        
        Ok(Self { pool, db_name })
    }
    
    /// Parse enum values from MySQL COLUMN_TYPE field
    /// Example: "enum('male','female','other')" -> vec!["male", "female", "other"]
    fn parse_enum_values(column_type: &str) -> Vec<String> {
        if !column_type.starts_with("enum(") || !column_type.ends_with(")") {
            return Vec::new();
        }
        
        let values_part = &column_type[5..column_type.len()-1]; // Remove "enum(" and ")"
        let mut values = Vec::new();
        let mut current_value = String::new();
        let mut in_quotes = false;
        let mut escape_next = false;
        
        for ch in values_part.chars() {
            if escape_next {
                current_value.push(ch);
                escape_next = false;
            } else if ch == '\\' {
                escape_next = true;
            } else if ch == '\'' {
                if in_quotes {
                    // End of quoted value
                    values.push(current_value.clone());
                    current_value.clear();
                    in_quotes = false;
                } else {
                    // Start of quoted value
                    in_quotes = true;
                }
            } else if in_quotes {
                current_value.push(ch);
            }
            // Ignore commas and spaces outside quotes
        }
        
        values
    }
    
    fn generate_field_yaml(&self, column: &ColumnInfo) -> String {
        let mut field = String::new();
        let escaped_field_name = escape_yaml_field_name(&column.name);
        field.push_str(&format!("    {}:\n", escaped_field_name));
        
        // Extract enum values if this is an enum type
        let enum_values = if let Some(column_type) = &column.column_type {
            Self::parse_enum_values(column_type)
        } else {
            Vec::new()
        };
        
        // Handle enum types
        if !enum_values.is_empty() {
            field.push_str("      type: enum\n");
            field.push_str("      values:\n");
            for value in &enum_values {
                field.push_str(&format!("        - \"{}\"\n", value));
            }
            // Set default to first enum value if no default specified
            if column.default_value.is_none() {
                if let Some(first_value) = enum_values.first() {
                    field.push_str(&format!("      default: \"{}\"\n", first_value));
                }
            } else if let Some(default) = &column.default_value {
                let clean_default = default.trim_matches('\'').trim_matches('"');
                field.push_str(&format!("      default: \"{}\"\n", clean_default));
            }
        } else {
            // Regular data type
            let data_type = if column.data_type.contains("varchar") || column.data_type.contains("char") {
                if let Some(max_length) = column.max_length {
                    format!("string({})", max_length)
                } else {
                    "string".to_string()
                }
            } else if column.data_type == "text" {
                "text".to_string()  // Don't parameterize text type
            } else if column.data_type.contains("decimal") {
                if let (Some(precision), Some(scale)) = (column.numeric_precision, column.numeric_scale) {
                    format!("decimal({},{})", precision, scale)
                } else {
                    "decimal".to_string()
                }
            } else {
                column.data_type.clone()
            };
            field.push_str(&format!("      type: {}\n", data_type));
            
            // Add default value or auto-increment flag
            if let Some(default) = &column.default_value {
                if default == "AUTO_INCREMENT" {
                    field.push_str("      auto: true\n");
                } else if !default.is_empty() && default != "NULL" {
                    field.push_str(&format!("      default: {}\n", default));
                }
            }
        }
        
        // Add lang_type
        let lang_type = self.map_to_rust_type(&column.data_type, column.is_nullable);
        field.push_str(&format!("      lang_type: {}\n", lang_type));
        
        // Add nullable flag
        if column.is_nullable {
            field.push_str("      nullable: true\n");
        } else {
            field.push_str("      required: true\n");
        }
        
        // Add primary key flag
        if column.is_primary_key {
            field.push_str("      primary_key: true\n");
        }
        
        // Add unique flag
        if column.is_unique {
            field.push_str("      unique: true\n");
        }
        
        // Add AI hint
        let ai_hint = generate_field_ai_hint(&column.name, &column.data_type, column.is_foreign_key);
        field.push_str(&format!("      ai: \"{}\"\n", ai_hint));
        
        // Add foreign key reference
        if column.is_foreign_key {
            if let (Some(table), Some(col)) = (&column.foreign_table, &column.foreign_column) {
                field.push_str(&format!("      foreign_key: {}.{}\n", table, col));
            }
        }
        
        // Add column comment if available
        if let Some(comment) = &column.comment {
            if !comment.is_empty() {
                field.push_str(&format!("      column_comment: \"{}\"\n", comment));
            }
        }
        
        field.push_str("\n");
        field
    }
    
    fn map_to_rust_type(&self, data_type: &str, is_nullable: bool) -> String {
        let base_type = match data_type {
            "int" | "integer" => "i32",
            "bigint" => "i64",
            "smallint" => "i16",
            "tinyint" => "i8",
            "mediumint" => "i32",
            "float" => "f32",
            "double" | "real" => "f64",
            "decimal" | "numeric" => "Decimal",
            "boolean" | "bool" => "bool",
            "varchar" | "char" | "text" | "longtext" | "mediumtext" | "tinytext" => "String",
            "date" => "NaiveDate",
            "time" => "NaiveTime",
            "datetime" | "timestamp" => "DateTime<Utc>",
            "json" => "serde_json::Value",
            "enum" => "String",
            _ => "String",
        };
        
        if is_nullable {
            format!("Option<{}>", base_type)
        } else {
            base_type.to_string()
        }
    }
    
    /// Fetch the SELECT body of a view from `information_schema.VIEWS`.
    /// Returns `None` for non-views or when no definition is stored.
    async fn get_view_definition(&self, name: &str) -> Result<Option<String>> {
        let row = sqlx::query(
            r#"
            SELECT VIEW_DEFINITION AS def
            FROM information_schema.VIEWS
            WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?
            "#,
        )
        .bind(name)
        .fetch_optional(&self.pool)
        .await?;

        let def = match row {
            Some(r) => r.try_get::<Option<String>, _>("def").ok().flatten(),
            None => None,
        };

        Ok(def
            .map(|s| s.trim().trim_end_matches(';').trim().to_string())
            .filter(|s| !s.is_empty()))
    }

    async fn generate_table_schema_yaml(&self, description: &TableDescription) -> Result<String> {
        let mut yaml = String::new();

        // Add header comment
        yaml.push_str(&format!("# {} entity - Generated from database\n\n", description.table.name));

        // Model name (convert table name to PascalCase)
        let model_name = to_pascal_case(&description.table.name);
        yaml.push_str(&format!("{}:\n", model_name));
        yaml.push_str(&format!("  table: {}\n", description.table.name));
        yaml.push_str(&format!("  database_type: mysql\n"));
        yaml.push_str(&format!("  database_name: {}\n", self.db_name));

        // Determine element type (table or view). MySQL has no materialized views.
        let element_type = if description.table.table_type.to_lowercase().contains("view") {
            "view"
        } else {
            "table"
        };
        yaml.push_str(&format!("  element_type: {}\n", element_type));
        yaml.push_str("  version: 1\n");

        // For views, capture the SQL body so DDL generation can recreate them.
        if element_type == "view" {
            if let Some(body) = self.get_view_definition(&description.table.name).await? {
                yaml.push_str("  view:\n");
                yaml.push_str("    sql: |\n");
                for line in body.lines() {
                    yaml.push_str(&format!("      {}\n", line));
                }
            }
        }
        
        // Add description if available
        if let Some(comment) = &description.table.comment {
            if !comment.is_empty() {
                yaml.push_str(&format!("  description: \"{}\"\n", comment));
            }
        }
        
        // Add AI context based on table name
        let ai_context = generate_table_ai_context(&description.table.name);
        yaml.push_str(&format!("  ai_context: \"{}\"\n", ai_context));
        
        yaml.push_str("  \n  fields:\n");
        
        // Generate fields
        for column in &description.columns {
            yaml.push_str(&self.generate_field_yaml(column));
        }
        
        // Generate relations if foreign keys exist
        let relations = generate_relations_yaml(&description.columns);
        if !relations.is_empty() {
            yaml.push_str("  \n  relations:\n");
            yaml.push_str(&relations);
        }
        
        // Generate indexes
        if !description.indexes.is_empty() {
            yaml.push_str("  \n  indexes:\n");
            for index in &description.indexes {
                if index.columns.len() == 1 {
                    yaml.push_str(&format!("    - {}\n", index.columns[0]));
                } else {
                    yaml.push_str(&format!("    - [{}]\n", index.columns.join(", ")));
                }
            }
        }
        
        // Generate triggers
        if !description.triggers.is_empty() {
            yaml.push_str("  \n  triggers:\n");
            for trigger in &description.triggers {
                yaml.push_str(&format!("    - name: {}\n", trigger.name));
                yaml.push_str(&format!("      event: {}\n", trigger.event));
                yaml.push_str(&format!("      timing: {}\n", trigger.timing));
                yaml.push_str(&format!("      for_each: {}\n", trigger.for_each));
                yaml.push_str(&format!("      ai: \"Database trigger - executes automatically on table changes. Consider application logic implications.\"\n"));
                yaml.push_str("\n");
            }
        }
        
        yaml.push_str("\n");
        Ok(yaml)
    }
}

#[async_trait]
impl DatabaseIntrospector for MySqlIntrospector {
    async fn list_tables(&self, metadata: bool) -> Result<Vec<TableInfo>> {
        let query = if metadata {
            r#"
            SELECT 
                CAST(t.TABLE_NAME AS CHAR) as table_name,
                CAST(t.TABLE_SCHEMA AS CHAR) as table_schema,
                CAST(t.TABLE_TYPE AS CHAR) as table_type,
                t.TABLE_ROWS as row_count,
                (t.DATA_LENGTH + t.INDEX_LENGTH) as size_bytes,
                CAST(t.TABLE_COMMENT AS CHAR) as comment
            FROM information_schema.tables t
            WHERE t.TABLE_SCHEMA = ?
            ORDER BY t.TABLE_NAME
            "#
        } else {
            r#"
            SELECT 
                CAST(TABLE_NAME AS CHAR) as table_name,
                CAST(TABLE_SCHEMA AS CHAR) as table_schema,
                CAST(TABLE_TYPE AS CHAR) as table_type,
                NULL as row_count,
                NULL as size_bytes,
                NULL as comment
            FROM information_schema.tables
            WHERE TABLE_SCHEMA = ?
            ORDER BY TABLE_NAME
            "#
        };
        
        let rows = sqlx::query(query)
            .bind(&self.db_name)
            .fetch_all(&self.pool)
            .await?;
            
        let mut tables = Vec::new();
        for row in rows {
            tables.push(TableInfo {
                name: row.try_get("table_name")?,
                schema: row.try_get("table_schema").ok(),
                table_type: row.try_get("table_type")?,
                row_count: row.try_get("row_count").ok().flatten(),
                size_bytes: row.try_get("size_bytes").ok().flatten(),
                comment: row.try_get("comment").ok().flatten(),
            });
        }
        
        Ok(tables)
    }
    
    async fn describe_table(&self, table_name: &str) -> Result<TableDescription> {
        // Get table info
        let table_row = sqlx::query(
            r#"
            SELECT 
                CAST(TABLE_NAME AS CHAR) as table_name,
                CAST(TABLE_SCHEMA AS CHAR) as table_schema,
                CAST(TABLE_TYPE AS CHAR) as table_type,
                TABLE_ROWS as row_count,
                (DATA_LENGTH + INDEX_LENGTH) as size_bytes,
                CAST(TABLE_COMMENT AS CHAR) as comment
            FROM information_schema.tables
            WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?
            "#
        )
        .bind(table_name)
        .bind(&self.db_name)
        .fetch_optional(&self.pool)
        .await?;
        
        let table_info = match table_row {
            Some(row) => TableInfo {
                name: row.try_get("table_name")?,
                schema: row.try_get("table_schema").ok(),
                table_type: row.try_get("table_type")?,
                row_count: row.try_get("row_count").ok().flatten(),
                size_bytes: row.try_get("size_bytes").ok().flatten(),
                comment: row.try_get("comment").ok().flatten(),
            },
            None => anyhow::bail!("Table '{}' not found", table_name),
        };
        
        // Get column information
        let column_rows = sqlx::query(
            r#"
            SELECT 
                CAST(c.COLUMN_NAME AS CHAR) as column_name,
                CAST(c.DATA_TYPE AS CHAR) as data_type,
                CAST(c.COLUMN_TYPE AS CHAR) as column_type,
                c.IS_NULLABLE as is_nullable,
                c.COLUMN_DEFAULT as column_default,
                c.CHARACTER_MAXIMUM_LENGTH as character_maximum_length,
                c.NUMERIC_PRECISION as numeric_precision,
                c.NUMERIC_SCALE as numeric_scale,
                CAST(c.COLUMN_COMMENT AS CHAR) as column_comment,
                CAST(c.COLUMN_KEY AS CHAR) as column_key,
                CAST(c.EXTRA AS CHAR) as extra
            FROM information_schema.columns c
            WHERE c.TABLE_NAME = ? AND c.TABLE_SCHEMA = ?
            ORDER BY c.ORDINAL_POSITION
            "#
        )
        .bind(table_name)
        .bind(&self.db_name)
        .fetch_all(&self.pool)
        .await?;
        
        // Get foreign key information
        let fk_rows = sqlx::query(
            r#"
            SELECT 
                CAST(kcu.COLUMN_NAME AS CHAR) as column_name,
                CAST(kcu.REFERENCED_TABLE_NAME AS CHAR) as referenced_table_name,
                CAST(kcu.REFERENCED_COLUMN_NAME AS CHAR) as referenced_column_name,
                CAST(rc.DELETE_RULE AS CHAR) as on_delete,
                CAST(rc.UPDATE_RULE AS CHAR) as on_update
            FROM information_schema.key_column_usage kcu
            LEFT JOIN information_schema.REFERENTIAL_CONSTRAINTS rc
                ON rc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
                AND rc.CONSTRAINT_SCHEMA = kcu.TABLE_SCHEMA
            WHERE kcu.TABLE_NAME = ? 
                AND kcu.TABLE_SCHEMA = ?
                AND kcu.REFERENCED_TABLE_NAME IS NOT NULL
            "#
        )
        .bind(table_name)
        .bind(&self.db_name)
        .fetch_all(&self.pool)
        .await?;
        
        // Build foreign key lookup
        let mut foreign_keys = HashMap::new();
        for fk_row in fk_rows {
            let column_name: String = fk_row.try_get("column_name")?;
            let referenced_table: String = fk_row.try_get("referenced_table_name")?;
            let referenced_column: String = fk_row.try_get("referenced_column_name")?;
            let on_delete: Option<String> = fk_row.try_get("on_delete").ok();
            let on_update: Option<String> = fk_row.try_get("on_update").ok();
            foreign_keys.insert(column_name, (referenced_table, referenced_column, on_delete, on_update));
        }
        
        let mut columns = Vec::new();
        for row in column_rows {
            let column_name: String = row.try_get("column_name")?;
            let column_key: String = row.try_get("column_key").unwrap_or_default();
            
            // Check for auto_increment in the EXTRA column
            let extra: Option<String> = row.try_get("extra").ok();
            let is_auto_increment = extra.as_ref().map_or(false, |s| s.contains("auto_increment"));
            
            let is_primary_key = column_key == "PRI";
            let is_unique = column_key == "UNI";
            let is_foreign_key = foreign_keys.contains_key(&column_name);
            
            let (foreign_table, foreign_column, on_delete, on_update) = if is_foreign_key {
                let (table, column, del, upd) = foreign_keys.get(&column_name).unwrap();
                (Some(table.clone()), Some(column.clone()), del.clone(), upd.clone())
            } else {
                (None, None, None, None)
            };
            
            // Mark auto-increment fields with a special default value marker
            let default_value = if is_auto_increment {
                Some("AUTO_INCREMENT".to_string())
            } else {
                row.try_get("column_default").ok()
            };
            
            columns.push(ColumnInfo {
                name: column_name,
                data_type: row.try_get("data_type")?,
                column_type: row.try_get("column_type").ok(),
                postgres_type_name: None, // MySQL doesn't use PostgreSQL type names
                is_nullable: row.try_get::<&str, _>("is_nullable")? == "YES",
                default_value,
                is_primary_key,
                is_unique,
                is_foreign_key,
                foreign_table,
                foreign_column,
                on_delete,
                on_update,
                comment: row.try_get("column_comment").ok(),
                max_length: row.try_get("character_maximum_length").ok(),
                numeric_precision: row.try_get("numeric_precision").ok(),
                numeric_scale: row.try_get("numeric_scale").ok(),
            });
        }
        
        // Get indexes
        let index_rows = sqlx::query(
            r#"
            SELECT 
                CAST(s.INDEX_NAME AS CHAR) as index_name,
                CAST(s.COLUMN_NAME AS CHAR) as column_name,
                s.NON_UNIQUE as non_unique,
                s.SEQ_IN_INDEX as seq_in_index
            FROM information_schema.statistics s
            WHERE s.TABLE_NAME = ? AND s.TABLE_SCHEMA = ?
            ORDER BY s.INDEX_NAME, s.SEQ_IN_INDEX
            "#
        )
        .bind(table_name)
        .bind(&self.db_name)
        .fetch_all(&self.pool)
        .await?;
        
        // Group indexes by name
        let mut index_map: HashMap<String, IndexInfo> = HashMap::new();
        for row in index_rows {
            let index_name: String = row.try_get("index_name")?;
            let column_name: String = row.try_get("column_name")?;
            let non_unique: i32 = row.try_get("non_unique")?;
            
            index_map.entry(index_name.clone())
                .or_insert_with(|| IndexInfo {
                    name: index_name.clone(),
                    columns: Vec::new(),
                    is_unique: non_unique == 0,
                    is_primary: index_name == "PRIMARY",
                })
                .columns.push(column_name);
        }
        let indexes: Vec<IndexInfo> = index_map.into_values().collect();
        
        // Get triggers
        let trigger_rows = sqlx::query(
            r#"
            SELECT 
                CAST(TRIGGER_NAME AS CHAR) as trigger_name,
                CAST(EVENT_MANIPULATION AS CHAR) as event,
                CAST(ACTION_TIMING AS CHAR) as timing,
                CAST(ACTION_ORIENTATION AS CHAR) as for_each,
                CAST(ACTION_CONDITION AS CHAR) as condition_expr,
                CAST(ACTION_STATEMENT AS CHAR) as body
            FROM information_schema.TRIGGERS
            WHERE EVENT_OBJECT_TABLE = ? AND EVENT_OBJECT_SCHEMA = ?
            "#
        )
        .bind(table_name)
        .bind(&self.db_name)
        .fetch_all(&self.pool)
        .await?;
        
        let mut triggers = Vec::new();
        for row in trigger_rows {
            triggers.push(TriggerInfo {
                name: row.try_get("trigger_name")?,
                event: row.try_get("event").unwrap_or_default(),
                timing: row.try_get("timing").unwrap_or_default(),
                for_each: row.try_get("for_each").unwrap_or_default(),
                condition: row.try_get("condition_expr").ok(),
                body: row.try_get("body").ok(),
                description: None,
            });
        }
        
        // Surface foreign-key relationships as constraints, matching the
        // SQLite/PostgreSQL introspectors. (MySQL CHECK constraints are only
        // reliably queryable on 8.0.16+ and are left out for now.)
        let mut constraints: Vec<ConstraintInfo> = foreign_keys
            .iter()
            .map(|(column, (ftable, fcol, on_delete, on_update))| ConstraintInfo {
                name: format!("fk_{}_{}", table_name, column),
                constraint_type: "FOREIGN KEY".to_string(),
                columns: vec![column.clone()],
                foreign_table: Some(ftable.clone()),
                foreign_columns: Some(vec![fcol.clone()]),
                check_expression: None,
                on_delete: on_delete.clone(),
                on_update: on_update.clone(),
            })
            .collect();
        constraints.sort_by(|a, b| a.name.cmp(&b.name));

        Ok(TableDescription {
            table: table_info,
            columns,
            indexes,
            constraints,
            triggers,
        })
    }
    
    async fn generate_schemas(
        &self,
        output_dir: &PathBuf,
        force: bool,
        filter_tables: &[String],
    ) -> Result<()> {
        // Create output directory if it doesn't exist
        if !output_dir.exists() {
            fs::create_dir_all(output_dir).await?;
        }
        
        let tables = self.list_tables(true).await?;
        println!("📋 Found {} tables", tables.len());
        
        let mut generated_count = 0;
        let mut skipped_count = 0;
        
        for table in tables {
            // Filter tables if specified
            if !filter_tables.is_empty() && !filter_tables.contains(&table.name) {
                continue;
            }
            
            let schema_file = output_dir.join(format!("{}.yaml", table.name));
            
            // Skip if file exists and force is false
            if schema_file.exists() && !force {
                println!("⚠️  Skipping existing schema: {}", schema_file.display());
                skipped_count += 1;
                continue;
            }
            
            // Get detailed table description
            let description = self.describe_table(&table.name).await?;
            
            // Generate YAML schema
            let yaml_content = self.generate_table_schema_yaml(&description).await?;
            
            // Write schema file
            fs::write(&schema_file, yaml_content).await?;
            println!("✅ Generated schema: {}", schema_file.display());
            generated_count += 1;
        }
        
        // Generate _meta.yaml
        let meta_file = output_dir.join("_meta.yaml");
        if !meta_file.exists() || force {
            let meta_content = self.generate_meta_yaml().await?;
            fs::write(&meta_file, meta_content).await?;
            println!("✅ Generated metadata: {}", meta_file.display());
        }
        
        println!("📊 Generated {} schemas, skipped {}", generated_count, skipped_count);
        Ok(())
    }
    
    async fn generate_meta_yaml(&self) -> Result<String> {
        let mut yaml = String::new();
        yaml.push_str("# Global schema configuration - Generated from MySQL database\n\n");
        yaml.push_str("version: \"1.0\"\n");
        yaml.push_str("database_type: mysql\n");
        yaml.push_str(&format!("database_name: {}\n", self.db_name));
        yaml.push_str(&format!("description: \"Schema generated from MySQL database '{}'\"\n", self.db_name));
        yaml.push_str("ai_context: \"Generated schemas from existing MySQL database structure with intelligent field mapping\"\n");
        yaml.push_str("\n");
        yaml.push_str("# Global field defaults\n");
        yaml.push_str("field_defaults:\n");
        yaml.push_str("  string:\n");
        yaml.push_str("    max_length: 255\n");
        yaml.push_str("    charset: utf8mb4\n");
        yaml.push_str("    \n");
        yaml.push_str("  timestamp:\n");
        yaml.push_str("    default: now\n");
        yaml.push_str("    on_update: now\n");
        yaml.push_str("    \n");
        yaml.push_str("# Code generation settings\n");
        yaml.push_str("generation:\n");
        yaml.push_str("  base_class: \"BaseModel\"\n");
        yaml.push_str("  use_traits: [HasTimestamps]\n");
        yaml.push_str("\n");
        
        Ok(yaml)
    }
    
    async fn get_database_name(&self) -> Result<String> {
        Ok(self.db_name.clone())
    }
    
    async fn export_data(&self, query: &str, format: &str) -> Result<String> {
        let rows = sqlx::query(query).fetch_all(&self.pool).await?;
        let headers = rows
            .first()
            .map(|row| {
                row.columns()
                    .iter()
                    .map(|column| column.name().to_string())
                    .collect::<Vec<_>>()
            })
            .unwrap_or_default();

        let mut json_rows = Vec::new();
        for row in &rows {
            let mut object = Map::new();
            for (index, column) in row.columns().iter().enumerate() {
                object.insert(
                    column.name().to_string(),
                    Self::mysql_value_to_json(row, index)?,
                );
            }
            json_rows.push(Value::Object(object));
        }

        match format {
            "json" => Ok(serde_json::to_string_pretty(&json_rows)?),
            "csv" => Ok(rows_to_csv(&headers, &json_rows)),
            _ => anyhow::bail!("Unsupported export format: {}", format),
        }
    }
}

impl MySqlIntrospector {
    /// Convert a single MySQL cell to JSON, dispatching on the column's SQL type.
    fn mysql_value_to_json(row: &sqlx::mysql::MySqlRow, index: usize) -> Result<Value> {
        let raw = row.try_get_raw(index)?;
        if raw.is_null() {
            return Ok(Value::Null);
        }

        let type_name = raw.type_info().name().to_uppercase();

        if type_name.contains("INT") {
            if let Ok(v) = row.try_get::<i64, _>(index) {
                return Ok(Value::from(v));
            }
            if let Ok(v) = row.try_get::<u64, _>(index) {
                return Ok(Value::from(v));
            }
        }
        if type_name.contains("DOUBLE") || type_name.contains("FLOAT") {
            if let Ok(v) = row.try_get::<f64, _>(index) {
                return Ok(Value::from(v));
            }
        }
        if type_name.contains("DECIMAL") || type_name.contains("NUMERIC") {
            if let Ok(v) = row.try_get::<sqlx::types::BigDecimal, _>(index) {
                return Ok(Value::String(v.to_string()));
            }
        }
        if type_name.contains("JSON") {
            if let Ok(v) = row.try_get::<Value, _>(index) {
                return Ok(v);
            }
        }
        if type_name.contains("DATETIME") || type_name.contains("TIMESTAMP") {
            if let Ok(v) = row.try_get::<chrono::NaiveDateTime, _>(index) {
                return Ok(Value::String(v.to_string()));
            }
            if let Ok(v) = row.try_get::<chrono::DateTime<chrono::Utc>, _>(index) {
                return Ok(Value::String(v.to_rfc3339()));
            }
        }
        if type_name == "DATE" {
            if let Ok(v) = row.try_get::<chrono::NaiveDate, _>(index) {
                return Ok(Value::String(v.to_string()));
            }
        }
        if type_name == "TIME" {
            if let Ok(v) = row.try_get::<chrono::NaiveTime, _>(index) {
                return Ok(Value::String(v.to_string()));
            }
        }
        if type_name.contains("BLOB") || type_name.contains("BINARY") {
            if let Ok(v) = row.try_get::<Vec<u8>, _>(index) {
                return Ok(Value::String(
                    v.iter().map(|b| format!("{:02x}", b)).collect::<String>(),
                ));
            }
        }
        if let Ok(v) = row.try_get::<String, _>(index) {
            return Ok(Value::String(v));
        }

        Ok(Value::Null)
    }
}