Skip to main content

ignis_db/dsl/
alias.rs

1//! Alias system for building type-safe database queries
2
3use crate::schema::Table;
4
5/// Table alias for use in queries
6#[derive(Debug, Clone)]
7pub struct TableAlias {
8    pub table: Table,
9    pub alias: String,
10}
11
12impl TableAlias {
13    /// Create a new table alias
14    pub fn new(table: Table, alias: impl Into<String>) -> Self {
15        Self {
16            table,
17            alias: alias.into(),
18        }
19    }
20
21    /// Get the alias name
22    pub fn alias(&self) -> &str {
23        &self.alias
24    }
25
26    /// Get the underlying table
27    pub fn table(&self) -> &Table {
28        &self.table
29    }
30}
31
32/// Extension trait for tables to create aliases
33pub trait TableExt {
34    fn alias(self, alias: impl Into<String>) -> TableAlias;
35}
36
37impl TableExt for Table {
38    fn alias(self, alias: impl Into<String>) -> TableAlias {
39        TableAlias::new(self, alias)
40    }
41}