Skip to main content

sql_fun_sqlast/sem/schema_file_context/
sequence_info.rs

1use crate::{
2    TrieMap,
3    sem::{
4        AlterSequence, AnalysisError, AnalysisProblem, BaseParseContext, CreateSequence, FullName,
5        SequenceInfoRead, SequenceInfoWrite,
6    },
7};
8
9use super::SchemaFileContext;
10
11#[derive(Debug, Default)]
12pub struct SequenceInfoCollection(TrieMap<CreateSequence>);
13
14impl SequenceInfoCollection {
15    fn get(&self, key: &FullName) -> Option<&CreateSequence> {
16        self.0.get(&key.to_string())
17    }
18
19    fn insert(&mut self, key: &FullName, definition: &CreateSequence) {
20        self.0.insert(&key.to_string(), definition.clone());
21    }
22}
23
24impl<TBaseContext> SequenceInfoRead for SchemaFileContext<TBaseContext>
25where
26    TBaseContext: BaseParseContext + std::fmt::Debug,
27{
28    fn get_sequence_impl(&self, key: &FullName) -> Option<&CreateSequence> {
29        if let Some(s) = self.sequences.get(key) {
30            Some(s)
31        } else {
32            self.base.get_sequence_impl(key)
33        }
34    }
35}
36
37impl<TBaseContext> SequenceInfoWrite for SchemaFileContext<TBaseContext>
38where
39    TBaseContext: BaseParseContext + std::fmt::Debug,
40{
41    fn apply_alter_sequence(mut self, alter_seq: &AlterSequence) -> Result<Self, AnalysisError>
42    where
43        Self: Sized,
44    {
45        let seq_name = alter_seq.seq_name();
46        let Some(mut seq) = self.get_sequence(seq_name).cloned() else {
47            self.report_problem(AnalysisProblem::sequence_not_found(seq_name))?;
48            return Ok(self);
49        };
50
51        seq.apply_alter(alter_seq)?;
52        self.sequences.insert(seq_name, &seq);
53        Ok(self)
54    }
55
56    fn apply_create_sequence(
57        mut self,
58        create_sequence: &CreateSequence,
59    ) -> Result<Self, AnalysisError> {
60        let name = create_sequence.name();
61        self.sequences.insert(name, create_sequence);
62        Ok(self)
63    }
64}