|
1 | | -use vespertide_core::TableDef; |
2 | | - |
3 | | -use crate::{seaorm::SeaOrmExporter, sqlalchemy::SqlAlchemyExporter, sqlmodel::SqlModelExporter}; |
4 | | - |
5 | | -/// Supported ORM targets. |
6 | | -#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
7 | | -pub enum Orm { |
8 | | - SeaOrm, |
9 | | - SqlAlchemy, |
10 | | - SqlModel, |
11 | | -} |
12 | | - |
13 | | -/// Standardized exporter interface for all supported ORMs. |
14 | | -pub trait OrmExporter { |
15 | | - fn render_entity(&self, table: &TableDef) -> Result<String, String>; |
16 | | - |
17 | | - /// Render entity with schema context for FK chain resolution. |
18 | | - /// Default implementation ignores schema context. |
19 | | - fn render_entity_with_schema( |
20 | | - &self, |
21 | | - table: &TableDef, |
22 | | - _schema: &[TableDef], |
23 | | - ) -> Result<String, String> { |
24 | | - self.render_entity(table) |
25 | | - } |
26 | | -} |
27 | | - |
28 | | -/// Render a single table definition for the selected ORM. |
29 | | -pub fn render_entity(orm: Orm, table: &TableDef) -> Result<String, String> { |
30 | | - match orm { |
31 | | - Orm::SeaOrm => SeaOrmExporter.render_entity(table), |
32 | | - Orm::SqlAlchemy => SqlAlchemyExporter.render_entity(table), |
33 | | - Orm::SqlModel => SqlModelExporter.render_entity(table), |
34 | | - } |
35 | | -} |
36 | | - |
37 | | -/// Render a single table definition with full schema context for FK chain resolution. |
38 | | -pub fn render_entity_with_schema( |
39 | | - orm: Orm, |
40 | | - table: &TableDef, |
41 | | - schema: &[TableDef], |
42 | | -) -> Result<String, String> { |
43 | | - match orm { |
44 | | - Orm::SeaOrm => SeaOrmExporter.render_entity_with_schema(table, schema), |
45 | | - Orm::SqlAlchemy => SqlAlchemyExporter.render_entity_with_schema(table, schema), |
46 | | - Orm::SqlModel => SqlModelExporter.render_entity_with_schema(table, schema), |
47 | | - } |
48 | | -} |
49 | | - |
50 | | -#[cfg(test)] |
51 | | -mod tests { |
52 | | - use super::*; |
53 | | - use insta::{assert_snapshot, with_settings}; |
54 | | - use rstest::rstest; |
55 | | - use vespertide_core::{ColumnDef, ColumnType, SimpleColumnType, TableConstraint}; |
56 | | - |
57 | | - fn simple_table() -> TableDef { |
58 | | - TableDef { |
59 | | - name: "test".into(), |
60 | | - description: None, |
61 | | - columns: vec![ColumnDef { |
62 | | - name: "id".into(), |
63 | | - r#type: ColumnType::Simple(SimpleColumnType::Integer), |
64 | | - nullable: false, |
65 | | - default: None, |
66 | | - comment: None, |
67 | | - primary_key: None, |
68 | | - unique: None, |
69 | | - index: None, |
70 | | - foreign_key: None, |
71 | | - }], |
72 | | - constraints: vec![TableConstraint::PrimaryKey { |
73 | | - auto_increment: false, |
74 | | - columns: vec!["id".into()], |
75 | | - }], |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - #[rstest] |
80 | | - #[case("seaorm", Orm::SeaOrm)] |
81 | | - #[case("sqlalchemy", Orm::SqlAlchemy)] |
82 | | - #[case("sqlmodel", Orm::SqlModel)] |
83 | | - fn test_render_entity_snapshots(#[case] name: &str, #[case] orm: Orm) { |
84 | | - let table = simple_table(); |
85 | | - let result = render_entity(orm, &table); |
86 | | - assert!(result.is_ok()); |
87 | | - with_settings!({ snapshot_suffix => name }, { |
88 | | - assert_snapshot!(result.unwrap()); |
89 | | - }); |
90 | | - } |
91 | | - |
92 | | - #[rstest] |
93 | | - #[case("seaorm", Orm::SeaOrm)] |
94 | | - #[case("sqlalchemy", Orm::SqlAlchemy)] |
95 | | - #[case("sqlmodel", Orm::SqlModel)] |
96 | | - fn test_render_entity_with_schema_snapshots(#[case] name: &str, #[case] orm: Orm) { |
97 | | - let table = simple_table(); |
98 | | - let schema = vec![table.clone()]; |
99 | | - let result = render_entity_with_schema(orm, &table, &schema); |
100 | | - assert!(result.is_ok()); |
101 | | - with_settings!({ snapshot_suffix => name }, { |
102 | | - assert_snapshot!(result.unwrap()); |
103 | | - }); |
104 | | - } |
105 | | -} |
| 1 | +use vespertide_core::TableDef; |
| 2 | + |
| 3 | +use crate::{seaorm::SeaOrmExporter, sqlalchemy::SqlAlchemyExporter, sqlmodel::SqlModelExporter}; |
| 4 | + |
| 5 | +/// Supported ORM targets. |
| 6 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 7 | +pub enum Orm { |
| 8 | + SeaOrm, |
| 9 | + SqlAlchemy, |
| 10 | + SqlModel, |
| 11 | +} |
| 12 | + |
| 13 | +/// Standardized exporter interface for all supported ORMs. |
| 14 | +pub trait OrmExporter { |
| 15 | + fn render_entity(&self, table: &TableDef) -> Result<String, String>; |
| 16 | + |
| 17 | + /// Render entity with schema context for FK chain resolution. |
| 18 | + /// Default implementation ignores schema context. |
| 19 | + fn render_entity_with_schema( |
| 20 | + &self, |
| 21 | + table: &TableDef, |
| 22 | + _schema: &[TableDef], |
| 23 | + ) -> Result<String, String> { |
| 24 | + self.render_entity(table) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/// Render a single table definition for the selected ORM. |
| 29 | +pub fn render_entity(orm: Orm, table: &TableDef) -> Result<String, String> { |
| 30 | + match orm { |
| 31 | + Orm::SeaOrm => SeaOrmExporter.render_entity(table), |
| 32 | + Orm::SqlAlchemy => SqlAlchemyExporter.render_entity(table), |
| 33 | + Orm::SqlModel => SqlModelExporter.render_entity(table), |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// Render a single table definition with full schema context for FK chain resolution. |
| 38 | +pub fn render_entity_with_schema( |
| 39 | + orm: Orm, |
| 40 | + table: &TableDef, |
| 41 | + schema: &[TableDef], |
| 42 | +) -> Result<String, String> { |
| 43 | + match orm { |
| 44 | + Orm::SeaOrm => SeaOrmExporter.render_entity_with_schema(table, schema), |
| 45 | + Orm::SqlAlchemy => SqlAlchemyExporter.render_entity_with_schema(table, schema), |
| 46 | + Orm::SqlModel => SqlModelExporter.render_entity_with_schema(table, schema), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +#[cfg(test)] |
| 51 | +mod tests { |
| 52 | + use super::*; |
| 53 | + use insta::{assert_snapshot, with_settings}; |
| 54 | + use rstest::rstest; |
| 55 | + use vespertide_core::{ColumnDef, ColumnType, SimpleColumnType, TableConstraint}; |
| 56 | + |
| 57 | + fn simple_table() -> TableDef { |
| 58 | + TableDef { |
| 59 | + name: "test".into(), |
| 60 | + description: None, |
| 61 | + columns: vec![ColumnDef { |
| 62 | + name: "id".into(), |
| 63 | + r#type: ColumnType::Simple(SimpleColumnType::Integer), |
| 64 | + nullable: false, |
| 65 | + default: None, |
| 66 | + comment: None, |
| 67 | + primary_key: None, |
| 68 | + unique: None, |
| 69 | + index: None, |
| 70 | + foreign_key: None, |
| 71 | + }], |
| 72 | + constraints: vec![TableConstraint::PrimaryKey { |
| 73 | + auto_increment: false, |
| 74 | + columns: vec!["id".into()], |
| 75 | + }], |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + #[rstest] |
| 80 | + #[case("seaorm", Orm::SeaOrm)] |
| 81 | + #[case("sqlalchemy", Orm::SqlAlchemy)] |
| 82 | + #[case("sqlmodel", Orm::SqlModel)] |
| 83 | + fn test_render_entity_snapshots(#[case] name: &str, #[case] orm: Orm) { |
| 84 | + let table = simple_table(); |
| 85 | + let result = render_entity(orm, &table); |
| 86 | + assert!(result.is_ok()); |
| 87 | + with_settings!({ snapshot_suffix => name }, { |
| 88 | + assert_snapshot!(result.unwrap()); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + #[rstest] |
| 93 | + #[case("seaorm", Orm::SeaOrm)] |
| 94 | + #[case("sqlalchemy", Orm::SqlAlchemy)] |
| 95 | + #[case("sqlmodel", Orm::SqlModel)] |
| 96 | + fn test_render_entity_with_schema_snapshots(#[case] name: &str, #[case] orm: Orm) { |
| 97 | + let table = simple_table(); |
| 98 | + let schema = vec![table.clone()]; |
| 99 | + let result = render_entity_with_schema(orm, &table, &schema); |
| 100 | + assert!(result.is_ok()); |
| 101 | + with_settings!({ snapshot_suffix => name }, { |
| 102 | + assert_snapshot!(result.unwrap()); |
| 103 | + }); |
| 104 | + } |
| 105 | +} |
0 commit comments