darold / pgFormatter

A PostgreSQL SQL syntax beautifier that can work as a console program or as a CGI. On-line demo site at http://sqlformat.darold.net/
PostgreSQL License
1.64k stars 101 forks source link

Ability to work in streaming mode #333

Open bogdan opened 4 months ago

bogdan commented 4 months ago

The binary always waits for the STDIN to be closed and than formats all supplied sql:

$  pg_format -
select * from companies;
select * from companies;
Ctrl+D
SELECT
    *
FROM
    companies;

SELECT
    *
FROM
    companies;

This restricts the ability to work in streaming mode. Is it possible to format one SQL statement as long as it finishes (e.g. by ;)? cat can be used as example as it triggers output on each line break

darold commented 4 months ago

Hi,

If you want to format a query in interactive mode you must use the following:

echo "select * from companies;" | pg_format -

and call pg_format - each time you want to format e new query.

I agree that this is not convenient for streaming mode. I will see if there is a simple way to allow that.

darold commented 4 months ago

This could be handled using the Perl module Term::ReadLine but I'm not sure it could be helpful to have such feature. What is your use case for this feature?

bogdan commented 4 months ago

I am calling pg_format executive from ruby program. It takes ~200ms to do, which is way too slow. If I can keep a running process in memory and interact with it through stdin and stdout (which what any programming language probably allows), the indent can be much faster.

Profpatsch commented 2 months ago

Maybe use the Record Separator ASCII/Unicode symbol? It’s 0x1E in Utf-8