pub struct TenantScope { /* private fields */ }Expand description
Helper for building tenant-scoped SQL WHERE clauses.
Mirrors gokit’s ScopeToTenant by associating a field name with a tenant
value. Adapter crates decide how to bind the returned value.
§Examples
use rskit_database::TenantScope;
use std::num::NonZeroUsize;
let scope = TenantScope::new("workspace_id", "ws-123").unwrap();
assert_eq!(
scope.where_clause(NonZeroUsize::new(1).unwrap()),
"workspace_id = $1",
);
assert_eq!(scope.value(), "ws-123");Implementations§
Source§impl TenantScope
impl TenantScope
Sourcepub fn new(
column: impl Into<String>,
value: impl Into<String>,
) -> AppResult<Self>
pub fn new( column: impl Into<String>, value: impl Into<String>, ) -> AppResult<Self>
Create a new TenantScope for the given column and value.
§Errors
Returns an error when the column is not a safe SQL identifier path or the tenant value is empty.
Sourcepub fn where_clause(&self, param_index: NonZeroUsize) -> String
pub fn where_clause(&self, param_index: NonZeroUsize) -> String
Return a WHERE clause fragment like "workspace_id = $1".
param_index is the positional parameter number for the bind
placeholder (1-based for PostgreSQL $N syntax).
Sourcepub fn apply(&self, query: &str, param_index: NonZeroUsize) -> String
pub fn apply(&self, query: &str, param_index: NonZeroUsize) -> String
Append a tenant-scoped WHERE clause to the given SQL query.
Returns the modified query with WHERE column = $N appended, where
N is param_index.
Sourcepub fn apply_and(&self, query: &str, param_index: NonZeroUsize) -> String
pub fn apply_and(&self, query: &str, param_index: NonZeroUsize) -> String
Append a tenant-scoped AND clause to the given SQL query.
Use this when the query already has a WHERE clause.
Trait Implementations§
Source§impl Clone for TenantScope
impl Clone for TenantScope
Source§fn clone(&self) -> TenantScope
fn clone(&self) -> TenantScope
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more