cufflink-cli 0.19.0

CLI for the Cufflink CRUD microservice platform — deploy, init, and manage services
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
use crate::config::CliConfig;
use crate::project_config::ProjectConfig;
use crate::workspace_config::WorkspaceConfig;
use comfy_table::{presets::NOTHING, Table, TableComponent};
use futures_util::StreamExt;
use serde::Deserialize;
use serde_json::Value;
use std::io::{IsTerminal, Write};

/// Maximum number of rows the TTY table preview will hold in memory before
/// showing a truncation hint. Overridden by `--all`.
const TTY_PREVIEW_ROWS: usize = 1000;

/// Run a read-only SQL query against a service's tenant database via the
/// platform `/api/services/{id}/query` endpoint.
#[allow(clippy::too_many_arguments)]
pub async fn run(
    query: &str,
    service: Option<&str>,
    tenant_override: Option<&str>,
    format: Option<&str>,
    all: bool,
    limit: Option<u64>,
    no_pager: bool,
    env: Option<&str>,
) -> eyre::Result<()> {
    let mut config = CliConfig::load_with_env(env)?;
    if let Some(t) = tenant_override {
        config.tenant_override = Some(t.to_string());
    }

    let service_name = resolve_service_name(service)?;
    let service_id = config.find_service_id(&service_name).await?;

    let effective_query = match limited(query, limit) {
        Limited::Wrapped(wrapped) => wrapped,
        Limited::AsTyped(why) => {
            if let Some(why) = why {
                eprintln!("--limit does not apply ({why}); the statement is sent as typed");
            }
            query.to_string()
        }
    };
    let client = config.http_client();
    let resp = config
        .auth_request(
            &client,
            reqwest::Method::POST,
            &format!("{}/api/services/{}/query", config.api_url, service_id),
        )
        .json(&serde_json::json!({ "sql": effective_query }))
        .send()
        .await?;

    if !resp.status().is_success() {
        let status = resp.status();
        let body = resp.text().await.unwrap_or_default();
        eyre::bail!("SQL request failed ({}): {}", status, body);
    }

    let format = format.unwrap_or("table");
    let tty = std::io::stdout().is_terminal();

    match (format, tty) {
        ("jsonl", _) => stream_flat(resp, RowFormat::Jsonl).await,
        ("csv", _) => stream_flat(resp, RowFormat::Csv).await,
        ("tsv", _) => stream_flat(resp, RowFormat::Tsv).await,
        ("table", true) => render_table(resp, all, no_pager).await,
        ("table", false) => stream_flat(resp, RowFormat::Jsonl).await,
        (other, _) => eyre::bail!(
            "Unknown --format '{}'. Use one of: table, jsonl, csv, tsv",
            other
        ),
    }
}

fn resolve_service_name(explicit: Option<&str>) -> eyre::Result<String> {
    if let Some(name) = explicit {
        return Ok(name.to_string());
    }
    let project = ProjectConfig::find_and_load()?.ok_or_else(|| {
        eyre::eyre!(
            "--service not given and no Cufflink.toml found in current directory or any parent."
        )
    })?;

    if let Some(name) = project.service.name.clone() {
        return Ok(name);
    }

    // Rust services leave [service].name empty (it's declared in the Rust
    // `service!` macro). Fall back to the workspace TOML: whichever service
    // path resolves to this project directory is the one we're in.
    if let Some(name) = lookup_name_from_workspace(&project.project_dir) {
        return Ok(name);
    }

    eyre::bail!(
        "Current service has no `name` in Cufflink.toml and cufflink-workspace.toml has no matching entry. Pass --service explicitly."
    )
}

fn lookup_name_from_workspace(project_dir: &std::path::Path) -> Option<String> {
    let (ws, root) = WorkspaceConfig::find_and_load().ok()?;
    match_service_by_path(&ws.services, &root, project_dir)
}

