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
//! The SQL this view generates.
//!
//! Split out from the view itself so the statements that read a page, filter
//! it, and write a row back are in one place: they share the table's quoting,
//! its column types, and the row key it addresses rows by. Anything else that
//! needs a statement about the open table — exporting it, describing it —
//! belongs here too rather than in the rendering.
//!
//! Values are never pasted in. Every value is bound as a parameter through
//! [`typed_placeholder`], with two deliberate exceptions: `IN` / `NOT IN` take
//! SQL the user wrote so a subquery can do the filtering, and a staged cell
//! that reads as one of [`keyword_literal`]'s keywords — `NOW()` and its kin —
//! is pasted in as that keyword rather than bound as text, the way a cell
//! already accepts `NULL`.

use gpui_kit::component::table::ColumnSort;

use crate::db::query::{Cell, QueryResult};
use crate::db::{
    Engine, RowKey, keyword_literal, placeholder, quote_identifier, text_type, typed_placeholder,
};
use crate::ui::data_grid::StagedRow;
use crate::ui::filter_bar::{FilterSpec, Operator};

use super::TableView;

impl TableView {
    /// The table this view reads and writes, qualified and quoted.
    fn target(&self) -> String {
        let engine = self.connection.config.engine;
        match &self.object.schema {
            Some(schema) => format!(
                "{}.{}",
                quote_identifier(schema, engine),
                quote_identifier(&self.object.name, engine)
            ),
            None => quote_identifier(&self.object.name, engine),
        }
    }

    /// The statement this view runs for its current page and sort.
    #[cfg(test)]
    pub fn query(&self, cx: &gpui_kit::App) -> String {
        self.query_with_params(cx).0
    }

    /// The statement and the values bound to it.
    ///
    /// A filter's value is a parameter rather than text pasted into the
    /// statement — except for `IN` and `NOT IN`, whose value is SQL the user
    /// wrote and is meant to run as written.
    pub fn query_with_params(&self, cx: &gpui_kit::App) -> (String, Vec<Cell>) {
        self.select_sql(true, cx)
    }

    /// The `select` for this view, paged for the grid or whole for an export.
    ///
    /// A whole-table export still honours the filters and the sort; only the
    /// `limit` / `offset` are left off, since every row is what an export is
    /// for.
    pub(super) fn select_sql(&self, paged: bool, cx: &gpui_kit::App) -> (String, Vec<Cell>) {
        let engine = self.connection.config.engine;
        let target = self.target();

        // A table with no primary key is addressed by the engine's own row
        // identifier, which `select *` leaves out, so it is asked for by name
        // and taken back out of the result before the grid sees it.
        let mut sql = match &self.row_key {
            Some(RowKey::RowId(id)) => format!("select {id}, * from {target}"),
            _ => format!("select * from {target}"),
        };

        let (conditions, params) = self.where_clause(cx);
        if !conditions.is_empty() {
            sql.push_str(&format!(" where {}", conditions.join(" and ")));
        }

        if let Some((column, sort)) = &self.sort {
            let direction = match sort {
                ColumnSort::Descending => "desc",
                _ => "asc",
            };
            sql.push_str(&format!(
                " order by {} {direction}",
                quote_identifier(column, engine)
            ));
        }

        if paged {
            sql.push_str(&format!(
                " limit {} offset {}",
                self.limit,
                self.page * self.limit
            ));
        }
        (sql, params)
    }

    /// The filter bar's lines as SQL conditions, and the values to bind.
    fn where_clause(&self, cx: &gpui_kit::App) -> (Vec<String>, Vec<Cell>) {
        let engine = self.connection.config.engine;
        let mut params: Vec<Cell> = Vec::new();
        let mut conditions = Vec::new();

        for spec in self.filters.read(cx).specs(cx) {
            let Some(condition) = self.condition(&spec, engine, &mut params) else {
                continue;
            };
            conditions.push(condition);
        }

        (conditions, params)
    }

