Skip to main content

pgrx_sql_entity_graph/pg_extern/
operator.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`#[pg_operator]` 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*/
18use proc_macro2::TokenStream as TokenStream2;
19use quote::{ToTokens, TokenStreamExt, quote};
20use syn::parse::{Parse, ParseBuffer};
21
22/// A parsed `#[pg_operator]` operator.
23///
24/// It is created during [`PgExtern`](crate::PgExtern) parsing.
25#[derive(Debug, Default, Clone)]
26pub struct PgOperator {
27    pub opname: Option<PgrxOperatorOpName>,
28    pub commutator: Option<PgrxOperatorAttributeWithIdent>,
29    pub negator: Option<PgrxOperatorAttributeWithIdent>,
30    pub restrict: Option<PgrxOperatorAttributeWithIdent>,
31    pub join: Option<PgrxOperatorAttributeWithIdent>,
32    pub hashes: bool,
33    pub merges: bool,
34}
35
36impl ToTokens for PgOperator {
37    fn to_tokens(&self, tokens: &mut TokenStream2) {
38        let opname = self.opname.iter().clone();
39        let commutator = self.commutator.iter().clone();
40        let negator = self.negator.iter().clone();
41        let restrict = self.restrict.iter().clone();
42        let join = self.join.iter().clone();
43        let hashes = self.hashes;
44        let merges = self.merges;
45        let quoted = quote! {
46            ::pgrx::pgrx_sql_entity_graph::PgOperatorEntity {
47                opname: None #( .unwrap_or(Some(#opname)) )*,
48                commutator: None #( .unwrap_or(Some(#commutator)) )*,
49                negator: None #( .unwrap_or(Some(#negator)) )*,
50                restrict: None #( .unwrap_or(Some(#restrict)) )*,
51                join: None #( .unwrap_or(Some(#join)) )*,
52                hashes: #hashes,
53                merges: #merges,
54            }
55        };
56        tokens.append_all(quoted);
57    }
58}
59
60impl PgOperator {
61    pub fn section_len_tokens(&self) -> TokenStream2 {
62        let opname_len = self
63            .opname
64            .as_ref()
65            .map(|item| {
66                let value = item.op_name.to_string().replacen(' ', "", 256);
67                quote! {
68                    ::pgrx::pgrx_sql_entity_graph::section::bool_len()
69                        + ::pgrx::pgrx_sql_entity_graph::section::str_len(#value)
70                }
71            })
72            .unwrap_or_else(|| quote! { ::pgrx::pgrx_sql_entity_graph::section::bool_len() });
73        let attr_len = |item: &Option<PgrxOperatorAttributeWithIdent>| {
74            item.as_ref()
75                .map(|item| {
76                    let value = item.fn_name.to_string().replace(' ', "");
77                    quote! {
78                        ::pgrx::pgrx_sql_entity_graph::section::bool_len()
79                            + ::pgrx::pgrx_sql_entity_graph::section::str_len(#value)
80                    }
81                })
82                .unwrap_or_else(|| quote! { ::pgrx::pgrx_sql_entity_graph::section::bool_len() })
83        };
84        let commutator_len = attr_len(&self.commutator);
85        let negator_len = attr_len(&self.negator);
86        let restrict_len = attr_len(&self.restrict);
87        let join_len = attr_len(&self.join);
88        quote! {
89            (#opname_len)
90                + (#commutator_len)
91                + (#negator_len)
92                + (#restrict_len)
93                + (#join_len)
94                + ::pgrx::pgrx_sql_entity_graph::section::bool_len()
95                + ::pgrx::pgrx_sql_entity_graph::section::bool_len()
96        }
97    }
98
99    pub fn section_writer_tokens(&self, writer: TokenStream2) -> TokenStream2 {
100        let opname_writer = self
101            .opname
102            .as_ref()
103            .map(|item| {
104                let value = item.op_name.to_string().replacen(' ', "", 256);
105                quote! { .bool(true).str(#value) }
106            })
107            .unwrap_or_else(|| quote! { .bool(false) });
108        let attr_writer = |item: &Option<PgrxOperatorAttributeWithIdent>| {
109            item.as_ref()
110                .map(|item| {
111                    let value = item.fn_name.to_string().replace(' ', "");
112                    quote! { .bool(true).str(#value) }
113                })
114                .unwrap_or_else(|| quote! { .bool(false) })
115        };
116        let commutator_writer = attr_writer(&self.commutator);
117        let negator_writer = attr_writer(&self.negator);
118        let restrict_writer = attr_writer(&self.restrict);
119        let join_writer = attr_writer(&self.join);
120        let hashes = self.hashes;
121        let merges = self.merges;
122        quote! {
123            #writer
124                #opname_writer
125                #commutator_writer
126                #negator_writer
127                #restrict_writer
128                #join_writer
129                .bool(#hashes)
130                .bool(#merges)
131        }
132    }
133}
134
135#[derive(Debug, Clone)]
136pub struct PgrxOperatorAttributeWithIdent {
137    pub fn_name: TokenStream2,
138}
139
140impl Parse for PgrxOperatorAttributeWithIdent {
141    fn parse(input: &ParseBuffer) -> Result<Self, syn::Error> {
142        Ok(PgrxOperatorAttributeWithIdent { fn_name: input.parse()? })
143    }
144}
145
146impl ToTokens for PgrxOperatorAttributeWithIdent {
147    fn to_tokens(&self, tokens: &mut TokenStream2) {
148        let fn_name = &self.fn_name;
149        let operator = fn_name.to_string().replace(' ', "");
150        let quoted = quote! {
151            #operator
152        };
153        tokens.append_all(quoted);
154    }
155}
156
157#[derive(Debug, Clone)]
158pub struct PgrxOperatorOpName {
159    pub op_name: TokenStream2,
160}
161
162impl Parse for PgrxOperatorOpName {
163    fn parse(input: &ParseBuffer) -> Result<Self, syn::Error> {
164        Ok(PgrxOperatorOpName { op_name: input.parse()? })
165    }
166}
167
168impl ToTokens for PgrxOperatorOpName {
169    fn to_tokens(&self, tokens: &mut TokenStream2) {
170        let op_name = &self.op_name;
171        let op_string = op_name.to_string().replacen(' ', "", 256);
172        let quoted = quote! {
173            #op_string
174        };
175        tokens.append_all(quoted);
176    }
177}