pgx_sql_entity_graph/schema/
entity.rs1use crate::pgx_sql::PgxSql;
18use crate::to_sql::ToSql;
19use crate::{SqlGraphEntity, SqlGraphIdentifier};
20
21#[derive(Debug, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
23pub struct SchemaEntity {
24 pub module_path: &'static str,
25 pub name: &'static str,
26 pub file: &'static str,
27 pub line: u32,
28}
29
30impl From<SchemaEntity> for SqlGraphEntity {
31 fn from(val: SchemaEntity) -> Self {
32 SqlGraphEntity::Schema(val)
33 }
34}
35
36impl SqlGraphIdentifier for SchemaEntity {
37 fn dot_identifier(&self) -> String {
38 format!("schema {}", self.module_path)
39 }
40 fn rust_identifier(&self) -> String {
41 self.module_path.to_string()
42 }
43
44 fn file(&self) -> Option<&'static str> {
45 Some(self.file)
46 }
47
48 fn line(&self) -> Option<u32> {
49 Some(self.line)
50 }
51}
52
53impl ToSql for SchemaEntity {
54 fn to_sql(&self, _context: &PgxSql) -> eyre::Result<String> {
55 let sql = format!(
56 "\n\
57 -- {file}:{line}\n\
58 CREATE SCHEMA IF NOT EXISTS {name}; /* {module_path} */\
59 ",
60 name = self.name,
61 file = self.file,
62 line = self.line,
63 module_path = self.module_path,
64 );
65 Ok(sql)
66 }
67}