1//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC.
2//LICENSE
3//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc.
4//LICENSE
5//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <contact@pgcentral.org>
6//LICENSE
7//LICENSE All rights reserved.
8//LICENSE
9//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
10/*!
1112`#[pg_schema]` related entities for Rust to SQL translation
1314> Like all of the [`sql_entity_graph`][crate] APIs, this is considered **internal**
15> to the `pgrx` framework and very subject to change between versions. While you may use this, please do it with caution.
1617*/
18use crate::pgrx_sql::PgrxSql;
19use crate::to_sql::ToSql;
20use crate::{SqlGraphEntity, SqlGraphIdentifier};
2122/// The output of a [`Schema`](crate::schema::Schema) from `quote::ToTokens::to_tokens`.
23#[derive(Debug, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
24pub struct SchemaEntity {
25pub module_path: &'static str,
26pub name: &'static str,
27pub file: &'static str,
28pub line: u32,
29}
3031impl From<SchemaEntity> for SqlGraphEntity {
32fn from(val: SchemaEntity) -> Self {
33 SqlGraphEntity::Schema(val)
34 }
35}
3637impl SqlGraphIdentifier for SchemaEntity {
38fn dot_identifier(&self) -> String {
39format!("schema {}", self.module_path)
40 }
41fn rust_identifier(&self) -> String {
42self.module_path.to_string()
43 }
4445fn file(&self) -> Option<&'static str> {
46Some(self.file)
47 }
4849fn line(&self) -> Option<u32> {
50Some(self.line)
51 }
52}
5354impl ToSql for SchemaEntity {
55fn to_sql(&self, _context: &PgrxSql) -> eyre::Result<String> {
56let SchemaEntity { name, file, line, module_path } = self;
57let sql = format!(
58"\n\
59 -- {file}:{line}\n\
60 CREATE SCHEMA IF NOT EXISTS {name}; /* {module_path} */\
61 ",
62 );
63Ok(sql)
64 }
65}