pgrx_sql_entity_graph/metadata/
function_metadata.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
12Function level metadata 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
18*/
19use super::{FunctionMetadataEntity, SqlTranslatable};
20
21/**
22Provide SQL generation related information on functions
23
24```rust
25use pgrx_sql_entity_graph::metadata::{FunctionMetadata, Returns, SqlMapping};
26fn floof(i: i32) -> String { todo!() }
27
28type FunctionPointer = fn(i32) -> String;
29let metadata = FunctionPointer::entity();
30assert_eq!(
31    metadata.retval.return_sql,
32    Ok(Returns::One(SqlMapping::As("TEXT".to_string()))),
33);
34```
35 */
36pub trait FunctionMetadata<A> {
37    fn path() -> &'static str {
38        core::any::type_name::<Self>()
39    }
40    fn entity() -> FunctionMetadataEntity;
41}
42
43macro_rules! impl_fn {
44    ($($A:ident),* $(,)?) => {
45        impl<$($A,)* R, F> FunctionMetadata<($($A,)*)> for F
46        where
47            $($A: SqlTranslatable,)*
48            R: SqlTranslatable,
49            F: FnMut($($A,)*) -> R,
50        {
51            fn entity() -> FunctionMetadataEntity {
52                FunctionMetadataEntity {
53                    arguments: vec![$(<$A>::entity()),*],
54                    retval: R::entity(),
55                    path: core::any::type_name::<Self>(),
56                }
57            }
58        }
59        impl<$($A,)* R> FunctionMetadata<($($A,)*)> for unsafe fn($($A,)*) -> R
60        where
61            $($A: SqlTranslatable,)*
62            R: SqlTranslatable,
63        {
64            fn entity() -> FunctionMetadataEntity {
65                FunctionMetadataEntity {
66                    arguments: vec![$(<$A>::entity()),*],
67                    retval: R::entity(),
68                    path: core::any::type_name::<Self>(),
69                }
70            }
71        }
72    };
73}
74
75impl_fn!();
76impl_fn!(T0);
77impl_fn!(T0, T1);
78impl_fn!(T0, T1, T2);
79impl_fn!(T0, T1, T2, T3);
80impl_fn!(T0, T1, T2, T3, T4);
81impl_fn!(T0, T1, T2, T3, T4, T5);
82impl_fn!(T0, T1, T2, T3, T4, T5, T6);
83impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7);
84impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8);
85impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9);
86impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
87impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
88impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
89impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13);
90impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14);
91impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15);
92impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16);
93impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17);
94impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18);
95impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19);
96impl_fn!(
97    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20
98);
99impl_fn!(
100    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
101    T21
102);
103impl_fn!(
104    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
105    T21, T22
106);
107impl_fn!(
108    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
109    T21, T22, T23
110);
111impl_fn!(
112    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
113    T21, T22, T23, T24
114);
115impl_fn!(
116    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
117    T21, T22, T23, T24, T25
118);
119impl_fn!(
120    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
121    T21, T22, T23, T24, T25, T26
122);
123impl_fn!(
124    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
125    T21, T22, T23, T24, T25, T26, T27
126);
127impl_fn!(
128    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
129    T21, T22, T23, T24, T25, T26, T27, T28
130);
131impl_fn!(
132    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
133    T21, T22, T23, T24, T25, T26, T27, T28, T29
134);
135impl_fn!(
136    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
137    T21, T22, T23, T24, T25, T26, T27, T28, T29, T30
138);
139impl_fn!(
140    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
141    T21, T22, T23, T24, T25, T26, T27, T28, T29, T30, T31
142);