Refactor database types
This commit is contained in:
parent
9eb02a767b
commit
258eabff54
1 changed files with 40 additions and 16 deletions
|
|
@ -29,7 +29,7 @@ pub fn parse_sql_schema(
|
||||||
let schema = schema_for_object(&mut schemas, &name);
|
let schema = schema_for_object(&mut schemas, &name);
|
||||||
schema.types.insert(
|
schema.types.insert(
|
||||||
name.clone(),
|
name.clone(),
|
||||||
Type::Composite {
|
UserDefinedType::Composite(CompositeType {
|
||||||
name: name.clone(),
|
name: name.clone(),
|
||||||
fields: columns
|
fields: columns
|
||||||
.iter()
|
.iter()
|
||||||
|
|
@ -38,7 +38,7 @@ pub fn parse_sql_schema(
|
||||||
r#type: column.data_type.clone(),
|
r#type: column.data_type.clone(),
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
},
|
}),
|
||||||
);
|
);
|
||||||
schema.tables.insert(
|
schema.tables.insert(
|
||||||
name.clone(),
|
name.clone(),
|
||||||
|
|
@ -73,7 +73,7 @@ pub fn parse_sql_schema(
|
||||||
let schema = schema_for_object(&mut schemas, &name);
|
let schema = schema_for_object(&mut schemas, &name);
|
||||||
schema.types.insert(
|
schema.types.insert(
|
||||||
name.clone(),
|
name.clone(),
|
||||||
Type::Composite {
|
UserDefinedType::Composite(CompositeType {
|
||||||
name,
|
name,
|
||||||
fields: attributes
|
fields: attributes
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|
@ -82,7 +82,7 @@ pub fn parse_sql_schema(
|
||||||
r#type: attr.data_type,
|
r#type: attr.data_type,
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
},
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
sqlparser::ast::Statement::CreateType {
|
sqlparser::ast::Statement::CreateType {
|
||||||
|
|
@ -92,10 +92,10 @@ pub fn parse_sql_schema(
|
||||||
let schema = schema_for_object(&mut schemas, &name);
|
let schema = schema_for_object(&mut schemas, &name);
|
||||||
schema.types.insert(
|
schema.types.insert(
|
||||||
name.clone(),
|
name.clone(),
|
||||||
Type::Enum {
|
UserDefinedType::Enum(EnumType {
|
||||||
name,
|
name,
|
||||||
variants: labels,
|
variants: labels,
|
||||||
},
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
|
|
@ -143,7 +143,7 @@ fn schema_for_object<'a>(
|
||||||
pub struct Schema {
|
pub struct Schema {
|
||||||
pub name: SchemaName,
|
pub name: SchemaName,
|
||||||
pub tables: HashMap<ObjectName, Table>,
|
pub tables: HashMap<ObjectName, Table>,
|
||||||
pub types: HashMap<ObjectName, Type>,
|
pub types: HashMap<ObjectName, UserDefinedType>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Table {
|
pub struct Table {
|
||||||
|
|
@ -152,18 +152,42 @@ pub struct Table {
|
||||||
pub constraints: Vec<TableConstraint>,
|
pub constraints: Vec<TableConstraint>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Type {
|
pub enum UserDefinedType {
|
||||||
Composite {
|
Composite(CompositeType),
|
||||||
name: ObjectName,
|
Enum(EnumType),
|
||||||
fields: Vec<Field>,
|
Domain(DomainType),
|
||||||
},
|
}
|
||||||
Enum {
|
|
||||||
name: ObjectName,
|
pub struct CompositeType {
|
||||||
variants: Vec<Ident>,
|
name: ObjectName,
|
||||||
},
|
fields: Vec<Field>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EnumType {
|
||||||
|
name: ObjectName,
|
||||||
|
variants: Vec<Ident>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DomainType {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Field {
|
pub struct Field {
|
||||||
pub name: Ident,
|
pub name: Ident,
|
||||||
pub r#type: DataType,
|
pub r#type: DataType,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum Constraint {
|
||||||
|
PrimaryKey(PrimaryKeyConstraint),
|
||||||
|
ForeignKey(ForeignKeyConstraint),
|
||||||
|
Unique(UniqueConstraint),
|
||||||
|
Check(CheckConstraint),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PrimaryKeyConstraint {}
|
||||||
|
|
||||||
|
pub struct ForeignKeyConstraint {}
|
||||||
|
|
||||||
|
pub struct UniqueConstraint {}
|
||||||
|
|
||||||
|
pub struct CheckConstraint {}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue