Skip to main content

pgrx_sql_entity_graph/postgres_hash/
mod.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`#[derive(PostgresHash)]` related macro expansion 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*/
18pub mod entity;
19
20use crate::enrich::{ToEntityGraphTokens, ToRustCodeTokens};
21use proc_macro2::TokenStream as TokenStream2;
22use quote::{format_ident, quote};
23use syn::parse::{Parse, ParseStream};
24use syn::{DeriveInput, Ident};
25
26use crate::{CodeEnrichment, ToSqlConfig};
27
28/// A parsed `#[derive(PostgresHash)]` item.
29///
30/// It should be used with [`syn::parse::Parse`] functions.
31///
32/// Using [`quote::ToTokens`] will output the declaration for a `pgrx::datum::pgrx_sql_entity_graph::InventoryPostgresHash`.
33///
34/// On structs:
35///
36/// ```rust
37/// use syn::{Macro, parse::Parse, parse_quote, parse};
38/// use quote::{quote, ToTokens};
39/// use pgrx_sql_entity_graph::PostgresHash;
40///
41/// # fn main() -> eyre::Result<()> {
42/// use pgrx_sql_entity_graph::CodeEnrichment;
43/// let parsed: CodeEnrichment<PostgresHash> = parse_quote! {
44///     #[derive(PostgresHash)]
45///     struct Example<'a> {
46///         demo: &'a str,
47///     }
48/// };
49/// let sql_graph_entity_tokens = parsed.to_token_stream();
50/// # Ok(())
51/// # }
52/// ```
53///
54/// On enums:
55///
56/// ```rust
57/// use syn::{Macro, parse::Parse, parse_quote, parse};
58/// use quote::{quote, ToTokens};
59/// use pgrx_sql_entity_graph::PostgresHash;
60///
61/// # fn main() -> eyre::Result<()> {
62/// use pgrx_sql_entity_graph::CodeEnrichment;
63/// let parsed: CodeEnrichment<PostgresHash> = parse_quote! {
64///     #[derive(PostgresHash)]
65///     enum Demo {
66///         Example,
67///     }
68/// };
69/// let sql_graph_entity_tokens = parsed.to_token_stream();
70/// # Ok(())
71/// # }
72/// ```
73#[derive(Debug, Clone)]
74pub struct PostgresHash {
75    pub name: Ident,
76    pub to_sql_config: ToSqlConfig,
77}
78
79impl PostgresHash {
80    pub fn new(
81        name: Ident,
82        to_sql_config: ToSqlConfig,
83    ) -> Result<CodeEnrichment<Self>, syn::Error> {
84        if !to_sql_config.overrides_default() {
85            crate::ident_is_acceptable_to_postgres(&name)?;
86        }
87        Ok(CodeEnrichment(Self { name, to_sql_config }))
88    }
89
90    pub fn from_derive_input(
91        derive_input: DeriveInput,
92    ) -> Result<CodeEnrichment<Self>, syn::Error> {
93        let to_sql_config =
94            ToSqlConfig::from_attributes(derive_input.attrs.as_slice())?.unwrap_or_default();
95        Self::new(derive_input.ident, to_sql_config)
96    }
97}
98
99impl ToEntityGraphTokens for PostgresHash {
100    fn to_entity_graph_tokens(&self) -> TokenStream2 {
101        let name = &self.name;
102        let sql_graph_entity_fn_name = format_ident!("__pgrx_schema_hash_{}", self.name);
103        let to_sql_config = &self.to_sql_config;
104        let to_sql_config_len = to_sql_config.section_len_tokens();
105        let payload_len = quote! {
106            ::pgrx::pgrx_sql_entity_graph::section::u8_len()
107                + ::pgrx::pgrx_sql_entity_graph::section::str_len(stringify!(#name))
108                + ::pgrx::pgrx_sql_entity_graph::section::str_len(file!())
109                + ::pgrx::pgrx_sql_entity_graph::section::u32_len()
110                + ::pgrx::pgrx_sql_entity_graph::section::str_len(stringify!(#name))
111                + ::pgrx::pgrx_sql_entity_graph::section::str_len(module_path!())
112                + ::pgrx::pgrx_sql_entity_graph::section::str_len(<#name as ::pgrx::pgrx_sql_entity_graph::metadata::SqlTranslatable>::TYPE_IDENT)
113                + (#to_sql_config_len)
114        };
115        let total_len = quote! {
116            ::pgrx::pgrx_sql_entity_graph::section::u32_len() + (#payload_len)
117        };
118        let writer = to_sql_config.section_writer_tokens(quote! {
119            ::pgrx::pgrx_sql_entity_graph::section::EntryWriter::<{ #total_len }>::new()
120                .u32((#payload_len) as u32)
121                .u8(::pgrx::pgrx_sql_entity_graph::section::ENTITY_HASH)
122                .str(stringify!(#name))
123                .str(file!())
124                .u32(line!())
125                .str(stringify!(#name))
126                .str(module_path!())
127                .str(<#name as ::pgrx::pgrx_sql_entity_graph::metadata::SqlTranslatable>::TYPE_IDENT)
128        });
129        quote! {
130            ::pgrx::pgrx_sql_entity_graph::__pgrx_schema_entry!(
131                #sql_graph_entity_fn_name,
132                #total_len,
133                #writer.finish()
134            );
135        }
136    }
137}
138
139impl ToRustCodeTokens for PostgresHash {}
140
141impl Parse for CodeEnrichment<PostgresHash> {
142    fn parse(input: ParseStream) -> Result<Self, syn::Error> {
143        use syn::Item;
144
145        let parsed = input.parse()?;
146        let (ident, attrs) = match &parsed {
147            Item::Enum(item) => (item.ident.clone(), item.attrs.as_slice()),
148            Item::Struct(item) => (item.ident.clone(), item.attrs.as_slice()),
149            _ => return Err(syn::Error::new(input.span(), "expected enum or struct")),
150        };
151
152        let to_sql_config = ToSqlConfig::from_attributes(attrs)?.unwrap_or_default();
153        PostgresHash::new(ident, to_sql_config)
154    }
155}