fn match_service_by_path(
    services: &[crate::workspace_config::WorkspaceService],
    root: &std::path::Path,
    project_dir: &std::path::Path,
) -> Option<String> {
    let project_canon = project_dir.canonicalize().ok()?;
    services
        .iter()
        .find(|s| {
            root.join(&s.path)
                .canonicalize()
                .is_ok_and(|p| p == project_canon)
        })
        .map(|s| s.name.clone())
}

/// A piece of a statement that is not a comment or a blank.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Kind {
    Word,
    /// A string, a quoted name or a dollar-quoted body.
    Quoted,
    Semicolon,
    Other,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Token {
    kind: Kind,
    start: usize,
    end: usize,
}

fn is_word(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || byte == b'_'
}

/// The end of the string that opens at `start`, where `''` is a quote and,
/// in an `E'…'` string, `\\` escapes what follows it.
fn literal_end(bytes: &[u8], start: usize, escapes: bool) -> usize {
    let mut i = start + 1;
    while i < bytes.len() {
        match (bytes[i], bytes.get(i + 1)) {
            (b'\\', Some(_)) if escapes => i += 2,
            (b'\'', Some(b'\'')) => i += 2,
            (b'\'', _) => return i + 1,
            _ => i += 1,
        }
    }
    bytes.len()
}

/// The end of the quoted name that opens at `start`, where `""` is a quote.
fn quoted_name_end(bytes: &[u8], start: usize) -> usize {
    let mut i = start + 1;
    while i < bytes.len() {
        match (bytes[i], bytes.get(i + 1)) {
            (b'"', Some(b'"')) => i += 2,
            (b'"', _) => return i + 1,
            _ => i += 1,
        }
    }
    bytes.len()
}

/// The `$tag$` that opens at `start`, if one does: a parameter such as
/// `$1` is not one.
fn dollar_tag(query: &str, start: usize) -> Option<&str> {
    let rest = &query.as_bytes()[start + 1..];
    let length = rest.iter().take_while(|b| is_word(**b)).count();
    let opens = rest.first().is_none_or(|b| !b.is_ascii_digit()) && rest.get(length) == Some(&b'$');
    opens.then(|| &query[start..start + length + 2])
}

/// The end of the block comment that opens at `start`; they nest.
fn block_comment_end(bytes: &[u8], start: usize) -> usize {
    let (mut i, mut depth) = (start + 2, 1);
    while i < bytes.len() {
        match (bytes[i], bytes.get(i + 1)) {
            (b'*', Some(b'/')) if depth == 1 => return i + 2,
            (b'*', Some(b'/')) => (i, depth) = (i + 2, depth - 1),
            (b'/', Some(b'*')) => (i, depth) = (i + 2, depth + 1),
            _ => i += 1,
        }
    }
    bytes.len()
}

/// The pieces of `query` as Postgres reads them, without its comments and
/// blanks.
fn tokens(query: &str) -> Vec<Token> {
    let bytes = query.as_bytes();
    let mut found: Vec<Token> = Vec::new();
    let mut i = 0;
    while i < bytes.len() {
        let token = |kind, end| Token {
            kind,
            start: i,
            end,
        };
        let next = bytes.get(i + 1).copied();
        let (skipped, token) = match bytes[i] {
            b if b.is_ascii_whitespace() => (i + 1, None),
            b'-' if next == Some(b'-') => (
                bytes[i..]
                    .iter()
                    .position(|b| *b == b'\n')
                    .map_or(bytes.len(), |line| i + line + 1),
                None,
            ),
            b'/' if next == Some(b'*') => (block_comment_end(bytes, i), None),
            b'\'' => {
                let escaped = found.last().is_some_and(|word| {
                    word.end == i && query[word.start..i].eq_ignore_ascii_case("e")
                });
                let end = literal_end(bytes, i, escaped);
                (end, Some(token(Kind::Quoted, end)))
            }
            b'"' => {
                let end = quoted_name_end(bytes, i);
                (end, Some(token(Kind::Quoted, end)))
            }
            b'$' => match dollar_tag(query, i) {
                Some(tag) => {
                    let body = i + tag.len();
                    let end = query[body..]
                        .find(tag)
                        .map_or(bytes.len(), |close| body + close + tag.len());
                    (end, Some(token(Kind::Quoted, end)))
                }
                None => (i + 1, Some(token(Kind::Other, i + 1))),
            },
            b';' => (i + 1, Some(token(Kind::Semicolon, i + 1))),
            b if is_word(b) => {
                let end = i + bytes[i..].iter().take_while(|b| is_word(**b)).count();
                (end, Some(token(Kind::Word, end)))
            }
            _ => (i + 1, Some(token(Kind::Other, i + 1))),
        };
        found.extend(token);
        i = skipped;
    }
    found
}

