pgrx_sql_entity_graph/postgres_hash/
entity.rs1use crate::pgrx_sql::PgrxSql;
19use crate::to_sql::ToSql;
20use crate::to_sql::entity::ToSqlConfigEntity;
21use crate::{SqlGraphEntity, SqlGraphIdentifier};
22
23#[derive(Debug, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
25pub struct PostgresHashEntity<'a> {
26 pub name: &'a str,
27 pub file: &'a str,
28 pub line: u32,
29 pub full_path: &'a str,
30 pub module_path: &'a str,
31 pub type_ident: &'a str,
32 pub to_sql_config: ToSqlConfigEntity<'a>,
33}
34
35impl PostgresHashEntity<'_> {
36 pub(crate) fn fn_name(&self) -> String {
37 format!("{}_hash", self.name.to_lowercase())
38 }
39}
40
41impl<'a> From<PostgresHashEntity<'a>> for SqlGraphEntity<'a> {
42 fn from(val: PostgresHashEntity<'a>) -> 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<&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 self_index = context.hashes[self];
67 let schema = context.schema_prefix_for(&self_index);
68 let sql = format!(
69 "\n\
70 -- {file}:{line}\n\
71 -- {full_path}\n\
72 CREATE OPERATOR FAMILY {schema}{name}_hash_ops USING hash;\n\
73 CREATE OPERATOR CLASS {schema}{name}_hash_ops DEFAULT FOR TYPE {schema}{name} USING hash FAMILY {schema}{name}_hash_ops AS\n\
74 \tOPERATOR 1 = ({schema}{name}, {schema}{name}),\n\
75 \tFUNCTION 1 {schema}{fn_name}({schema}{name});\
76 ",
77 name = self.name,
78 full_path = self.full_path,
79 file = self.file,
80 line = self.line,
81 fn_name = self.fn_name(),
82 );
83 Ok(sql)
84 }
85}