elefant_tools/models/
schema.rs1use crate::models::enumeration::PostgresEnum;
2use crate::models::sequence::PostgresSequence;
3use crate::models::table::PostgresTable;
4use crate::models::view::PostgresView;
5use crate::object_id::ObjectId;
6use crate::quoting::AttemptedKeywordUsage::ColumnName;
7use crate::quoting::{quote_value_string, IdentifierQuoter, Quotable};
8use crate::{PostgresAggregateFunction, PostgresDomain, PostgresFunction, PostgresTrigger};
9use serde::{Deserialize, Serialize};
10
11#[derive(Debug, Eq, PartialEq, Default, Clone, Serialize, Deserialize)]
12pub struct PostgresSchema {
13 pub tables: Vec<PostgresTable>,
14 pub sequences: Vec<PostgresSequence>,
15 pub views: Vec<PostgresView>,
16 pub functions: Vec<PostgresFunction>,
17 pub aggregate_functions: Vec<PostgresAggregateFunction>,
18 pub triggers: Vec<PostgresTrigger>,
19 pub enums: Vec<PostgresEnum>,
20 pub name: String,
21 pub comment: Option<String>,
22 pub domains: Vec<PostgresDomain>,
23 pub object_id: ObjectId,
24}
25
26impl PostgresSchema {
27 pub fn get_create_statement(&self, identifier_quoter: &IdentifierQuoter) -> String {
28 let mut sql = format!(
29 "create schema if not exists {};",
30 self.name.quote(identifier_quoter, ColumnName)
31 );
32
33 if let Some(comment) = &self.comment {
34 sql.push_str("\ncomment on schema ");
35 sql.push_str(&self.name.quote(identifier_quoter, ColumnName));
36 sql.push_str(" is ");
37 sql.push_str("e_value_string(comment));
38 sql.push(';');
39 }
40
41 sql
42 }
43
44 pub(crate) fn try_get_table(&self, table_name: &str) -> Option<&PostgresTable> {
45 self.tables.iter().find(|t| t.name == table_name)
46 }
47}