zippa-db 0.1.1

A fast, lightweight, cross-platform database client for PostgreSQL, MySQL, and SQLite.
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Reaching into a session from a test.
//!
//! These stand in for the parts of a session a test cannot drive through the
//! window — a live server, a file dialog — and read back what the user would
//! see. Kept beside the session rather than in it so the view itself reads as
//! what the app does.

use gpui_kit::component::dock::DockArea;
use gpui_kit::{App, Context, Entity, SharedString, Window};

use crate::db::DatabaseObject;
use crate::ui::console::ConsoleView;
use crate::ui::data_grid::DataGrid;
use crate::ui::import_dialog::ImportView;
use crate::ui::plan_view::PlanView;
use crate::ui::process_list::ProcessListView;
use crate::ui::query_digest::QueryDigestView;
use crate::ui::query_editor::QueryEditor;
use crate::ui::schema_view::SchemaView;
use crate::ui::server_variables::ServerVariablesView;
use crate::ui::sqlite_maintenance::SqliteMaintenanceView;
use crate::ui::table_view::TableView;

use super::Session;
use super::panel::{CloseScope, SessionPanel, SessionPanelEvent};
use super::sidebar::compile_filter;
use super::tab::Status;

impl Session {
    /// Feed the grid a result without going through a live query.
    #[cfg(test)]
    pub(crate) fn show_result_for_test(
        &mut self,
        result: crate::db::query::QueryResult,
        cx: &mut Context<Self>,
    ) {
        let Some(panel) = self.active_panel_for_test() else {
            return;
        };
        panel.update(cx, |panel, cx| {
            panel.set_status(Status::Done(result.summary()));
            if let Some(grid) = panel.grid() {
                grid.update(cx, |grid, cx| grid.set_result(result, cx));
            }
            cx.notify();
        });
    }

    #[cfg(test)]
    pub(crate) fn tab_titles(&self, cx: &App) -> Vec<String> {
        self.panels
            .iter()
            .map(|panel| panel.read(cx).title().to_string())
            .collect()
    }

    /// The kind icon of every tab, in creation order. A data tab and the
    /// structure tab of the same table share a title, so this is what tells
    /// them apart on screen.
    #[cfg(test)]
    pub(crate) fn tab_icons(&self, cx: &App) -> Vec<gpui_kit::assets::IconName> {
        self.panels
            .iter()
            .map(|panel| panel.read(cx).icon())
            .collect()
    }

    /// Whether the tab at `index` has unsaved changes.
    #[cfg(test)]
    pub(crate) fn tab_is_dirty_for_test(&self, index: usize, cx: &App) -> bool {
        self.panels[index].read(cx).is_dirty(cx)
    }

    #[cfg(test)]
    pub(crate) fn active_sql(&self, cx: &App) -> String {
        let Some(panel) = self.active_panel_for_test() else {
            return String::new();
        };
        let panel = panel.read(cx);
        if let Some(editor) = panel.editor() {
            return editor.read(cx).sql(cx);
        }
        if let Some(view) = panel.table_view() {
            return view.read(cx).query(cx);
        }
        String::new()
    }

    /// The table view in the active tab, if this is a table tab.
    #[cfg(test)]
    pub(crate) fn active_table_view(&self, cx: &App) -> Option<Entity<TableView>> {
        self.active_panel_for_test()?.read(cx).table_view()
    }

    /// The schema view in the active tab, if this is a structure tab.
    #[cfg(test)]
    pub(crate) fn active_schema_view(&self, cx: &App) -> Option<Entity<SchemaView>> {
        self.active_panel_for_test()?.read(cx).schema_view()
    }

    /// The console view in the active tab, if this is the console tab.
    #[cfg(test)]
    pub(crate) fn active_console_view(&self, cx: &App) -> Option<Entity<ConsoleView>> {
        self.active_panel_for_test()?.read(cx).console_view()
    }

    /// Open the console tab, or bring it forward, the way the sidebar's
    /// button does.
    #[cfg(test)]
    pub(crate) fn open_console_for_test(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        self.open_console(window, cx);
    }

    /// The process list view in the active tab, if this is the process list
    /// tab.
    #[cfg(test)]
    pub(crate) fn active_process_list_view(&self, cx: &App) -> Option<Entity<ProcessListView>> {
        self.active_panel_for_test()?.read(cx).process_list_view()
    }

    /// Open the process list tab, or bring it forward, the way the sidebar's
    /// button does.
    #[cfg(test)]
    pub(crate) fn open_process_list_for_test(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.open_process_list(window, cx);
    }

    /// The server variables view in the active tab, if this is the server
    /// variables tab.
    #[cfg(test)]
    pub(crate) fn active_server_variables_view(
        &self,
        cx: &App,
    ) -> Option<Entity<ServerVariablesView>> {
        self.active_panel_for_test()?
            .read(cx)
            .server_variables_view()
    }

