Skip to main content

dsl_completion/
source_tables.rs

1//! Build a synthetic catalog from CREATE TABLE statements in the current
2//! file (and a few text-scanned object families) so completion / hover /
3//! validation work even offline -- before the DB knows about anything,
4//! or when there is no DB connection configured at all.
5//!
6//! Offline-mode coverage:
7//!   * Tables  -- AST: every CREATE TABLE in the parsed file.
8//!   * Sequences -- text-scan: CREATE SEQUENCE <name>.
9//!   * Types -- text-scan: CREATE TYPE <name> AS ENUM/DOMAIN/(...).
10//!   * Extensions -- text-scan: CREATE EXTENSION [IF NOT EXISTS] <name>.
11//!   * Functions -- text-scan: CREATE [OR REPLACE] FUNCTION <name>.
12//!   * Roles -- text-scan: CREATE ROLE/USER/GROUP <name>; plus a
13//!     fixed offline fallback set (postgres, pg_read_all_data,
14//!     pg_write_all_data) so role-completion / role-hover work even
15//!     in a brand-new file.
16//!
17//! This is purely additive: the live catalog from `dsl-catalog` and the
18//! source-derived one are merged at lookup time. When both define the
19//! same object, the live catalog wins because it has richer metadata
20//! (constraints, indexes, comments).
21
22/// Byte-safe `s[i..].starts_with(pat)`.
23///
24/// Every scanner in this module walks `i` over raw bytes, so slicing the
25/// `str` directly panics the instant a buffer holds a multi-byte
26/// character -- one CJK comment or accented identifier anywhere in a
27/// workspace `.sql` file was enough to take the whole scan down with
28/// "byte index is not a char boundary". Comparing bytes sidesteps the
29/// boundary question entirely, and every pattern here is ASCII.
30fn starts_with_at(s: &str, i: usize, pat: &str) -> bool {
31  s.as_bytes().get(i..).is_some_and(|b| b.starts_with(pat.as_bytes()))
32}
33
34use dsl_catalog::{
35  CATALOG_VERSION, Catalog, Column, Constraint, ConstraintKind, ConstraintRef, Extension, Function, FunctionArg,
36  IndexDef, Policy, Schema, Sequence, Table, TableKind, Trigger, Type, TypeKind,
37};
38use dsl_parse::{ParsedFile, StatementKind};
39
40/// Build a [`Catalog`] from every CREATE TABLE statement in `file`.
41/// Returns an empty catalog if none are found.
42pub fn from_file(file: &ParsedFile) -> Catalog {
43  let mut public = Schema { name: "public".into(), tables: Vec::new() };
44  let mut other: std::collections::BTreeMap<String, Schema> = Default::default();
45
46  for stmt in &file.statements {
47    let StatementKind::CreateTable(ct) = &stmt.kind else {
48      continue;
49    };
50    let schema_name = ct.table.schema.clone().unwrap_or_else(|| "public".into());
51    let table = Table {
52      schema: schema_name.clone(),
53      name: ct.table.name.clone(),
54      kind: TableKind::Table,
55      columns: ct
56        .columns
57        .iter()
58        .map(|c| Column {
59          name: c.name.clone(),
60          data_type: c.type_name.clone(),
61          nullable: c.nullable,
62          default: c.default.clone(),
63          comment: None,
64          generated: None,
65          json_keys: None,
66        })
67        .collect(),
68      constraints: Vec::new(),
69      indexes: Vec::new(),
70      triggers: Vec::new(),
71      policies: Vec::new(),
72      // No synthetic comment -- the table is real, not a doc artifact.
73      // Real COMMENT ON TABLE statements get picked up in
74      // scan_table_comments() below when present.
75      comment: None,
76      row_estimate: None,
77      owner: None,
78      definition: None,
79      strict: false,
80      options: None,
81    };
82    if schema_name.eq_ignore_ascii_case("public") {
83      public.tables.push(table);
84    } else {
85      other
86        .entry(schema_name.clone())
87        .or_insert_with(|| Schema { name: schema_name, tables: Vec::new() })
88        .tables
89        .push(table);
90    }
91  }
92
93  let mut schemas = Vec::new();
94  if !public.tables.is_empty() {
95    schemas.push(public);
96  }
97  schemas.extend(other.into_values());
98
99  Catalog {
100    version: CATALOG_VERSION,
101    connection_id: "<source>".into(),
102    schemas,
103    functions: Vec::new(),
104    types: Vec::new(),
105    roles: Vec::new(),
106    sequences: Vec::new(),
107    extensions: Vec::new(),
108  }
109}
110
111/// Full offline-mode catalog: tables from AST + sequences / types /
112/// extensions / functions / roles harvested from raw source text. The
113/// scans are intentionally lenient -- partial / unparseable input is
114/// fine because the goal is "give the user *something* without a DB".
115/// `source` is the buffer text the AST in `file` came from.
116pub fn from_source(file: &ParsedFile, source: &str) -> Catalog {
117  let mut cat = from_file(file);
118  // Text-fallback for CREATE TABLE statements pg_query rejected (often
119  // a single bad column expression aborts the whole stmt -- the rest
120  // of the table is still recoverable from the source). Without this
121  // pass a single syntax error invisibly removes the entire table from
122  // hover / completion / sql001 resolution.
123  recover_failed_create_tables(&mut cat, source);
124  cat.sequences = scan_sequences(source);
125  cat.types = scan_types(source);
126  cat.extensions = scan_extensions(source);
127  cat.functions = scan_functions(source);
128  cat.roles = scan_roles(source);
129  // Add PARTITION OF child tables by copying the parent's columns.
130  // The CREATE TABLE child PARTITION OF parent form has no inline
131  // columns, so from_file misses them; downstream rules (sql002 etc)
132  // then can't resolve `child.col`.
133  inherit_partition_columns(&mut cat, source);
134  // Walk every CREATE TABLE body and pull inline + table-level
135  // PRIMARY KEY / UNIQUE / FOREIGN KEY constraints from the source.
136  // The AST doesn't carry these (CreateTableStmt::columns only has
137  // name + type + nullable + default), so this text-scan is how
138  // offline mode learns about FKs at all.
139  let comments_by_col = scan_column_comments(source);
140  let table_comments = scan_table_comments(source);
141  let indexes_by_table = scan_indexes(source);
142  let triggers_by_table = scan_triggers(source);
143  let policies_by_table = scan_policies(source);
144  let insert_counts = scan_dml_row_deltas(source);
145  for schema in cat.schemas.iter_mut() {
146    for table in schema.tables.iter_mut() {
147      table.constraints = scan_constraints_for(source, &table.name);
148      let key = table.name.to_ascii_lowercase();
149      if let Some(c) = table_comments.get(&key) {
150        table.comment = Some(c.clone());
151      }
152      if let Some(idxs) = indexes_by_table.get(&key) {
153        table.indexes = idxs.clone();
154      }
155      if let Some(trgs) = triggers_by_table.get(&key) {
156        table.triggers = trgs.clone();
157      }
158      if let Some(pols) = policies_by_table.get(&key) {
159        table.policies = pols.clone();
160      }
161      // Offline-mode row estimate: count INSERT VALUES tuples that
162      // target this table across the source. Gives a usable chip in
163      // workspaces without a live DB (learning projects, test
164      // fixtures). Live introspection overrides this via merge.
165      if let Some(&n) = insert_counts.get(&key) {
166        // Clamp negative (DELETE without prior INSERT in buffer) to 0
167        // so the codelens chip never displays a negative count.
168        table.row_estimate = Some((n.max(0)) as f64);
169      }
170      let generated_by_col = scan_generated_for(source, &table.name);
171      let json_keys_by_col = scan_json_keys_for(source, &table.name);
172      let defaults_by_col = scan_defaults_for(source, &table.name);
173      // `ALTER TABLE <t> OWNER TO <role>` -- last occurrence in the
174      // buffer wins, matching PG's "last write" semantics across a
175      // migration file.
176      if let Some(o) = scan_owner_for(source, &table.name) {
177        table.owner = Some(o);
178      }
179      for col in table.columns.iter_mut() {
180        let ckey = format!("{}.{}", table.name.to_ascii_lowercase(), col.name.to_ascii_lowercase());
181        if let Some(c) = comments_by_col.get(&ckey) {
182          col.comment = Some(c.clone());
183        }
184        if let Some(g) = generated_by_col.get(&col.name.to_ascii_lowercase()) {
185          col.generated = Some(g.clone());
186        }
187        if let Some(keys) = json_keys_by_col.get(&col.name.to_ascii_lowercase()) {
188          col.json_keys = Some(keys.clone());
189        }
190        // pg_query's raw_expr deparse for ConstrDefault isn't available
191        // through the public surface, so the AST converter leaves
192        // col.default = None for offline-mode CREATE TABLE statements.
193        // Text-scan the table body for `DEFAULT <expr>` per column so
194        // hover renders `id int DEFAULT gen_random_uuid()` instead of
195        // dropping the DEFAULT clause.
196        if col.default.is_none()
197          && let Some(d) = defaults_by_col.get(&col.name.to_ascii_lowercase())
198        {
199          col.default = Some(d.clone());
200        }
201      }
202    }
203  }
204
205  // Views: the AST converter drops CREATE VIEW (it only models CREATE
206  // TABLE), so offline mode has no views at all without this scan. Surface
207  // them with their defining SELECT so hover can show what they select.
208  for (schema_name, name, kind, columns, definition) in scan_views(source) {
209    if cat
210      .schemas
211      .iter()
212      .any(|s| s.name.eq_ignore_ascii_case(&schema_name) && s.tables.iter().any(|t| t.name.eq_ignore_ascii_case(&name)))
213    {
214      continue; // already present (e.g. a same-named table) -- don't clobber.
215    }
216    let cols: Vec<Column> = columns
217      .into_iter()
218      .map(|c| Column {
219        name: c,
220        data_type: String::new(),
221        nullable: true,
222        default: None,
223        comment: None,
224        generated: None,
225        json_keys: None,
226      })
227      .collect();
228    let view = Table {
229      schema: schema_name.clone(),
230      name,
231      kind,
232      columns: cols,
233      constraints: Vec::new(),
234      indexes: Vec::new(),
235      triggers: Vec::new(),
236      policies: Vec::new(),
237      comment: None,
238      row_estimate: None,
239      owner: None,
240      definition: Some(definition),
241      strict: false,
242      options: None,
243    };
244    if let Some(s) = cat.schemas.iter_mut().find(|s| s.name.eq_ignore_ascii_case(&schema_name)) {
245      s.tables.push(view);
246    } else {
247      cat.schemas.push(Schema { name: schema_name, tables: vec![view] });
248    }
249  }
250
251  cat
252}
253
254/// Scan `CREATE [OR REPLACE] [TEMP|TEMPORARY] [MATERIALIZED] [RECURSIVE] VIEW
255/// [schema.]name [(col, ...)] AS <select>;` statements and return
256/// `(schema, name, kind, column_names, select_body)` for each. Best-effort
257/// and lenient -- offline mode, so odd / partial input is skipped.
258#[allow(clippy::type_complexity)]
259fn scan_views(src: &str) -> Vec<(String, String, TableKind, Vec<String>, String)> {
260  let upper = src.to_ascii_uppercase();
261  let bytes = src.as_bytes();
262  let n = bytes.len();
263  let is_ident = |b: u8| b.is_ascii_alphanumeric() || b == b'_';
264  let skip_ws = |mut i: usize| {
265    while i < n && bytes[i].is_ascii_whitespace() {
266      i += 1;
267    }
268    i
269  };
270  // Read the next ASCII-word, returning it uppercased plus the end offset.
271  let read_word = |i: usize| -> (String, usize) {
272    let mut j = i;
273    while j < n && is_ident(bytes[j]) {
274      j += 1;
275    }
276    (upper[i..j].to_string(), j)
277  };
278
279  let mut out = Vec::new();
280  let mut from = 0usize;
281  while let Some(rel) = upper[from..].find("CREATE") {
282    let at = from + rel;
283    from = at + 6;
284    if at > 0 && is_ident(bytes[at - 1]) {
285      continue;
286    }
287    // Consume modifier keywords until VIEW (or bail on anything else).
288    let mut p = at + 6;
289    let mut materialized = false;
290    let mut is_view = false;
291    loop {
292      p = skip_ws(p);
293      let (w, np) = read_word(p);
294      if np == p {
295        break;
296      }
297      match w.as_str() {
298        "OR" | "REPLACE" | "TEMP" | "TEMPORARY" | "GLOBAL" | "LOCAL" | "RECURSIVE" | "UNLOGGED" => p = np,
299        "MATERIALIZED" => {
300          materialized = true;
301          p = np;
302        },
303        "VIEW" => {
304          p = np;
305          is_view = true;
306          break;
307        },
308        _ => break,
309      }
310    }
311    if !is_view {
312      continue;
313    }
314    // Optional IF NOT EXISTS.
315    {
316      let (w1, q1) = read_word(skip_ws(p));
317      if w1 == "IF" {
318        let (w2, q2) = read_word(skip_ws(q1));
319        let (w3, q3) = read_word(skip_ws(q2));
320        if w2 == "NOT" && w3 == "EXISTS" {
321          p = q3;
322        }
323      }
324    }
325    // Qualified view name.
326    let Some((schema_opt, name, after)) = read_qualified_ident(src, skip_ws(p)) else { continue };
327    let schema = schema_opt.unwrap_or_else(|| "public".into());
328    p = skip_ws(after);
329    // Optional output-column list.
330    let mut columns = Vec::new();
331    if p < n && bytes[p] == b'(' {
332      if let Some(close) = find_matching_paren(src, p) {
333        for c in src[p + 1..close].split(',') {
334          let c = c.trim().trim_matches('"');
335          if !c.is_empty() {
336            columns.push(c.to_string());
337          }
338        }
339        p = skip_ws(close + 1);
340      } else {
341        continue;
342      }
343    }
344    // Require the AS keyword, then the SELECT body up to the top-level `;`.
345    let (w, np) = read_word(p);
346    if w != "AS" {
347      continue;
348    }
349    let body_start = skip_ws(np);
350    let end = view_stmt_end(bytes, body_start);
351    let definition = src[body_start..end].trim().to_string();
352    if definition.is_empty() {
353      continue;
354    }
355    let kind = if materialized { TableKind::MaterializedView } else { TableKind::View };
356    out.push((schema, name, kind, columns, definition));
357    from = end;
358  }
359  out
360}
361
362/// Offset of the statement-terminating top-level `;` (or end of input),
363/// skipping `;` inside parens and quoted runs.
364fn view_stmt_end(bytes: &[u8], start: usize) -> usize {
365  let n = bytes.len();
366  let mut i = start;
367  let mut depth = 0i32;
368  while i < n {
369    match bytes[i] {
370      b'\'' | b'"' | b'`' => {
371        let q = bytes[i];
372        i += 1;
373        while i < n && bytes[i] != q {
374          i += 1;
375        }
376      },
377      b'(' => depth += 1,
378      b')' => depth -= 1,
379      b';' if depth == 0 => return i,
380      _ => {},
381    }
382    i += 1;
383  }
384  n
385}
386
387/// `CREATE [OR REPLACE] POLICY <name> ON <tbl>
388///   [AS {PERMISSIVE|RESTRICTIVE}]
389///   [FOR {ALL|SELECT|INSERT|UPDATE|DELETE}]
390///   [TO <roles>]
391///   [USING (<expr>)]
392///   [WITH CHECK (<expr>)]`
393/// -> map<table_lower, Vec<Policy>>.
394/// `CREATE TABLE child PARTITION OF parent ...` declares `child`
395/// inheriting parent's full column list. The AST converter only sees
396/// the explicit `table_elts`, so the child lands in the catalog with
397/// zero columns -- unknown-column rules can't resolve `child.col`.
398/// Walk the source, find every PARTITION OF and copy parent columns
399/// onto matching children already in the catalog.
400/// Scan the source for `CREATE TABLE [schema.]<name> ( <columns> )`
401/// blocks whose table did NOT make it into the AST-derived catalog
402/// (usually because one column expression aborted pg_query). Build a
403/// minimal Table from the text body so hover / completion / sql001
404/// have *something* to resolve. Best effort -- only inline column
405/// definitions are picked up; constraint clauses are skipped.
406fn recover_failed_create_tables(cat: &mut Catalog, src: &str) {
407  let cleaned = strip_string_literals(src);
408  let upper = cleaned.to_ascii_uppercase();
409  let bytes = cleaned.as_bytes();
410  let n = bytes.len();
411  let needle = "CREATE TABLE";
412  let mut from = 0usize;
413  while let Some(rel) = upper[from..].find(needle) {
414    let at = from + rel;
415    // Word-boundary guard.
416    if at > 0 {
417      let prev = bytes[at - 1] as char;
418      if prev.is_ascii_alphanumeric() || prev == '_' {
419        from = at + needle.len();
420        continue;
421      }
422    }
423    let mut k = at + needle.len();
424    // Skip optional modifiers.
425    for kw in ["IF NOT EXISTS", "UNLOGGED", "TEMP", "TEMPORARY", "GLOBAL", "LOCAL"] {
426      while k < n && bytes[k].is_ascii_whitespace() {
427        k += 1;
428      }
429      if k + kw.len() <= n && upper.as_bytes()[k..k + kw.len()] == *kw.as_bytes() {
430        k += kw.len();
431      }
432    }
433    while k < n && bytes[k].is_ascii_whitespace() {
434      k += 1;
435    }
436    let (schema, name, after_name) = match read_qualified_ident(&cleaned, k) {
437      Some(t) => t,
438      None => {
439        from = at + needle.len();
440        continue;
441      },
442    };
443    from = at + needle.len();
444    if name.is_empty() {
445      continue;
446    }
447    let schema_name = schema.unwrap_or_else(|| "public".to_string()).to_ascii_lowercase();
448    let table_name_lc = name.to_ascii_lowercase();
449    if cat.schemas.iter().any(|s| {
450      s.name.eq_ignore_ascii_case(&schema_name) && s.tables.iter().any(|t| t.name.eq_ignore_ascii_case(&table_name_lc))
451    }) {
452      continue;
453    }
454    // Find the body parenthesis after the name.
455    let mut p = after_name;
456    while p < n && bytes[p].is_ascii_whitespace() {
457      p += 1;
458    }
459    if p >= n || bytes[p] != b'(' {
460      continue;
461    }
462    let body_start = p + 1;
463    let body_end = match find_matching_paren(&cleaned, p) {
464      Some(e) => e,
465      None => continue,
466    };
467    let body = &cleaned[body_start..body_end];
468    let columns = parse_create_table_columns(body);
469    if columns.is_empty() {
470      continue;
471    }
472    let table = Table {
473      schema: schema_name.clone(),
474      name: name.clone(),
475      kind: TableKind::Table,
476      columns,
477      constraints: Vec::new(),
478      indexes: Vec::new(),
479      triggers: Vec::new(),
480      policies: Vec::new(),
481      comment: None,
482      row_estimate: None,
483      owner: None,
484      definition: None,
485      strict: false,
486      options: None,
487    };
488    if let Some(s) = cat.schemas.iter_mut().find(|s| s.name.eq_ignore_ascii_case(&schema_name)) {
489      s.tables.push(table);
490    } else {
491      cat.schemas.push(Schema { name: schema_name, tables: vec![table] });
492    }
493  }
494}
495
496/// Read an optionally-quoted ident, then optional `.<ident>` for a
497/// schema-qualified pair. Returns (schema, name, byte_after_last_char).
498fn read_qualified_ident(src: &str, pos: usize) -> Option<(Option<String>, String, usize)> {
499  let (first, after_first) = read_ident_simple(src, pos)?;
500  if first.is_empty() {
501    return None;
502  }
503  let bytes = src.as_bytes();
504  let mut p = after_first;
505  while p < bytes.len() && bytes[p].is_ascii_whitespace() {
506    p += 1;
507  }
508  if p < bytes.len() && bytes[p] == b'.' {
509    let (second, after_second) = read_ident_simple(src, p + 1)?;
510    if !second.is_empty() {
511      return Some((Some(first), second, after_second));
512    }
513  }
514  Some((None, first, after_first))
515}
516
517/// The closing delimiter for an identifier that opens with `open`.
518///
519/// Every dialect this server parses quotes identifiers, and only Postgres
520/// uses `"`. SQLite accepts all three, and it is the one that matters here:
521/// the DDL sqlite stores for a table created by most tools is bracketed.
522fn ident_close(open: u8) -> Option<u8> {
523  match open {
524    b'"' => Some(b'"'),
525    b'[' => Some(b']'),
526    b'`' => Some(b'`'),
527    _ => None,
528  }
529}
530
531/// Strip one layer of identifier quoting, whichever dialect wrote it.
532///
533/// Only when the whole string is one quoted identifier. `"a" = "b"` opens and
534/// closes with a quote without being quoted, and stripping its ends would
535/// hand back `a" = "b` as though it were a column name.
536pub(crate) fn unquote_ident(raw: &str) -> &str {
537  let bytes = raw.as_bytes();
538  let (Some(&first), Some(&last)) = (bytes.first(), bytes.last()) else {
539    return raw;
540  };
541  if bytes.len() >= 2 && ident_close(first) == Some(last) && !bytes[1..bytes.len() - 1].contains(&last) {
542    // `get` rather than a slice: the ends are ASCII quotes here, but the rule
543    // in this file is that byte arithmetic never indexes a `&str` directly.
544    if let Some(inner) = raw.get(1..raw.len() - 1) {
545      return inner;
546    }
547  }
548  raw
549}
550
551fn read_ident_simple(src: &str, pos: usize) -> Option<(String, usize)> {
552  let bytes = src.as_bytes();
553  let n = bytes.len();
554  let mut i = pos;
555  while i < n && bytes[i].is_ascii_whitespace() {
556    i += 1;
557  }
558  if i >= n {
559    return None;
560  }
561  if let Some(close) = ident_close(bytes[i]) {
562    let start = i + 1;
563    let mut j = start;
564    while j < n && bytes[j] != close {
565      j += 1;
566    }
567    if j >= n {
568      return None;
569    }
570    return Some((src[start..j].to_string(), j + 1));
571  }
572  let start = i;
573  while i < n && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
574    i += 1;
575  }
576  if i == start {
577    return None;
578  }
579  Some((src[start..i].to_string(), i))
580}
581
582fn find_matching_paren(src: &str, open: usize) -> Option<usize> {
583  let bytes = src.as_bytes();
584  if open >= bytes.len() || bytes[open] != b'(' {
585    return None;
586  }
587  let mut depth = 1i32;
588  let mut i = open + 1;
589  while i < bytes.len() {
590    match bytes[i] {
591      b'(' => depth += 1,
592      b')' => {
593        depth -= 1;
594        if depth == 0 {
595          return Some(i);
596        }
597      },
598      _ => {},
599    }
600    i += 1;
601  }
602  None
603}
604
605/// Parse the body of a CREATE TABLE (between the outer parens) into a
606/// list of Columns. Skips table-level constraint rows (PRIMARY KEY,
607/// CHECK, UNIQUE, FOREIGN KEY, CONSTRAINT) and entries that don't look
608/// like column definitions.
609fn parse_create_table_columns(body: &str) -> Vec<Column> {
610  let mut out = Vec::new();
611  for raw_line in split_top_level_commas(body) {
612    let trimmed = raw_line.trim();
613    if trimmed.is_empty() {
614      continue;
615    }
616    let upper = trimmed.to_ascii_uppercase();
617    if upper.starts_with("PRIMARY KEY")
618      || upper.starts_with("UNIQUE")
619      || upper.starts_with("CHECK")
620      || upper.starts_with("FOREIGN KEY")
621      || upper.starts_with("CONSTRAINT")
622      || upper.starts_with("LIKE")
623      || upper.starts_with("EXCLUDE")
624    {
625      continue;
626    }
627    let (name, after_name) = match read_ident_simple(trimmed, 0) {
628      Some(t) => t,
629      None => continue,
630    };
631    let rest = trimmed[after_name..].trim();
632    if rest.is_empty() {
633      continue;
634    }
635    // Take the type up to the first NOT NULL / NULL / DEFAULT / PRIMARY
636    // / UNIQUE / CHECK / REFERENCES / GENERATED / COLLATE / -- stopword.
637    let stop_words: &[&str] = &[
638      "NOT NULL",
639      "NULL",
640      "DEFAULT",
641      "PRIMARY",
642      "UNIQUE",
643      "CHECK",
644      "REFERENCES",
645      "GENERATED",
646      "COLLATE",
647      "CONSTRAINT",
648    ];
649    let upper_rest = rest.to_ascii_uppercase();
650    let mut cut = upper_rest.len();
651    for kw in stop_words {
652      if let Some(at) = find_word(&upper_rest, kw)
653        && at < cut
654      {
655        cut = at;
656      }
657    }
658    let type_text = rest[..cut].trim().to_string();
659    if type_text.is_empty() {
660      continue;
661    }
662    let nullable = !upper_rest[..cut.min(upper_rest.len())].contains("NOT NULL") && !upper_rest.contains("PRIMARY KEY");
663    out.push(Column {
664      name,
665      data_type: type_text,
666      nullable,
667      default: None,
668      comment: None,
669      generated: None,
670      json_keys: None,
671    });
672  }
673  out
674}
675
676/// Split `body` on commas that sit at parenthesis-depth 0 only.
677fn split_top_level_commas(body: &str) -> Vec<&str> {
678  let mut out = Vec::new();
679  let bytes = body.as_bytes();
680  let mut depth = 0i32;
681  let mut start = 0usize;
682  let mut i = 0usize;
683  while i < bytes.len() {
684    match bytes[i] {
685      b'(' => depth += 1,
686      b')' => depth -= 1,
687      b',' if depth == 0 => {
688        out.push(&body[start..i]);
689        start = i + 1;
690      },
691      _ => {},
692    }
693    i += 1;
694  }
695  if start < bytes.len() {
696    out.push(&body[start..]);
697  }
698  out
699}
700
701/// Find `kw` in `haystack` only when it sits at a word boundary on
702/// both sides. Case-sensitive (callers upper both first).
703fn find_word(haystack: &str, kw: &str) -> Option<usize> {
704  let mut from = 0;
705  while let Some(rel) = haystack[from..].find(kw) {
706    let at = from + rel;
707    let prev_ok = at == 0 || {
708      let p = haystack.as_bytes()[at - 1];
709      !(p.is_ascii_alphanumeric() || p == b'_')
710    };
711    let end = at + kw.len();
712    let next_ok = end == haystack.len() || {
713      let n = haystack.as_bytes()[end];
714      !(n.is_ascii_alphanumeric() || n == b'_')
715    };
716    if prev_ok && next_ok {
717      return Some(at);
718    }
719    from = at + kw.len();
720  }
721  None
722}
723
724fn inherit_partition_columns(cat: &mut Catalog, src: &str) {
725  let cleaned = strip_string_literals(src);
726  let upper = cleaned.to_ascii_uppercase();
727  let bytes = cleaned.as_bytes();
728  let n = bytes.len();
729  let needle = "CREATE TABLE";
730  let mut from = 0usize;
731  let mut pairs: Vec<(String, String)> = Vec::new();
732  while let Some(rel) = upper[from..].find(needle) {
733    let at = from + rel;
734    let mut k = at + needle.len();
735    while k < n && bytes[k].is_ascii_whitespace() {
736      k += 1
737    }
738    if starts_with_at(&upper, k, "IF NOT EXISTS") {
739      k += "IF NOT EXISTS".len();
740      while k < n && bytes[k].is_ascii_whitespace() {
741        k += 1
742      }
743    }
744    let id_start = k;
745    while k < n && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'.' || bytes[k] == b'"') {
746      k += 1
747    }
748    let child = cleaned[id_start..k].trim_matches('"').to_string();
749    from = k;
750    while k < n && bytes[k].is_ascii_whitespace() {
751      k += 1
752    }
753    if !starts_with_at(&upper, k, "PARTITION OF") {
754      continue;
755    }
756    k += "PARTITION OF".len();
757    while k < n && bytes[k].is_ascii_whitespace() {
758      k += 1
759    }
760    let p_start = k;
761    while k < n && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'.' || bytes[k] == b'"') {
762      k += 1
763    }
764    let parent = cleaned[p_start..k].trim_matches('"').to_string();
765    pairs.push((child, parent));
766  }
767  if pairs.is_empty() {
768    return;
769  }
770  // For each pair, locate parent in catalog, copy its columns onto child.
771  for (child_fq, parent_fq) in pairs {
772    let p_bare = parent_fq.rsplit('.').next().unwrap_or(&parent_fq).to_string();
773    let c_bare = child_fq.rsplit('.').next().unwrap_or(&child_fq).to_string();
774    let parent_cols: Option<Vec<Column>> = cat
775      .schemas
776      .iter()
777      .flat_map(|s| s.tables.iter())
778      .find(|t| t.name.eq_ignore_ascii_case(&p_bare))
779      .map(|t| t.columns.clone());
780    let Some(cols) = parent_cols else { continue };
781    for schema in cat.schemas.iter_mut() {
782      for t in schema.tables.iter_mut() {
783        if t.name.eq_ignore_ascii_case(&c_bare) && t.columns.is_empty() {
784          t.columns = cols.clone();
785        }
786      }
787    }
788  }
789}
790
791fn strip_string_literals(s: &str) -> String {
792  let mut out: Vec<u8> = s.as_bytes().to_vec();
793  let n = out.len();
794  let mut i = 0usize;
795  while i < n {
796    if i + 1 < n && out[i] == b'-' && out[i + 1] == b'-' {
797      while i < n && out[i] != b'\n' {
798        out[i] = b' ';
799        i += 1
800      }
801      continue;
802    }
803    if out[i] == b'\'' {
804      out[i] = b' ';
805      i += 1;
806      while i < n && out[i] != b'\'' {
807        out[i] = b' ';
808        i += 1
809      }
810      if i < n {
811        out[i] = b' ';
812        i += 1
813      }
814      continue;
815    }
816    i += 1;
817  }
818  String::from_utf8(out).unwrap_or_else(|_| s.to_string())
819}
820
821fn scan_policies(src: &str) -> std::collections::HashMap<String, Vec<Policy>> {
822  let upper = src.to_ascii_uppercase();
823  let bytes = src.as_bytes();
824  let mut out: std::collections::HashMap<String, Vec<Policy>> = std::collections::HashMap::new();
825  for needle in ["CREATE OR REPLACE POLICY ", "CREATE POLICY "] {
826    let mut from = 0usize;
827    while let Some(rel) = upper[from..].find(needle) {
828      let after = from + rel + needle.len();
829      let mut k = after;
830      while k < bytes.len() && bytes[k].is_ascii_whitespace() {
831        k += 1;
832      }
833      let name_start = k;
834      while k < bytes.len() && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'"') {
835        k += 1;
836      }
837      let name = src[name_start..k].trim_matches('"').to_string();
838      let stmt_end = src[after..].find(';').map(|i| after + i).unwrap_or(src.len());
839      let stmt = &src[after..stmt_end];
840      let stmt_upper = stmt.to_ascii_uppercase();
841      let tbl = if let Some(on_at) = stmt_upper.find(" ON ") {
842        let rest = &stmt[on_at + 4..];
843        let lead = rest.len() - rest.trim_start().len();
844        let raw = &rest[lead..];
845        let id_end =
846          raw.find(|c: char| !c.is_ascii_alphanumeric() && c != '_' && c != '.' && c != '"').unwrap_or(raw.len());
847        raw[..id_end].rsplit('.').next().unwrap_or(&raw[..id_end]).trim_matches('"').to_ascii_lowercase()
848      } else {
849        from = after;
850        continue;
851      };
852      let permissive = if stmt_upper.contains("RESTRICTIVE") { "RESTRICTIVE" } else { "PERMISSIVE" };
853      let command = ["ALL", "SELECT", "INSERT", "UPDATE", "DELETE"]
854        .iter()
855        .find(|c| stmt_upper.contains(&format!(" FOR {c}")))
856        .map(|s| s.to_string())
857        .unwrap_or_else(|| "ALL".into());
858      let roles = if let Some(to_at) = stmt_upper.find(" TO ") {
859        let rest = &stmt[to_at + 4..];
860        let end = rest.find([';', '\n']).unwrap_or(rest.len());
861        let raw = rest[..end].trim();
862        // Trim trailing clauses like USING / WITH CHECK.
863        let raw_upper = raw.to_ascii_uppercase();
864        let cut = ["USING", "WITH CHECK"].iter().filter_map(|kw| raw_upper.find(kw)).min().unwrap_or(raw.len());
865        raw[..cut].trim().to_string()
866      } else {
867        "PUBLIC".into()
868      };
869      let using_expr = extract_paren_after(stmt, "USING");
870      let check_expr = extract_paren_after(stmt, "WITH CHECK");
871      out.entry(tbl).or_default().push(Policy {
872        name,
873        permissive: permissive.to_string(),
874        roles,
875        command,
876        using_expr,
877        check_expr,
878      });
879      from = after;
880    }
881  }
882  out
883}
884
885fn extract_paren_after(stmt: &str, kw: &str) -> Option<String> {
886  let upper = stmt.to_ascii_uppercase();
887  let at = upper.find(kw)?;
888  let after = at + kw.len();
889  let rest_bytes = stmt.as_bytes();
890  let mut k = after;
891  while k < rest_bytes.len() && rest_bytes[k].is_ascii_whitespace() {
892    k += 1;
893  }
894  if k >= rest_bytes.len() || rest_bytes[k] != b'(' {
895    return None;
896  }
897  let close = match_paren(rest_bytes, k);
898  if close >= rest_bytes.len() {
899    return None;
900  }
901  Some(stmt[k + 1..close].trim().to_string())
902}
903
904/// `COMMENT ON TABLE <tbl> IS '<text>'` -> map<table_lower, text>.
905fn scan_table_comments(src: &str) -> std::collections::HashMap<String, String> {
906  let upper = src.to_ascii_uppercase();
907  let mut out = std::collections::HashMap::new();
908  let mut from = 0usize;
909  while let Some(rel) = upper[from..].find("COMMENT ON TABLE ") {
910    let after = from + rel + "COMMENT ON TABLE ".len();
911    let bytes = src.as_bytes();
912    let mut k = after;
913    while k < bytes.len() && bytes[k].is_ascii_whitespace() {
914      k += 1;
915    }
916    let id_start = k;
917    while k < bytes.len()
918      && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'.' || bytes[k] == b'"')
919    {
920      k += 1;
921    }
922    let id = &src[id_start..k];
923    let bare = id.rsplit('.').next().unwrap_or(id).trim_matches('"').to_ascii_lowercase();
924    while k < bytes.len() && bytes[k].is_ascii_whitespace() {
925      k += 1;
926    }
927    if k + 2 > bytes.len() || !starts_with_at(&upper, k, "IS") {
928      from = after;
929      continue;
930    }
931    k += 2;
932    while k < bytes.len() && bytes[k].is_ascii_whitespace() {
933      k += 1;
934    }
935    if k >= bytes.len() || bytes[k] != b'\'' {
936      from = after;
937      continue;
938    }
939    let s_start = k + 1;
940    let mut s_end = s_start;
941    while s_end < bytes.len() && bytes[s_end] != b'\'' {
942      s_end += 1;
943    }
944    if s_end >= bytes.len() {
945      break;
946    }
947    out.insert(bare, src[s_start..s_end].to_string());
948    from = s_end + 1;
949  }
950  out
951}
952
953/// `CREATE [UNIQUE] INDEX [IF NOT EXISTS] <name> ON <tbl> (cols) [USING ...]`
954/// -> map<table_lower, Vec<IndexDef>>.
955fn scan_indexes(src: &str) -> std::collections::HashMap<String, Vec<IndexDef>> {
956  let upper = src.to_ascii_uppercase();
957  let bytes = src.as_bytes();
958  let mut out: std::collections::HashMap<String, Vec<IndexDef>> = std::collections::HashMap::new();
959  for (needle, unique) in [
960    ("CREATE UNIQUE INDEX IF NOT EXISTS ", true),
961    ("CREATE UNIQUE INDEX ", true),
962    ("CREATE INDEX IF NOT EXISTS ", false),
963    ("CREATE INDEX ", false),
964  ] {
965    let mut from = 0usize;
966    while let Some(rel) = upper[from..].find(needle) {
967      let after = from + rel + needle.len();
968      let mut k = after;
969      while k < bytes.len() && bytes[k].is_ascii_whitespace() {
970        k += 1;
971      }
972      let name_start = k;
973      while k < bytes.len() && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'"') {
974        k += 1;
975      }
976      let name = src[name_start..k].trim_matches('"').to_string();
977      while k < bytes.len() && bytes[k].is_ascii_whitespace() {
978        k += 1;
979      }
980      if starts_with_at(&upper, k, "ON ") {
981        k += 3;
982      } else {
983        from = after;
984        continue;
985      }
986      while k < bytes.len() && bytes[k].is_ascii_whitespace() {
987        k += 1;
988      }
989      let tbl_start = k;
990      while k < bytes.len()
991        && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'.' || bytes[k] == b'"')
992      {
993        k += 1;
994      }
995      let raw_tbl = &src[tbl_start..k];
996      let bare = raw_tbl.rsplit('.').next().unwrap_or(raw_tbl).trim_matches('"').to_ascii_lowercase();
997      // Find `(`.
998      while k < bytes.len() && bytes[k] != b'(' && bytes[k] != b';' {
999        k += 1;
1000      }
1001      let cols = if k < bytes.len() && bytes[k] == b'(' {
1002        let end = match_paren(bytes, k);
1003        let body = &src[k + 1..end];
1004        split_top_commas(body)
1005          .into_iter()
1006          .map(|c| c.trim().trim_matches('"').to_string())
1007          .filter(|c| !c.is_empty())
1008          .collect()
1009      } else {
1010        Vec::new()
1011      };
1012      // Find the end of this statement.
1013      let stmt_end = src[name_start..].find(';').map(|i| name_start + i + 1).unwrap_or(src.len());
1014      let definition = src[from + rel..stmt_end].trim().to_string();
1015      out.entry(bare).or_default().push(IndexDef { name, columns: cols, unique, definition: Some(definition) });
1016      from = after;
1017    }
1018  }
1019  out
1020}
1021
1022/// `CREATE [OR REPLACE] TRIGGER <name> {BEFORE|AFTER|INSTEAD OF}
1023/// {INSERT|UPDATE|DELETE|TRUNCATE} ON <tbl> FOR EACH {ROW|STATEMENT}
1024/// EXECUTE FUNCTION <fn>()` -> map<table_lower, Vec<Trigger>>.
1025fn scan_triggers(src: &str) -> std::collections::HashMap<String, Vec<Trigger>> {
1026  let upper = src.to_ascii_uppercase();
1027  let bytes = src.as_bytes();
1028  let mut out: std::collections::HashMap<String, Vec<Trigger>> = std::collections::HashMap::new();
1029  for needle in ["CREATE OR REPLACE TRIGGER ", "CREATE TRIGGER ", "CREATE CONSTRAINT TRIGGER "] {
1030    let mut from = 0usize;
1031    while let Some(rel) = upper[from..].find(needle) {
1032      let after = from + rel + needle.len();
1033      let mut k = after;
1034      while k < bytes.len() && bytes[k].is_ascii_whitespace() {
1035        k += 1;
1036      }
1037      let name_start = k;
1038      while k < bytes.len() && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'"') {
1039        k += 1;
1040      }
1041      let name = src[name_start..k].trim_matches('"').to_string();
1042      let stmt_end = src[after..].find(';').map(|i| after + i).unwrap_or(src.len());
1043      let stmt = &src[after..stmt_end];
1044      let stmt_upper = stmt.to_ascii_uppercase();
1045      let timing = if stmt_upper.contains("INSTEAD OF") {
1046        "INSTEAD OF"
1047      } else if stmt_upper.contains("BEFORE") {
1048        "BEFORE"
1049      } else if stmt_upper.contains("AFTER") {
1050        "AFTER"
1051      } else {
1052        ""
1053      };
1054      let mut events = Vec::new();
1055      if stmt_upper.contains("INSERT") {
1056        events.push("INSERT");
1057      }
1058      if stmt_upper.contains("UPDATE") {
1059        events.push("UPDATE");
1060      }
1061      if stmt_upper.contains("DELETE") {
1062        events.push("DELETE");
1063      }
1064      if stmt_upper.contains("TRUNCATE") {
1065        events.push("TRUNCATE");
1066      }
1067      let event = events.join(" OR ");
1068      // `ON <tbl>`.
1069      let tbl = if let Some(on_at) = stmt_upper.find(" ON ") {
1070        let after_on = on_at + 4;
1071        let rest = &stmt[after_on..];
1072        let lead = rest.len() - rest.trim_start().len();
1073        let raw = &rest[lead..];
1074        let id_end =
1075          raw.find(|c: char| !c.is_ascii_alphanumeric() && c != '_' && c != '.' && c != '"').unwrap_or(raw.len());
1076        raw[..id_end].rsplit('.').next().unwrap_or(&raw[..id_end]).trim_matches('"').to_ascii_lowercase()
1077      } else {
1078        String::new()
1079      };
1080      let granularity = if stmt_upper.contains("FOR EACH ROW") {
1081        "ROW"
1082      } else if stmt_upper.contains("FOR EACH STATEMENT") {
1083        "STATEMENT"
1084      } else {
1085        "ROW"
1086      };
1087      let function =
1088        if let Some(at) = stmt_upper.find("EXECUTE FUNCTION ").or_else(|| stmt_upper.find("EXECUTE PROCEDURE ")) {
1089          let after_kw = at + "EXECUTE FUNCTION ".len();
1090          let rest = &stmt[after_kw..];
1091          let lead = rest.len() - rest.trim_start().len();
1092          let raw = &rest[lead..];
1093          let id_end = raw.find(|c: char| !c.is_ascii_alphanumeric() && c != '_' && c != '.').unwrap_or(raw.len());
1094          raw[..id_end].to_string()
1095        } else {
1096          String::new()
1097        };
1098      if !tbl.is_empty() {
1099        out.entry(tbl).or_default().push(Trigger {
1100          name,
1101          timing: timing.to_string(),
1102          event,
1103          granularity: granularity.to_string(),
1104          function,
1105        });
1106      }
1107      from = after;
1108    }
1109  }
1110  out
1111}
1112
1113/// Harvest `COMMENT ON COLUMN <tbl>.<col> IS '<text>'` statements
1114/// (case-insensitive) from raw source. Returns a `tbl.col -> text` map
1115/// (both keys lowercased) so attach_column_comments can match without
1116/// a second pass. Permissive scan -- malformed/half-written input is
1117/// silently skipped.
1118fn scan_column_comments(src: &str) -> std::collections::HashMap<String, String> {
1119  let upper = src.to_ascii_uppercase();
1120  let mut out = std::collections::HashMap::new();
1121  let mut from = 0usize;
1122  while let Some(rel) = upper[from..].find("COMMENT ON COLUMN ") {
1123    let after = from + rel + "COMMENT ON COLUMN ".len();
1124    let bytes = src.as_bytes();
1125    let mut k = after;
1126    while k < bytes.len() && bytes[k].is_ascii_whitespace() {
1127      k += 1;
1128    }
1129    let id_start = k;
1130    while k < bytes.len()
1131      && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'.' || bytes[k] == b'"')
1132    {
1133      k += 1;
1134    }
1135    let id = &src[id_start..k];
1136    // Need at least `table.column` (two segments).
1137    let parts: Vec<&str> = id.split('.').map(|s| s.trim_matches('"')).collect();
1138    if parts.len() < 2 {
1139      from = after;
1140      continue;
1141    }
1142    let tbl = parts[parts.len() - 2].to_ascii_lowercase();
1143    let col = parts[parts.len() - 1].to_ascii_lowercase();
1144    // Look for `IS '<text>'`.
1145    while k < bytes.len() && bytes[k].is_ascii_whitespace() {
1146      k += 1;
1147    }
1148    if k + 2 > bytes.len() || !starts_with_at(&upper, k, "IS") {
1149      from = after;
1150      continue;
1151    }
1152    k += 2;
1153    while k < bytes.len() && bytes[k].is_ascii_whitespace() {
1154      k += 1;
1155    }
1156    if k >= bytes.len() || bytes[k] != b'\'' {
1157      from = after;
1158      continue;
1159    }
1160    let s_start = k + 1;
1161    let mut s_end = s_start;
1162    while s_end < bytes.len() && bytes[s_end] != b'\'' {
1163      s_end += 1;
1164    }
1165    if s_end >= bytes.len() {
1166      break;
1167    }
1168    let text = src[s_start..s_end].to_string();
1169    out.insert(format!("{tbl}.{col}"), text);
1170    from = s_end + 1;
1171  }
1172  out
1173}
1174
1175/// Locate the CREATE TABLE body for `name` and pull out every
1176/// PK/UNIQUE/FK constraint we can spot. Both inline-on-column and
1177/// table-level forms. Forgiving -- text-scan, not a real parser.
1178/// Walks the body of `CREATE TABLE <name> (...)` and pulls the
1179/// `GENERATED ALWAYS AS (expr) STORED` clause for each column that
1180/// has one. Returns a map of lowercase column name -> the expr
1181/// text (without the keywords).
1182fn scan_generated_for(src: &str, name: &str) -> std::collections::HashMap<String, String> {
1183  let upper = src.to_ascii_uppercase();
1184  let mut out: std::collections::HashMap<String, String> = Default::default();
1185  for needle in ["CREATE TABLE IF NOT EXISTS ", "CREATE TEMP TABLE ", "CREATE TEMPORARY TABLE ", "CREATE TABLE "] {
1186    let mut from = 0usize;
1187    while let Some(rel) = upper[from..].find(needle) {
1188      let after = from + rel + needle.len();
1189      let bytes = src.as_bytes();
1190      let mut k = after;
1191      while k < bytes.len() && bytes[k].is_ascii_whitespace() {
1192        k += 1
1193      }
1194      let id_start = k;
1195      while k < bytes.len()
1196        && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'.' || bytes[k] == b'"')
1197      {
1198        k += 1
1199      }
1200      let raw = &src[id_start..k];
1201      let bare = raw.rsplit('.').next().unwrap_or(raw).trim_matches('"');
1202      if !bare.eq_ignore_ascii_case(name) {
1203        from = after;
1204        continue;
1205      }
1206      while k < bytes.len() && bytes[k] != b'(' {
1207        k += 1
1208      }
1209      if k >= bytes.len() {
1210        return out;
1211      }
1212      let body_start = k + 1;
1213      let body_end = match_paren(bytes, k);
1214      let body = &src[body_start..body_end];
1215      for line in split_top_level(body) {
1216        let upper_line = line.to_ascii_uppercase();
1217        let Some(g_at) = upper_line.find("GENERATED") else { continue };
1218        let post = &line[g_at..];
1219        let post_upper = post.to_ascii_uppercase();
1220        if !post_upper.starts_with("GENERATED ALWAYS AS") {
1221          continue;
1222        }
1223        let after_kw = g_at + "GENERATED ALWAYS AS".len();
1224        let rest = line[after_kw..].trim_start();
1225        if !rest.starts_with('(') {
1226          continue;
1227        }
1228        let abs_open = after_kw + (line[after_kw..].len() - rest.len());
1229        let Some(close_rel) = match_paren_offset(&line, abs_open) else { continue };
1230        let expr = &line[abs_open + 1..close_rel];
1231        let col_name = line.split_whitespace().next().unwrap_or("").trim_matches('"').to_ascii_lowercase();
1232        if !col_name.is_empty() {
1233          out.insert(col_name, expr.to_string());
1234        }
1235      }
1236      return out;
1237    }
1238  }
1239  out
1240}
1241
1242/// Scan the source for `ALTER TABLE <name> OWNER TO <role>` and return
1243/// the last owner declaration. Matches the table name case-insensitively
1244/// and accepts both bare and quoted role names.
1245fn scan_owner_for(src: &str, name: &str) -> Option<String> {
1246  let upper = src.to_ascii_uppercase();
1247  let bytes = src.as_bytes();
1248  let needle = "ALTER TABLE";
1249  let mut from = 0usize;
1250  let mut last: Option<String> = None;
1251  while let Some(rel) = upper[from..].find(needle) {
1252    let at = from + rel;
1253    let mut k = at + needle.len();
1254    while k < bytes.len() && bytes[k].is_ascii_whitespace() {
1255      k += 1;
1256    }
1257    if starts_with_at(&upper, k, "IF EXISTS") {
1258      k += 9;
1259      while k < bytes.len() && bytes[k].is_ascii_whitespace() {
1260        k += 1;
1261      }
1262    }
1263    if starts_with_at(&upper, k, "ONLY") {
1264      k += 4;
1265      while k < bytes.len() && bytes[k].is_ascii_whitespace() {
1266        k += 1;
1267      }
1268    }
1269    let id_start = k;
1270    while k < bytes.len()
1271      && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'.' || bytes[k] == b'"')
1272    {
1273      k += 1;
1274    }
1275    let raw = &src[id_start..k];
1276    let bare = raw.rsplit('.').next().unwrap_or(raw).trim_matches('"');
1277    if !bare.eq_ignore_ascii_case(name) {
1278      from = at + needle.len();
1279      continue;
1280    }
1281    let after = upper[k..].trim_start();
1282    if !after.starts_with("OWNER TO") {
1283      from = k;
1284      continue;
1285    }
1286    let skip = " OWNER TO ".len();
1287    let owner_start_rel = upper[k..].find("OWNER TO")? + "OWNER TO".len();
1288    let owner_start = k + owner_start_rel;
1289    let mut j = owner_start;
1290    while j < bytes.len() && bytes[j].is_ascii_whitespace() {
1291      j += 1;
1292    }
1293    let id_start = j;
1294    while j < bytes.len() && (bytes[j].is_ascii_alphanumeric() || bytes[j] == b'_' || bytes[j] == b'"') {
1295      j += 1;
1296    }
1297    let role = src[id_start..j].trim_matches('"').to_string();
1298    if !role.is_empty() {
1299      last = Some(role);
1300    }
1301    let _ = skip;
1302    from = j;
1303  }
1304  last
1305}
1306
1307/// Walks the body of `CREATE TABLE <name> (...)` and extracts the
1308/// `DEFAULT <expr>` clause for each column definition. Returns a map of
1309/// lowercase column name -> the expression text (everything between
1310/// `DEFAULT` and the next comma / `NOT NULL` / `CHECK` / `REFERENCES` /
1311/// `UNIQUE` / `PRIMARY KEY` / `GENERATED` / end-of-line at depth 0).
1312fn scan_defaults_for(src: &str, name: &str) -> std::collections::HashMap<String, String> {
1313  let upper = src.to_ascii_uppercase();
1314  let mut out: std::collections::HashMap<String, String> = Default::default();
1315  for needle in ["CREATE TABLE IF NOT EXISTS ", "CREATE TEMP TABLE ", "CREATE TEMPORARY TABLE ", "CREATE TABLE "] {
1316    let mut from = 0usize;
1317    while let Some(rel) = upper[from..].find(needle) {
1318      let after = from + rel + needle.len();
1319      let bytes = src.as_bytes();
1320      let mut k = after;
1321      while k < bytes.len() && bytes[k].is_ascii_whitespace() {
1322        k += 1
1323      }
1324      let id_start = k;
1325      while k < bytes.len()
1326        && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'.' || bytes[k] == b'"')
1327      {
1328        k += 1
1329      }
1330      let raw = &src[id_start..k];
1331      let bare = raw.rsplit('.').next().unwrap_or(raw).trim_matches('"');
1332      if !bare.eq_ignore_ascii_case(name) {
1333        from = after;
1334        continue;
1335      }
1336      while k < bytes.len() && bytes[k] != b'(' {
1337        k += 1
1338      }
1339      if k >= bytes.len() {
1340        return out;
1341      }
1342      let body_start = k + 1;
1343      let body_end = match_paren(bytes, k);
1344      let body = &src[body_start..body_end];
1345      for line in split_top_level(body) {
1346        let upper_line = line.to_ascii_uppercase();
1347        // Skip table-level constraints.
1348        let head_upper = upper_line.split_whitespace().next().unwrap_or("");
1349        if matches!(head_upper, "CONSTRAINT" | "PRIMARY" | "UNIQUE" | "FOREIGN" | "CHECK" | "EXCLUDE" | "LIKE") {
1350          continue;
1351        }
1352        // Find a word-boundary `DEFAULT` at top paren depth.
1353        let line_bytes = line.as_bytes();
1354        let mut depth = 0i32;
1355        let mut def_at: Option<usize> = None;
1356        let mut i = 0usize;
1357        while i < line_bytes.len() {
1358          match line_bytes[i] {
1359            b'(' => depth += 1,
1360            b')' => depth -= 1,
1361            b'\'' => {
1362              i += 1;
1363              while i < line_bytes.len() && line_bytes[i] != b'\'' {
1364                i += 1;
1365              }
1366            },
1367            _ => {},
1368          }
1369          if depth == 0
1370            && starts_with_at(&upper_line, i, "DEFAULT")
1371            && (i == 0 || !line_bytes[i - 1].is_ascii_alphanumeric() && line_bytes[i - 1] != b'_')
1372            && i + 7 < line_bytes.len()
1373            && line_bytes[i + 7].is_ascii_whitespace()
1374          {
1375            def_at = Some(i);
1376            break;
1377          }
1378          i += 1;
1379        }
1380        let Some(def_at) = def_at else { continue };
1381        // Skip `DEFAULT` keyword + whitespace.
1382        let mut j = def_at + "DEFAULT".len();
1383        while j < line_bytes.len() && line_bytes[j].is_ascii_whitespace() {
1384          j += 1;
1385        }
1386        // Walk the expression to its terminator: comma at depth 0 OR
1387        // one of the trailing column-level keywords (NOT NULL, CHECK,
1388        // REFERENCES, UNIQUE, PRIMARY KEY, GENERATED, COLLATE).
1389        let mut depth = 0i32;
1390        let expr_start = j;
1391        let mut expr_end = line_bytes.len();
1392        while j < line_bytes.len() {
1393          match line_bytes[j] {
1394            b'(' => depth += 1,
1395            b')' => depth -= 1,
1396            b'\'' => {
1397              j += 1;
1398              while j < line_bytes.len() && line_bytes[j] != b'\'' {
1399                j += 1;
1400              }
1401            },
1402            _ => {},
1403          }
1404          if depth == 0
1405            && (line_bytes[j] == b','
1406              || [" NOT NULL", " CHECK", " REFERENCES", " UNIQUE", " PRIMARY KEY", " GENERATED", " COLLATE"]
1407                .iter()
1408                .any(|kw| starts_with_at(&upper_line, j, kw)))
1409          {
1410            expr_end = j;
1411            break;
1412          }
1413          j += 1;
1414        }
1415        let expr = line[expr_start..expr_end].trim().to_string();
1416        let col_name = line.split_whitespace().next().unwrap_or("").trim_matches('"').to_ascii_lowercase();
1417        if !col_name.is_empty() && !expr.is_empty() {
1418          out.insert(col_name, expr);
1419        }
1420      }
1421      return out;
1422    }
1423  }
1424  out
1425}
1426
1427/// `-- @json-keys: a, b, c` annotation comment placed on its own line
1428/// above a jsonb column. Walks the CREATE TABLE body line-by-line,
1429/// remembers the most recent annotation, and attaches its keys to the
1430/// next non-comment, non-constraint column line. Returns
1431/// lowercase-column-name -> Vec<String>.
1432fn scan_json_keys_for(src: &str, name: &str) -> std::collections::HashMap<String, Vec<String>> {
1433  let upper = src.to_ascii_uppercase();
1434  let mut out: std::collections::HashMap<String, Vec<String>> = Default::default();
1435  for needle in ["CREATE TABLE IF NOT EXISTS ", "CREATE TEMP TABLE ", "CREATE TEMPORARY TABLE ", "CREATE TABLE "] {
1436    let mut from = 0usize;
1437    while let Some(rel) = upper[from..].find(needle) {
1438      let after = from + rel + needle.len();
1439      let bytes = src.as_bytes();
1440      let mut k = after;
1441      while k < bytes.len() && bytes[k].is_ascii_whitespace() {
1442        k += 1
1443      }
1444      let id_start = k;
1445      while k < bytes.len()
1446        && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'.' || bytes[k] == b'"')
1447      {
1448        k += 1
1449      }
1450      let raw = &src[id_start..k];
1451      let bare = raw.rsplit('.').next().unwrap_or(raw).trim_matches('"');
1452      if !bare.eq_ignore_ascii_case(name) {
1453        from = after;
1454        continue;
1455      }
1456      while k < bytes.len() && bytes[k] != b'(' {
1457        k += 1
1458      }
1459      if k >= bytes.len() {
1460        return out;
1461      }
1462      let body_start = k + 1;
1463      let body_end = match_paren(bytes, k);
1464      let body = &src[body_start..body_end];
1465      let mut pending: Option<Vec<String>> = None;
1466      let mut paren_depth = 0i32;
1467      for line in body.lines() {
1468        let trimmed = line.trim();
1469        if let Some(at) = trimmed.find("@json-keys:") {
1470          let payload = &trimmed[at + "@json-keys:".len()..];
1471          let keys: Vec<String> =
1472            payload.split(',').map(|s| s.trim().trim_matches('"').to_string()).filter(|s| !s.is_empty()).collect();
1473          if !keys.is_empty() {
1474            pending = Some(keys);
1475          }
1476          continue;
1477        }
1478        if trimmed.is_empty() || trimmed.starts_with("--") {
1479          continue;
1480        }
1481        let entered_at = paren_depth;
1482        for b in trimmed.as_bytes() {
1483          match b {
1484            b'(' => paren_depth += 1,
1485            b')' => paren_depth -= 1,
1486            _ => {},
1487          }
1488        }
1489        if entered_at != 0 {
1490          continue;
1491        }
1492        let Some(keys) = pending.take() else { continue };
1493        let head = trimmed.split_whitespace().next().unwrap_or("");
1494        let head_upper = head.to_ascii_uppercase();
1495        if matches!(head_upper.as_str(), "CONSTRAINT" | "PRIMARY" | "UNIQUE" | "FOREIGN" | "CHECK" | "EXCLUDE" | "LIKE")
1496        {
1497          continue;
1498        }
1499        let col_name = head.trim_matches('"').trim_end_matches(',').to_ascii_lowercase();
1500        if !col_name.is_empty() {
1501          out.insert(col_name, keys);
1502        }
1503      }
1504      return out;
1505    }
1506  }
1507  out
1508}
1509
1510fn split_top_level(text: &str) -> Vec<String> {
1511  let mut out = Vec::new();
1512  let bytes = text.as_bytes();
1513  let mut depth = 0i32;
1514  let mut start = 0usize;
1515  let mut i = 0usize;
1516  while i < bytes.len() {
1517    match bytes[i] {
1518      b'(' => depth += 1,
1519      b')' => depth -= 1,
1520      b',' if depth == 0 => {
1521        out.push(text[start..i].trim().to_string());
1522        start = i + 1;
1523      },
1524      b'\'' => {
1525        i += 1;
1526        while i < bytes.len() && bytes[i] != b'\'' {
1527          i += 1
1528        }
1529      },
1530      _ => {},
1531    }
1532    i += 1;
1533  }
1534  out.push(text[start..].trim().to_string());
1535  out
1536}
1537
1538fn match_paren_offset(s: &str, open: usize) -> Option<usize> {
1539  let bytes = s.as_bytes();
1540  let mut depth = 0i32;
1541  let mut i = open;
1542  while i < bytes.len() {
1543    match bytes[i] {
1544      b'(' => depth += 1,
1545      b')' => {
1546        depth -= 1;
1547        if depth == 0 {
1548          return Some(i);
1549        }
1550      },
1551      b'\'' => {
1552        i += 1;
1553        while i < bytes.len() && bytes[i] != b'\'' {
1554          i += 1
1555        }
1556      },
1557      _ => {},
1558    }
1559    i += 1;
1560  }
1561  None
1562}
1563
1564fn scan_constraints_for(src: &str, name: &str) -> Vec<Constraint> {
1565  let upper = src.to_ascii_uppercase();
1566  let mut out: Vec<Constraint> = Vec::new();
1567  for needle in ["CREATE TABLE IF NOT EXISTS ", "CREATE TEMP TABLE ", "CREATE TEMPORARY TABLE ", "CREATE TABLE "] {
1568    let mut from = 0usize;
1569    while let Some(rel) = upper[from..].find(needle) {
1570      let after = from + rel + needle.len();
1571      let bytes = src.as_bytes();
1572      let mut k = after;
1573      while k < bytes.len() && bytes[k].is_ascii_whitespace() {
1574        k += 1;
1575      }
1576      let id_start = k;
1577      while k < bytes.len()
1578        && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'.' || bytes[k] == b'"')
1579      {
1580        k += 1;
1581      }
1582      let raw = &src[id_start..k];
1583      let bare = raw.rsplit('.').next().unwrap_or(raw).trim_matches('"');
1584      if !bare.eq_ignore_ascii_case(name) {
1585        from = after;
1586        continue;
1587      }
1588      while k < bytes.len() && bytes[k] != b'(' {
1589        k += 1;
1590      }
1591      if k >= bytes.len() {
1592        return out;
1593      }
1594      let body_start = k + 1;
1595      let body_end = match_paren(bytes, k);
1596      let body = &src[body_start..body_end];
1597      out.extend(parse_constraints(body));
1598      return out;
1599    }
1600  }
1601  out
1602}
1603
1604fn match_paren(bytes: &[u8], open: usize) -> usize {
1605  let n = bytes.len();
1606  let mut depth = 0i32;
1607  let mut i = open;
1608  while i < n {
1609    match bytes[i] {
1610      b'(' => depth += 1,
1611      b')' => {
1612        depth -= 1;
1613        if depth == 0 {
1614          return i;
1615        }
1616      },
1617      b'\'' => {
1618        i += 1;
1619        while i < n && bytes[i] != b'\'' {
1620          i += 1;
1621        }
1622      },
1623      _ => {},
1624    }
1625    i += 1;
1626  }
1627  n
1628}
1629
1630fn parse_constraints(body: &str) -> Vec<Constraint> {
1631  let mut out = Vec::new();
1632  for entry in split_top_commas(body) {
1633    // Skip any leading `-- ...` / `/* ... */` lines so the constraint
1634    // parser sees the real declaration. Without this, a comment between
1635    // a column row's trailing `,` and a CONSTRAINT line ends up as the
1636    // entry's prefix and breaks name extraction (`pk_--` etc).
1637    let mut entry = entry;
1638    loop {
1639      let t = entry.trim_start_matches(['\n', '\r', ' ', '\t']);
1640      if let Some(rest) = t.strip_prefix("--") {
1641        let end = rest.find('\n').map(|i| i + 1).unwrap_or(rest.len());
1642        entry = &rest[end..];
1643        continue;
1644      }
1645      if t.starts_with("/*")
1646        && let Some(close) = t.find("*/")
1647      {
1648        entry = &t[close + 2..];
1649        continue;
1650      }
1651      entry = t;
1652      break;
1653    }
1654    let trimmed = entry.trim();
1655    if trimmed.is_empty() {
1656      continue;
1657    }
1658    let upper = trimmed.to_ascii_uppercase();
1659    // Table-level: PRIMARY KEY (...), UNIQUE (...), FOREIGN KEY (...) REFERENCES ...
1660    if upper.starts_with("PRIMARY KEY") {
1661      if let Some(cols) = paren_csv(trimmed) {
1662        out.push(Constraint {
1663          name: "pk".into(),
1664          kind: ConstraintKind::PrimaryKey,
1665          columns: cols,
1666          references: None,
1667          definition: Some(trimmed.to_string()),
1668          inline: false,
1669        });
1670      }
1671      continue;
1672    }
1673    if upper.starts_with("UNIQUE") {
1674      if let Some(cols) = paren_csv(trimmed) {
1675        out.push(Constraint {
1676          name: "uniq".into(),
1677          kind: ConstraintKind::Unique,
1678          columns: cols,
1679          references: None,
1680          definition: Some(trimmed.to_string()),
1681          inline: false,
1682        });
1683      }
1684      continue;
1685    }
1686    if upper.starts_with("FOREIGN KEY") {
1687      if let (Some(local), Some(refs)) = (paren_csv(trimmed), parse_references(trimmed)) {
1688        out.push(Constraint {
1689          name: "fk".into(),
1690          kind: ConstraintKind::ForeignKey,
1691          columns: local,
1692          references: Some(refs),
1693          definition: Some(trimmed.to_string()),
1694          inline: false,
1695        });
1696      }
1697      continue;
1698    }
1699    if upper.starts_with("CHECK") {
1700      out.push(Constraint {
1701        name: "ck".into(),
1702        kind: ConstraintKind::Check,
1703        columns: Vec::new(),
1704        references: None,
1705        definition: Some(trimmed.to_string()),
1706        inline: false,
1707      });
1708      continue;
1709    }
1710    if upper.starts_with("CONSTRAINT") {
1711      // CONSTRAINT <name> {PK|UNIQUE|FK|CHECK} ...
1712      let rest = trimmed[10..].trim_start();
1713      let name_end = rest.find(char::is_whitespace).unwrap_or(rest.len());
1714      let cname = rest[..name_end].trim_matches('"').to_string();
1715      let body = rest[name_end..].trim_start();
1716      let body_upper = body.to_ascii_uppercase();
1717      if body_upper.starts_with("PRIMARY KEY") {
1718        out.push(Constraint {
1719          name: cname,
1720          kind: ConstraintKind::PrimaryKey,
1721          columns: paren_csv(body).unwrap_or_default(),
1722          references: None,
1723          definition: Some(body.to_string()),
1724          inline: false,
1725        });
1726      } else if body_upper.starts_with("UNIQUE") {
1727        out.push(Constraint {
1728          name: cname,
1729          kind: ConstraintKind::Unique,
1730          columns: paren_csv(body).unwrap_or_default(),
1731          references: None,
1732          definition: Some(body.to_string()),
1733          inline: false,
1734        });
1735      } else if body_upper.starts_with("FOREIGN KEY") {
1736        out.push(Constraint {
1737          name: cname,
1738          kind: ConstraintKind::ForeignKey,
1739          columns: paren_csv(body).unwrap_or_default(),
1740          references: parse_references(body),
1741          definition: Some(body.to_string()),
1742          inline: false,
1743        });
1744      } else if body_upper.starts_with("CHECK") {
1745        out.push(Constraint {
1746          name: cname,
1747          kind: ConstraintKind::Check,
1748          columns: Vec::new(),
1749          references: None,
1750          definition: Some(body.to_string()),
1751          inline: false,
1752        });
1753      }
1754      continue;
1755    }
1756    // Inline column form: `<col> <type> ... [PRIMARY KEY] [REFERENCES ...]`
1757    let col = unquote_ident(trimmed.split_whitespace().next().unwrap_or("")).to_string();
1758    if col.is_empty() {
1759      continue;
1760    }
1761    if upper.contains(" PRIMARY KEY") || upper.contains("\tPRIMARY KEY") {
1762      out.push(Constraint {
1763        name: format!("pk_{col}"),
1764        kind: ConstraintKind::PrimaryKey,
1765        columns: vec![col.clone()],
1766        references: None,
1767        definition: Some(format!("PRIMARY KEY ({col})")),
1768        inline: true,
1769      });
1770    }
1771    if upper.contains("UNIQUE") && !upper.starts_with("UNIQUE") {
1772      out.push(Constraint {
1773        name: format!("uniq_{col}"),
1774        kind: ConstraintKind::Unique,
1775        columns: vec![col.clone()],
1776        references: None,
1777        definition: Some(format!("UNIQUE ({col})")),
1778        inline: true,
1779      });
1780    }
1781    if let Some(refs) = inline_references(trimmed) {
1782      let def = format!("FOREIGN KEY ({col}) REFERENCES {}.{} ({})", refs.schema, refs.table, refs.columns.join(", "));
1783      out.push(Constraint {
1784        name: format!("fk_{col}"),
1785        kind: ConstraintKind::ForeignKey,
1786        columns: vec![col],
1787        references: Some(refs),
1788        definition: Some(def),
1789        inline: true,
1790      });
1791    }
1792  }
1793  out
1794}
1795
1796/// `(col1, col2)` -> `["col1", "col2"]`. None if no first `(`.
1797fn paren_csv(s: &str) -> Option<Vec<String>> {
1798  let open = s.find('(')?;
1799  let close = match_paren(s.as_bytes(), open);
1800  if close >= s.len() {
1801    return None;
1802  }
1803  Some(
1804    s.get(open + 1..close)?.split(',').map(|c| unquote_ident(c.trim()).to_string()).filter(|c| !c.is_empty()).collect(),
1805  )
1806}
1807
1808/// `REFERENCES <tbl>(<col>)` or `REFERENCES <schema>.<tbl>(<col>)`.
1809fn parse_references(s: &str) -> Option<ConstraintRef> {
1810  let upper = s.to_ascii_uppercase();
1811  let at = upper.find("REFERENCES")?;
1812  let rest = s[at + "REFERENCES".len()..].trim_start();
1813  let name_end = rest
1814    .find(|c: char| !c.is_ascii_alphanumeric() && c != '_' && c != '.' && c != '"' && c != '[' && c != ']' && c != '`')
1815    .unwrap_or(rest.len());
1816  // Unquote each part rather than the whole: `[dbo].[users]` opens and closes
1817  // with brackets, so stripping the outside first would leave `dbo].[users`.
1818  let raw = rest.get(..name_end).unwrap_or("");
1819  let unquoted = unquote_ident(raw);
1820  // A dot only separates when it is not inside the quoting: `[my.table]` is one
1821  // table whose name has a dot in it, not `my` dot `table`.
1822  let dot = if unquoted == raw { raw.find('.') } else { None };
1823  let (schema, table) = match dot {
1824    Some(at) => (unquote_ident(&raw[..at]).to_string(), unquote_ident(&raw[at + 1..]).to_string()),
1825    None => ("public".into(), unquoted.to_string()),
1826  };
1827  let cols = paren_csv(&rest[name_end..]).unwrap_or_default();
1828  Some(ConstraintRef { schema, table, columns: cols })
1829}
1830
1831fn inline_references(s: &str) -> Option<ConstraintRef> {
1832  if !s.to_ascii_uppercase().contains("REFERENCES") {
1833    return None;
1834  }
1835  parse_references(s)
1836}
1837
1838/// The conventional roles that exist in any fresh Postgres install.
1839/// Returned as a Vec so callers can union them into the offline cat.
1840pub fn default_offline_roles() -> Vec<String> {
1841  vec![
1842    "postgres".into(),
1843    "pg_read_all_data".into(),
1844    "pg_write_all_data".into(),
1845    "pg_monitor".into(),
1846    "pg_signal_backend".into(),
1847  ]
1848}
1849
1850fn scan_sequences(src: &str) -> Vec<Sequence> {
1851  scan_create_named(src, &["CREATE SEQUENCE "])
1852    .into_iter()
1853    .map(|name| Sequence {
1854      schema: "public".into(),
1855      name,
1856      data_type: "bigint".into(),
1857      start_value: 1,
1858      min_value: 1,
1859      max_value: i64::MAX,
1860      increment_by: 1,
1861      cycle: false,
1862      owned_by_column: None,
1863      comment: Some("defined in current buffer".into()),
1864    })
1865    .collect()
1866}
1867
1868fn scan_extensions(src: &str) -> Vec<Extension> {
1869  let mut out = Vec::new();
1870  for name in scan_create_named(src, &["CREATE EXTENSION IF NOT EXISTS ", "CREATE EXTENSION "]) {
1871    out.push(Extension {
1872      name: name.trim_matches('"').to_string(),
1873      schema: "public".into(),
1874      version: "?".into(),
1875      comment: Some("declared in current buffer".into()),
1876    });
1877  }
1878  out
1879}
1880
1881fn scan_types(src: &str) -> Vec<Type> {
1882  let mut out = Vec::new();
1883  let upper = src.to_ascii_uppercase();
1884  for (prefix, kind) in [("CREATE TYPE ", TypeKind::Composite), ("CREATE DOMAIN ", TypeKind::Domain)] {
1885    for name in scan_create_named_with_upper(src, &upper, prefix) {
1886      // Peek a few chars past the name for AS ENUM -> Enum.
1887      let resolved_kind = if name_followed_by_as_enum(src, &upper, prefix, &name) { TypeKind::Enum } else { kind };
1888      out.push(Type { schema: "public".into(), name, kind: resolved_kind });
1889    }
1890  }
1891  out
1892}
1893
1894fn name_followed_by_as_enum(src: &str, upper: &str, prefix: &str, name: &str) -> bool {
1895  let needle = format!("{prefix}{name}");
1896  let needle_upper = needle.to_ascii_uppercase();
1897  let Some(rel) = upper.find(&needle_upper) else {
1898    return false;
1899  };
1900  let after = rel + needle.len();
1901  let tail = src[after..].trim_start();
1902  tail.to_ascii_uppercase().starts_with("AS ENUM")
1903}
1904
1905/// Pull the type out of `RETURNS <type>` in a CREATE FUNCTION DDL.
1906/// Stops at `AS`, `LANGUAGE`, `$$`, or newline. Returns "?" when
1907/// the clause isn't found.
1908/// Parse the `(arg_name arg_type, ...)` block immediately after the
1909/// function name. Used by both completion (hover signature) and
1910/// sql513 arg-count validation. Best effort: bails (returns empty
1911/// vec) on malformed input.
1912fn extract_arguments(src: &str, name_end: usize) -> Vec<FunctionArg> {
1913  let bytes = src.as_bytes();
1914  let n = bytes.len();
1915  let mut i = name_end;
1916  while i < n && bytes[i].is_ascii_whitespace() {
1917    i += 1;
1918  }
1919  if i >= n || bytes[i] != b'(' {
1920    return Vec::new();
1921  }
1922  let Some(close) = match_paren_opt(bytes, i) else { return Vec::new() };
1923  let body = &src[i + 1..close];
1924  if body.trim().is_empty() {
1925    return Vec::new();
1926  }
1927  let mut out = Vec::new();
1928  for raw in split_top_level_commas(body) {
1929    let trimmed = raw.trim();
1930    if trimmed.is_empty() {
1931      continue;
1932    }
1933    // Strip optional argmode prefix (IN / OUT / INOUT / VARIADIC).
1934    let mut rest = trimmed;
1935    let upper = rest.to_ascii_uppercase();
1936    for mode in ["VARIADIC ", "INOUT ", "IN ", "OUT "] {
1937      if upper.starts_with(mode) {
1938        rest = rest[mode.len()..].trim_start();
1939        break;
1940      }
1941    }
1942    // Tokenize: first token might be name or type; if there's a
1943    // second token, name=first, type=rest.
1944    let (name, ty) = match rest.find(char::is_whitespace) {
1945      Some(at) => {
1946        let head = &rest[..at];
1947        let tail = rest[at..].trim();
1948        if tail.is_empty() { (None, head.to_string()) } else { (Some(head.to_string()), tail.to_string()) }
1949      },
1950      None => (None, rest.to_string()),
1951    };
1952    // Preserve DEFAULT clause marker inside data_type so sql513 can
1953    // see it for the arity-min computation.
1954    let data_type = if ty.is_empty() { "?".to_string() } else { ty };
1955    out.push(FunctionArg { name, data_type });
1956  }
1957  out
1958}
1959
1960fn extract_returns(ddl: &str) -> String {
1961  let upper = ddl.to_ascii_uppercase();
1962  let Some(at) = upper.find(" RETURNS ") else { return "?".into() };
1963  let after = at + 9;
1964  let rest = &ddl[after..];
1965  let stop = rest
1966    .find('\n')
1967    .or_else(|| {
1968      let u = rest.to_ascii_uppercase();
1969      u.find(" AS ").or_else(|| u.find(" LANGUAGE "))
1970    })
1971    .unwrap_or(rest.len());
1972  let raw = rest[..stop].trim();
1973  // Drop trailing punctuation / TABLE() schema noise.
1974  let bare = raw.split_whitespace().next().unwrap_or(raw).trim_end_matches(',');
1975  if bare.is_empty() { "?".into() } else { bare.to_string() }
1976}
1977
1978fn scan_functions(src: &str) -> Vec<Function> {
1979  let upper = src.to_ascii_uppercase();
1980  let bytes = src.as_bytes();
1981  let mut out = Vec::new();
1982  for prefix in [
1983    "CREATE OR REPLACE FUNCTION ",
1984    "CREATE FUNCTION ",
1985    "CREATE PROCEDURE ",
1986    "CREATE OR REPLACE PROCEDURE ",
1987    "CREATE AGGREGATE ",
1988    "CREATE OR REPLACE AGGREGATE ",
1989  ] {
1990    let mut from = 0usize;
1991    while let Some(rel) = upper[from..].find(prefix) {
1992      let stmt_start = from + rel;
1993      let after = stmt_start + prefix.len();
1994      let mut k = after;
1995      while k < bytes.len() && bytes[k].is_ascii_whitespace() {
1996        k += 1;
1997      }
1998      let name_start = k;
1999      while k < bytes.len() && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'.') {
2000        k += 1;
2001      }
2002      if k == name_start {
2003        from = after;
2004        continue;
2005      }
2006      let raw = &src[name_start..k];
2007      // `app.current_user_id` -> schema=app, name=current_user_id.
2008      // Bare `foo` -> schema=public, name=foo.
2009      let (schema, bare) = match raw.rsplit_once('.') {
2010        Some((s, n)) => (s.trim_matches('"').to_ascii_lowercase(), n.trim_matches('"').to_string()),
2011        None => ("public".into(), raw.to_string()),
2012      };
2013      // Find the full DDL up to the matching `;` after the dollar-
2014      // quoted body (or just up to the end of file if unterminated).
2015      let stmt_end = find_function_end(src, k);
2016      let full_ddl = src[stmt_start..stmt_end].trim().to_string();
2017      // Extract RETURNS <type> from the DDL header (before AS $$).
2018      let return_type = extract_returns(&full_ddl);
2019      // Extract arg signature from `<name>(<args>)` after the name.
2020      let arguments = extract_arguments(src, k);
2021      out.push(Function {
2022        schema,
2023        name: bare,
2024        arguments,
2025        return_type,
2026        // Render layer reads `comment` for the source block when it
2027        // starts with CREATE; ship the full DDL so the hover shows
2028        // the body inline.
2029        comment: Some(full_ddl),
2030      });
2031      from = after;
2032    }
2033  }
2034  out
2035}
2036
2037/// Walk from `pos` to the end of the CREATE FUNCTION statement.
2038/// Respects the dollar-quoted body so `;` inside `$$ ... $$` doesn't
2039/// terminate the scan prematurely.
2040fn find_function_end(src: &str, start: usize) -> usize {
2041  let bytes = src.as_bytes();
2042  let n = bytes.len();
2043  let mut i = start;
2044  let mut in_dollar: Option<String> = None;
2045  while i < n {
2046    if let Some(tag) = &in_dollar {
2047      if i + tag.len() <= n && src.as_bytes()[i..i + tag.len()] == *tag.as_bytes() {
2048        i += tag.len();
2049        in_dollar = None;
2050        continue;
2051      }
2052      i += 1;
2053      continue;
2054    }
2055    let c = bytes[i];
2056    if c == b'$' {
2057      let mut j = i + 1;
2058      while j < n && (bytes[j].is_ascii_alphanumeric() || bytes[j] == b'_') {
2059        j += 1;
2060      }
2061      if j < n && bytes[j] == b'$' {
2062        in_dollar = Some(src[i..=j].to_string());
2063        i = j + 1;
2064        continue;
2065      }
2066    }
2067    if c == b';' {
2068      return i + 1;
2069    }
2070    if c == b'\'' {
2071      i += 1;
2072      while i < n && bytes[i] != b'\'' {
2073        i += 1;
2074      }
2075      i = (i + 1).min(n);
2076      continue;
2077    }
2078    i += 1;
2079  }
2080  n
2081}
2082
2083/// Scan the source for `INSERT INTO <table> [(...)] VALUES (...), (...)`
2084/// occurrences and return how many tuples land in each table. Keys are
2085/// lowercased table names (bare; schema prefix stripped). Used by
2086/// Tally signed row deltas per table from DML statements in `src`:
2087///   - INSERT INTO t VALUES (...), (...), (...)  -> +tuples
2088///   - INSERT INTO t (cols) SELECT FROM generate_series(a, b) -> +(b-a+1)
2089///   - DELETE FROM t                              -> -prior_total  (clears)
2090///   - DELETE FROM t WHERE ...                    -> -1  (best effort)
2091///   - TRUNCATE TABLE t / TRUNCATE t              -> -prior_total  (clears)
2092///
2093/// Re-runs on every parse so the codelens row-count chip updates as
2094/// the user types more INSERT/DELETE statements in the same buffer.
2095fn scan_dml_row_deltas(src: &str) -> std::collections::HashMap<String, i64> {
2096  // Merge INSERT + DELETE + TRUNCATE events with their source-byte
2097  // position, sort, then walk chronologically so a TRUNCATE in the
2098  // middle wipes the prior count but doesn't erase subsequent inserts.
2099  let mut events: Vec<(usize, String, Event)> = Vec::new();
2100  for (pos, name, n) in scan_insert_inserts(src) {
2101    events.push((pos, name.to_ascii_lowercase(), Event::Insert(n)));
2102  }
2103  for (pos, name, kind) in scan_clears(src) {
2104    events.push((pos, name.to_ascii_lowercase(), Event::Clear(kind)));
2105  }
2106  events.sort_by_key(|(p, _, _)| *p);
2107  let mut out: std::collections::HashMap<String, i64> = Default::default();
2108  for (_, key, e) in events {
2109    match e {
2110      Event::Insert(n) => {
2111        *out.entry(key).or_insert(0) += n;
2112      },
2113      Event::Clear(ClearKind::Truncate | ClearKind::DeleteAll) => {
2114        out.insert(key, 0);
2115      },
2116      Event::Clear(ClearKind::DeleteWhere) => {
2117        let entry = out.entry(key).or_insert(0);
2118        *entry = (*entry - 1).max(0);
2119      },
2120    }
2121  }
2122  out
2123}
2124
2125enum Event {
2126  Insert(i64),
2127  Clear(ClearKind),
2128}
2129
2130enum ClearKind {
2131  Truncate,
2132  DeleteAll,
2133  DeleteWhere,
2134}
2135
2136/// Find every TRUNCATE / DELETE statement and return (table, kind).
2137fn scan_clears(src: &str) -> Vec<(usize, String, ClearKind)> {
2138  let upper = src.to_ascii_uppercase();
2139  let bytes = src.as_bytes();
2140  let n = bytes.len();
2141  let mut out = Vec::new();
2142  // TRUNCATE [TABLE] [ONLY] <name>[, <name>...]
2143  let mut from = 0usize;
2144  while let Some(rel) = upper[from..].find("TRUNCATE") {
2145    let at = from + rel;
2146    let prev_ok = at == 0 || !is_word_char(bytes[at - 1]);
2147    if !prev_ok {
2148      from = at + 8;
2149      continue;
2150    }
2151    let mut i = at + "TRUNCATE".len();
2152    while i < n && bytes[i].is_ascii_whitespace() {
2153      i += 1;
2154    }
2155    if starts_with_at(&upper, i, "TABLE") && i + 5 < n && bytes[i + 5].is_ascii_whitespace() {
2156      i += 5;
2157      while i < n && bytes[i].is_ascii_whitespace() {
2158        i += 1;
2159      }
2160    }
2161    loop {
2162      if starts_with_at(&upper, i, "ONLY") && i + 4 < n && bytes[i + 4].is_ascii_whitespace() {
2163        i += 4;
2164        while i < n && bytes[i].is_ascii_whitespace() {
2165          i += 1;
2166        }
2167      }
2168      let id_start = i;
2169      while i < n && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_' || bytes[i] == b'.' || bytes[i] == b'"') {
2170        i += 1;
2171      }
2172      if i > id_start {
2173        let raw = &src[id_start..i];
2174        let bare = raw.rsplit('.').next().unwrap_or(raw).trim_matches('"').to_string();
2175        if !bare.is_empty() {
2176          out.push((at, bare, ClearKind::Truncate));
2177        }
2178      }
2179      while i < n && bytes[i].is_ascii_whitespace() {
2180        i += 1;
2181      }
2182      if i < n && bytes[i] == b',' {
2183        i += 1;
2184        while i < n && bytes[i].is_ascii_whitespace() {
2185          i += 1;
2186        }
2187        continue;
2188      }
2189      break;
2190    }
2191    from = i.max(at + 8);
2192  }
2193  // DELETE FROM [ONLY] <name> [WHERE ...]
2194  let mut from = 0usize;
2195  while let Some(rel) = upper[from..].find("DELETE") {
2196    let at = from + rel;
2197    let prev_ok = at == 0 || !is_word_char(bytes[at - 1]);
2198    if !prev_ok {
2199      from = at + 6;
2200      continue;
2201    }
2202    let mut i = at + "DELETE".len();
2203    while i < n && bytes[i].is_ascii_whitespace() {
2204      i += 1;
2205    }
2206    if !starts_with_at(&upper, i, "FROM") {
2207      from = at + 6;
2208      continue;
2209    }
2210    i += "FROM".len();
2211    while i < n && bytes[i].is_ascii_whitespace() {
2212      i += 1;
2213    }
2214    if starts_with_at(&upper, i, "ONLY") && i + 4 < n && bytes[i + 4].is_ascii_whitespace() {
2215      i += 4;
2216      while i < n && bytes[i].is_ascii_whitespace() {
2217        i += 1;
2218      }
2219    }
2220    let id_start = i;
2221    while i < n && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_' || bytes[i] == b'.' || bytes[i] == b'"') {
2222      i += 1;
2223    }
2224    if i == id_start {
2225      from = at + 6;
2226      continue;
2227    }
2228    let raw = &src[id_start..i];
2229    let bare = raw.rsplit('.').next().unwrap_or(raw).trim_matches('"').to_string();
2230    // Look for a WHERE clause before the next `;` -- presence of WHERE
2231    // means we can't predict the row count, so treat as a single-row
2232    // delete (best effort). No WHERE = clear table.
2233    let stmt_end = src[i..].find(';').map(|p| i + p).unwrap_or(n);
2234    let stmt_tail_upper = upper[i..stmt_end].to_string();
2235    let has_where = stmt_tail_upper.split(|c: char| !c.is_ascii_alphanumeric() && c != '_').any(|w| w == "WHERE");
2236    if !bare.is_empty() {
2237      out.push((at, bare, if has_where { ClearKind::DeleteWhere } else { ClearKind::DeleteAll }));
2238    }
2239    from = stmt_end.max(at + 6);
2240  }
2241  out
2242}
2243
2244/// Walk every INSERT in `src` and return (table, +rows) pairs.
2245/// VALUES counts the tuples; INSERT ... SELECT FROM generate_series(a, b)
2246/// counts b - a + 1 (best-effort generator literal detection).
2247fn scan_insert_inserts(src: &str) -> Vec<(usize, String, i64)> {
2248  let mut out = Vec::new();
2249  let upper = src.to_ascii_uppercase();
2250  let bytes = src.as_bytes();
2251  let n = bytes.len();
2252  let needle = "INSERT INTO";
2253  let mut from = 0usize;
2254  while let Some(rel) = upper[from..].find(needle) {
2255    let at = from + rel;
2256    let prev_ok = at == 0 || !is_word_char(bytes[at - 1]);
2257    if !prev_ok {
2258      from = at + needle.len();
2259      continue;
2260    }
2261    let mut i = at + needle.len();
2262    while i < n && bytes[i].is_ascii_whitespace() {
2263      i += 1;
2264    }
2265    if starts_with_at(&upper, i, "ONLY") && i + 4 < n && bytes[i + 4].is_ascii_whitespace() {
2266      i += 4;
2267      while i < n && bytes[i].is_ascii_whitespace() {
2268        i += 1;
2269      }
2270    }
2271    let id_start = i;
2272    while i < n && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_' || bytes[i] == b'.' || bytes[i] == b'"') {
2273      i += 1;
2274    }
2275    if i == id_start {
2276      from = at + needle.len();
2277      continue;
2278    }
2279    let raw = &src[id_start..i];
2280    let bare = raw.rsplit('.').next().unwrap_or(raw).trim_matches('"').to_ascii_lowercase();
2281    // Optional column list.
2282    while i < n && bytes[i].is_ascii_whitespace() {
2283      i += 1;
2284    }
2285    if i < n && bytes[i] == b'(' {
2286      if let Some(close) = match_paren_opt(bytes, i) {
2287        i = close + 1;
2288      } else {
2289        from = i;
2290        continue;
2291      }
2292    }
2293    while i < n && bytes[i].is_ascii_whitespace() {
2294      i += 1;
2295    }
2296    // Find end-of-statement for this INSERT for the generate_series scan.
2297    let stmt_end = src[i..].find(';').map(|p| i + p).unwrap_or(n);
2298    if starts_with_at(&upper, i, "VALUES") {
2299      i += "VALUES".len();
2300      let mut tuples = 0i64;
2301      loop {
2302        while i < n && bytes[i].is_ascii_whitespace() {
2303          i += 1;
2304        }
2305        if i >= n || bytes[i] != b'(' {
2306          break;
2307        }
2308        let Some(close) = match_paren_opt(bytes, i) else { break };
2309        tuples += 1;
2310        i = close + 1;
2311        while i < n && bytes[i].is_ascii_whitespace() {
2312          i += 1;
2313        }
2314        if i < n && bytes[i] == b',' {
2315          i += 1;
2316        } else {
2317          break;
2318        }
2319      }
2320      if !bare.is_empty() && tuples > 0 {
2321        out.push((at, bare, tuples));
2322      }
2323      from = i.max(at + needle.len());
2324      continue;
2325    }
2326    if upper[i..stmt_end].contains("SELECT") {
2327      // INSERT ... SELECT ... -- estimate via generate_series literal.
2328      if let Some(rows) = generate_series_literal_count(&src[i..stmt_end])
2329        && !bare.is_empty()
2330      {
2331        out.push((at, bare, rows));
2332      }
2333    }
2334    from = stmt_end.max(at + needle.len());
2335  }
2336  out
2337}
2338
2339/// Return the row count for a `generate_series(a, b)` or
2340/// `generate_series(a, b, step)` literal call embedded anywhere in
2341/// `chunk`. Only inspects integer literals; bails on column refs or
2342/// other non-literal forms.
2343fn generate_series_literal_count(chunk: &str) -> Option<i64> {
2344  let upper = chunk.to_ascii_uppercase();
2345  let at = upper.find("GENERATE_SERIES")?;
2346  let after = chunk[at + "GENERATE_SERIES".len()..].trim_start();
2347  let after = after.strip_prefix('(')?;
2348  // Find matching `)`.
2349  let mut depth = 1i32;
2350  let mut end = 0usize;
2351  for (k, b) in after.bytes().enumerate() {
2352    match b {
2353      b'(' => depth += 1,
2354      b')' => {
2355        depth -= 1;
2356        if depth == 0 {
2357          end = k;
2358          break;
2359        }
2360      },
2361      _ => {},
2362    }
2363  }
2364  if depth != 0 {
2365    return None;
2366  }
2367  let args: Vec<&str> = after[..end].split(',').map(|s| s.trim()).collect();
2368  if args.len() < 2 {
2369    return None;
2370  }
2371  let a: i64 = args[0].parse().ok()?;
2372  let b: i64 = args[1].parse().ok()?;
2373  let step: i64 = if args.len() >= 3 { args[2].parse().ok().unwrap_or(1) } else { 1 };
2374  if step == 0 {
2375    return None;
2376  }
2377  // Reject impossible series (a<b with negative step, or a>b with
2378  // positive step) -> zero rows.
2379  if (a < b && step < 0) || (a > b && step > 0) {
2380    return Some(0);
2381  }
2382  Some(((b - a) / step).abs() + 1)
2383}
2384
2385/// `from_source` to seed an offline `row_estimate` so the SELECT chip
2386/// shows a useful count even without a live DB connection.
2387#[allow(dead_code)]
2388fn scan_insert_tuple_counts(src: &str) -> std::collections::HashMap<String, usize> {
2389  let mut out: std::collections::HashMap<String, usize> = Default::default();
2390  let upper = src.to_ascii_uppercase();
2391  let bytes = src.as_bytes();
2392  let n = bytes.len();
2393  let needle = "INSERT INTO";
2394  let mut from = 0usize;
2395  while let Some(rel) = upper[from..].find(needle) {
2396    let at = from + rel;
2397    let prev_ok = at == 0 || !is_word_char(bytes[at - 1]);
2398    if !prev_ok {
2399      from = at + needle.len();
2400      continue;
2401    }
2402    let mut i = at + needle.len();
2403    while i < n && bytes[i].is_ascii_whitespace() {
2404      i += 1;
2405    }
2406    // Optional `ONLY` keyword.
2407    if starts_with_at(&upper, i, "ONLY") && i + 4 < n && (bytes[i + 4] as char).is_whitespace() {
2408      i += 4;
2409      while i < n && bytes[i].is_ascii_whitespace() {
2410        i += 1;
2411      }
2412    }
2413    let id_start = i;
2414    while i < n && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_' || bytes[i] == b'.' || bytes[i] == b'"') {
2415      i += 1;
2416    }
2417    if i == id_start {
2418      from = at + needle.len();
2419      continue;
2420    }
2421    let raw = &src[id_start..i];
2422    let bare = raw.rsplit('.').next().unwrap_or(raw).trim_matches('"').to_ascii_lowercase();
2423    // Skip any optional `(col, col)` column list.
2424    while i < n && bytes[i].is_ascii_whitespace() {
2425      i += 1;
2426    }
2427    if i < n && bytes[i] == b'(' {
2428      if let Some(close) = match_paren_opt(bytes, i) {
2429        i = close + 1;
2430      } else {
2431        from = i;
2432        continue;
2433      }
2434    }
2435    while i < n && bytes[i].is_ascii_whitespace() {
2436      i += 1;
2437    }
2438    // Look for VALUES; bail if next token is SELECT (INSERT ... SELECT).
2439    if !starts_with_at(&upper, i, "VALUES") {
2440      from = i;
2441      continue;
2442    }
2443    i += "VALUES".len();
2444    // Count the (...) tuples that follow, separated by commas.
2445    let mut tuples = 0usize;
2446    loop {
2447      while i < n && bytes[i].is_ascii_whitespace() {
2448        i += 1;
2449      }
2450      if i >= n || bytes[i] != b'(' {
2451        break;
2452      }
2453      let Some(close) = match_paren_opt(bytes, i) else { break };
2454      tuples += 1;
2455      i = close + 1;
2456      while i < n && bytes[i].is_ascii_whitespace() {
2457        i += 1;
2458      }
2459      if i < n && bytes[i] == b',' {
2460        i += 1;
2461      } else {
2462        break;
2463      }
2464    }
2465    if !bare.is_empty() && tuples > 0 {
2466      *out.entry(bare).or_insert(0) += tuples;
2467    }
2468    from = i.max(at + needle.len());
2469  }
2470  out
2471}
2472
2473fn match_paren_opt(bytes: &[u8], open: usize) -> Option<usize> {
2474  let mut depth = 0i32;
2475  let mut i = open;
2476  while i < bytes.len() {
2477    match bytes[i] {
2478      b'(' => depth += 1,
2479      b')' => {
2480        depth -= 1;
2481        if depth == 0 {
2482          return Some(i);
2483        }
2484      },
2485      b'\'' => {
2486        i += 1;
2487        while i < bytes.len() && bytes[i] != b'\'' {
2488          i += 1;
2489        }
2490      },
2491      _ => {},
2492    }
2493    i += 1;
2494  }
2495  None
2496}
2497
2498fn is_word_char(b: u8) -> bool {
2499  b.is_ascii_alphanumeric() || b == b'_'
2500}
2501
2502fn scan_roles(src: &str) -> Vec<String> {
2503  let mut out: std::collections::BTreeSet<String> = default_offline_roles().into_iter().collect();
2504  for prefix in ["CREATE ROLE ", "CREATE USER ", "CREATE GROUP "] {
2505    for name in scan_create_named(src, &[prefix]) {
2506      out.insert(name);
2507    }
2508  }
2509  out.into_iter().collect()
2510}
2511
2512/// Walk the source for each prefix; for each match return the identifier
2513/// (possibly schema-qualified, returned bare) immediately following.
2514fn scan_create_named(src: &str, prefixes: &[&str]) -> Vec<String> {
2515  let upper = src.to_ascii_uppercase();
2516  let mut out = Vec::new();
2517  for prefix in prefixes {
2518    out.extend(scan_create_named_with_upper(src, &upper, prefix));
2519  }
2520  out.sort();
2521  out.dedup();
2522  out
2523}
2524
2525fn scan_create_named_with_upper(src: &str, upper: &str, prefix: &str) -> Vec<String> {
2526  let bytes = src.as_bytes();
2527  let n = bytes.len();
2528  let mut out = Vec::new();
2529  let mut from = 0usize;
2530  while let Some(rel) = upper[from..].find(prefix) {
2531    let after = from + rel + prefix.len();
2532    let mut k = after;
2533    while k < n && bytes[k].is_ascii_whitespace() {
2534      k += 1;
2535    }
2536    let id_start = k;
2537    while k < n && (bytes[k].is_ascii_alphanumeric() || bytes[k] == b'_' || bytes[k] == b'.' || bytes[k] == b'"') {
2538      k += 1;
2539    }
2540    if k > id_start {
2541      let raw = &src[id_start..k];
2542      let bare = raw.rsplit('.').next().unwrap_or(raw).trim_matches('"').to_string();
2543      if !bare.is_empty() {
2544        out.push(bare);
2545      }
2546    }
2547    from = after;
2548  }
2549  out
2550}
2551
2552/// Text-scan fallback: harvest the column-name list from a possibly
2553/// unclosed `CREATE TABLE <name> (` body. Returns names in order of
2554/// appearance. Used when the SQL parser can't produce a clean AST yet
2555/// (cursor inside the body the user is still typing).
2556pub fn buffer_column_names(source: &str, table: &str) -> Vec<String> {
2557  let upper_target = table.to_ascii_uppercase();
2558  let mut from = 0usize;
2559  let upper = source.to_ascii_uppercase();
2560  while let Some(rel) = upper[from..].find("CREATE TABLE") {
2561    let start = from + rel;
2562    let after = start + "CREATE TABLE".len();
2563    let mut rest_start = after;
2564    // Skip whitespace + optional IF NOT EXISTS.
2565    let after_str = &source[rest_start..];
2566    let trim_lead = after_str.len() - after_str.trim_start().len();
2567    rest_start += trim_lead;
2568    if starts_with_at(&upper, rest_start, "IF NOT EXISTS") {
2569      rest_start += "IF NOT EXISTS".len();
2570      let after2 = &source[rest_start..];
2571      rest_start += after2.len() - after2.trim_start().len();
2572    }
2573    // Read the name token.
2574    let name: String =
2575      source[rest_start..].chars().take_while(|c| c.is_alphanumeric() || *c == '_' || *c == '.').collect();
2576    let name_only = name.rsplit('.').next().unwrap_or(&name);
2577    if !name_only.eq_ignore_ascii_case(table) && !name.to_ascii_uppercase().contains(&upper_target) {
2578      from = after;
2579      continue;
2580    }
2581    // Find the body opener.
2582    let body_open = source[rest_start + name.len()..].find('(');
2583    let Some(rel_open) = body_open else {
2584      from = after;
2585      continue;
2586    };
2587    let open = rest_start + name.len() + rel_open + 1;
2588    // Walk paren depth to find close (or EOF when unclosed).
2589    let bytes = source.as_bytes();
2590    let n = bytes.len();
2591    let mut depth = 1i32;
2592    let mut i = open;
2593    let close;
2594    loop {
2595      if i >= n {
2596        close = n;
2597        break;
2598      }
2599      match bytes[i] {
2600        b'\'' => {
2601          i += 1;
2602          while i < n {
2603            if bytes[i] == b'\'' {
2604              i += 1;
2605              break;
2606            }
2607            i += 1;
2608          }
2609        },
2610        b'(' => {
2611          depth += 1;
2612          i += 1;
2613        },
2614        b')' => {
2615          depth -= 1;
2616          i += 1;
2617          if depth == 0 {
2618            close = i - 1;
2619            break;
2620          }
2621        },
2622        _ => i += 1,
2623      }
2624    }
2625    let body = &source[open..close.min(n)];
2626    return harvest_names(body);
2627  }
2628  Vec::new()
2629}
2630
2631fn harvest_names(body: &str) -> Vec<String> {
2632  let mut out = Vec::new();
2633  for raw in split_top_commas(body) {
2634    let trimmed = raw.trim();
2635    if trimmed.is_empty() {
2636      continue;
2637    }
2638    let head_upper = trimmed.to_ascii_uppercase();
2639    // Skip table-level constraint lines.
2640    let first = head_upper.split_ascii_whitespace().next().unwrap_or("");
2641    if matches!(first, "CONSTRAINT" | "PRIMARY" | "FOREIGN" | "UNIQUE" | "CHECK" | "EXCLUDE" | "LIKE") {
2642      continue;
2643    }
2644    // First identifier is the column name. Skip the trailing partial
2645    // token that the cursor is in the middle of -- the user already
2646    // sees that as they type.
2647    let name: String = trimmed.chars().take_while(|c| c.is_alphanumeric() || *c == '_').collect();
2648    if !name.is_empty() && !out.contains(&name) {
2649      out.push(name);
2650    }
2651  }
2652  out
2653}
2654
2655fn split_top_commas(s: &str) -> Vec<&str> {
2656  let bytes = s.as_bytes();
2657  let n = bytes.len();
2658  let mut depth = 0i32;
2659  let mut last = 0usize;
2660  let mut out = Vec::new();
2661  let mut i = 0usize;
2662  while i < n {
2663    match bytes[i] {
2664      b'\'' => {
2665        i += 1;
2666        while i < n {
2667          if bytes[i] == b'\'' {
2668            i += 1;
2669            break;
2670          }
2671          i += 1;
2672        }
2673      },
2674      b'(' => {
2675        depth += 1;
2676        i += 1;
2677      },
2678      b')' => {
2679        depth -= 1;
2680        i += 1;
2681      },
2682      b',' if depth == 0 => {
2683        out.push(&s[last..i]);
2684        last = i + 1;
2685        i += 1;
2686      },
2687      _ => i += 1,
2688    }
2689  }
2690  out.push(&s[last..]);
2691  out
2692}
2693
2694/// Merge two catalogs into one. `live` wins on name collisions across
2695/// every collection (tables, sequences, types, extensions, functions,
2696/// roles). Derived entries fill in only where live has a gap, so a
2697/// live DB connection always takes precedence over text-scanned
2698/// guesses but offline use still gets the full set.
2699pub fn merge(live: &Catalog, derived: &Catalog) -> Catalog {
2700  let mut out = live.clone();
2701  for ds in &derived.schemas {
2702    let target = match out.schemas.iter_mut().find(|s| s.name.eq_ignore_ascii_case(&ds.name)) {
2703      Some(s) => s,
2704      None => {
2705        out.schemas.push(Schema { name: ds.name.clone(), tables: Vec::new() });
2706        out.schemas.last_mut().unwrap()
2707      },
2708    };
2709    for dt in &ds.tables {
2710      if !target.tables.iter().any(|t| t.name.eq_ignore_ascii_case(&dt.name)) {
2711        target.tables.push(dt.clone());
2712      }
2713    }
2714  }
2715  for s in &derived.sequences {
2716    if !out.sequences.iter().any(|x| x.name.eq_ignore_ascii_case(&s.name)) {
2717      out.sequences.push(s.clone());
2718    }
2719  }
2720  for t in &derived.types {
2721    if !out.types.iter().any(|x| x.name.eq_ignore_ascii_case(&t.name)) {
2722      out.types.push(t.clone());
2723    }
2724  }
2725  for e in &derived.extensions {
2726    if !out.extensions.iter().any(|x| x.name.eq_ignore_ascii_case(&e.name)) {
2727      out.extensions.push(e.clone());
2728    }
2729  }
2730  for f in &derived.functions {
2731    if !out.functions.iter().any(|x| x.name.eq_ignore_ascii_case(&f.name)) {
2732      out.functions.push(f.clone());
2733    }
2734  }
2735  for r in &derived.roles {
2736    if !out.roles.iter().any(|x| x.eq_ignore_ascii_case(r)) {
2737      out.roles.push(r.clone());
2738    }
2739  }
2740  out
2741}
2742
2743#[cfg(test)]
2744mod tests {
2745  use super::*;
2746  use dsl_parse::Dialect;
2747
2748  #[test]
2749  fn json_keys_annotation_populates_column() {
2750    let src = "CREATE TABLE event (\n  id int,\n  -- @json-keys: name, age, tier\n  payload jsonb\n);\n";
2751    let parsed = dsl_parse::parse(src, Dialect::Postgres);
2752    let cat = from_source(&parsed, src);
2753    let tbl = cat.schemas.iter().flat_map(|s| s.tables.iter()).find(|t| t.name == "event").expect("event table");
2754    let payload = tbl.columns.iter().find(|c| c.name == "payload").expect("payload col");
2755    assert_eq!(payload.json_keys.as_deref(), Some(&["name".to_string(), "age".to_string(), "tier".to_string()][..]));
2756    let id = tbl.columns.iter().find(|c| c.name == "id").expect("id col");
2757    assert!(id.json_keys.is_none());
2758  }
2759
2760  #[test]
2761  fn json_keys_annotation_quoted_keys() {
2762    let src = "CREATE TABLE t (\n  -- @json-keys: \"a-b\", c\n  data jsonb\n);\n";
2763    let parsed = dsl_parse::parse(src, Dialect::Postgres);
2764    let cat = from_source(&parsed, src);
2765    let tbl = cat.schemas.iter().flat_map(|s| s.tables.iter()).find(|t| t.name == "t").unwrap();
2766    let col = tbl.columns.iter().find(|c| c.name == "data").unwrap();
2767    assert_eq!(col.json_keys.as_deref(), Some(&["a-b".to_string(), "c".to_string()][..]));
2768  }
2769
2770  #[test]
2771  fn scan_views_basic_and_modifiers() {
2772    let src = "\
2773      CREATE VIEW active_users AS SELECT * FROM users WHERE active;\n\
2774      CREATE OR REPLACE MATERIALIZED VIEW reporting.daily (d, total) AS\n\
2775        SELECT day, sum(amount) FROM sales GROUP BY day;\n";
2776    let views = scan_views(src);
2777    assert_eq!(views.len(), 2);
2778
2779    let (schema, name, kind, cols, def) = &views[0];
2780    assert_eq!(schema, "public");
2781    assert_eq!(name, "active_users");
2782    assert_eq!(*kind, TableKind::View);
2783    assert!(cols.is_empty());
2784    assert_eq!(def, "SELECT * FROM users WHERE active");
2785
2786    let (schema, name, kind, cols, def) = &views[1];
2787    assert_eq!(schema, "reporting");
2788    assert_eq!(name, "daily");
2789    assert_eq!(*kind, TableKind::MaterializedView);
2790    assert_eq!(cols, &vec!["d".to_string(), "total".to_string()]);
2791    assert!(def.starts_with("SELECT day, sum(amount)"));
2792    assert!(!def.contains(';'));
2793  }
2794
2795  #[test]
2796  fn from_source_surfaces_view_definition() {
2797    let src = "CREATE VIEW v AS SELECT id, name FROM t WHERE id > 0;";
2798    let parsed = dsl_parse::parse(src, Dialect::Postgres);
2799    let cat = from_source(&parsed, src);
2800    let view = cat.schemas.iter().flat_map(|s| s.tables.iter()).find(|t| t.name == "v").expect("view v");
2801    assert_eq!(view.kind, TableKind::View);
2802    assert_eq!(view.definition.as_deref(), Some("SELECT id, name FROM t WHERE id > 0"));
2803  }
2804
2805  #[test]
2806  fn scan_views_ignores_create_table() {
2807    assert!(scan_views("CREATE TABLE t (id int);").is_empty());
2808  }
2809
2810  #[test]
2811  fn unquote_ident_handles_every_dialects_quoting() {
2812    assert_eq!(unquote_ident("plain"), "plain");
2813    assert_eq!(unquote_ident("\"quoted\""), "quoted");
2814    assert_eq!(unquote_ident("[bracketed]"), "bracketed");
2815    assert_eq!(unquote_ident("`backticked`"), "backticked");
2816    // Not a matched pair, so it is left exactly as it came in.
2817    assert_eq!(unquote_ident("[half"), "[half");
2818    assert_eq!(unquote_ident("\"a\" = \"b\""), "\"a\" = \"b\"");
2819    assert_eq!(unquote_ident(""), "");
2820    assert_eq!(unquote_ident("\""), "\"");
2821  }
2822
2823  /// The DDL sqlite stores for a table most tools created. Read as postgres
2824  /// this is a syntax error and yields nothing; read as sqlite the brackets
2825  /// have to come off, or the table's own constraints reference columns it
2826  /// does not appear to have.
2827  #[test]
2828  fn bracket_quoted_sqlite_ddl_yields_columns_and_constraints() {
2829    let src = "\
2830CREATE TABLE \"albums\"\n\
2831(\n\
2832    [AlbumId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n\
2833    [Title] NVARCHAR(160)  NOT NULL,\n\
2834    [ArtistId] INTEGER  NOT NULL,\n\
2835    FOREIGN KEY ([ArtistId]) REFERENCES \"artists\" ([ArtistId])\n\
2836);\n";
2837    let parsed = dsl_parse::parse(src, Dialect::SQLite);
2838    let cat = from_source(&parsed, src);
2839    let table = cat
2840      .schemas
2841      .iter()
2842      .flat_map(|s| s.tables.iter())
2843      .find(|t| t.name.eq_ignore_ascii_case("albums"))
2844      .expect("albums table");
2845
2846    let columns: Vec<&str> = table.columns.iter().map(|c| c.name.as_str()).collect();
2847    assert_eq!(columns, vec!["AlbumId", "Title", "ArtistId"], "brackets are not part of the name");
2848
2849    // Inline constraints go through their own path, so the brackets have to
2850    // come off there too or only half the table resolves.
2851    let pk = table.constraints.iter().find(|c| c.kind == ConstraintKind::PrimaryKey).expect("the primary key");
2852    assert_eq!(pk.columns, vec!["AlbumId".to_string()]);
2853    assert_eq!(pk.name, "pk_AlbumId");
2854
2855    let fk = table.constraints.iter().find(|c| c.kind == ConstraintKind::ForeignKey).expect("the foreign key");
2856    assert_eq!(fk.columns, vec!["ArtistId".to_string()]);
2857    let reference = fk.references.as_ref().expect("the foreign key names a target");
2858    assert_eq!(reference.table, "artists");
2859    assert_eq!(reference.columns, vec!["ArtistId".to_string()]);
2860  }
2861
2862  #[test]
2863  fn a_dot_inside_quoting_is_part_of_the_name() {
2864    let one = parse_references("REFERENCES [my.table] ([id])").expect("a reference");
2865    assert_eq!((one.schema.as_str(), one.table.as_str()), ("public", "my.table"));
2866
2867    let qualified = parse_references("REFERENCES [dbo].[users] ([id])").expect("a reference");
2868    assert_eq!((qualified.schema.as_str(), qualified.table.as_str()), ("dbo", "users"));
2869  }
2870
2871  #[test]
2872  fn backtick_quoted_mysql_ddl_yields_columns() {
2873    let src = "CREATE TABLE `orders` (\n  `id` INT NOT NULL,\n  `total` DECIMAL(10,2)\n);\n";
2874    let parsed = dsl_parse::parse(src, Dialect::MySql);
2875    let cat = from_source(&parsed, src);
2876    let table = cat.schemas.iter().flat_map(|s| s.tables.iter()).find(|t| t.name == "orders").expect("orders table");
2877
2878    assert_eq!(table.columns.iter().map(|c| c.name.as_str()).collect::<Vec<_>>(), vec!["id", "total"]);
2879  }
2880}