pgx_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::pgx_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::pgx_sql::PgxSql;
18use crate::to_sql::entity::ToSqlConfigEntity;
19use crate::to_sql::ToSql;
20use crate::{SqlGraphEntity, SqlGraphIdentifier};
21
22/// The output of a [`PostgresOrd`](crate::postgres_ord::PostgresOrd) from `quote::ToTokens::to_tokens`.
23#[derive(Debug, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
24pub struct PostgresOrdEntity {
25    pub name: &'static str,
26    pub file: &'static str,
27    pub line: u32,
28    pub full_path: &'static str,
29    pub module_path: &'static str,
30    pub id: core::any::TypeId,
31    pub to_sql_config: ToSqlConfigEntity,
32}
33
34impl PostgresOrdEntity {
35    pub(crate) fn cmp_fn_name(&self) -> String {
36        format!("{}_cmp", self.name.to_lowercase())
37    }
38
39    pub(crate) fn lt_fn_name(&self) -> String {
40        format!("{}_lt", self.name.to_lowercase())
41    }
42
43    pub(crate) fn le_fn_name(&self) -> String {
44        format!("{}_le", self.name.to_lowercase())
45    }
46
47    pub(crate) fn eq_fn_name(&self) -> String {
48        format!("{}_eq", self.name.to_lowercase())
49    }
50
51    pub(crate) fn gt_fn_name(&self) -> String {
52        format!("{}_gt", self.name.to_lowercase())
53    }
54
55    pub(crate) fn ge_fn_name(&self) -> String {
56        format!("{}_ge", self.name.to_lowercase())
57    }
58}
59
60impl From<PostgresOrdEntity> for SqlGraphEntity {
61    fn from(val: PostgresOrdEntity) -> Self {
62        SqlGraphEntity::Ord(val)
63    }
64}
65
66impl SqlGraphIdentifier for PostgresOrdEntity {
67    fn dot_identifier(&self) -> String {
68        format!("ord {}", self.full_path)
69    }
70    fn rust_identifier(&self) -> String {
71        self.full_path.to_string()
72    }
73
74    fn file(&self) -> Option<&'static str> {
75        Some(self.file)
76    }
77
78    fn line(&self) -> Option<u32> {
79        Some(self.line)
80    }
81}
82
83impl ToSql for PostgresOrdEntity {
84    fn to_sql(&self, _context: &PgxSql) -> eyre::Result<String> {
85        let sql = format!("\n\
86                            -- {file}:{line}\n\
87                            -- {full_path}\n\
88                            CREATE OPERATOR FAMILY {name}_btree_ops USING btree;\n\
89                            CREATE OPERATOR CLASS {name}_btree_ops DEFAULT FOR TYPE {name} USING btree FAMILY {name}_btree_ops AS\n\
90                                  \tOPERATOR 1 <,\n\
91                                  \tOPERATOR 2 <=,\n\
92                                  \tOPERATOR 3 =,\n\
93                                  \tOPERATOR 4 >=,\n\
94                                  \tOPERATOR 5 >,\n\
95                                  \tFUNCTION 1 {cmp_fn_name}({name}, {name});\
96                            ",
97                          name = self.name,
98                          full_path = self.full_path,
99                          file = self.file,
100                          line = self.line,
101                          cmp_fn_name = self.cmp_fn_name(),
102        );
103        Ok(sql)
104    }
105}