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::{quote, ToTokens, TokenStreamExt};
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
60#[derive(Debug, Clone)]
61pub struct PgrxOperatorAttributeWithIdent {
62    pub fn_name: TokenStream2,
63}
64
65impl Parse for PgrxOperatorAttributeWithIdent {
66    fn parse(input: &ParseBuffer) -> Result<Self, syn::Error> {
67        Ok(PgrxOperatorAttributeWithIdent { fn_name: input.parse()? })
68    }
69}
70
71impl ToTokens for PgrxOperatorAttributeWithIdent {
72    fn to_tokens(&self, tokens: &mut TokenStream2) {
73        let fn_name = &self.fn_name;
74        let operator = fn_name.to_string().replace(' ', "");
75        let quoted = quote! {
76            #operator
77        };
78        tokens.append_all(quoted);
79    }
80}
81
82#[derive(Debug, Clone)]
83pub struct PgrxOperatorOpName {
84    pub op_name: TokenStream2,
85}
86
87impl Parse for PgrxOperatorOpName {
88    fn parse(input: &ParseBuffer) -> Result<Self, syn::Error> {
89        Ok(PgrxOperatorOpName { op_name: input.parse()? })
90    }
91}
92
93impl ToTokens for PgrxOperatorOpName {
94    fn to_tokens(&self, tokens: &mut TokenStream2) {
95        let op_name = &self.op_name;
96        let op_string = op_name.to_string().replacen(' ', "", 256);
97        let quoted = quote! {
98            #op_string
99        };
100        tokens.append_all(quoted);
101    }
102}