    /// Open the server variables tab, or bring it forward, the way the
    /// sidebar's button does.
    #[cfg(test)]
    pub(crate) fn open_server_variables_for_test(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.open_server_variables(window, cx);
    }

    /// The query digest view in the active tab, if this is the query digest
    /// tab.
    #[cfg(test)]
    pub(crate) fn active_query_digest_view(&self, cx: &App) -> Option<Entity<QueryDigestView>> {
        self.active_panel_for_test()?.read(cx).query_digest_view()
    }

    /// Open the query digest tab, or bring it forward, the way the sidebar's
    /// button does.
    #[cfg(test)]
    pub(crate) fn open_query_digest_for_test(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.open_query_digest(window, cx);
    }

    /// The maintenance view in the active tab, if this is the maintenance
    /// tab.
    #[cfg(test)]
    pub(crate) fn active_maintenance_view(
        &self,
        cx: &App,
    ) -> Option<Entity<SqliteMaintenanceView>> {
        self.active_panel_for_test()?.read(cx).maintenance_view()
    }

    /// Open the maintenance tab, or bring it forward, the way the sidebar's
    /// button does.
    #[cfg(test)]
    pub(crate) fn open_maintenance_for_test(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.open_maintenance(window, cx);
    }

    /// Open `object`'s structure tab, the way the sidebar's row menu does.
    #[cfg(test)]
    pub(crate) fn open_schema_for_test(
        &mut self,
        object: &DatabaseObject,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.open_object(object, super::tab::ObjectViewMode::Schema, window, cx);
    }

    #[cfg(test)]
    pub(crate) fn open_tab_for_test(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        self.open_tab(None, String::new(), false, window, cx);
    }

    #[cfg(test)]
    pub(crate) fn close_tab_for_test(
        &mut self,
        index: usize,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let panel = self.panel_for_test(index);
        self.close_tab(&panel, window, cx);
    }

    /// Drive one of a tab's menu commands the way its own item does — emit the
    /// event the item emits.
    ///
    /// The menu itself cannot be opened in a test: a `PopupMenu` keeps itself
    /// alive through the subscription its context menu registers, so a test
    /// that opens one ends in the harness's leaked-entity panic (see
    /// `ui/tests/mod.rs`).
    #[cfg(test)]
    pub(crate) fn close_scope_for_test(
        &mut self,
        index: usize,
        scope: CloseScope,
        cx: &mut Context<Self>,
    ) {
        let panel = self.panels[index].clone();
        panel.update(cx, |_, cx| {
            cx.emit(SessionPanelEvent::CloseScopeRequested(scope))
        });
    }

    #[cfg(test)]
    pub(crate) fn activate_tab_for_test(
        &mut self,
        index: usize,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.activate_tab(index, window, cx);
    }

    /// Put the session in the sidebar's Management tab, the way clicking its
    /// top tab does.
    #[cfg(test)]
    pub(crate) fn show_sidebar_tab_for_test(&mut self, management: bool, cx: &mut Context<Self>) {
        self.sidebar_tab = if management {
            super::sidebar::SidebarTab::Management
        } else {
            super::sidebar::SidebarTab::Schema
        };
        cx.notify();
    }

    /// Set the filter text the way typing in the box does.
    #[cfg(test)]
    pub(crate) fn set_filter_for_test(
        &mut self,
        pattern: &str,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let filter = self.filter.clone();
        filter.update(cx, |state, cx| {
            state.set_value(pattern.to_string(), window, cx)
        });
        self.matcher = compile_filter(pattern.trim());
        cx.notify();
    }

    #[cfg(test)]
    pub(crate) fn visible_object_labels(&self) -> Vec<String> {
        self.visible_objects()
            .into_iter()
            .map(|object| object.label())
            .collect()
    }

    /// Type into the active tab's editor and put focus there.
    #[cfg(test)]
    pub(crate) fn prepare_active_editor_for_test(
        &mut self,
        sql: &str,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(editor) = self
            .active_panel_for_test()
            .and_then(|panel| panel.read(cx).editor())
        else {
            return;
        };
        editor.update(cx, |editor, cx| {
            editor.set_sql(sql, window, cx);
            editor.focus(window, cx);
        });
    }

    /// The editor of the active tab, for a test to drive.
    #[cfg(test)]
    pub(crate) fn active_editor_for_test(&self, cx: &App) -> Option<Entity<QueryEditor>> {
        self.active_panel_for_test()?.read(cx).editor()
    }

    /// How many results the last run left, and which one is showing.
    #[cfg(test)]
    pub(crate) fn results_for_test(&self, cx: &App) -> (usize, usize) {
        self.active_panel_for_test()
            .map(|panel| panel.read(cx).results())
            .unwrap_or((0, 0))
    }

