pgx_utils/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::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_utils::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 Aliase 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}
50
51/// A mapping from a Rust source fragment to a SQL type, typically for type aliases.
52///
53/// In general, this can only offer a fuzzy matching, as it does not use [`core::any::TypeId`].
54///
55/// ```rust
56/// use pgx_utils::sql_entity_graph::RustSourceOnlySqlMapping;
57///
58/// let constructed = RustSourceOnlySqlMapping::new(
59/// String::from("Oid"),
60/// String::from("int"),
61/// );
62/// ```
63#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
64pub struct RustSourceOnlySqlMapping {
65 pub rust: String,
66 pub sql: String,
67}
68
69impl RustSourceOnlySqlMapping {
70 pub fn new(rust: String, sql: String) -> Self {
71 Self { rust: rust.to_string(), sql: sql.to_string() }
72 }
73}