pgrx_sql_entity_graph/postgres_hash/
entity.rs1use crate::pgrx_sql::PgrxSql;
19use crate::to_sql::entity::ToSqlConfigEntity;
20use crate::to_sql::ToSql;
21use crate::{SqlGraphEntity, SqlGraphIdentifier};
22
23#[derive(Debug, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
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 From<PostgresHashEntity> for SqlGraphEntity {
42 fn from(val: PostgresHashEntity) -> Self {
43 SqlGraphEntity::Hash(val)
44 }
45}
46
47impl SqlGraphIdentifier for PostgresHashEntity {
48 fn dot_identifier(&self) -> String {
49 format!("hash {}", self.full_path)
50 }
51 fn rust_identifier(&self) -> String {
52 self.full_path.to_string()
53 }
54
55 fn file(&self) -> Option<&'static str> {
56 Some(self.file)
57 }
58
59 fn line(&self) -> Option<u32> {
60 Some(self.line)
61 }
62}
63
64impl ToSql for PostgresHashEntity {
65 fn to_sql(&self, _context: &PgrxSql) -> eyre::Result<String> {
66 let sql = format!("\n\
67 -- {file}:{line}\n\
68 -- {full_path}\n\
69 CREATE OPERATOR FAMILY {name}_hash_ops USING hash;\n\
70 CREATE OPERATOR CLASS {name}_hash_ops DEFAULT FOR TYPE {name} USING hash FAMILY {name}_hash_ops AS\n\
71 \tOPERATOR 1 = ({name}, {name}),\n\
72 \tFUNCTION 1 {fn_name}({name});\
73 ",
74 name = self.name,
75 full_path = self.full_path,
76 file = self.file,
77 line = self.line,
78 fn_name = self.fn_name(),
79 );
80 Ok(sql)
81 }
82}