mz_sql/session/
metadata.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use std::fmt::Debug;
11use std::net::IpAddr;
12
13use mz_adapter_types::connection::ConnectionId;
14use mz_repr::role_id::RoleId;
15
16use crate::plan::PlanContext;
17use crate::session::user::{RoleMetadata, User};
18use crate::session::vars::SessionVars;
19
20pub trait SessionMetadata: Debug + Sync {
21    /// Returns the session vars for this session.
22    fn vars(&self) -> &SessionVars;
23    /// Returns the connection ID associated with the session.
24    fn conn_id(&self) -> &ConnectionId;
25    /// Returns the client address associated with the session.
26    fn client_ip(&self) -> Option<&IpAddr>;
27    /// Returns the current transaction's PlanContext. Panics if there is not a
28    /// current transaction.
29    fn pcx(&self) -> &PlanContext;
30    /// Returns the role metadata for this session.
31    fn role_metadata(&self) -> &RoleMetadata;
32
33    /// Returns the session's current role ID.
34    ///
35    /// # Panics
36    /// If the session has not connected successfully.
37    fn current_role_id(&self) -> &RoleId {
38        &self.role_metadata().current_role
39    }
40
41    /// Returns the session's session role ID.
42    ///
43    /// # Panics
44    /// If the session has not connected successfully.
45    fn session_role_id(&self) -> &RoleId {
46        &self.role_metadata().session_role
47    }
48
49    fn user(&self) -> &User {
50        self.vars().user()
51    }
52
53    fn database(&self) -> &str {
54        self.vars().database()
55    }
56
57    fn search_path(&self) -> &[mz_sql_parser::ast::Ident] {
58        self.vars().search_path()
59    }
60
61    fn is_superuser(&self) -> bool {
62        self.vars().is_superuser()
63    }
64
65    fn enable_session_rbac_checks(&self) -> bool {
66        self.vars().enable_session_rbac_checks()
67    }
68}