-
-
Notifications
You must be signed in to change notification settings - Fork 254
/
attribute.rs
221 lines (213 loc) · 8.35 KB
/
attribute.rs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC.
//LICENSE
//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc.
//LICENSE
//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <[email protected]>
//LICENSE
//LICENSE All rights reserved.
//LICENSE
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
/*!
`#[pg_extern]` related attributes for Rust to SQL translation
> Like all of the [`sql_entity_graph`][crate] APIs, this is considered **internal**
to the `pgrx` framework and very subject to change between versions. While you may use this, please do it with caution.
*/
use crate::positioning_ref::PositioningRef;
use crate::to_sql::ToSqlConfig;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{quote, ToTokens, TokenStreamExt};
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::Token;
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum Attribute {
Immutable,
Strict,
Stable,
Volatile,
Raw,
NoGuard,
CreateOrReplace,
SecurityDefiner,
SecurityInvoker,
ParallelSafe,
ParallelUnsafe,
ParallelRestricted,
Error(syn::LitStr),
Schema(syn::LitStr),
Name(syn::LitStr),
Cost(syn::Expr),
Requires(Punctuated<PositioningRef, Token![,]>),
Sql(ToSqlConfig),
}
impl Attribute {
pub(crate) fn to_sql_entity_graph_tokens(&self) -> TokenStream2 {
match self {
Attribute::Immutable => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::Immutable }
}
Attribute::Strict => quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::Strict },
Attribute::Stable => quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::Stable },
Attribute::Volatile => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::Volatile }
}
Attribute::Raw => quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::Raw },
Attribute::NoGuard => quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::NoGuard },
Attribute::CreateOrReplace => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::CreateOrReplace }
}
Attribute::SecurityDefiner => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::SecurityDefiner}
}
Attribute::SecurityInvoker => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::SecurityInvoker}
}
Attribute::ParallelSafe => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::ParallelSafe }
}
Attribute::ParallelUnsafe => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::ParallelUnsafe }
}
Attribute::ParallelRestricted => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::ParallelRestricted }
}
Attribute::Error(s) => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::Error(String::from(#s)) }
}
Attribute::Schema(s) => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::Schema(String::from(#s)) }
}
Attribute::Name(s) => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::Name(String::from(#s)) }
}
Attribute::Cost(s) => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::Cost(format!("{}", #s)) }
}
Attribute::Requires(items) => {
let items_iter = items.iter().map(|x| x.to_token_stream()).collect::<Vec<_>>();
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::Requires(vec![#(#items_iter),*],) }
}
// This attribute is handled separately
Attribute::Sql(_) => {
quote! {}
}
}
}
}
impl ToTokens for Attribute {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let quoted = match self {
Attribute::Immutable => quote! { immutable },
Attribute::Strict => quote! { strict },
Attribute::Stable => quote! { stable },
Attribute::Volatile => quote! { volatile },
Attribute::Raw => quote! { raw },
Attribute::NoGuard => quote! { no_guard },
Attribute::CreateOrReplace => quote! { create_or_replace },
Attribute::SecurityDefiner => {
quote! {security_definer}
}
Attribute::SecurityInvoker => {
quote! {security_invoker}
}
Attribute::ParallelSafe => {
quote! { parallel_safe }
}
Attribute::ParallelUnsafe => {
quote! { parallel_unsafe }
}
Attribute::ParallelRestricted => {
quote! { parallel_restricted }
}
Attribute::Error(s) => {
quote! { error = #s }
}
Attribute::Schema(s) => {
quote! { schema = #s }
}
Attribute::Name(s) => {
quote! { name = #s }
}
Attribute::Cost(s) => {
quote! { cost = #s }
}
Attribute::Requires(items) => {
let items_iter = items.iter().map(|x| x.to_token_stream()).collect::<Vec<_>>();
quote! { requires = [#(#items_iter),*] }
}
// This attribute is handled separately
Attribute::Sql(to_sql_config) => {
quote! { sql = #to_sql_config }
}
};
tokens.append_all(quoted);
}
}
impl Parse for Attribute {
fn parse(input: ParseStream) -> Result<Self, syn::Error> {
let ident: syn::Ident = input.parse()?;
let found = match ident.to_string().as_str() {
"immutable" => Self::Immutable,
"strict" => Self::Strict,
"stable" => Self::Stable,
"volatile" => Self::Volatile,
"raw" => Self::Raw,
"no_guard" => Self::NoGuard,
"create_or_replace" => Self::CreateOrReplace,
"security_definer" => Self::SecurityDefiner,
"security_invoker" => Self::SecurityInvoker,
"parallel_safe" => Self::ParallelSafe,
"parallel_unsafe" => Self::ParallelUnsafe,
"parallel_restricted" => Self::ParallelRestricted,
"error" => {
let _eq: Token![=] = input.parse()?;
let literal: syn::LitStr = input.parse()?;
Self::Error(literal)
}
"schema" => {
let _eq: Token![=] = input.parse()?;
let literal: syn::LitStr = input.parse()?;
Attribute::Schema(literal)
}
"name" => {
let _eq: Token![=] = input.parse()?;
let literal: syn::LitStr = input.parse()?;
Self::Name(literal)
}
"cost" => {
let _eq: Token![=] = input.parse()?;
let literal: syn::Expr = input.parse()?;
Self::Cost(literal)
}
"requires" => {
let _eq: syn::token::Eq = input.parse()?;
let content;
let _bracket = syn::bracketed!(content in input);
Self::Requires(content.parse_terminated(PositioningRef::parse)?)
}
"sql" => {
use crate::pgrx_attribute::ArgValue;
use syn::Lit;
let _eq: Token![=] = input.parse()?;
match input.parse::<ArgValue>()? {
ArgValue::Path(p) => Self::Sql(ToSqlConfig::from(p)),
ArgValue::Lit(Lit::Bool(b)) => Self::Sql(ToSqlConfig::from(b.value)),
ArgValue::Lit(Lit::Str(s)) => Self::Sql(ToSqlConfig::from(s)),
ArgValue::Lit(other) => {
return Err(syn::Error::new(
other.span(),
"expected boolean, path, or string literal",
))
}
}
}
e => {
return Err(syn::Error::new(
Span::call_site(),
format!("Invalid option `{e}` inside `{ident} {input}`"),
))
}
};
Ok(found)
}
}