A C parser for CREATE and ALTER TABLE SQLite statements

  • Huh, weirdly enough I've been hoping for exactly this kind of thing - albeit easily called from Python.

    My use-case is that with SQLite there are aspects of the table - things like generated columns - which aren't yet available via the regular SQLite PRAGMAs for introspecting tables.

    For those things, the only way to introspect them is to read the CREATE TABLE statement out of the "sqlite_master" table and parse it.

    But you need a very robust parser!

    I've been investigating tree-sitter recently for this, notes here: https://til.simonwillison.net/python/tree-sitter

    If anyone needs an implementation of that advanced ALTER TABLE mechanism (where you create a new table with the new schema, copy the data cross from the old one and then swap the names) my sqlite-utils CLI tool and Python library implements that, as described here: https://simonwillison.net/2020/Sep/23/sqlite-advanced-alter-...

  • don't you need a full parse even just for create and alter statements since you could always encounter an "as" followed by arbitrarily complex sql?

  • What was the reason for a hand-written lexer and parser vs using Flex / Bison?

  • The section that says sqlite doesn't support drop column links to documentation that says it's supported?

  • So this is more than just on parser for those statements. Very confusing.