1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
pub mod entity;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{quote, ToTokens, TokenStreamExt};
use syn::parse::{Parse, ParseStream};
use syn::{DeriveInput, Ident};
use crate::sql_entity_graph::ToSqlConfig;
#[derive(Debug, Clone)]
pub struct PostgresOrd {
pub name: Ident,
pub to_sql_config: ToSqlConfig,
}
impl PostgresOrd {
pub fn new(name: Ident, to_sql_config: ToSqlConfig) -> Result<Self, syn::Error> {
if !to_sql_config.overrides_default() {
crate::ident_is_acceptable_to_postgres(&name)?;
}
Ok(Self { name, to_sql_config })
}
pub fn from_derive_input(derive_input: DeriveInput) -> Result<Self, syn::Error> {
let to_sql_config =
ToSqlConfig::from_attributes(derive_input.attrs.as_slice())?.unwrap_or_default();
Self::new(derive_input.ident, to_sql_config)
}
}
impl Parse for PostgresOrd {
fn parse(input: ParseStream) -> Result<Self, syn::Error> {
use syn::Item;
let parsed = input.parse()?;
let (ident, attrs) = match &parsed {
Item::Enum(item) => (item.ident.clone(), item.attrs.as_slice()),
Item::Struct(item) => (item.ident.clone(), item.attrs.as_slice()),
_ => return Err(syn::Error::new(input.span(), "expected enum or struct")),
};
let to_sql_config = ToSqlConfig::from_attributes(attrs)?.unwrap_or_default();
Self::new(ident, to_sql_config)
}
}
impl ToTokens for PostgresOrd {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let name = &self.name;
let sql_graph_entity_fn_name =
syn::Ident::new(&format!("__pgx_internals_ord_{}", self.name), Span::call_site());
let to_sql_config = &self.to_sql_config;
let inv = quote! {
#[no_mangle]
#[doc(hidden)]
pub extern "Rust" fn #sql_graph_entity_fn_name() -> ::pgx::utils::sql_entity_graph::SqlGraphEntity {
use core::any::TypeId;
extern crate alloc;
use alloc::vec::Vec;
use alloc::vec;
let submission = ::pgx::utils::sql_entity_graph::PostgresOrdEntity {
name: stringify!(#name),
file: file!(),
line: line!(),
full_path: core::any::type_name::<#name>(),
module_path: module_path!(),
id: TypeId::of::<#name>(),
to_sql_config: #to_sql_config,
};
::pgx::utils::sql_entity_graph::SqlGraphEntity::Ord(submission)
}
};
tokens.append_all(inv);
}
}