Skip to main content

dbkit_core/
schema.rs

1use std::marker::PhantomData;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub struct Table {
5    pub name: &'static str,
6    pub schema: Option<&'static str>,
7    pub alias: Option<&'static str>,
8}
9
10impl Table {
11    pub const fn new(name: &'static str) -> Self {
12        Self {
13            name,
14            schema: None,
15            alias: None,
16        }
17    }
18
19    pub const fn with_schema(mut self, schema: &'static str) -> Self {
20        self.schema = Some(schema);
21        self
22    }
23
24    pub const fn with_alias(mut self, alias: &'static str) -> Self {
25        self.alias = Some(alias);
26        self
27    }
28
29    pub fn qualifier(&self) -> &'static str {
30        if let Some(alias) = self.alias {
31            alias
32        } else {
33            self.name
34        }
35    }
36
37    pub fn qualified_name(&self) -> String {
38        match self.schema {
39            Some(schema) => format!("{}.{}", schema, self.name),
40            None => self.name.to_string(),
41        }
42    }
43}
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub struct ColumnRef {
47    pub table: Table,
48    pub name: &'static str,
49}
50
51impl ColumnRef {
52    pub const fn new(table: Table, name: &'static str) -> Self {
53        Self { table, name }
54    }
55
56    pub fn qualified_name(&self) -> String {
57        format!("{}.{}", self.table.qualifier(), self.name)
58    }
59}
60
61#[derive(Debug, Clone, Copy)]
62pub struct Column<M, T> {
63    pub table: Table,
64    pub name: &'static str,
65    _marker: PhantomData<(M, T)>,
66}
67
68impl<M, T> Column<M, T> {
69    pub const fn new(table: Table, name: &'static str) -> Self {
70        Self {
71            table,
72            name,
73            _marker: PhantomData,
74        }
75    }
76
77    pub const fn as_ref(&self) -> ColumnRef {
78        ColumnRef::new(self.table, self.name)
79    }
80}