Format SQL - Improve readability

Feature requests and their debate

Format SQL - Improve readability

Postby gustavotrott » Thu Sep 04, 2014 1:01 pm

When I use the auto "Format SQL", the generated result is something like this:

Code: Select all
SELECT
   ...
FROM
   TABLE_A JOIN TABLE_B
      ON TABLE_B.CD = TABLE_A.CD



I propose a new way that would improve the readability.
*Add Line break before "JOIN"
*Remove line break before "ON"

Code: Select all
SELECT
   ...
FROM TABLE_A
JOIN TABLE_B ON TABLE_B.CD = TABLE_A.CD


Also,
when i'm writing a function, the auto formatter put line breaks between the parameters, which ends up generating a waste of lines.

Example:
Code: Select all
SELECT
   to_char(
      COLUMN_A,
      'YYYY-MM-DD'
   ) as "desc",


could be:

Code: Select all
SELECT
   to_char(COLUMN_A,'YYYY-MM-DD') as "desc"


I hope you like my suggestions.

Thanks,
Gustavo
gustavotrott
 
Posts: 11
Joined: Wed Feb 12, 2014 12:49 pm

Re: Format SQL - Improve readability

Postby gustavotrott » Fri Sep 12, 2014 7:34 pm

I really like DbVisualizer way to customize it.

Image

Image

Image
gustavotrott
 
Posts: 11
Joined: Wed Feb 12, 2014 12:49 pm

Re: Format SQL - Improve readability

Postby beat-x » Mon Nov 10, 2014 9:41 am

Hi Gustavo, thanks for mentioning the subject. I support your request.

Sometimes, the beaver's formatter is a bit annoying. Here is another example with a CASE-WHEN clause:
Code: Select all
SELECT CASE (x) WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'unknown' END FROM (SELECT 1 x FROM dual)

is formatted as:
Code: Select all
SELECT
    CASE
        (x) WHEN 1
        THEN 'one' WHEN 2
        THEN 'two'
        ELSE 'unknown'
    END
FROM
    (
        SELECT
            1 x
        FROM
            dual
    )

but would be much more readable when the WHENs were are the beginning of the line, like this:
Code: Select all
SELECT
    CASE (x)
        WHEN 1
            THEN 'one'
        WHEN 2
            THEN 'two'
        ELSE
            'unknown'
    END
FROM
    (
        SELECT
            1 x
        FROM
            dual
    )


I can understand that generating "nice" looking queries automatically is really hard. Personally I would not use that many line breaks, but this is perfectly ok for an automatic process and really helps you to understand the structure of complicated queries. However, the line break rules of DBeaver are a but strange sometimes, and if they were customizable as suggested by Gustavo, this would really be helpful.
beat-x
 
Posts: 10
Joined: Sun Dec 02, 2012 4:32 pm

Re: Format SQL - Improve readability

Postby kawinga » Thu Mar 03, 2016 3:22 pm

i support this idea! +1

SquirrelSQL gives you the possibility to controll how the keywords should be handled (attached).
Attachments
squirrel_sql_format_preferences.png
SquirrelSQL Format Preferences
squirrel_sql_format_preferences.png (43.53 KiB) Viewed 36027 times
kawinga
 
Posts: 1
Joined: Thu Mar 03, 2016 3:21 pm

Re: Format SQL - Improve readability

Postby nzimmerman » Wed Mar 16, 2016 3:11 pm

I would really like to see this feature too. I find the auto-formatting difficult to follow due to the line breaks in the middle of join clauses.
nzimmerman
 
Posts: 5
Joined: Tue Aug 26, 2014 9:07 pm

Re: Format SQL - Improve readability

Postby Serge » Sat Mar 19, 2016 12:25 pm

Version 3.6.3 will include improved formatter + some options + external formatter support.
Serge
 
Posts: 1526
Joined: Sat Feb 26, 2011 8:24 pm
Location: SPb

Re: Format SQL - Improve readability

Postby gustavotrott » Tue Apr 12, 2016 2:34 am

I'm excited with this update!
Is there any explanation about how to use the external formatter?
gustavotrott
 
Posts: 11
Joined: Wed Feb 12, 2014 12:49 pm

Re: Format SQL - Improve readability

Postby gustavotrott » Tue Apr 12, 2016 3:15 am

I got it! I tried with a PHP script!

Command Line: php /Users/myuser/script.php

PHP SCRIPT:
Code: Select all
<?php
$handle = fopen('php://stdin', 'r');
$sql = '';
while(!feof($handle)) $sql .= fgets($handle);
fclose($handle);
echo strtoupper($sql); die;


And it worked perfectly!! :D :D Thanks!!
gustavotrott
 
Posts: 11
Joined: Wed Feb 12, 2014 12:49 pm


Return to Feature Requests



Who is online

Users browsing this forum: No registered users and 4 guests