    /// One filter as a condition, pushing whatever it binds onto `params`.
    fn condition(
        &self,
        spec: &FilterSpec,
        engine: Engine,
        params: &mut Vec<Cell>,
    ) -> Option<String> {
        let column = quote_identifier(&spec.column, engine);
        let type_name = self
            .columns
            .iter()
            .position(|name| *name == spec.column)
            .and_then(|index| self.column_types.get(index).cloned())
            .unwrap_or_default();

        Some(match spec.operator {
            Operator::IsNull => format!("{column} is null"),
            Operator::IsNotNull => format!("{column} is not null"),
            // The value is a list or a subquery the user wrote, so it goes in
            // as written; nothing else here does.
            Operator::In => format!("{column} in ({})", spec.value),
            Operator::NotIn => format!("{column} not in ({})", spec.value),
            // A pattern is text whatever the column holds, so the column is
            // the thing that gets cast here.
            Operator::Like | Operator::NotLike => {
                params.push(Some(spec.value.clone()));
                let keyword = if spec.operator == Operator::Like {
                    "like"
                } else {
                    "not like"
                };
                format!(
                    "cast({column} as {}) {keyword} {}",
                    text_type(engine),
                    placeholder(engine, params.len())
                )
            }
            operator => {
                params.push(Some(spec.value.clone()));
                format!(
                    "{column} {} {}",
                    operator.label(),
                    typed_placeholder(engine, params.len(), &type_name)
                )
            }
        })
    }

    /// Take the row identifier column back out of a freshly loaded page.
    ///
    /// It was asked for so rows could be addressed, not so it could be shown,
    /// so its values are kept here and the grid is handed the table's own
    /// columns.
    pub(super) fn take_key_column(&mut self, result: &mut QueryResult) {
        self.key_values.clear();
        self.key_type = None;

        if !matches!(self.row_key, Some(RowKey::RowId(_))) {
            return;
        }

        let (values, key_type) = take_row_id(result);
        self.key_values = values;
        self.key_type = key_type;
    }

    /// How a row is addressed in a write: the left-hand side, the type to cast
    /// the parameter to, and the value itself.
    fn key_for(&self, row: usize, cx: &gpui_kit::App) -> Option<Vec<(String, String, Cell)>> {
        let engine = self.connection.config.engine;
        match self.row_key.as_ref()? {
            RowKey::Columns(columns) => {
                let loaded = self.grid.read(cx).baseline_row(row, cx)?;
                columns
                    .iter()
                    .map(|column| {
                        let index = self.columns.iter().position(|name| name == column)?;
                        let value = loaded.get(index)?.clone();
                        // A NULL key would never match with `=`, and a key
                        // column should not be NULL in the first place.
                        value.as_ref()?;
                        Some((
                            quote_identifier(column, engine),
                            self.column_types.get(index).cloned().unwrap_or_default(),
                            value,
                        ))
                    })
                    .collect()
            }
            RowKey::RowId(id) => {
                let value = self.key_values.get(row)?.clone();
                value.as_ref()?;
                Some(vec![(
                    id.to_string(),
                    self.key_type.clone().unwrap_or_default(),
                    value,
                )])
            }
            RowKey::Unavailable(_) => None,
        }
    }

    /// The `UPDATE` for one row's staged cells, and the values to bind to it.
    pub(super) fn update_statement(
        &self,
        staged: &StagedRow,
        cx: &gpui_kit::App,
    ) -> Option<(String, Vec<Cell>)> {
        if staged.cells.is_empty() {
            return None;
        }
        let engine = self.connection.config.engine;
        let key = self.key_for(staged.row, cx)?;

        let mut params: Vec<Cell> = Vec::with_capacity(staged.cells.len() + key.len());

        let assignments: Vec<String> = staged
            .cells
            .iter()
            .map(|(column, value)| {
                let name = self.columns.get(*column).cloned().unwrap_or_default();
                // A recognized keyword is pasted as written: bound as a
                // parameter, a driver would quote it as text instead of
                // evaluating it.
                if let Some(keyword) = value.as_deref().and_then(keyword_literal) {
                    return format!("{} = {keyword}", quote_identifier(&name, engine));
                }
                params.push(value.clone());
                let type_name = self.column_types.get(*column).cloned().unwrap_or_default();
                format!(
                    "{} = {}",
                    quote_identifier(&name, engine),
                    typed_placeholder(engine, params.len(), &type_name)
                )
            })
            .collect();

        let conditions: Vec<String> = key
            .into_iter()
            .map(|(left, type_name, value)| {
                params.push(value);
                format!(
                    "{left} = {}",
                    typed_placeholder(engine, params.len(), &type_name)
                )
            })
            .collect();

        Some((
            format!(
                "update {} set {} where {}",
                self.target(),
                assignments.join(", "),
                conditions.join(" and ")
            ),
            params,
        ))
    }

