diesel/pg/
metadata_lookup.rs

1use super::{PgConnection, PgTypeMetadata};
2use prelude::*;
3
4/// Determines the OID of types at runtime
5#[allow(missing_debug_implementations)]
6#[repr(transparent)]
7pub struct PgMetadataLookup {
8    conn: PgConnection,
9}
10
11impl PgMetadataLookup {
12    #[allow(clippy::new_ret_no_self)]
13    pub(crate) fn new(conn: &PgConnection) -> &Self {
14        unsafe { &*(conn as *const PgConnection as *const PgMetadataLookup) }
15    }
16
17    /// Determine the type metadata for the given `type_name`
18    ///
19    /// This function should only be used for user defined types, or types which
20    /// come from an extension. This function may perform a SQL query to look
21    /// up the type. For built-in types, a static OID should be preferred.
22    pub fn lookup_type(&self, type_name: &str) -> PgTypeMetadata {
23        use self::pg_type::dsl::*;
24
25        pg_type
26            .select((oid, typarray))
27            .filter(typname.eq(type_name))
28            .first(&self.conn)
29            .unwrap_or_default()
30    }
31}
32
33table! {
34    pg_type (oid) {
35        oid -> Oid,
36        typname -> Text,
37        typarray -> Oid,
38    }
39}