Skip to main content

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 */
36#[diagnostic::on_unimplemented(
37    message = "cannot generate SQL schema for this function",
38    label = "some types in {Self} cannot be translated to SQL"
39)]
40pub trait FunctionMetadata<A> {
41    fn path() -> &'static str {
42        core::any::type_name::<Self>()
43    }
44    fn entity() -> FunctionMetadataEntity;
45}
46
47macro_rules! impl_fn {
48    ($($A:ident),* $(,)?) => {
49        #[diagnostic::do_not_recommend]
50        impl<$($A,)* R, F> FunctionMetadata<($($A,)*)> for F
51        where
52            $($A: SqlTranslatable,)*
53            R: SqlTranslatable,
54            F: FnMut($($A,)*) -> R,
55        {
56            fn entity() -> FunctionMetadataEntity {
57                FunctionMetadataEntity {
58                    arguments: vec![$(<$A>::entity()),*],
59                    retval: R::entity(),
60                    path: core::any::type_name::<Self>(),
61                }
62            }
63        }
64
65        #[diagnostic::do_not_recommend]
66        impl<$($A,)* R> FunctionMetadata<($($A,)*)> for unsafe fn($($A,)*) -> R
67        where
68            $($A: SqlTranslatable,)*
69            R: SqlTranslatable,
70        {
71            fn entity() -> FunctionMetadataEntity {
72                FunctionMetadataEntity {
73                    arguments: vec![$(<$A>::entity()),*],
74                    retval: R::entity(),
75                    path: core::any::type_name::<Self>(),
76                }
77            }
78        }
79    };
80}
81
82impl_fn!();
83impl_fn!(T0);
84impl_fn!(T0, T1);
85impl_fn!(T0, T1, T2);
86impl_fn!(T0, T1, T2, T3);
87impl_fn!(T0, T1, T2, T3, T4);
88impl_fn!(T0, T1, T2, T3, T4, T5);
89impl_fn!(T0, T1, T2, T3, T4, T5, T6);
90impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7);
91impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8);
92impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9);
93impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
94impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
95impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
96impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13);
97impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14);
98impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15);
99impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16);
100impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17);
101impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18);
102impl_fn!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19);
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);
106impl_fn!(
107    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
108    T21
109);
110impl_fn!(
111    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
112    T21, T22
113);
114impl_fn!(
115    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
116    T21, T22, T23
117);
118impl_fn!(
119    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
120    T21, T22, T23, T24
121);
122impl_fn!(
123    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
124    T21, T22, T23, T24, T25
125);
126impl_fn!(
127    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
128    T21, T22, T23, T24, T25, T26
129);
130impl_fn!(
131    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
132    T21, T22, T23, T24, T25, T26, T27
133);
134impl_fn!(
135    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
136    T21, T22, T23, T24, T25, T26, T27, T28
137);
138impl_fn!(
139    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
140    T21, T22, T23, T24, T25, T26, T27, T28, T29
141);
142impl_fn!(
143    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
144    T21, T22, T23, T24, T25, T26, T27, T28, T29, T30
145);
146impl_fn!(
147    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,
148    T21, T22, T23, T24, T25, T26, T27, T28, T29, T30, T31
149);