-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathstatement_identifier.rs
More file actions
99 lines (88 loc) · 2.82 KB
/
statement_identifier.rs
File metadata and controls
99 lines (88 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct RootId {
inner: usize,
}
#[cfg(test)]
impl From<RootId> for usize {
fn from(val: RootId) -> Self {
val.inner
}
}
#[cfg(test)]
impl From<usize> for RootId {
fn from(inner: usize) -> Self {
RootId { inner }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
/// `StatementId` can represent IDs for nested statements.
///
/// For example, an SQL function really consist of two statements; the function creation
/// and the body:
///
/// ```sql
/// create or replace function get_product_name(product_id INT) -- the root statement
/// returns varchar as $$
/// select * from … -- the child statement
/// $$ LANGUAGE plpgsql;
/// ```
///
/// For now, we only support SQL functions – no complex, nested statements.
///
/// An SQL function only ever has ONE child, that's why the inner `RootId` of a `Root`
/// is the same as the one of its `Child`.
pub enum StatementId {
Root(RootId),
// StatementId is the same as the root id since we can only have a single sql function body per Root
Child(RootId),
}
impl Default for StatementId {
fn default() -> Self {
StatementId::Root(RootId { inner: 0 })
}
}
impl StatementId {
pub fn raw(&self) -> usize {
match self {
StatementId::Root(s) => s.inner,
StatementId::Child(s) => s.inner,
}
}
}
/// Helper struct to generate unique statement ids
pub struct StatementIdGenerator {
next_id: usize,
}
impl StatementIdGenerator {
pub fn new() -> Self {
Self { next_id: 0 }
}
pub fn next(&mut self) -> StatementId {
let id = self.next_id;
self.next_id += 1;
StatementId::Root(RootId { inner: id })
}
}
impl StatementId {
/// Use this to get the matching `StatementId::Child` for
/// a `StatementId::Root`.
/// If the `StatementId` was already a `Child`, this will return `None`.
/// It is not guaranteed that the `Root` actually has a `Child` statement in the workspace.
pub fn get_child_id(&self) -> Option<StatementId> {
match self {
StatementId::Root(id) => Some(StatementId::Child(RootId { inner: id.inner })),
StatementId::Child(_) => None,
}
}
/// Use this if you need to create a matching `StatementId::Child` for `Root`.
/// You cannot create a `Child` of a `Child`.
pub fn create_child(&self) -> StatementId {
match self {
StatementId::Root(id) => StatementId::Child(RootId { inner: id.inner }),
StatementId::Child(_) => panic!("Cannot create child from a child statement id"),
}
}
}