Skip to main content

safe_migrate/ast/
identifiers.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
4pub struct Ident {
5    pub text: String,
6    pub quoted: bool,
7}
8
9impl Ident {
10    pub fn new(text: impl Into<String>, quoted: bool) -> Self {
11        Self {
12            text: text.into(),
13            quoted,
14        }
15    }
16
17    /// Returns the lookup spelling used by the analyzer. Quoted identifiers
18    /// preserve their contents; unquoted identifiers are lowercased.
19    pub fn resolve(&self) -> String {
20        if self.quoted {
21            self.text.clone()
22        } else {
23            self.text.to_lowercase()
24        }
25    }
26}
27
28#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
29pub struct QualifiedName {
30    pub schema: Option<Ident>,
31    pub name: Ident,
32}
33
34impl QualifiedName {
35    pub fn new(schema: Option<Ident>, name: Ident) -> Self {
36        Self { schema, name }
37    }
38}
39
40/// ObjectId represents a fully resolved, state-machine tracked database object.
41/// Its schema and name must already use their resolved lookup spelling.
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct ObjectId {
44    pub schema: String,
45    pub name: String,
46    #[serde(default)]
47    pub inferred_schema: bool,
48}
49
50impl PartialEq for ObjectId {
51    fn eq(&self, other: &Self) -> bool {
52        self.schema == other.schema && self.name == other.name
53    }
54}
55
56impl Eq for ObjectId {}
57
58impl std::hash::Hash for ObjectId {
59    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
60        self.schema.hash(state);
61        self.name.hash(state);
62    }
63}
64
65impl ObjectId {
66    pub fn new(schema: impl Into<String>, name: impl Into<String>) -> Self {
67        Self {
68            schema: schema.into(),
69            name: name.into(),
70            inferred_schema: false,
71        }
72    }
73}
74
75impl std::fmt::Display for ObjectId {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        if self.inferred_schema {
78            write!(f, "{}.{} (inferred)", self.schema, self.name)
79        } else {
80            write!(f, "{}.{}", self.schema, self.name)
81        }
82    }
83}