Skip to main content

column_defs

Function column_defs 

Source
pub fn column_defs(create_sql: &str) -> Option<Vec<(String, String)>>
Expand description

Best-effort extraction of (column_name, declared_type) for each column declared in a CREATE TABLE statement, without a SQL-parser dependency.

Algorithm (deliberately conservative — wrong names are worse than no names):

  1. Take the outermost parenthesized list (first ( to its matching ), tracking nesting and skipping over '…', "…", `…`, and […] so a comma or paren inside a string/identifier/CHECK(...) never splits).
  2. Split that list on top-level commas (depth 0).
  3. For each part, read the first identifier — handling "quoted", `backtick`, [bracketed], and bare names. Skip a part whose first word is a table-level constraint keyword (CONSTRAINT, PRIMARY, UNIQUE, CHECK, FOREIGN, KEY) — those declare constraints, not columns.
  4. The remaining tokens of the part (up to the next top-level comma) form the declared type; an empty remainder is no declared type.

Returns None when no parenthesized list is found or no column survives — the low-confidence signal the caller turns into a c0..cN fallback.