    /// The `INSERT` for one row being built by hand, and its values.
    ///
    /// Columns nobody typed into are left out, so the server fills them in
    /// itself: leaving them out is the only way to get a default or a
    /// generated key.
    pub(super) fn insert_statement(&self, cells: &[(usize, Cell)]) -> Option<(String, Vec<Cell>)> {
        if cells.is_empty() {
            return None;
        }

        let engine = self.connection.config.engine;
        let mut params: Vec<Cell> = Vec::with_capacity(cells.len());
        let mut columns = Vec::with_capacity(cells.len());
        let mut values = Vec::with_capacity(cells.len());

        for (column, value) in cells {
            let name = self.columns.get(*column).cloned().unwrap_or_default();
            if name.is_empty() {
                return None;
            }
            columns.push(quote_identifier(&name, engine));

            // A recognized keyword is pasted as written: bound as a
            // parameter, a driver would quote it as text instead of
            // evaluating it.
            if let Some(keyword) = value.as_deref().and_then(keyword_literal) {
                values.push(keyword.to_string());
                continue;
            }
            let type_name = self.column_types.get(*column).cloned().unwrap_or_default();
            params.push(value.clone());
            values.push(typed_placeholder(engine, params.len(), &type_name));
        }

        Some((
            format!(
                "insert into {} ({}) values ({})",
                self.target(),
                columns.join(", "),
                values.join(", ")
            ),
            params,
        ))
    }

    /// The statement to re-read one binary cell's raw bytes, for the value
    /// dialog's preview — addressed by the row's own key rather than its
    /// position on the page, the same as a write.
    pub(super) fn binary_select(
        &self,
        row: usize,
        column: &str,
        cx: &gpui_kit::App,
    ) -> Option<(String, Vec<Cell>)> {
        let engine = self.connection.config.engine;
        let key = self.key_for(row, cx)?;

        let mut params: Vec<Cell> = Vec::with_capacity(key.len());
        let conditions: Vec<String> = key
            .into_iter()
            .enumerate()
            .map(|(index, (left, type_name, value))| {
                params.push(value);
                format!(
                    "{left} = {}",
                    typed_placeholder(engine, index + 1, &type_name)
                )
            })
            .collect();

        Some((
            format!(
                "select {} from {} where {}",
                quote_identifier(column, engine),
                self.target(),
                conditions.join(" and ")
            ),
            params,
        ))
    }

    /// The `DELETE` for one row marked for deletion, and its values.
    pub(super) fn delete_statement(
        &self,
        row: usize,
        cx: &gpui_kit::App,
    ) -> Option<(String, Vec<Cell>)> {
        let engine = self.connection.config.engine;
        let key = self.key_for(row, cx)?;

        let mut params: Vec<Cell> = Vec::with_capacity(key.len());
        let conditions: Vec<String> = key
            .into_iter()
            .enumerate()
            .map(|(index, (left, type_name, value))| {
                params.push(value);
                format!(
                    "{left} = {}",
                    typed_placeholder(engine, index + 1, &type_name)
                )
            })
            .collect();

        Some((
            format!(
                "delete from {} where {}",
                self.target(),
                conditions.join(" and ")
            ),
            params,
        ))
    }

    /// Whether a staged row changes a column the write addresses it by.
    pub(super) fn touches_key(&self, staged: &StagedRow) -> bool {
        let Some(RowKey::Columns(key)) = &self.row_key else {
            return false;
        };
        staged.cells.iter().any(|(column, _)| {
            self.columns
                .get(*column)
                .is_some_and(|name| key.contains(name))
        })
    }
}

/// Take the row identifier column out of a freshly loaded result, handing back
/// its values and its driver type.
///
/// It was asked for so rows could be addressed by a write, not so it could be
/// shown, so the grid is handed the table's own columns. An export drops it for
/// the same reason and ignores what comes back.
pub(super) fn take_row_id(result: &mut QueryResult) -> (Vec<Cell>, Option<String>) {
    let mut values = Vec::new();
    if result.columns.is_empty() {
        return (values, None);
    }

    result.columns.remove(0);
    let key_type = if result.column_types.is_empty() {
        None
    } else {
        Some(result.column_types.remove(0))
    };
    for row in &mut result.rows {
        if row.is_empty() {
            values.push(None);
            continue;
        }
        values.push(row.remove(0));
    }
    (values, key_type)
}