    /// Put the active tab into its running state, the way a query in flight
    /// does. Under test a query finishes before anything can be cancelled, so
    /// this is what the cancel path is driven with.
    #[cfg(test)]
    pub(crate) fn mark_running_for_test(&mut self, cx: &mut Context<Self>) {
        let Some(panel) = self.active_panel_for_test() else {
            return;
        };
        let Some(editor) = panel.read(cx).editor() else {
            return;
        };
        panel.update(cx, |panel, cx| {
            panel.set_status(Status::Running);
            cx.notify();
        });
        editor.update(cx, |editor, cx| editor.set_running(true, cx));
    }

    /// The plan viewer in the active tab, if one has been built.
    #[cfg(test)]
    pub(crate) fn active_plan_view_for_test(&self, cx: &App) -> Option<Entity<PlanView>> {
        self.active_panel_for_test()?.read(cx).plan_view()
    }

    /// Whether the active tab is showing its plan rather than its grid.
    #[cfg(test)]
    pub(crate) fn active_plan_showing_for_test(&self, cx: &App) -> bool {
        self.active_panel_for_test()
            .is_some_and(|panel| panel.read(cx).showing_plan())
    }

    /// Reach the active tab's grid from a test.
    #[cfg(test)]
    pub(crate) fn active_grid(&self, cx: &App) -> Option<Entity<DataGrid>> {
        self.active_panel_for_test()?.read(cx).grid()
    }

    /// Whether the active tab has a query in flight.
    #[cfg(test)]
    pub(crate) fn active_is_running(&self, cx: &App) -> bool {
        self.active_panel_for_test()
            .is_some_and(|panel| panel.read(cx).is_running())
    }

    /// What the status bar says for the active tab, as plain text.
    #[cfg(test)]
    pub(crate) fn active_status_for_test(&self, cx: &App) -> String {
        self.active_panel_for_test()
            .map(|panel| panel.read(cx).status_message())
            .unwrap_or_default()
    }

    /// Fill the sidebar without waiting on the server.
    #[cfg(test)]
    pub(crate) fn set_metadata_for_test(
        &mut self,
        databases: Vec<String>,
        objects: Vec<DatabaseObject>,
        cx: &mut Context<Self>,
    ) {
        self.databases = databases;
        self.objects = objects;
        self.metadata_error = None;
        self.rebuild_tree(cx);
        cx.notify();
    }

    /// The sidebar tree's highlighted object, if any.
    #[cfg(test)]
    pub(crate) fn selected_object_label_for_test(&self, cx: &App) -> Option<String> {
        self.objects_tree
            .read(cx)
            .selected_item()
            .map(|item| item.id.to_string())
    }

    /// The panel the user was last focused in, for a test to drive.
    #[cfg(test)]
    pub(crate) fn active_panel_for_test(&self) -> Option<Entity<SessionPanel>> {
        self.active_panel()
    }

    /// Whether the active tab has a transaction open on its connection.
    #[cfg(test)]
    pub(crate) fn active_transaction_for_test(&self, cx: &App) -> crate::db::TxnState {
        self.active_panel()
            .map(|panel| panel.read(cx).transaction())
            .unwrap_or_default()
    }

    /// The panel at `index` in creation order, for a test to drive.
    #[cfg(test)]
    pub(crate) fn panel_for_test(&self, index: usize) -> Entity<SessionPanel> {
        self.panels[index].clone()
    }

    /// The import dialog, if one is open in this session.
    #[cfg(test)]
    pub(crate) fn import_view_for_test(&self) -> Option<Entity<ImportView>> {
        self.import.clone()
    }

    /// Open the import dialog for `path` without going through the file picker.
    #[cfg(test)]
    pub(crate) fn open_import_for_test(
        &mut self,
        path: std::path::PathBuf,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.open_import_dialog(path, window, cx);
    }

    /// The id of the close button on the tab at `index`, which is keyed by
    /// the panel's stable `key` rather than its position.
    #[cfg(test)]
    pub(crate) fn close_button_id_for_test(&self, index: usize, cx: &App) -> SharedString {
        SharedString::from(format!("close-tab-{}", self.panels[index].read(cx).key()))
    }

    /// Exercise the `…`-menu close path, which never reaches
    /// [`Session::close_tab`] — it removes the panel from the dock directly.
    #[cfg(test)]
    pub(crate) fn dock_remove_for_test(
        &mut self,
        panel: &Entity<SessionPanel>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let dock = self.dock_for_test();
        dock.update(cx, |dock, cx| dock.remove_panel(panel.clone(), window, cx));
    }

    #[cfg(test)]
    fn dock_for_test(&self) -> Entity<DockArea> {
        self.dock.clone()
    }
}