pgrx_sql_entity_graph/postgres_ord/
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 PostgresOrdEntity {
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 PostgresOrdEntity {
36 pub(crate) fn cmp_fn_name(&self) -> String {
37 format!("{}_cmp", self.name.to_lowercase())
38 }
39
40 pub(crate) fn lt_fn_name(&self) -> String {
41 format!("{}_lt", self.name.to_lowercase())
42 }
43
44 pub(crate) fn le_fn_name(&self) -> String {
45 format!("{}_le", self.name.to_lowercase())
46 }
47
48 pub(crate) fn eq_fn_name(&self) -> String {
49 format!("{}_eq", self.name.to_lowercase())
50 }
51
52 pub(crate) fn gt_fn_name(&self) -> String {
53 format!("{}_gt", self.name.to_lowercase())
54 }
55
56 pub(crate) fn ge_fn_name(&self) -> String {
57 format!("{}_ge", self.name.to_lowercase())
58 }
59}
60
61impl From<PostgresOrdEntity> for SqlGraphEntity {
62 fn from(val: PostgresOrdEntity) -> Self {
63 SqlGraphEntity::Ord(val)
64 }
65}
66
67impl SqlGraphIdentifier for PostgresOrdEntity {
68 fn dot_identifier(&self) -> String {
69 format!("ord {}", self.full_path)
70 }
71 fn rust_identifier(&self) -> String {
72 self.full_path.to_string()
73 }
74
75 fn file(&self) -> Option<&'static str> {
76 Some(self.file)
77 }
78
79 fn line(&self) -> Option<u32> {
80 Some(self.line)
81 }
82}
83
84impl ToSql for PostgresOrdEntity {
85 fn to_sql(&self, _context: &PgrxSql) -> eyre::Result<String> {
86 let PostgresOrdEntity { name, full_path, file, line, .. } = self;
87 let sql = format!("\n\
88 -- {file}:{line}\n\
89 -- {full_path}\n\
90 CREATE OPERATOR FAMILY {name}_btree_ops USING btree;\n\
91 CREATE OPERATOR CLASS {name}_btree_ops DEFAULT FOR TYPE {name} USING btree FAMILY {name}_btree_ops AS\n\
92 \tOPERATOR 1 <,\n\
93 \tOPERATOR 2 <=,\n\
94 \tOPERATOR 3 =,\n\
95 \tOPERATOR 4 >=,\n\
96 \tOPERATOR 5 >,\n\
97 \tFUNCTION 1 {cmp_fn_name}({name}, {name});\
98 ",
99 cmp_fn_name = self.cmp_fn_name(),
100 );
101 Ok(sql)
102 }
103}