Monday, September 10, 2018

Today I have still be documenting and tidying up before pushing my work on the new accounting report.

More SQL summaries

I had some work with Exploring SQL activity in Lino Tera. Since there is no more virtual table ledger.AccountingReport, I thought Okay let’s analyze the SQL generated by lino_xl.lib.sheets.Report.run_update_plan(). Oops, our lino.api.doctest.show_sql_summary() didn’t yet swallow INSERT INTO and DELETE FROM statements.

This is where I searched for “Python SQL parser” and found sqlparse.

>>> import sqlparse
>>> sql = "SELECT a, b, c FROM foo WHERE bar = 5;"
>>> parsed = sqlparse.parse(sql)
>>> for stmt in parsed:
...     print(stmt)
SELECT a, b, c FROM foo WHERE bar = 5;
>>> stmt = parsed[0]
>>> print(stmt.get_type())
SELECT
>>> for token in stmt:
...     print token.__class__, token
<class 'sqlparse.sql.Token'> SELECT
<class 'sqlparse.sql.Token'>
<class 'sqlparse.sql.IdentifierList'> a, b, c
<class 'sqlparse.sql.Token'>
<class 'sqlparse.sql.Token'> FROM
<class 'sqlparse.sql.Token'>
<class 'sqlparse.sql.Identifier'> foo
<class 'sqlparse.sql.Token'>
<class 'sqlparse.sql.Where'> WHERE bar = 5;
>>> sql = "SELECT COUNT(*) AS __count FROM foo WHERE bar = 5;"
>>> parsed = sqlparse.parse(sql)
>>> stmt = parsed[0]
>>> for token in stmt:
...     print token.__class__, token

Corporate-driven open source

This reminds me Brett Cannon’s blog post Setting expectations for open source participation where he writes “For a day job I am the dev lead for the Python extension for Visual Studio Code. Originally the extension was an open source, personal project, but then Microsoft decided it would be a good idea to support the most popular extension for VS Code, so we hired its creator (Don Jayamanne), put me on the team, and then we re-launched the extension as an official open source project from Microsoft (and now we are a team of four developers and a product manager). So I participate in what I would call corporate open source for a living because even if we never received a single outside contribution ever again, the extension’s development will continue thanks to Microsoft.”