Skip to main content

pgrx_sql_entity_graph/metadata/
entity.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 and type level metadata entities 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::{ArgumentError, Returns, ReturnsError, SqlMapping};
20
21/// Describes whether a SQL type reference should resolve to schema emitted by this
22/// extension or be treated as an external SQL type.
23#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
24pub enum TypeOrigin {
25    /// The extension being built is responsible for emitting this type into the
26    /// schema graph.
27    ThisExtension,
28    /// The type already exists outside this extension's schema graph.
29    External,
30}
31
32#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
33pub struct FunctionMetadataEntity<'a> {
34    pub arguments: Vec<FunctionMetadataTypeEntity<'a>>,
35    pub retval: FunctionMetadataTypeEntity<'a>,
36    pub path: &'a str,
37}
38
39#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
40pub struct FunctionMetadataTypeEntity<'a> {
41    pub resolution: Option<FunctionMetadataTypeResolutionEntity<'a>>,
42    pub argument_sql: Result<SqlMapping, ArgumentError>,
43    pub return_sql: Result<Returns, ReturnsError>,
44}
45
46#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
47pub struct FunctionMetadataTypeResolutionEntity<'a> {
48    pub type_ident: &'a str,
49    pub type_origin: TypeOrigin,
50}
51
52impl<'a> FunctionMetadataTypeEntity<'a> {
53    pub const fn resolved(
54        type_ident: &'a str,
55        type_origin: TypeOrigin,
56        argument_sql: Result<SqlMapping, ArgumentError>,
57        return_sql: Result<Returns, ReturnsError>,
58    ) -> Self {
59        Self {
60            resolution: Some(FunctionMetadataTypeResolutionEntity { type_ident, type_origin }),
61            argument_sql,
62            return_sql,
63        }
64    }
65
66    pub const fn sql_only(
67        argument_sql: Result<SqlMapping, ArgumentError>,
68        return_sql: Result<Returns, ReturnsError>,
69    ) -> Self {
70        Self { resolution: None, argument_sql, return_sql }
71    }
72
73    pub const fn type_ident(&self) -> Option<&'a str> {
74        match self.resolution {
75            Some(resolution) => Some(resolution.type_ident),
76            None => None,
77        }
78    }
79
80    pub const fn type_origin(&self) -> Option<TypeOrigin> {
81        match self.resolution {
82            Some(resolution) => Some(resolution.type_origin),
83            None => None,
84        }
85    }
86
87    pub const fn needs_type_resolution(&self) -> bool {
88        self.resolution.is_some()
89    }
90}