pgx_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::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::ToSqlFn;
19use crate::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 /// Helper used to implement traits (`Eq`, `Ord`, etc) despite `ToSqlFn` not
43 /// having an implementation for them.
44 #[inline]
45 fn fields(&self) -> (bool, Option<&str>, Option<usize>) {
46 (self.enabled, self.content, self.callback.map(|f| f as usize))
47 }
48 /// Given a SqlGraphEntity, this function converts it to SQL based on the current configuration.
49 ///
50 /// If the config overrides the default behavior (i.e. using the `ToSql` trait), then `Some(eyre::Result)`
51 /// is returned. If the config does not override the default behavior, then `None` is returned. This can
52 /// be used to dispatch SQL generation in a single line, e.g.:
53 ///
54 /// ```rust,ignore
55 /// config.to_sql(entity, context).unwrap_or_else(|| entity.to_sql(context))?
56 /// ```
57 pub fn to_sql(
58 &self,
59 entity: &SqlGraphEntity,
60 context: &PgxSql,
61 ) -> Option<eyre::Result<String>> {
62 use eyre::{eyre, WrapErr};
63
64 if !self.enabled {
65 return Some(Ok(format!(
66 "\n\
67 {sql_anchor_comment}\n\
68 -- Skipped due to `#[pgx(sql = false)]`\n",
69 sql_anchor_comment = entity.sql_anchor_comment(),
70 )));
71 }
72
73 if let Some(content) = self.content {
74 let module_pathname = context.get_module_pathname();
75
76 let content = content.replace("@MODULE_PATHNAME@", &module_pathname);
77
78 return Some(Ok(format!(
79 "\n\
80 {sql_anchor_comment}\n\
81 {content}\n\
82 ",
83 content = content,
84 sql_anchor_comment = entity.sql_anchor_comment()
85 )));
86 }
87
88 if let Some(callback) = self.callback {
89 let content = callback(entity, context)
90 .map_err(|e| eyre!(e))
91 .wrap_err("Failed to run specified `#[pgx(sql = path)] function`");
92 return match content {
93 Ok(content) => {
94 let module_pathname = &context.get_module_pathname();
95
96 let content = content.replace("@MODULE_PATHNAME@", &module_pathname);
97
98 Some(Ok(format!(
99 "\n\
100 {sql_anchor_comment}\n\
101 {content}\
102 ",
103 content = content,
104 sql_anchor_comment = entity.sql_anchor_comment(),
105 )))
106 }
107 Err(e) => Some(Err(e)),
108 };
109 }
110
111 None
112 }
113}
114
115impl std::cmp::PartialOrd for ToSqlConfigEntity {
116 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
117 Some(self.cmp(&other))
118 }
119}
120impl std::cmp::Ord for ToSqlConfigEntity {
121 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
122 self.fields().cmp(&other.fields())
123 }
124}
125impl std::cmp::PartialEq for ToSqlConfigEntity {
126 fn eq(&self, other: &Self) -> bool {
127 self.fields() == other.fields()
128 }
129}
130impl std::cmp::Eq for ToSqlConfigEntity {}
131impl std::hash::Hash for ToSqlConfigEntity {
132 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
133 self.fields().hash(state);
134 }
135}
136impl std::fmt::Debug for ToSqlConfigEntity {
137 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
138 let (enabled, content, callback) = self.fields();
139 f.debug_struct("ToSqlConfigEntity")
140 .field("enabled", &enabled)
141 .field("callback", &callback)
142 .field("content", &content)
143 .finish()
144 }
145}