pgx_utils/sql_entity_graph/postgres_hash/
entity.rs

1/*
2Portions Copyright 2019-2021 ZomboDB, LLC.
3Portions Copyright 2021-2022 Technology Concepts & Design, Inc. <support@tcdi.com>
4
5All rights reserved.
6
7Use of this source code is governed by the MIT license that can be found in the LICENSE file.
8*/
9/*!
10
11`#[derive(PostgresHash)]` related entities for Rust to SQL translation
12
13> Like all of the [`sql_entity_graph`][crate::sql_entity_graph] APIs, this is considered **internal**
14to the `pgx` framework and very subject to change between versions. While you may use this, please do it with caution.
15
16*/
17use crate::sql_entity_graph::pgx_sql::PgxSql;
18use crate::sql_entity_graph::to_sql::entity::ToSqlConfigEntity;
19use crate::sql_entity_graph::to_sql::ToSql;
20use crate::sql_entity_graph::{SqlGraphEntity, SqlGraphIdentifier};
21use std::cmp::Ordering;
22
23/// The output of a [`PostgresHash`](crate::sql_entity_graph::postgres_hash::PostgresHash) from `quote::ToTokens::to_tokens`.
24#[derive(Debug, Clone, Hash, PartialEq, Eq)]
25pub struct PostgresHashEntity {
26    pub name: &'static str,
27    pub file: &'static str,
28    pub line: u32,
29    pub full_path: &'static str,
30    pub module_path: &'static str,
31    pub id: core::any::TypeId,
32    pub to_sql_config: ToSqlConfigEntity,
33}
34
35impl PostgresHashEntity {
36    pub(crate) fn fn_name(&self) -> String {
37        format!("{}_hash", self.name.to_lowercase())
38    }
39}
40
41impl Ord for PostgresHashEntity {
42    fn cmp(&self, other: &Self) -> Ordering {
43        self.file.cmp(other.file).then_with(|| self.file.cmp(other.file))
44    }
45}
46
47impl PartialOrd for PostgresHashEntity {
48    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
49        Some(self.cmp(other))
50    }
51}
52
53impl From<PostgresHashEntity> for SqlGraphEntity {
54    fn from(val: PostgresHashEntity) -> Self {
55        SqlGraphEntity::Hash(val)
56    }
57}
58
59impl SqlGraphIdentifier for PostgresHashEntity {
60    fn dot_identifier(&self) -> String {
61        format!("hash {}", self.full_path)
62    }
63    fn rust_identifier(&self) -> String {
64        self.full_path.to_string()
65    }
66
67    fn file(&self) -> Option<&'static str> {
68        Some(self.file)
69    }
70
71    fn line(&self) -> Option<u32> {
72        Some(self.line)
73    }
74}
75
76impl ToSql for PostgresHashEntity {
77    #[tracing::instrument(level = "debug", err, skip(self, _context), fields(identifier = %self.rust_identifier()))]
78    fn to_sql(&self, _context: &PgxSql) -> eyre::Result<String> {
79        let sql = format!("\n\
80                            -- {file}:{line}\n\
81                            -- {full_path}\n\
82                            CREATE OPERATOR FAMILY {name}_hash_ops USING hash;\n\
83                            CREATE OPERATOR CLASS {name}_hash_ops DEFAULT FOR TYPE {name} USING hash FAMILY {name}_hash_ops AS\n\
84                                \tOPERATOR    1   =  ({name}, {name}),\n\
85                                \tFUNCTION    1   {fn_name}({name});\
86                            ",
87                          name = self.name,
88                          full_path = self.full_path,
89                          file = self.file,
90                          line = self.line,
91                          fn_name = self.fn_name(),
92        );
93        tracing::trace!(%sql);
94        Ok(sql)
95    }
96}