Re:A C parser for CREATE and ALTER table SQLite statements
by cjohnson318 on Tuesday August 15, 2023 @02:30AM
(#37130863)
Porting this to Python is left as an exercise to the reader.
Re:A C parser for CREATE and ALTER table SQLite statements
by masklinn on Tuesday August 15, 2023 @05:12AM
(#37131773)
It's two files, you can probably just cffi it in.
Re:A C parser for CREATE and ALTER table SQLite statements
by oefrha on Tuesday August 15, 2023 @03:47AM
(#37131307)
An SQLite extension could be implemented on top of this parser, converting the C functions into SQLite functions. Then it could be fairly easily called from any language.
Re:A C parser for CREATE and ALTER table SQLite statements
by indigo945 on Tuesday August 15, 2023 @04:44AM
(#37131592)
At that point, it seems like it might be faster to just fork SQLite and add the PRAGMA that you need, instead of duplicating a part of SQLite's functionality in a non-future-proof way.
Re:A C parser for CREATE and ALTER table SQLite statements
by reportgunner on Tuesday August 15, 2023 @06:30AM
(#37132211)
Have you checked sqlite-utils[0] ?
Re:A C parser for CREATE and ALTER table SQLite statements
by oefrha on Tuesday August 15, 2023 @07:34AM
(#37132653)
simonw wrote sqlite-utils, so I’m sure he has checked it. It’s also mentioned in gp.
Re:A C parser for CREATE and ALTER table SQLite statements
by Someone on Tuesday August 15, 2023 @09:20AM
(#37133658)
> But you need a very robust parser!
Making it robust should not be hard, should it? In general, the difficult part of parsing is the handling of erroneous input: generating good error messages and error recovery (not getting into the woods after seeing one error). This parser only has to parse valid SQL, and one statement at a time, so it needs neither.
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-...