pgrx_sql_entity_graph/to_sql/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`sql = ...` fragment 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::SqlGraphEntity;
19use crate::pgrx_sql::PgrxSql;
20/// Represents configuration options for tuning the SQL generator.
21///
22/// When an item that can be rendered to SQL has these options at hand, they should be
23/// respected. If an item does not have them, then it is not expected that the SQL generation
24/// for those items can be modified.
25///
26/// The default configuration has `enabled` set to `true`, which indicates that the default SQL
27/// generation behavior will be used.
28///
29/// When `enabled` is false, no SQL is generated for the item being configured.
30///
31#[derive(Default, Clone)]
32pub struct ToSqlConfigEntity<'a> {
33 pub enabled: bool,
34 pub content: Option<&'a str>,
35}
36impl ToSqlConfigEntity<'_> {
37 #[inline]
38 fn fields(&self) -> (bool, Option<&str>) {
39 (self.enabled, self.content)
40 }
41 /// Given a SqlGraphEntity, this function converts it to SQL based on the current configuration.
42 ///
43 /// If the config overrides the default behavior (i.e. using the `ToSql` trait), then `Some(eyre::Result)`
44 /// is returned. If the config does not override the default behavior, then `None` is returned. This can
45 /// be used to dispatch SQL generation in a single line, e.g.:
46 ///
47 /// ```rust,ignore
48 /// config.to_sql(entity, context).unwrap_or_else(|| entity.to_sql(context))?
49 /// ```
50 pub fn to_sql(
51 &self,
52 entity: &SqlGraphEntity<'_>,
53 context: &PgrxSql<'_>,
54 ) -> Option<eyre::Result<String>> {
55 if !self.enabled {
56 return Some(Ok(format!(
57 "\n\
58 {sql_anchor_comment}\n\
59 -- Skipped due to `#[pgrx(sql = false)]`\n",
60 sql_anchor_comment = entity.sql_anchor_comment(),
61 )));
62 }
63
64 if let Some(content) = self.content {
65 let module_pathname = context.get_module_pathname();
66
67 let content = content.replace("@MODULE_PATHNAME@", &module_pathname);
68
69 return Some(Ok(format!(
70 "\n\
71 {sql_anchor_comment}\n\
72 {content}\n\
73 ",
74 content = content,
75 sql_anchor_comment = entity.sql_anchor_comment()
76 )));
77 }
78
79 None
80 }
81}
82
83impl std::cmp::PartialOrd for ToSqlConfigEntity<'_> {
84 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
85 Some(self.cmp(other))
86 }
87}
88impl std::cmp::Ord for ToSqlConfigEntity<'_> {
89 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
90 self.fields().cmp(&other.fields())
91 }
92}
93impl std::cmp::PartialEq for ToSqlConfigEntity<'_> {
94 fn eq(&self, other: &Self) -> bool {
95 self.fields() == other.fields()
96 }
97}
98impl std::cmp::Eq for ToSqlConfigEntity<'_> {}
99impl std::hash::Hash for ToSqlConfigEntity<'_> {
100 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
101 self.fields().hash(state);
102 }
103}
104impl std::fmt::Debug for ToSqlConfigEntity<'_> {
105 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
106 let (enabled, content) = self.fields();
107 f.debug_struct("ToSqlConfigEntity")
108 .field("enabled", &enabled)
109 .field("content", &content)
110 .finish()
111 }
112}