pgx/
namespace.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//! A helper struct for creating a Postgres `List` of `String`s to qualify an object name
11
12use crate::list::PgList;
13use crate::pg_sys;
14use pgx_pg_sys::AsPgCStr;
15
16/// A helper struct for creating a Postgres `List` of `String`s to qualify an object name
17pub struct PgQualifiedNameBuilder {
18    #[cfg(feature = "pg15")]
19    list: PgList<pg_sys::String>,
20    #[cfg(not(feature = "pg15"))]
21    list: PgList<pg_sys::Value>,
22}
23
24impl Default for PgQualifiedNameBuilder {
25    fn default() -> Self {
26        Self::new()
27    }
28}
29
30impl PgQualifiedNameBuilder {
31    pub fn new() -> PgQualifiedNameBuilder {
32        PgQualifiedNameBuilder {
33            #[cfg(feature = "pg15")]
34            list: PgList::<pg_sys::String>::new(),
35            #[cfg(not(feature = "pg15"))]
36            list: PgList::<pg_sys::Value>::new(),
37        }
38    }
39
40    pub fn push(mut self, value: &str) -> PgQualifiedNameBuilder {
41        unsafe {
42            // SAFETY:  the result of pg_sys::makeString is always a valid pointer
43            self.list.push(pg_sys::makeString(value.as_pg_cstr()));
44        }
45        self
46    }
47
48    pub fn get_operator_oid(self, lhs_type: pg_sys::Oid, rhs_type: pg_sys::Oid) -> pg_sys::Oid {
49        unsafe { pg_sys::OpernameGetOprid(self.list.into_pg(), lhs_type, rhs_type) }
50    }
51}