pgx_utils/sql_entity_graph/postgres_ord/
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(PostgresOrd)]` 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 [`PostgresOrd`](crate::sql_entity_graph::postgres_ord::PostgresOrd) from `quote::ToTokens::to_tokens`.
24#[derive(Debug, Clone, Hash, PartialEq, Eq)]
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 Ord for PostgresOrdEntity {
62    fn cmp(&self, other: &Self) -> Ordering {
63        self.file.cmp(other.file).then_with(|| self.file.cmp(other.file))
64    }
65}
66
67impl PartialOrd for PostgresOrdEntity {
68    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
69        Some(self.cmp(other))
70    }
71}
72
73impl From<PostgresOrdEntity> for SqlGraphEntity {
74    fn from(val: PostgresOrdEntity) -> Self {
75        SqlGraphEntity::Ord(val)
76    }
77}
78
79impl SqlGraphIdentifier for PostgresOrdEntity {
80    fn dot_identifier(&self) -> String {
81        format!("ord {}", self.full_path)
82    }
83    fn rust_identifier(&self) -> String {
84        self.full_path.to_string()
85    }
86
87    fn file(&self) -> Option<&'static str> {
88        Some(self.file)
89    }
90
91    fn line(&self) -> Option<u32> {
92        Some(self.line)
93    }
94}
95
96impl ToSql for PostgresOrdEntity {
97    #[tracing::instrument(level = "debug", err, skip(self, _context), fields(identifier = %self.rust_identifier()))]
98    fn to_sql(&self, _context: &PgxSql) -> eyre::Result<String> {
99        let sql = format!("\n\
100                            -- {file}:{line}\n\
101                            -- {full_path}\n\
102                            CREATE OPERATOR FAMILY {name}_btree_ops USING btree;\n\
103                            CREATE OPERATOR CLASS {name}_btree_ops DEFAULT FOR TYPE {name} USING btree FAMILY {name}_btree_ops AS\n\
104                                  \tOPERATOR 1 <,\n\
105                                  \tOPERATOR 2 <=,\n\
106                                  \tOPERATOR 3 =,\n\
107                                  \tOPERATOR 4 >=,\n\
108                                  \tOPERATOR 5 >,\n\
109                                  \tFUNCTION 1 {cmp_fn_name}({name}, {name});\
110                            ",
111                          name = self.name,
112                          full_path = self.full_path,
113                          file = self.file,
114                          line = self.line,
115                          cmp_fn_name = self.cmp_fn_name(),
116        );
117        tracing::trace!(%sql);
118        Ok(sql)
119    }
120}