pgrx_sql_entity_graph/
mapping.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
12Rust to SQL mapping support for dependency graph generation
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 core::any::TypeId;
19
20/// A mapping from a Rust type to a SQL type, with a `TypeId`.
21///
22/// ```rust
23/// use pgrx_sql_entity_graph::RustSqlMapping;
24///
25/// let constructed = RustSqlMapping::of::<i32>(String::from("int"));
26/// let raw = RustSqlMapping {
27///     rust: core::any::type_name::<i32>().to_string(),
28///     sql: String::from("int"),
29///     id: core::any::TypeId::of::<i32>(),
30/// };
31///
32/// assert_eq!(constructed, raw);
33/// ```
34#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
35pub struct RustSqlMapping {
36    // This is the **resolved** type, not the raw source. This means a Type Alias of `type Foo = u32` would appear as `u32`.
37    pub rust: String,
38    pub sql: String,
39    pub id: TypeId,
40}
41
42impl RustSqlMapping {
43    pub fn of<T: 'static>(sql: String) -> Self {
44        Self {
45            rust: core::any::type_name::<T>().to_string(),
46            sql: sql.to_string(),
47            id: core::any::TypeId::of::<T>(),
48        }
49    }
50}