Skip to main content

pg_query/
plpgsql_catalog.rs

1//! Catalog snapshots for PostgreSQL's PL/pgSQL type lookup callbacks.
2
3use std::collections::BTreeMap;
4use std::ffi::{c_void, CStr, CString};
5
6use crate::bindings::*;
7use crate::{Error, Result};
8
9/// The pg_type attributes needed to distinguish scalar, domain, array, and composite declarations.
10#[derive(Debug, Clone)]
11pub struct PlpgsqlType {
12    pub oid: u32,
13    pub namespace_oid: u32,
14    pub name: String,
15    pub length: i16,
16    pub by_value: bool,
17    pub type_kind: u8,
18    pub category: u8,
19    pub alignment: u8,
20    pub storage: u8,
21    pub array_oid: u32,
22    pub element_oid: u32,
23    pub base_type_oid: u32,
24    pub collation_oid: u32,
25    pub subscript_handler_oid: u32,
26}
27
28/// An immutable catalog snapshot for one parse. `search_path` is the caller's effective namespace order, including pg_catalog where appropriate. Built-in types remain available through PostgreSQL's own fallback catalog.
29#[derive(Debug, Clone, Default)]
30pub struct PlpgsqlCatalog {
31    pub namespaces: BTreeMap<String, u32>,
32    pub search_path: Vec<String>,
33    pub types: Vec<PlpgsqlType>,
34}
35
36struct CatalogContext<'a> {
37    catalog: &'a PlpgsqlCatalog,
38    names: Vec<CString>,
39}
40
41impl CatalogContext<'_> {
42    fn write_type(&self, index: usize, output: *mut PgQueryPlpgsqlTypeMetadata) {
43        let ty = &self.catalog.types[index];
44        let value = PgQueryPlpgsqlTypeMetadata {
45            oid: ty.oid,
46            namespace_oid: ty.namespace_oid,
47            name: self.names[index].as_ptr(),
48            length: ty.length,
49            by_value: ty.by_value,
50            type_kind: ty.type_kind as _,
51            category: ty.category as _,
52            alignment: ty.alignment as _,
53            storage: ty.storage as _,
54            array_oid: ty.array_oid,
55            element_oid: ty.element_oid,
56            base_type_oid: ty.base_type_oid,
57            collation_oid: ty.collation_oid,
58            subscript_handler_oid: ty.subscript_handler_oid,
59        };
60        // The C parser supplies a valid output pointer and copies this metadata before the next callback.
61        unsafe { output.write(value) };
62    }
63}
64
65unsafe extern "C" fn lookup_namespace(
66    context: *mut c_void,
67    name: *const std::os::raw::c_char,
68    output: *mut u32,
69) -> PgQueryCatalogLookupResult {
70    let context = &*(context as *const CatalogContext<'_>);
71    let name = CStr::from_ptr(name).to_bytes();
72    match context
73        .catalog
74        .namespaces
75        .iter()
76        .find(|(candidate, _)| candidate.as_bytes() == name)
77    {
78        Some((_, oid)) => {
79            output.write(*oid);
80            PgQueryCatalogLookupResult_PG_QUERY_CATALOG_LOOKUP_FOUND
81        }
82        None => PgQueryCatalogLookupResult_PG_QUERY_CATALOG_LOOKUP_NOT_FOUND,
83    }
84}
85
86unsafe extern "C" fn lookup_type_by_name(
87    context: *mut c_void,
88    schema: *const std::os::raw::c_char,
89    name: *const std::os::raw::c_char,
90    output: *mut PgQueryPlpgsqlTypeMetadata,
91) -> PgQueryCatalogLookupResult {
92    let context = &*(context as *const CatalogContext<'_>);
93    let name = CStr::from_ptr(name).to_bytes();
94    let in_schema = |schema: &[u8]| {
95        let namespace = context
96            .catalog
97            .namespaces
98            .iter()
99            .find(|(candidate, _)| candidate.as_bytes() == schema)
100            .map(|(_, oid)| *oid)?;
101        context
102            .catalog
103            .types
104            .iter()
105            .position(|ty| ty.namespace_oid == namespace && ty.name.as_bytes() == name)
106    };
107    let found = if schema.is_null() {
108        context
109            .catalog
110            .search_path
111            .iter()
112            .find_map(|schema| in_schema(schema.as_bytes()))
113    } else {
114        in_schema(CStr::from_ptr(schema).to_bytes())
115    };
116    match found {
117        Some(index) => {
118            context.write_type(index, output);
119            PgQueryCatalogLookupResult_PG_QUERY_CATALOG_LOOKUP_FOUND
120        }
121        None => PgQueryCatalogLookupResult_PG_QUERY_CATALOG_LOOKUP_NOT_FOUND,
122    }
123}
124
125unsafe extern "C" fn lookup_type_by_oid(
126    context: *mut c_void,
127    oid: u32,
128    output: *mut PgQueryPlpgsqlTypeMetadata,
129) -> PgQueryCatalogLookupResult {
130    let context = &*(context as *const CatalogContext<'_>);
131    match context.catalog.types.iter().position(|ty| ty.oid == oid) {
132        Some(index) => {
133            context.write_type(index, output);
134            PgQueryCatalogLookupResult_PG_QUERY_CATALOG_LOOKUP_FOUND
135        }
136        None => PgQueryCatalogLookupResult_PG_QUERY_CATALOG_LOOKUP_NOT_FOUND,
137    }
138}
139
140unsafe extern "C" fn catalog_error(_context: *mut c_void) -> *const std::os::raw::c_char {
141    std::ptr::null()
142}
143
144/// Parse PL/pgSQL with the caller's catalog types instead of treating every unknown type as a record. The snapshot and callback storage remain local to this synchronous parse; no pointers or callback state escape it.
145pub fn parse_plpgsql_with_catalog(
146    stmt: &str,
147    catalog: &PlpgsqlCatalog,
148) -> Result<serde_json::Value> {
149    let input = CString::new(stmt)?;
150    let mut context = CatalogContext {
151        catalog,
152        names: catalog
153            .types
154            .iter()
155            .map(|ty| CString::new(ty.name.as_str()))
156            .collect::<std::result::Result<Vec<_>, _>>()?,
157    };
158    let callbacks = PgQueryPlpgsqlCatalog {
159        context: (&mut context as *mut CatalogContext<'_>).cast(),
160        lookup_namespace: Some(lookup_namespace),
161        lookup_type_by_name: Some(lookup_type_by_name),
162        lookup_type_by_oid: Some(lookup_type_by_oid),
163        get_error: Some(catalog_error),
164    };
165    // All callbacks only inspect the immutable snapshot. Its C strings and context outlive the C parser's synchronous callback scope.
166    let result = unsafe { pg_query_parse_plpgsql_with_catalog(input.as_ptr(), &callbacks) };
167    let structure = if !result.error.is_null() {
168        let message = unsafe { CStr::from_ptr((*result.error).message) }
169            .to_string_lossy()
170            .to_string();
171        Err(Error::Parse(message))
172    } else if result.plpgsql_funcs.is_null() {
173        Err(Error::InvalidPointer)
174    } else {
175        let raw = unsafe { CStr::from_ptr(result.plpgsql_funcs) };
176        serde_json::from_str(&raw.to_string_lossy())
177            .map_err(|error| Error::InvalidJson(error.to_string()))
178    };
179    unsafe { pg_query_free_plpgsql_parse_result(result) };
180    structure
181}