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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use crate::{Database, Schema, Table};

#[derive(Debug, Clone)]
pub struct TreeItemInfo {
    indent: u8,
    visible: bool,
}

impl TreeItemInfo {
    pub const fn new(indent: u8, visible: bool) -> Self {
        Self { indent, visible }
    }

    pub const fn is_visible(&self) -> bool {
        self.visible
    }

    pub const fn indent(&self) -> u8 {
        self.indent
    }

    pub fn unindent(&mut self) {
        self.indent = self.indent.saturating_sub(1);
    }

    pub fn set_visible(&mut self, visible: bool) {
        self.visible = visible;
    }
}

/// `DatabaseTreeItem` can be of two kinds
#[derive(PartialEq, Debug, Clone)]
pub enum DatabaseTreeItemKind {
    Database {
        name: String,
        collapsed: bool,
    },
    Table {
        database: Database,
        table: Table,
    },
    Schema {
        database: Database,
        schema: Schema,
        collapsed: bool,
    },
}

impl DatabaseTreeItemKind {
    pub const fn is_database(&self) -> bool {
        matches!(self, Self::Database { .. })
    }

    pub const fn is_table(&self) -> bool {
        matches!(self, Self::Table { .. })
    }

    pub const fn is_schema(&self) -> bool {
        matches!(self, Self::Schema { .. })
    }

    pub const fn is_database_collapsed(&self) -> bool {
        match self {
            Self::Database { collapsed, .. } => *collapsed,
            Self::Table { .. } => false,
            Self::Schema { .. } => false,
        }
    }

    pub const fn is_schema_collapsed(&self) -> bool {
        match self {
            Self::Database { .. } => false,
            Self::Table { .. } => false,
            Self::Schema { collapsed, .. } => *collapsed,
        }
    }

    pub fn name(&self) -> String {
        match self {
            Self::Database { name, .. } => name.to_string(),
            Self::Table { table, .. } => table.name.clone(),
            Self::Schema { schema, .. } => schema.name.clone(),
        }
    }

    pub fn database_name(&self) -> Option<String> {
        match self {
            Self::Database { .. } => None,
            Self::Table { database, .. } => Some(database.name.clone()),
            Self::Schema { database, .. } => Some(database.name.clone()),
        }
    }

    pub fn schema_name(&self) -> Option<String> {
        match self {
            Self::Database { .. } => None,
            Self::Table { table, .. } => table.schema.clone(),
            Self::Schema { .. } => None,
        }
    }
}

/// `DatabaseTreeItem` can be of two kinds: see `DatabaseTreeItem` but shares an info
#[derive(Debug, Clone)]
pub struct DatabaseTreeItem {
    info: TreeItemInfo,
    kind: DatabaseTreeItemKind,
}

impl DatabaseTreeItem {
    pub fn new_table(database: &Database, table: &Table) -> Self {
        Self {
            info: TreeItemInfo::new(if table.schema.is_some() { 2 } else { 1 }, false),
            kind: DatabaseTreeItemKind::Table {
                database: database.clone(),
                table: table.clone(),
            },
        }
    }

    pub fn new_schema(database: &Database, schema: &Schema, _collapsed: bool) -> Self {
        Self {
            info: TreeItemInfo::new(1, false),
            kind: DatabaseTreeItemKind::Schema {
                database: database.clone(),
                schema: schema.clone(),
                collapsed: true,
            },
        }
    }

    pub fn new_database(database: &Database, _collapsed: bool) -> Self {
        Self {
            info: TreeItemInfo::new(0, true),
            kind: DatabaseTreeItemKind::Database {
                name: database.name.to_string(),
                collapsed: true,
            },
        }
    }

    pub fn set_collapsed(&mut self, collapsed: bool) {
        if let DatabaseTreeItemKind::Database { name, .. } = self.kind() {
            self.kind = DatabaseTreeItemKind::Database {
                name: name.to_string(),
                collapsed,
            }
        }
    }

    pub const fn info(&self) -> &TreeItemInfo {
        &self.info
    }

    pub fn info_mut(&mut self) -> &mut TreeItemInfo {
        &mut self.info
    }

    pub const fn kind(&self) -> &DatabaseTreeItemKind {
        &self.kind
    }

    pub fn collapse_database(&mut self) {
        if let DatabaseTreeItemKind::Database { name, .. } = &self.kind {
            self.kind = DatabaseTreeItemKind::Database {
                name: name.to_string(),
                collapsed: true,
            }
        }
    }

    pub fn expand_database(&mut self) {
        if let DatabaseTreeItemKind::Database { name, .. } = &self.kind {
            self.kind = DatabaseTreeItemKind::Database {
                name: name.to_string(),
                collapsed: false,
            };
        }
    }

    pub fn collapse_schema(&mut self) {
        if let DatabaseTreeItemKind::Schema {
            schema, database, ..
        } = &self.kind
        {
            self.kind = DatabaseTreeItemKind::Schema {
                database: database.clone(),
                schema: schema.clone(),
                collapsed: true,
            }
        }
    }

    pub fn expand_schema(&mut self) {
        if let DatabaseTreeItemKind::Schema {
            schema, database, ..
        } = &self.kind
        {
            self.kind = DatabaseTreeItemKind::Schema {
                database: database.clone(),
                schema: schema.clone(),
                collapsed: false,
            };
        }
    }

    pub fn show(&mut self) {
        self.info.visible = true;
    }

    pub fn hide(&mut self) {
        self.info.visible = false;
    }

    pub fn is_match(&self, filter_text: &str) -> bool {
        match self.kind.clone() {
            DatabaseTreeItemKind::Database { name, .. } => name.contains(filter_text),
            DatabaseTreeItemKind::Table { table, .. } => table.name.contains(filter_text),
            DatabaseTreeItemKind::Schema { schema, .. } => schema.name.contains(filter_text),
        }
    }

    pub fn is_database(&self) -> bool {
        self.kind.is_database()
    }
}

impl Eq for DatabaseTreeItem {}

impl PartialEq for DatabaseTreeItem {
    fn eq(&self, other: &Self) -> bool {
        if self.kind.is_database() && other.kind().is_database() {
            return self.kind.name().eq(&other.kind.name());
        }
        if !self.kind.is_database() && !other.kind.is_database() {
            return self.kind.name().eq(&other.kind.name());
        }
        false
    }
}

impl PartialOrd for DatabaseTreeItem {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.kind.name().partial_cmp(&other.kind.name())
    }
}

impl Ord for DatabaseTreeItem {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.kind.name().cmp(&other.kind.name())
    }
}