pgrx_sql_entity_graph/postgres_ord/
entity.rs

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/*!
11
12`#[derive(PostgresOrd)]` related entities for Rust to SQL translation
13
14> 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.
16
17*/
18use crate::pgrx_sql::PgrxSql;
19use crate::to_sql::entity::ToSqlConfigEntity;
20use crate::to_sql::ToSql;
21use crate::{SqlGraphEntity, SqlGraphIdentifier};
22
23/// The output of a [`PostgresOrd`](crate::postgres_ord::PostgresOrd) from `quote::ToTokens::to_tokens`.
24#[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}