pgrx_sql_entity_graph/pg_extern/
mod.rs1mod argument;
19mod attribute;
20mod cast;
21pub mod entity;
22mod operator;
23mod returning;
24mod search_path;
25
26pub use argument::PgExternArgument;
27pub(crate) use attribute::Attribute;
28pub use cast::PgCast;
29pub use operator::PgOperator;
30pub use returning::NameMacro;
31use syn::token::Comma;
32
33use self::returning::Returning;
34use crate::ExternArgs;
35use crate::ToSqlConfig;
36use crate::enrich::{CodeEnrichment, ToEntityGraphTokens, ToRustCodeTokens};
37use crate::finfo::{finfo_v1_extern_c, finfo_v1_tokens};
38use crate::fmt::ErrHarder;
39use operator::{PgrxOperatorAttributeWithIdent, PgrxOperatorOpName};
40use search_path::SearchPathList;
41
42use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
43use quote::{ToTokens, quote};
44use syn::parse::{Parse, ParseStream, Parser};
45use syn::punctuated::Punctuated;
46use syn::spanned::Spanned;
47use syn::{Meta, Token};
48
49macro_rules! quote_spanned {
50 ($span:expr=> $($expansion:tt)*) => {
51 {
52 let synthetic = Span::mixed_site();
53 let synthetic = synthetic.located_at($span);
54 quote::quote_spanned! {synthetic=> $($expansion)* }
55 }
56 };
57}
58
59macro_rules! format_ident {
60 ($s:literal, $e:expr) => {{
61 let mut synthetic = $e.clone();
62 synthetic.set_span(Span::call_site().located_at($e.span()));
63 quote::format_ident!($s, synthetic)
64 }};
65}
66
67#[derive(Debug, Clone)]
90pub struct PgExtern {
91 attrs: Vec<Attribute>,
92 func: syn::ItemFn,
93 to_sql_config: ToSqlConfig,
94 operator: Option<PgOperator>,
95 cast: Option<PgCast>,
96 search_path: Option<SearchPathList>,
97 inputs: Vec<PgExternArgument>,
98 returns: Returning,
99}
100
101impl PgExtern {
102 #[track_caller]
103 pub fn new(attr: TokenStream2, item: TokenStream2) -> Result<CodeEnrichment<Self>, syn::Error> {
104 let mut attrs = Vec::new();
105 let mut to_sql_config: Option<ToSqlConfig> = None;
106
107 let parser = Punctuated::<Attribute, Token![,]>::parse_terminated;
108 let attr_ts = attr.clone();
109 let punctuated_attrs = parser
110 .parse2(attr)
111 .more_error(lazy_err!(attr_ts, "failed parsing pg_extern arguments"))?;
112 for pair in punctuated_attrs.into_pairs() {
113 match pair.into_value() {
114 Attribute::Sql(config) => to_sql_config = to_sql_config.or(Some(config)),
115 attr => attrs.push(attr),
116 }
117 }
118
119 let mut to_sql_config = to_sql_config.unwrap_or_default();
120
121 let func = syn::parse2::<syn::ItemFn>(item)?;
122
123 if let Some(ref mut content) = to_sql_config.content {
124 let value = content.value();
125 let span = content.span();
127 let updated_value =
128 value.replace("@FUNCTION_NAME@", &(func.sig.ident.to_string() + "_wrapper")) + "\n";
129 *content = syn::LitStr::new(&updated_value, span);
130 }
131
132 if !to_sql_config.overrides_default() {
133 crate::ident_is_acceptable_to_postgres(&func.sig.ident)?;
134 }
135 let operator = Self::operator(&func)?;
136 let search_path = Self::search_path(&func)?;
137 let inputs = Self::inputs(&func)?;
138 let returns = Returning::try_from(&func.sig.output)?;
139 Ok(CodeEnrichment(Self {
140 attrs,
141 func,
142 to_sql_config,
143 operator,
144 cast: None,
145 search_path,
146 inputs,
147 returns,
148 }))
149 }
150
151 pub fn as_cast(&self, pg_cast: PgCast) -> PgExtern {
153 let mut result = self.clone();
154 result.cast = Some(pg_cast);
155 result
156 }
157
158 fn name(&self) -> String {
159 self.attrs
160 .iter()
161 .find_map(|a| match a {
162 Attribute::Name(name) => Some(name.value()),
163 _ => None,
164 })
165 .unwrap_or_else(|| self.func.sig.ident.to_string())
166 }
167
168 fn schema(&self) -> Option<String> {
169 self.attrs.iter().find_map(|a| match a {
170 Attribute::Schema(name) => Some(name.value()),
171 _ => None,
172 })
173 }
174
175 pub fn extern_attrs(&self) -> &[Attribute] {
176 self.attrs.as_slice()
177 }
178
179 #[track_caller]
180 fn overridden(&self) -> Option<syn::LitStr> {
181 let mut span = None;
182 let mut retval = None;
183 let mut in_commented_sql_block = false;
184 for meta in self.func.attrs.iter().filter_map(|attr| {
185 if attr.meta.path().is_ident("doc") { Some(attr.meta.clone()) } else { None }
186 }) {
187 let Meta::NameValue(syn::MetaNameValue { ref value, .. }) = meta else { continue };
188 let syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(inner), .. }) = value else {
189 continue;
190 };
191 span.get_or_insert(value.span());
192 if !in_commented_sql_block && inner.value().trim() == "```pgrxsql" {
193 in_commented_sql_block = true;
194 } else if in_commented_sql_block && inner.value().trim() == "```" {
195 in_commented_sql_block = false;
196 } else if in_commented_sql_block {
197 let line = inner
198 .value()
199 .trim_start()
200 .replace("@FUNCTION_NAME@", &(self.func.sig.ident.to_string() + "_wrapper"))
201 + "\n";
202 retval.get_or_insert_with(String::default).push_str(&line);
203 }
204 }
205 retval.map(|s| syn::LitStr::new(s.as_ref(), span.unwrap()))
206 }
207
208 #[track_caller]
209 fn operator(func: &syn::ItemFn) -> syn::Result<Option<PgOperator>> {
210 let mut skel = Option::<PgOperator>::default();
211 for attr in &func.attrs {
212 let last_segment = attr.path().segments.last().unwrap();
213 match last_segment.ident.to_string().as_str() {
214 s @ "opname" => {
215 let attr = attr
217 .parse_args::<PgrxOperatorOpName>()
218 .more_error(lazy_err!(attr.clone(), "bad parse of {s}?"))?;
219 skel.get_or_insert_with(Default::default).opname.get_or_insert(attr);
220 }
221 s @ "commutator" => {
222 let attr: PgrxOperatorAttributeWithIdent = attr
223 .parse_args()
224 .more_error(lazy_err!(attr.clone(), "bad parse of {s}?"))?;
225
226 skel.get_or_insert_with(Default::default).commutator.get_or_insert(attr);
227 }
228 s @ "negator" => {
229 let attr: PgrxOperatorAttributeWithIdent = attr
230 .parse_args()
231 .more_error(lazy_err!(attr.clone(), "bad parse of {s}?"))?;
232 skel.get_or_insert_with(Default::default).negator.get_or_insert(attr);
233 }
234 s @ "join" => {
235 let attr: PgrxOperatorAttributeWithIdent = attr
236 .parse_args()
237 .more_error(lazy_err!(attr.clone(), "bad parse of {s}?"))?;
238
239 skel.get_or_insert_with(Default::default).join.get_or_insert(attr);
240 }
241 s @ "restrict" => {
242 let attr: PgrxOperatorAttributeWithIdent = attr
243 .parse_args()
244 .more_error(lazy_err!(attr.clone(), "bad parse of {s}?"))?;
245
246 skel.get_or_insert_with(Default::default).restrict.get_or_insert(attr);
247 }
248 "hashes" => {
249 skel.get_or_insert_with(Default::default).hashes = true;
250 }
251 "merges" => {
252 skel.get_or_insert_with(Default::default).merges = true;
253 }
254 _ => (),
255 }
256 }
257 Ok(skel)
258 }
259
260 fn search_path(func: &syn::ItemFn) -> syn::Result<Option<SearchPathList>> {
261 func.attrs
262 .iter()
263 .find(|f| {
264 f.path()
265 .segments
266 .first()
267 .map(|f| f.ident == Ident::new("search_path", func.span()))
269 .unwrap_or_default()
270 })
271 .map(|attr| attr.parse_args::<SearchPathList>())
272 .transpose()
273 }
274
275 fn inputs(func: &syn::ItemFn) -> syn::Result<Vec<PgExternArgument>> {
276 let mut args = Vec::default();
277 for input in &func.sig.inputs {
278 let arg = PgExternArgument::build(input.clone())?;
279 args.push(arg);
280 }
281 Ok(args)
282 }
283
284 fn entity_tokens(&self) -> TokenStream2 {
285 let ident = &self.func.sig.ident;
286 let name = self.name();
287 let schema = self.schema();
288 let inputs = &self.inputs;
289 let returns = &self.returns;
290 let to_sql_config = match self.overridden() {
291 None => self.to_sql_config.clone(),
292 Some(content) => ToSqlConfig { content: Some(content), ..self.to_sql_config.clone() },
293 };
294 let sql_graph_entity_fn_name = format_ident!("__pgrx_schema_fn_{}", ident);
295 let input_count = self.inputs.len();
296 let extern_attr_count = self.attrs.len();
297 let args_len = inputs.iter().map(PgExternArgument::section_len_tokens);
298 let return_len = returns.section_len_tokens();
299 let schema_len = schema
300 .as_ref()
301 .map(|schema| {
302 quote! {
303 ::pgrx::pgrx_sql_entity_graph::section::bool_len()
304 + ::pgrx::pgrx_sql_entity_graph::section::str_len(#schema)
305 }
306 })
307 .unwrap_or_else(|| quote! { ::pgrx::pgrx_sql_entity_graph::section::bool_len() });
308 let extern_attr_lens = self.attrs.iter().map(|attr| {
309 let extern_arg = match attr {
310 Attribute::CreateOrReplace => ExternArgs::CreateOrReplace,
311 Attribute::Immutable => ExternArgs::Immutable,
312 Attribute::Strict => ExternArgs::Strict,
313 Attribute::Stable => ExternArgs::Stable,
314 Attribute::Volatile => ExternArgs::Volatile,
315 Attribute::Raw => ExternArgs::Raw,
316 Attribute::NoGuard => ExternArgs::NoGuard,
317 Attribute::SecurityDefiner => ExternArgs::SecurityDefiner,
318 Attribute::SecurityInvoker => ExternArgs::SecurityInvoker,
319 Attribute::ParallelSafe => ExternArgs::ParallelSafe,
320 Attribute::ParallelUnsafe => ExternArgs::ParallelUnsafe,
321 Attribute::ParallelRestricted => ExternArgs::ParallelRestricted,
322 Attribute::ShouldPanic(value) => ExternArgs::ShouldPanic(value.value()),
323 Attribute::Schema(value) => ExternArgs::Schema(value.value()),
324 Attribute::Support(value) => ExternArgs::Support(value.clone()),
325 Attribute::Name(value) => ExternArgs::Name(value.value()),
326 Attribute::Cost(value) => ExternArgs::Cost(value.to_token_stream().to_string()),
327 Attribute::Requires(items) => ExternArgs::Requires(items.iter().cloned().collect()),
328 Attribute::Sql(_) => unreachable!("sql attributes are handled separately"),
329 };
330 extern_arg.section_len_tokens()
331 });
332 let search_path_len = self
333 .search_path
334 .as_ref()
335 .map(SearchPathList::section_len_tokens)
336 .unwrap_or_else(|| quote! { ::pgrx::pgrx_sql_entity_graph::section::bool_len() });
337 let operator_len = self
338 .operator
339 .as_ref()
340 .map(|operator| {
341 let inner = operator.section_len_tokens();
342 quote! {
343 ::pgrx::pgrx_sql_entity_graph::section::bool_len() + (#inner)
344 }
345 })
346 .unwrap_or_else(|| quote! { ::pgrx::pgrx_sql_entity_graph::section::bool_len() });
347 let cast_len = self
348 .cast
349 .as_ref()
350 .map(|cast| {
351 let inner = cast.section_len_tokens();
352 quote! {
353 ::pgrx::pgrx_sql_entity_graph::section::bool_len() + (#inner)
354 }
355 })
356 .unwrap_or_else(|| quote! { ::pgrx::pgrx_sql_entity_graph::section::bool_len() });
357 let to_sql_config_len = to_sql_config.section_len_tokens();
358 let payload_len = quote! {
359 ::pgrx::pgrx_sql_entity_graph::section::u8_len()
360 + ::pgrx::pgrx_sql_entity_graph::section::str_len(#name)
361 + ::pgrx::pgrx_sql_entity_graph::section::str_len(stringify!(#ident))
362 + ::pgrx::pgrx_sql_entity_graph::section::str_len(core::module_path!())
363 + ::pgrx::pgrx_sql_entity_graph::section::str_len(concat!(core::module_path!(), "::", stringify!(#ident)))
364 + ::pgrx::pgrx_sql_entity_graph::section::list_len(&[
365 #( #args_len ),*
366 ])
367 + (#return_len)
368 + (#schema_len)
369 + ::pgrx::pgrx_sql_entity_graph::section::str_len(file!())
370 + ::pgrx::pgrx_sql_entity_graph::section::u32_len()
371 + ::pgrx::pgrx_sql_entity_graph::section::list_len(&[
372 #( #extern_attr_lens ),*
373 ])
374 + (#search_path_len)
375 + (#operator_len)
376 + (#cast_len)
377 + (#to_sql_config_len)
378 };
379 let total_len = quote! {
380 ::pgrx::pgrx_sql_entity_graph::section::u32_len() + (#payload_len)
381 };
382 let writer_ident =
383 Ident::new("__pgrx_schema_writer", Span::mixed_site().located_at(ident.span()));
384 let arg_writers =
385 inputs.iter().map(|arg| arg.section_writer_tokens(quote! { #writer_ident }));
386 let return_writer = returns.section_writer_tokens(quote! { #writer_ident });
387 let schema_writer = schema
388 .as_ref()
389 .map(|schema| quote! { .bool(true).str(#schema) })
390 .unwrap_or_else(|| quote! { .bool(false) });
391 let extern_attr_writers = self.attrs.iter().map(|attr| {
392 let extern_arg = match attr {
393 Attribute::CreateOrReplace => ExternArgs::CreateOrReplace,
394 Attribute::Immutable => ExternArgs::Immutable,
395 Attribute::Strict => ExternArgs::Strict,
396 Attribute::Stable => ExternArgs::Stable,
397 Attribute::Volatile => ExternArgs::Volatile,
398 Attribute::Raw => ExternArgs::Raw,
399 Attribute::NoGuard => ExternArgs::NoGuard,
400 Attribute::SecurityDefiner => ExternArgs::SecurityDefiner,
401 Attribute::SecurityInvoker => ExternArgs::SecurityInvoker,
402 Attribute::ParallelSafe => ExternArgs::ParallelSafe,
403 Attribute::ParallelUnsafe => ExternArgs::ParallelUnsafe,
404 Attribute::ParallelRestricted => ExternArgs::ParallelRestricted,
405 Attribute::ShouldPanic(value) => ExternArgs::ShouldPanic(value.value()),
406 Attribute::Schema(value) => ExternArgs::Schema(value.value()),
407 Attribute::Support(value) => ExternArgs::Support(value.clone()),
408 Attribute::Name(value) => ExternArgs::Name(value.value()),
409 Attribute::Cost(value) => ExternArgs::Cost(value.to_token_stream().to_string()),
410 Attribute::Requires(items) => ExternArgs::Requires(items.iter().cloned().collect()),
411 Attribute::Sql(_) => unreachable!("sql attributes are handled separately"),
412 };
413 extern_arg.section_writer_tokens(quote! { #writer_ident })
414 });
415 let search_path_writer = self
416 .search_path
417 .as_ref()
418 .map(|search_path| search_path.section_writer_tokens(quote! { #writer_ident }))
419 .unwrap_or_else(|| quote! { #writer_ident.bool(false) });
420 let operator_writer = self
421 .operator
422 .as_ref()
423 .map(|operator| operator.section_writer_tokens(quote! { #writer_ident.bool(true) }))
424 .unwrap_or_else(|| quote! { #writer_ident.bool(false) });
425 let cast_writer = self
426 .cast
427 .as_ref()
428 .map(|cast| cast.section_writer_tokens(quote! { #writer_ident.bool(true) }))
429 .unwrap_or_else(|| quote! { #writer_ident.bool(false) });
430 let to_sql_config_writer = to_sql_config.section_writer_tokens(quote! { #writer_ident });
431 quote_spanned! { self.func.sig.span() =>
432 ::pgrx::pgrx_sql_entity_graph::__pgrx_schema_entry!(
433 #sql_graph_entity_fn_name,
434 #total_len,
435 {
436 let #writer_ident = ::pgrx::pgrx_sql_entity_graph::section::EntryWriter::<{ #total_len }>::new()
437 .u32((#payload_len) as u32)
438 .u8(::pgrx::pgrx_sql_entity_graph::section::ENTITY_FUNCTION)
439 .str(#name)
440 .str(stringify!(#ident))
441 .str(core::module_path!())
442 .str(concat!(core::module_path!(), "::", stringify!(#ident)))
443 .u32(#input_count as u32);
444 #( let #writer_ident = { #arg_writers }; )*
445 let #writer_ident = { #return_writer };
446 let #writer_ident = #writer_ident
447 #schema_writer
448 .str(file!())
449 .u32(line!())
450 .u32(#extern_attr_count as u32);
451 #( let #writer_ident = { #extern_attr_writers }; )*
452 let #writer_ident = { #search_path_writer };
453 let #writer_ident = { #operator_writer };
454 let #writer_ident = { #cast_writer };
455 let #writer_ident = { #to_sql_config_writer };
456 #writer_ident.finish()
457 }
458 );
459 }
460 }
461
462 pub fn wrapper_func(&self) -> Result<syn::ItemFn, syn::Error> {
463 let signature = &self.func.sig;
464 let func_name = &signature.ident;
465 let synthetic_ident_span = Span::mixed_site().located_at(signature.ident.span());
467 let fcinfo_ident = syn::Ident::new("fcinfo", synthetic_ident_span);
468 let mut lifetimes = signature
469 .generics
470 .lifetimes()
471 .cloned()
472 .collect::<syn::punctuated::Punctuated<_, Comma>>();
473 let fc_lt = lifetimes
476 .first()
477 .map(|lt_p| lt_p.lifetime.clone())
478 .filter(|lt| lt.ident != "static")
479 .unwrap_or(syn::Lifetime::new("'fcx", Span::mixed_site()));
480 let fc_ltparam = syn::LifetimeParam::new(fc_lt.clone());
482 if lifetimes.first() != Some(&fc_ltparam) {
483 lifetimes.insert(0, fc_ltparam)
484 }
485
486 let args = &self.inputs;
487 let arg_pats = args.iter().map(|v| format_ident!("{}_", &v.pat)).collect::<Vec<_>>();
489 let args_ident = proc_macro2::Ident::new("_args", Span::call_site());
490 let arg_fetches = arg_pats.iter().map(|pat| {
491 quote_spanned!{ pat.span() =>
492 let #pat = #args_ident.next_arg_unchecked().unwrap_or_else(|| panic!("unboxing {} argument failed", stringify!(#pat)));
493 }
494 }
495 );
496
497 match &self.returns {
498 Returning::None
499 | Returning::Type(_)
500 | Returning::SetOf { .. }
501 | Returning::Iterated { .. } => {
502 let ret_ty = match &signature.output {
503 syn::ReturnType::Default => syn::parse_quote! { () },
504 syn::ReturnType::Type(_, ret_ty) => ret_ty.clone(),
505 };
506 let wrapper_code = quote_spanned! { self.func.block.span() =>
507 fn _internal_wrapper<#lifetimes>(fcinfo: &mut ::pgrx::callconv::FcInfo<#fc_lt>) -> ::pgrx::datum::Datum<#fc_lt> {
508 #[allow(unused_unsafe)]
509 unsafe {
510 let call_flow = <#ret_ty as ::pgrx::callconv::RetAbi>::check_and_prepare(fcinfo);
511 let result = match call_flow {
512 ::pgrx::callconv::CallCx::WrappedFn(mcx) => {
513 let mut mcx = ::pgrx::PgMemoryContexts::For(mcx);
514 let #args_ident = &mut fcinfo.args();
515 let call_result = mcx.switch_to(|_| {
516 #(#arg_fetches)*
517 #func_name( #(#arg_pats),* )
518 });
519 ::pgrx::callconv::RetAbi::to_ret(call_result)
520 }
521 ::pgrx::callconv::CallCx::RestoreCx => <#ret_ty as ::pgrx::callconv::RetAbi>::ret_from_fcx(fcinfo),
522 };
523 unsafe { <#ret_ty as ::pgrx::callconv::RetAbi>::box_ret_in(fcinfo, result) }
524 }
525 }
526 let datum = unsafe {
528 ::pgrx::pg_sys::submodules::panic::pgrx_extern_c_guard(|| {
529 let mut fcinfo = ::pgrx::callconv::FcInfo::from_ptr(#fcinfo_ident);
530 _internal_wrapper(&mut fcinfo)
531 })
532 };
533 datum.sans_lifetime()
534 };
535 finfo_v1_extern_c(&self.func, fcinfo_ident, wrapper_code)
536 }
537 }
538 }
539}
540
541trait LastIdent {
542 fn filter_last_ident(&self, id: &str) -> Option<&syn::PathSegment>;
543 #[inline]
544 fn last_ident_is(&self, id: &str) -> bool {
545 self.filter_last_ident(id).is_some()
546 }
547}
548
549impl LastIdent for syn::Type {
550 #[inline]
551 fn filter_last_ident(&self, id: &str) -> Option<&syn::PathSegment> {
552 let syn::Type::Path(syn::TypePath { path, .. }) = self else { return None };
553 path.filter_last_ident(id)
554 }
555}
556impl LastIdent for syn::TypePath {
557 #[inline]
558 fn filter_last_ident(&self, id: &str) -> Option<&syn::PathSegment> {
559 self.path.filter_last_ident(id)
560 }
561}
562impl LastIdent for syn::Path {
563 #[inline]
564 fn filter_last_ident(&self, id: &str) -> Option<&syn::PathSegment> {
565 self.segments.filter_last_ident(id)
566 }
567}
568impl<P> LastIdent for Punctuated<syn::PathSegment, P> {
569 #[inline]
570 fn filter_last_ident(&self, id: &str) -> Option<&syn::PathSegment> {
571 self.last().filter(|segment| segment.ident == id)
572 }
573}
574
575impl ToEntityGraphTokens for PgExtern {
576 fn to_entity_graph_tokens(&self) -> TokenStream2 {
577 self.entity_tokens()
578 }
579}
580
581impl ToRustCodeTokens for PgExtern {
582 fn to_rust_code_tokens(&self) -> TokenStream2 {
583 let original_func = &self.func;
584 let wrapper_func = self.wrapper_func().unwrap();
585 let finfo_tokens = finfo_v1_tokens(wrapper_func.sig.ident.clone()).unwrap();
586
587 quote_spanned! { self.func.sig.span() =>
588 #original_func
589 #wrapper_func
590 #finfo_tokens
591 }
592 }
593}
594
595impl Parse for CodeEnrichment<PgExtern> {
596 fn parse(input: ParseStream) -> Result<Self, syn::Error> {
597 let parser = Punctuated::<Attribute, Token![,]>::parse_terminated;
598 let punctuated_attrs = input.call(parser).ok().unwrap_or_default();
599 let attrs = punctuated_attrs.into_pairs().map(|pair| pair.into_value());
600 PgExtern::new(quote! {#(#attrs)*}, input.parse()?)
601 }
602}