/// What `query` holds outside its strings, quoted names and comments: its
/// words, upper-cased, the statements it is, and where the last ends
/// (before the comments, blanks and `;` that trail it).
#[derive(Debug, Default, PartialEq, Eq)]
struct Scan {
    words: Vec<String>,
    statements: usize,
    end: usize,
}

fn scan(query: &str) -> Scan {
    let tokens = tokens(query);
    let statements = tokens
        .split(|token| token.kind == Kind::Semicolon)
        .filter(|statement| !statement.is_empty())
        .count();
    Scan {
        words: tokens
            .iter()
            .filter(|token| token.kind == Kind::Word)
            .map(|word| query[word.start..word.end].to_ascii_uppercase())
            .collect(),
        statements,
        end: tokens
            .iter()
            .rev()
            .find(|token| token.kind != Kind::Semicolon)
            .map_or(0, |last| last.end),
    }
}

/// The words of a statement that writes, or makes a table, which a
/// subquery cannot hold.
const WRITES: [&str; 5] = ["INSERT", "UPDATE", "DELETE", "MERGE", "INTO"];

/// What `--limit` makes of what was typed.
#[derive(Debug, PartialEq, Eq)]
enum Limited {
    /// One query that writes nothing, held to the limit by wrapping it.
    Wrapped(String),
    /// Sent as typed, and why the limit does not apply.
    AsTyped(Option<&'static str>),
}

/// `query` held to `limit` rows by wrapping it, so a `LIMIT` of its own, a
/// trailing comment or a trailing `;` changes nothing. Only one statement
/// that answers rows and writes nothing is wrapped: several statements, a
/// statement that is not a query, a data-modifying `WITH` and a
/// `SELECT … INTO` are sent as typed.
fn limited(query: &str, limit: Option<u64>) -> Limited {
    let Some(limit) = limit else {
        return Limited::AsTyped(None);
    };
    let scan = scan(query);
    let answers_rows = scan
        .words
        .first()
        .is_some_and(|first| ["SELECT", "WITH", "VALUES", "TABLE"].contains(&first.as_str()));
    if scan.statements > 1 {
        Limited::AsTyped(Some("it is several statements"))
    } else if !answers_rows {
        Limited::AsTyped(Some("it is not a query"))
    } else if scan
        .words
        .iter()
        .any(|word| WRITES.contains(&word.as_str()))
    {
        Limited::AsTyped(Some("it writes"))
    } else {
        Limited::Wrapped(format!(
            "SELECT * FROM (\n{}\n) q LIMIT {limit}",
            &query[..scan.end]
        ))
    }
}

#[derive(Debug, Deserialize)]
struct ColumnMeta {
    name: String,
}

#[derive(Copy, Clone)]
enum RowFormat {
    Jsonl,
    Csv,
    Tsv,
}

enum Frame {
    Columns(Vec<ColumnMeta>),
    Row(Vec<Value>),
    Done {
        rows: u64,
        truncated: bool,
        elapsed_ms: u64,
    },
    Error(String),
}

fn parse_frame(line: &str) -> eyre::Result<Frame> {
    let v: Value = serde_json::from_str(line)
        .map_err(|e| eyre::eyre!("Malformed NDJSON frame: {} — line was: {}", e, line))?;
    match v.get("type").and_then(|t| t.as_str()) {
        Some("columns") => {
            let cols: Vec<ColumnMeta> =
                serde_json::from_value(v.get("columns").cloned().unwrap_or(Value::Array(vec![])))?;
            Ok(Frame::Columns(cols))
        }
        Some("row") => {
            let values: Vec<Value> = v
                .get("values")
                .and_then(|x| x.as_array())
                .cloned()
                .unwrap_or_default();
            Ok(Frame::Row(values))
        }
        Some("done") => Ok(Frame::Done {
            rows: v.get("rows").and_then(|x| x.as_u64()).unwrap_or(0),
            truncated: v
                .get("truncated")
                .and_then(|x| x.as_bool())
                .unwrap_or(false),
            elapsed_ms: v.get("elapsed_ms").and_then(|x| x.as_u64()).unwrap_or(0),
        }),
        Some("error") => {
            let msg = v
                .get("message")
                .and_then(|x| x.as_str())
                .unwrap_or("(no message)")
                .to_string();
            Ok(Frame::Error(msg))
        }
        other => eyre::bail!("Unknown NDJSON frame type: {:?}", other),
    }
}

/// Stream every row directly to stdout in the chosen row format.
/// No buffering beyond line boundaries; safe to pipe into `head`, `jq`, etc.
async fn stream_flat(resp: reqwest::Response, kind: RowFormat) -> eyre::Result<()> {
    let mut stream = resp.bytes_stream();
    let mut buf: Vec<u8> = Vec::new();
    let stdout = std::io::stdout();
    let mut out = stdout.lock();
    let mut columns: Vec<ColumnMeta> = Vec::new();

    while let Some(chunk) = stream.next().await {
        let chunk = chunk?;
        buf.extend_from_slice(&chunk);
        while let Some(pos) = buf.iter().position(|&b| b == b'\n') {
            let line_bytes: Vec<u8> = buf.drain(..=pos).collect();
            let line = std::str::from_utf8(&line_bytes[..line_bytes.len() - 1])?.trim();
            if line.is_empty() {
                continue;
            }
            match parse_frame(line)? {
                Frame::Columns(c) => {
                    match kind {
                        RowFormat::Csv => writeln!(out, "{}", join_header(&c, ','))?,
                        RowFormat::Tsv => writeln!(out, "{}", join_header(&c, '\t'))?,
                        RowFormat::Jsonl => {}
                    }
                    columns = c;
                }
                Frame::Row(values) => match kind {
                    RowFormat::Jsonl => {
                        let obj = if columns.is_empty() {
                            Value::Array(values)
                        } else {
                            let mut map = serde_json::Map::with_capacity(columns.len());
                            for (c, v) in columns.iter().zip(values.into_iter()) {
                                map.insert(c.name.clone(), v);
                            }
                            Value::Object(map)
                        };
                        writeln!(out, "{}", serde_json::to_string(&obj)?)?;
                    }
                    RowFormat::Csv => writeln!(out, "{}", encode_row(&values, ','))?,
                    RowFormat::Tsv => writeln!(out, "{}", encode_row(&values, '\t'))?,
                },
                Frame::Done {
                    rows, truncated, ..
                } => {
                    if truncated {
                        eprintln!("(server row cap reached after {rows} rows)");
                    }
                }
                Frame::Error(msg) => eyre::bail!("Server error during streaming: {msg}"),
            }
        }
    }
    Ok(())
}

/// Buffer rows (up to TTY_PREVIEW_ROWS unless `--all`) and render a table.
async fn render_table(resp: reqwest::Response, all: bool, no_pager: bool) -> eyre::Result<()> {
    let mut stream = resp.bytes_stream();
    let mut buf: Vec<u8> = Vec::new();
    let mut columns: Vec<ColumnMeta> = Vec::new();
    let mut rows: Vec<Vec<Value>> = Vec::new();
    let mut skipped: u64 = 0;
    let mut total: u64 = 0;
    let mut server_truncated = false;
    let mut elapsed_ms: u64 = 0;

    while let Some(chunk) = stream.next().await {
        let chunk = chunk?;
        buf.extend_from_slice(&chunk);
        while let Some(pos) = buf.iter().position(|&b| b == b'\n') {
            let line_bytes: Vec<u8> = buf.drain(..=pos).collect();
            let line = std::str::from_utf8(&line_bytes[..line_bytes.len() - 1])?.trim();
            if line.is_empty() {
                continue;
            }
            match parse_frame(line)? {
                Frame::Columns(c) => columns = c,
                Frame::Row(values) => {
                    total += 1;
                    if all || rows.len() < TTY_PREVIEW_ROWS {
                        rows.push(values);
                    } else {
                        skipped += 1;
                    }
                }
                Frame::Done {
                    rows: server_rows,
                    truncated,
                    elapsed_ms: elapsed,
                } => {
                    server_truncated = truncated;
                    elapsed_ms = elapsed;
                    if server_rows > 0 && server_rows != total {
                        // Trust the server count over our row accumulator.
                        total = server_rows;
                    }
                }
                Frame::Error(msg) => eyre::bail!("Server error during streaming: {msg}"),
            }
        }
    }

    if columns.is_empty() && rows.is_empty() {
        println!("(empty result)");
        return Ok(());
    }

    let mut table = Table::new();
    table.load_preset(NOTHING);
    table.set_style(TableComponent::HeaderLines, '-');
    table.set_style(TableComponent::MiddleHeaderIntersections, ' ');
    let header: Vec<String> = columns.iter().map(|c| c.name.clone()).collect();
    table.set_header(header);
    for row in &rows {
        let cells: Vec<String> = row.iter().map(cell_string).collect();
        table.add_row(cells);
    }

    let rendered = format!("{table}");

    if all && !no_pager && !rendered.is_empty() {
        pipe_to_pager(&rendered).unwrap_or_else(|_| println!("{rendered}"));
    } else {
        println!("{rendered}");
    }

    let hint = match (skipped > 0, server_truncated) {
        (true, true) => Some(format!(
            "{skipped} rows hidden. Server cap hit at {total}. Use --format csv|jsonl to stream, or reduce the query."
        )),
        (true, false) => Some(format!(
            "{skipped} rows hidden (showing first {TTY_PREVIEW_ROWS}). Use --all, --limit N, or --format csv|jsonl."
        )),
        (false, true) => Some(format!(
            "Server cap hit at {total} rows. Query returned more than the platform allows."
        )),
        (false, false) => None,
    };
    if let Some(h) = hint {
        eprintln!("{h}");
    }
    if elapsed_ms > 0 {
        eprintln!("({total} rows, {elapsed_ms} ms)");
    }

    Ok(())
}

fn cell_string(v: &Value) -> String {
    match v {
        Value::Null => String::new(),
        Value::String(s) => s.clone(),
        other => other.to_string(),
    }
}

fn join_header(cols: &[ColumnMeta], sep: char) -> String {
    cols.iter()
        .map(|c| csv_escape(&c.name, sep))
        .collect::<Vec<_>>()
        .join(&sep.to_string())
}

fn encode_row(values: &[Value], sep: char) -> String {
    values
        .iter()
        .map(|v| csv_escape(&cell_string(v), sep))
        .collect::<Vec<_>>()
        .join(&sep.to_string())
}

/// Minimal RFC 4180-style escaping for CSV/TSV cells.
fn csv_escape(s: &str, sep: char) -> String {
    if s.contains(sep) || s.contains('"') || s.contains('\n') || s.contains('\r') {
        let escaped = s.replace('"', "\"\"");
        format!("\"{escaped}\"")
    } else {
        s.to_string()
    }
}

fn pipe_to_pager(content: &str) -> eyre::Result<()> {
    let pager = std::env::var("PAGER").unwrap_or_else(|_| "less -SRX".to_string());
    let mut parts = pager.split_whitespace();
    let program = parts
        .next()
        .ok_or_else(|| eyre::eyre!("PAGER env var is empty"))?;
    let args: Vec<&str> = parts.collect();

    let mut child = std::process::Command::new(program)
        .args(&args)
        .stdin(std::process::Stdio::piped())
        .spawn()?;
    if let Some(mut stdin) = child.stdin.take() {
        stdin.write_all(content.as_bytes())?;
    }
    child.wait()?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_limit_wraps_the_query() {
        for (query, limited_to) in [
            (
                "select * from items",
                "SELECT * FROM (\nselect * from items\n) q LIMIT 10",
            ),
            (
                "select * from items; \n",
                "SELECT * FROM (\nselect * from items\n) q LIMIT 10",
            ),
            (
                "SELECT * FROM items LIMIT 500",
                "SELECT * FROM (\nSELECT * FROM items LIMIT 500\n) q LIMIT 10",
            ),
            (
                "select * from items -- all of them",
                "SELECT * FROM (\nselect * from items\n) q LIMIT 10",
            ),
            (
                "select 1; -- note",
                "SELECT * FROM (\nselect 1\n) q LIMIT 10",
            ),
            (
                "select 1 /* one */ ; ; /* done */\n",
                "SELECT * FROM (\nselect 1\n) q LIMIT 10",
            ),
            (
                "select name from limits where note = ' LIMIT '",
                "SELECT * FROM (\nselect name from limits where note = ' LIMIT '\n) q LIMIT 10",
            ),
            (
                "select 'it''s; -- not a comment' as a",
                "SELECT * FROM (\nselect 'it''s; -- not a comment' as a\n) q LIMIT 10",
            ),
            (
                "select \"delete\", 'insert into x' from \"update\"",
                "SELECT * FROM (\nselect \"delete\", 'insert into x' from \"update\"\n) q LIMIT 10",
            ),
            (
                "select $$ ; delete $$, $tag$ it's $tag$",
                "SELECT * FROM (\nselect $$ ; delete $$, $tag$ it's $tag$\n) q LIMIT 10",
            ),
            (
                "select E'\\'; delete'",
                "SELECT * FROM (\nselect E'\\'; delete'\n) q LIMIT 10",
            ),
            (
                "-- the newest\n/* first */ WITH n AS (SELECT 1) SELECT * FROM n",
                "SELECT * FROM (\n-- the newest\n/* first */ WITH n AS (SELECT 1) SELECT * FROM n\n) q LIMIT 10",
            ),
            (
                "values (1), (2)",
                "SELECT * FROM (\nvalues (1), (2)\n) q LIMIT 10",
            ),
            (
                "TABLE items",
                "SELECT * FROM (\nTABLE items\n) q LIMIT 10",
            ),
            (
                "( select 1) union (select 2)",
                "SELECT * FROM (\n( select 1) union (select 2)\n) q LIMIT 10",
            ),
            (
                "select * from t where id = $1",
                "SELECT * FROM (\nselect * from t where id = $1\n) q LIMIT 10",
            ),
        ] {
            assert_eq!(
                limited(query, Some(10)),
                Limited::Wrapped(limited_to.to_string()),
                "{query}"
            );
        }
    }

    #[test]
    fn only_one_query_that_writes_nothing_is_limited() {
        for (statement, why) in [
            ("show search_path", "it is not a query"),
            ("explain select * from items", "it is not a query"),
            ("selector", "it is not a query"),
            ("-- select\nshow search_path", "it is not a query"),
            ("/* unterminated select", "it is not a query"),
            ("/* nested /* select */ */ show all", "it is not a query"),
            ("", "it is not a query"),
            (
                "WITH d AS (DELETE FROM items RETURNING *) SELECT * FROM d",
                "it writes",
            ),
            (
                "with moved as (update items set n = 1 returning id) select * from moved",
                "it writes",
            ),
            ("SELECT * INTO copy FROM items", "it writes"),
            ("select 1; select 2", "it is several statements"),
            ("select 1; -- one\nselect 2;", "it is several statements"),
        ] {
            assert_eq!(
                limited(statement, Some(10)),
                Limited::AsTyped(Some(why)),
                "{statement}"
            );
        }
        assert_eq!(limited("select 1;", None), Limited::AsTyped(None));
    }

    #[test]
    fn parses_columns_frame() {
        let line = r#"{"type":"columns","columns":[{"name":"id","type_name":"uuid"},{"name":"email","type_name":"text"}]}"#;
        match parse_frame(line).unwrap() {
            Frame::Columns(c) => {
                assert_eq!(c.len(), 2);
                assert_eq!(c[0].name, "id");
                assert_eq!(c[1].name, "email");
            }
            _ => panic!("expected Columns"),
        }
    }

    #[test]
    fn parses_row_frame() {
        let line = r#"{"type":"row","values":["abc",42,null,true]}"#;
        match parse_frame(line).unwrap() {
            Frame::Row(vs) => {
                assert_eq!(vs.len(), 4);
                assert_eq!(vs[1].as_i64().unwrap(), 42);
                assert!(vs[2].is_null());
            }
            _ => panic!("expected Row"),
        }
    }

    #[test]
    fn parses_done_frame() {
        let line = r#"{"type":"done","rows":17,"truncated":true,"elapsed_ms":250}"#;
        match parse_frame(line).unwrap() {
            Frame::Done {
                rows,
                truncated,
                elapsed_ms,
            } => {
                assert_eq!(rows, 17);
                assert!(truncated);
                assert_eq!(elapsed_ms, 250);
            }
            _ => panic!("expected Done"),
        }
    }

    #[test]
    fn csv_escape_quotes_embedded_commas() {
        assert_eq!(csv_escape("hello", ','), "hello");
        assert_eq!(csv_escape("a,b", ','), "\"a,b\"");
        assert_eq!(csv_escape("a\"b", ','), "\"a\"\"b\"");
        assert_eq!(csv_escape("multi\nline", ','), "\"multi\nline\"");
        // TSV doesn't escape commas.
        assert_eq!(csv_escape("a,b", '\t'), "a,b");
        assert_eq!(csv_escape("a\tb", '\t'), "\"a\tb\"");
    }

    #[test]
    fn cell_string_flattens_null_to_empty() {
        assert_eq!(cell_string(&Value::Null), "");
        assert_eq!(cell_string(&Value::String("x".into())), "x");
        assert_eq!(cell_string(&Value::Bool(true)), "true");
    }

    #[test]
    fn match_service_by_path_resolves_rust_service() {
        use crate::workspace_config::WorkspaceService;

        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        std::fs::create_dir_all(root.join("backend/asset-service")).unwrap();
        std::fs::create_dir_all(root.join("apps/portal")).unwrap();

        let services = vec![
            WorkspaceService {
                name: "asset-service".into(),
                path: "backend/asset-service".into(),
                seed: None,
                preview_tables: None,
            },
            WorkspaceService {
                name: "portal".into(),
                path: "apps/portal".into(),
                seed: None,
                preview_tables: None,
            },
        ];

        let got = match_service_by_path(&services, root, &root.join("backend/asset-service"));
        assert_eq!(got.as_deref(), Some("asset-service"));

        let got = match_service_by_path(&services, root, &root.join("apps/portal"));
        assert_eq!(got.as_deref(), Some("portal"));

        let got = match_service_by_path(&services, root, &root.join("does/not/exist"));
        assert!(got.is_none());
    }
}