In the following example, termsql removes whitespace between the tokens 1 and from, leading to invalid syntax:
$ echo 1 | uv run --with sqlparse ./termsql -Q 'select (select 1 from tbl)'
select(select 1from tbl) from tbl ;
sqlparse formats it as:
'select\n (select 1\n from tbl)'
But when termsql is joining lines, the line starting with ' from' hits this condition:
elif line.lower().startswith("select") or line.startswith(' '):
s += line.lstrip()
so all the spaces are removed, and so is the newline.