pub struct Schema {
pub version: Option<u32>,
pub tables: Vec<TableDef>,
}Expand description
Schema containing all table definitions
Fields§
§version: Option<u32>Schema format version (extracted from -- qail: version=N directive)
tables: Vec<TableDef>Implementations§
Source§impl Schema
impl Schema
Sourcepub fn parse(input: &str) -> Result<Self, String>
pub fn parse(input: &str) -> Result<Self, String>
Parse a schema from .qail format string
Examples found in repository?
examples/test_schema_parse.rs (line 26)
5fn main() {
6 let schema_content = r#"
7-- Test Schema
8table users (
9 id uuid primary_key,
10 email text not null,
11 name varchar(255),
12 tags text[],
13 created_at timestamp
14)
15
16table orders (
17 id serial primary_key,
18 user_id uuid not null references users(id),
19 total decimal(10,2),
20 status text check(status in ('pending', 'completed'))
21)
22"#;
23
24 println!("Testing schema parsing...\n");
25
26 match Schema::parse(schema_content) {
27 Ok(schema) => {
28 println!("✓ Parsed {} tables", schema.tables.len());
29
30 for table in &schema.tables {
31 println!("\nTable: {}", table.name);
32 for col in &table.columns {
33 print!(" - {} {}", col.name, col.typ);
34 if col.primary_key {
35 print!(" PK");
36 }
37 if !col.nullable {
38 print!(" NOT NULL");
39 }
40 if col.is_array {
41 print!(" ARRAY");
42 }
43 if col.is_serial {
44 print!(" SERIAL");
45 }
46 if let Some(ref check) = col.check {
47 print!(" CHECK({})", check);
48 }
49 if let Some(ref refs) = col.references {
50 print!(" -> {}", refs);
51 }
52 if let Some(ref params) = col.type_params {
53 print!(" ({})", params.join(","));
54 }
55 println!();
56 }
57 }
58
59 println!("\n✓ JSON export:");
60 match schema.to_json() {
61 Ok(json) => println!("{}", &json[..300.min(json.len())]),
62 Err(e) => println!("JSON error: {}", e),
63 }
64 }
65 Err(e) => {
66 eprintln!("✗ Parse error: {}", e);
67 std::process::exit(1);
68 }
69 }
70}Sourcepub fn find_table(&self, name: &str) -> Option<&TableDef>
pub fn find_table(&self, name: &str) -> Option<&TableDef>
Find a table by name
Sourcepub fn to_json(&self) -> Result<String, String>
pub fn to_json(&self) -> Result<String, String>
Export schema to JSON string (for qail-macros compatibility)
Examples found in repository?
examples/test_schema_parse.rs (line 60)
5fn main() {
6 let schema_content = r#"
7-- Test Schema
8table users (
9 id uuid primary_key,
10 email text not null,
11 name varchar(255),
12 tags text[],
13 created_at timestamp
14)
15
16table orders (
17 id serial primary_key,
18 user_id uuid not null references users(id),
19 total decimal(10,2),
20 status text check(status in ('pending', 'completed'))
21)
22"#;
23
24 println!("Testing schema parsing...\n");
25
26 match Schema::parse(schema_content) {
27 Ok(schema) => {
28 println!("✓ Parsed {} tables", schema.tables.len());
29
30 for table in &schema.tables {
31 println!("\nTable: {}", table.name);
32 for col in &table.columns {
33 print!(" - {} {}", col.name, col.typ);
34 if col.primary_key {
35 print!(" PK");
36 }
37 if !col.nullable {
38 print!(" NOT NULL");
39 }
40 if col.is_array {
41 print!(" ARRAY");
42 }
43 if col.is_serial {
44 print!(" SERIAL");
45 }
46 if let Some(ref check) = col.check {
47 print!(" CHECK({})", check);
48 }
49 if let Some(ref refs) = col.references {
50 print!(" -> {}", refs);
51 }
52 if let Some(ref params) = col.type_params {
53 print!(" ({})", params.join(","));
54 }
55 println!();
56 }
57 }
58
59 println!("\n✓ JSON export:");
60 match schema.to_json() {
61 Ok(json) => println!("{}", &json[..300.min(json.len())]),
62 Err(e) => println!("JSON error: {}", e),
63 }
64 }
65 Err(e) => {
66 eprintln!("✗ Parse error: {}", e);
67 std::process::exit(1);
68 }
69 }
70}Trait Implementations§
Source§impl<'de> Deserialize<'de> for Schema
impl<'de> Deserialize<'de> for Schema
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for Schema
impl RefUnwindSafe for Schema
impl Send for Schema
impl Sync for Schema
impl Unpin for Schema
impl UnwindSafe for Schema
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more