pgx_sql_entity_graph/
mapping.rs

1/*
2Portions Copyright 2019-2021 ZomboDB, LLC.
3Portions Copyright 2021-2022 Technology Concepts & Design, Inc. <support@tcdi.com>
4
5All rights reserved.
6
7Use of this source code is governed by the MIT license that can be found in the LICENSE file.
8*/
9/*!
10
11Rust to SQL mapping support for dependency graph generation
12
13> Like all of the [`sql_entity_graph`][crate::pgx_sql_entity_graph] APIs, this is considered **internal**
14to the `pgx` framework and very subject to change between versions. While you may use this, please do it with caution.
15
16*/
17use core::any::TypeId;
18
19/// A mapping from a Rust type to a SQL type, with a `TypeId`.
20///
21/// ```rust
22/// use pgx_sql_entity_graph::RustSqlMapping;
23///
24/// let constructed = RustSqlMapping::of::<i32>(String::from("int"));
25/// let raw = RustSqlMapping {
26///     rust: core::any::type_name::<i32>().to_string(),
27///     sql: String::from("int"),
28///     id: core::any::TypeId::of::<i32>(),
29/// };
30///
31/// assert_eq!(constructed, raw);
32/// ```
33#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
34pub struct RustSqlMapping {
35    // This is the **resolved** type, not the raw source. This means a Type Alias of `type Foo = u32` would appear as `u32`.
36    pub rust: String,
37    pub sql: String,
38    pub id: TypeId,
39}
40
41impl RustSqlMapping {
42    pub fn of<T: 'static>(sql: String) -> Self {
43        Self {
44            rust: core::any::type_name::<T>().to_string(),
45            sql: sql.to_string(),
46            id: core::any::TypeId::of::<T>(),
47        }
48    }
49}