pgx_utils/sql_entity_graph/to_sql/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`sql = ...` fragment 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::ToSqlFn;
19use crate::sql_entity_graph::SqlGraphEntity;
20
21/// Represents configuration options for tuning the SQL generator.
22///
23/// When an item that can be rendered to SQL has these options at hand, they should be
24/// respected. If an item does not have them, then it is not expected that the SQL generation
25/// for those items can be modified.
26///
27/// The default configuration has `enabled` set to `true`, and `callback` to `None`, which indicates
28/// that the default SQL generation behavior will be used. These are intended to be mutually exclusive
29/// options, so `callback` should only be set if generation is enabled.
30///
31/// When `enabled` is false, no SQL is generated for the item being configured.
32///
33/// When `callback` has a value, the corresponding `ToSql` implementation should invoke the
34/// callback instead of performing their default behavior.
35#[derive(Default, Clone)]
36pub struct ToSqlConfigEntity {
37 pub enabled: bool,
38 pub callback: Option<ToSqlFn>,
39 pub content: Option<&'static str>,
40}
41impl ToSqlConfigEntity {
42 /// Given a SqlGraphEntity, this function converts it to SQL based on the current configuration.
43 ///
44 /// If the config overrides the default behavior (i.e. using the `ToSql` trait), then `Some(eyre::Result)`
45 /// is returned. If the config does not override the default behavior, then `None` is returned. This can
46 /// be used to dispatch SQL generation in a single line, e.g.:
47 ///
48 /// ```rust,ignore
49 /// config.to_sql(entity, context).unwrap_or_else(|| entity.to_sql(context))?
50 /// ```
51 pub fn to_sql(
52 &self,
53 entity: &SqlGraphEntity,
54 context: &PgxSql,
55 ) -> Option<eyre::Result<String>> {
56 use eyre::{eyre, WrapErr};
57
58 if !self.enabled {
59 return Some(Ok(format!(
60 "\n\
61 {sql_anchor_comment}\n\
62 -- Skipped due to `#[pgx(sql = false)]`\n",
63 sql_anchor_comment = entity.sql_anchor_comment(),
64 )));
65 }
66
67 if let Some(content) = self.content {
68 let module_pathname = context.get_module_pathname();
69
70 let content = content.replace("@MODULE_PATHNAME@", &module_pathname);
71
72 return Some(Ok(format!(
73 "\n\
74 {sql_anchor_comment}\n\
75 {content}\n\
76 ",
77 content = content,
78 sql_anchor_comment = entity.sql_anchor_comment()
79 )));
80 }
81
82 if let Some(callback) = self.callback {
83 let content = callback(entity, context)
84 .map_err(|e| eyre!(e))
85 .wrap_err("Failed to run specified `#[pgx(sql = path)] function`");
86 return match content {
87 Ok(content) => {
88 let module_pathname = &context.get_module_pathname();
89
90 let content = content.replace("@MODULE_PATHNAME@", &module_pathname);
91
92 Some(Ok(format!(
93 "\n\
94 {sql_anchor_comment}\n\
95 {content}\
96 ",
97 content = content,
98 sql_anchor_comment = entity.sql_anchor_comment(),
99 )))
100 }
101 Err(e) => Some(Err(e)),
102 };
103 }
104
105 None
106 }
107}
108
109impl std::cmp::PartialEq for ToSqlConfigEntity {
110 fn eq(&self, other: &Self) -> bool {
111 (self.enabled, self.content, self.callback.map(|f| f as usize))
112 == (other.enabled, other.content, other.callback.map(|f| f as usize))
113 }
114}
115impl std::cmp::Eq for ToSqlConfigEntity {}
116impl std::hash::Hash for ToSqlConfigEntity {
117 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
118 self.enabled.hash(state);
119 self.callback.map(|cb| cb as usize).hash(state);
120 self.content.hash(state);
121 }
122}
123impl std::fmt::Debug for ToSqlConfigEntity {
124 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
125 let callback = self.callback.map(|cb| cb as usize);
126 f.debug_struct("ToSqlConfigEntity")
127 .field("enabled", &self.enabled)
128 .field("callback", &format_args!("{:?}", &callback))
129 .field("content", &self.content)
130 .finish()
131 }
132}